#include <iostream> #pragma warning(disable:4703) using namespace std; /* 计算税款,根据美国2002年的数据 说实话连人家的这些例子都觉得特别棒!!! */ int singleFilers[5] = { 6000, 27950, 67700, 141250, 307050 }; int marriedFileJointly[5] = {12000,46700,112850,171950,307050}; int marriedFileSeparately[5] = { 6000, 23350, 56425, 85975, 153525 }; int headOfHousehold[5] = { 10000, 37450, 96700, 15600, 307050 }; #define stageOne 10.0 #define stageTwo 15.0 #define stageThree 27.0 #define stageFour 30.0 #define stageFive 35.0 #define stageSix 38.6 double taxStageRate[6] = { stageOne / 100, stageTwo / 100, stageThree/100, stageFour/100, stageFive/100, stageSix/100 }; double getTax(double money, int* moneyStage, double* taxRateStage){ } /* 根据这个人的状态和 挣到的钱,来决定纳税金额。 现在的想法就是是不是可以因为这一组数据很有规律,所以想做一个数组,来规范这样的话,只需要根据不同的状态,来分不同的税率阶段就可以了 */ double getTax(int status,double money){ int* MoneyStage; if (status == 0){ MoneyStage = singleFilers; } else if(status ==1){ MoneyStage = marriedFileJointly; } else if (status ==2){ MoneyStage = marriedFileSeparately; } else if (status == 3){ MoneyStage = headOfHousehold; } double tax = 0; if (money <= MoneyStage[0]){ tax = (money-0)*taxStageRate[0];//10 } else if (money <= MoneyStage[1]){ tax = MoneyStage[0] * taxStageRate[0] + (money - MoneyStage[0]) * taxStageRate[1];//6000*10+(money-6000)*15 } else if (money <= MoneyStage[2]){ tax = MoneyStage[0] * taxStageRate[0] + (MoneyStage[1] - MoneyStage[0]) * taxStageRate[1] + (money - MoneyStage[1])*taxStageRate[2]; } else if (money <= MoneyStage[3]){ tax = MoneyStage[0] * taxStageRate[0] + (MoneyStage[1] - MoneyStage[0]) * taxStageRate[1] + (MoneyStage[2] - MoneyStage[1]) * taxStageRate[2] + (money - MoneyStage[2])*taxStageRate[3]; } else if (money <= MoneyStage[4]){ tax = MoneyStage[0] * taxStageRate[0] + (MoneyStage[1] - MoneyStage[0]) * taxStageRate[1] + (MoneyStage[2] - MoneyStage[1]) * taxStageRate[2] + (MoneyStage[3] - MoneyStage[2]) * taxStageRate[3] + (money - MoneyStage[3])*taxStageRate[4]; } else{ tax = MoneyStage[0] * taxStageRate[0] + (MoneyStage[1] - MoneyStage[0]) * taxStageRate[1] + (MoneyStage[2] - MoneyStage[1]) * taxStageRate[2] + (MoneyStage[3] - MoneyStage[2]) * taxStageRate[3] + (MoneyStage[4] - MoneyStage[3]) * taxStageRate[4] + (money - MoneyStage[4])*taxStageRate[5]; } tax = getTax(money,MoneyStage,taxStageRate); return tax; } int main(){ cout << getTax(0, 400000); system("pause"); return 0; }
老实讲,有没有办法抽取一下,毕竟每一次都会比上一次多那么一项,有没有办法把那几组if else 抽取出来。这分了5个档还好要是分了50个档,前面得写50项。。。
if (status == 0){
MoneyStage = singleFilers;
这一组 if else 比较好重构,用集合存储 MoneyStage,如果 status 是递增的话,用 std::vector 存储 MoneyState,取的时候按照索引取就行了,如果不是,可以使用 std::unordered_map 存储。
第二组 if else 按照这个思路也能采用此方法,凡是 < MoneyStage[i] 的都以 MoneyStage[i] 为 key,然后在 std::unordered_map 中以 MoneyStage 的每一元素为 key,映射到一个 std::function,这样代码就可以简化为:
auto _Iter = std::find_if(std::cbegin(MoneyStage),std::cend(MoneyStage),
[money](int _Stage) {
return money < _Stage;
});
int key = _Iter == std::cend(MoneyState) ? INT_MAX : *_Iter;
auto _Func = std::unordered_map<int,std::function<double(double ,int * )>>::find(key);
auto tax = _Func(money,MoneyState);