用if else
源码:
//计算税收
#include <iostream>
using namespace std;
void main()
{
system("color F1"); //设置控制台窗口的背景及字体颜色
int tax[] = { 0, 105, 555, 1005, 2755, 5505, 13505 }; //初始化速算扣除数
float percent[] = { 0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45 };//初始化税收百分比;
float Money;
float taxhand = 0;
cout << "请输入应纳税所得额:";
cin >> Money;
//用if else实现;
while (Money < 0)
{
cout << "请输入大于0的数额!";
cin >> Money;
}
if (Money < 1500)
taxhand = Money * percent[0] - tax[0];
else if (Money <= 4500)
taxhand = Money * percent[1] - tax[1];
else if (Money <= 9000)
taxhand = Money * percent[2] - tax[2];
else if (Money <= 35000)
taxhand = Money * percent[3] - tax[3];
else if (Money <= 55000)
taxhand = Money * percent[4] - tax[4];
else if (Money <= 80000)
taxhand = Money * percent[5] - tax[5];
else
taxhand = Money * percent[6] - tax[6];
cout << "你需要缴纳的税额:" << taxhand << endl;
system("pause");
}
用switch实现
//计算税收
#include <iostream>
using namespace std;
void main()
{
system("color F1"); //设置控制台窗口的背景及字体颜色
int tax[] = { 0, 105, 555, 1005, 2755, 5505, 13505 }; //初始化速算扣除数
float percent[] = { 0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45 };//初始化税收百分比;
float Money;
float taxhand = 0;
cout << "请输入应纳税所得额:";
cin >> Money;
while (Money < 0)
{
cout << "请输入大于0的数额!";
cin >> Money;
}
//用switch实现
int i = (Money >= 1500) + (Money >= 4500) + (Money >= 9000) + (Money >= 35000) + (Money >= 55000) + (Money >= 80000); //计算case 值;
switch (i)
{
case 0:taxhand = Money * percent[i] - tax[i];
break;
case 1:taxhand = Money * percent[i] - tax[i];
break;
case 2:taxhand = Money * percent[i] - tax[i];
break;
case 3:taxhand = Money * percent[i] - tax[i];
break;
case 4:taxhand = Money * percent[i] - tax[i];
break;
case 5:taxhand = Money * percent[i] - tax[i];
break;
case 6:taxhand = Money * percent[i] - tax[i];
break;
}
cout << "你需要缴纳的税额:" << taxhand << endl;
system("pause");
}
用switch时,这一段代码很难想到,看到这段代码是不是有一种豁然开朗的感觉呢,哈哈
int i = (Money >= 1500) + (Money >= 4500) + (Money >= 9000) + (Money >= 35000) + (Money >= 55000) + (Money >= 80000);