#include<stdio.h>
int main()
{
double tax, rate;
int deduction, salary, flag;
printf("Enter the salary: ");
scanf("%d", &salary);
if (salary <= 3500)
{
rate = 0;
deduction = 0;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n", tax);
}
else
{
flag = (salary > 3500) + (salary > 5000) + (salary > 8000) + (salary > 12500) + (salary > 38500) + (salary > 58500) + (salary > 83500);
switch (flag)
{
case 1:
{
rate = 0.03;
deduction = 0;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n", tax);
break;
}
case 2:
{
rate = 0.10;
deduction = 105;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n", tax);
break;
}
case 3:
{
rate = 0.20;
deduction = 555;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n", tax);
break;
}
case 4:
{
rate = 0.25;
deduction = 1005;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n",tax);
break;
}
case 5:
{
rate = 0.30;
deduction = 2755;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n", tax);
break;
}
case 6:
{
rate = 0.35;
deduction = 5505;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n", tax);
break;
}
case 7:
{
rate = 0.45;
deduction = 13505;
tax = rate * (salary - 3500) - deduction;
printf("tax=%.2f\n", tax);
break;
}
}
}
return 0;
}
本程序是为了帮助大家计算个人所得税的。
本文章有一个亮点就是帮助在使用switch时,我进行了关系运算符,来确定常量,这样不仅符合了switch的用法更便于使用,如果用if—else不便于代码的优化。