请求验证符合税率规则的薪资所得税计算C程序正确性
薪资所得税计算C程序逻辑验证
需求说明
计算规则为:
- 前1000比索:按2%税率计税
- 接下来的2000比索(即1001-3000比索区间):按7%税率计税
- 超过3000比索的剩余部分:按11%税率计税
第一个版本代码分析
#include <stdio.h> int main() { float salary, incometax; printf("Enter salary: "); scanf("%f", &salary); if (salary <= 1000) { incometax = salary * 0.02; printf("Income tax to be paid: %.2f\n", incometax); } else if (salary <= 2000) { incometax = salary * 0.07; printf("Income tax to be paid: %.2f\n", incometax); } else if (salary > 2000) { incometax = salary * 0.11; printf("Income tax to be paid: %.2f\n", incometax); } return 0; }
这个版本逻辑完全错误,没有实现阶梯计税规则:
- 当薪资在1001-2000区间时,代码直接按全额7%计税,而非仅超出1000的部分按7%、前1000仍按2%计算;
- 薪资超过2000时,直接全额按11%计税,完全忽略了前1000和中间2000的低税率区间。
第二个版本代码分析
#include <stdio.h> int main() { float salary, incometax; printf("Enter salary: "); scanf("%f", &salary); if (salary <= 1000) { incometax = salary * 0.02; printf("Income tax to be paid: %.2f Pesos\n", incometax); } else if (salary < 3000) { incometax = (salary - 1000) * 0.07 + (1000 * 0.2); printf("Income tax to be paid: %.2f Pesos\n", incometax); } else { incometax = ((salary - 3000) * 0.11) + 2000 * 0.07 + 1000 * 0.02; printf("Income tax to be paid: %.2f Pesos\n", incometax); } return 0; }
这个版本的阶梯逻辑方向正确,但存在两处错误:
- 中间区间计算中,
1000 * 0.2是笔误,正确应为1000 * 0.02(把2%写成了20%); - 中间区间的条件
salary < 3000错误,应改为salary <= 3000——因为3000比索刚好是前1000+接下来2000的总和,属于中间区间的上限,不应进入超出3000的分支。
修正后的正确代码
#include <stdio.h> int main() { float salary, incometax; printf("Enter salary: "); scanf("%f", &salary); if (salary <= 1000) { incometax = salary * 0.02; } else if (salary <= 3000) { // 前1000按2%,超出1000的部分按7% incometax = (1000 * 0.02) + ((salary - 1000) * 0.07); } else { // 前1000按2%,中间2000按7%,超出3000部分按11% incometax = (1000 * 0.02) + (2000 * 0.07) + ((salary - 3000) * 0.11); } printf("Income tax to be paid: %.2f Pesos\n", incometax); return 0; }
验证示例
- 薪资=1000:1000*0.02=20比索
- 薪资=2500:(10000.02)+(15000.07)=20+105=125比索
- 薪资=3500:(10000.02)+(20000.07)+(500*0.11)=20+140+55=215比索
内容的提问来源于stack exchange,提问作者shampeyndadi
相关产品推荐
相关产品推荐

