qfedu-c-level/day6/homework/h9.c

75 lines
2.5 KiB
C
Raw Normal View History

2023-07-11 01:33:28 +08:00
// 请编程设计计算个人所得税的函数返回应缴纳所得税的金额。免税额为35007级超额累进税率
// 全月应纳税所得额 税率 速算扣除数(元)
// 全月应纳税额不超过1500元 3 %
// 0
// 全月应纳税额超过1500元至4500元 10 %
// 105
// 全月应纳税额超过4500元至9000元 20 %
// 555
// 全月应纳税额超过9000元至35000元 25 %
// 1005
// 全月应纳税额超过35000元至55000元 30 %
// 2755
// 全月应纳税额超过55000元至80000元 35 %
// 5505
// 全月应纳税额超过80000元 45 %
// 13505
#include <stdio.h>
#include <stdlib.h> // 用于 malloc 函数调用
2023-07-11 01:33:28 +08:00
// int outNums[2]; // 3.全局地址?为什么要全局地址
int *result(int, float *, int *);
2023-07-11 01:33:28 +08:00
int main()
{
int pretexIncome; // 税前收入
float rates[8] = {0, 0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45}; // 税率数组
int deducts[8] = {0, 0, 105, 555, 1005, 2755, 5505, 13505}; // 速算扣除数数组
2023-07-11 01:33:28 +08:00
printf("请输入你的税前收入: ");
scanf("%d", &pretexIncome);
int *res;
res = result(pretexIncome, rates, deducts);
printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", res[0], res[1]);
2023-07-11 01:33:28 +08:00
free(res); // 释放手动分配的内存空间
2023-07-11 01:33:28 +08:00
return 0;
}
int *result(int pretexIncomeArgs, float *rateArgs, int *deductArgs)
2023-07-11 01:33:28 +08:00
{
int level; // 税率等级
int tex; // 缴纳税额
int aftertexIncome; // 税后收入
int diff = pretexIncomeArgs - 3500; // 征税差值
// static int outNums[2]; // 1.静态地址?为什么要静态地址 (静态变量会放在全局区,仅会在程序结束时释放)
int *outNums = (int *)malloc(2 * sizeof(int *)); // 2.手动分配内存空间?为什么
2023-07-11 01:33:28 +08:00
if (diff <= 0)
level = 0;
else if (diff > 0 && diff <= 1500)
level = 1;
else if (diff > 1500 && diff <= 4500)
level = 2;
else if (diff > 4500 && diff <= 9000)
level = 3;
else if (diff > 9000 && diff <= 35000)
level = 4;
else if (diff > 35000 && diff <= 55000)
level = 5;
else if (diff > 55000 && diff <= 80000)
level = 6;
else if (diff > 80000)
level = 7;
tex = diff * rateArgs[level] - deductArgs[level];
aftertexIncome = pretexIncomeArgs - tex;
outNums[0] = aftertexIncome;
outNums[1] = tex;
2023-07-11 01:33:28 +08:00
return outNums; // 返回结果数组
2023-07-11 01:33:28 +08:00
}