From 1ad50f4df7b048ebbb66e117676645e596fb12e8 Mon Sep 17 00:00:00 2001 From: flykhan Date: Tue, 11 Jul 2023 10:30:30 +0800 Subject: [PATCH] =?UTF-8?q?day6=20homework:=20=E5=87=BD=E6=95=B0=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=AE=9E=E7=8E=B0=E7=BA=B3=E7=A8=8E=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E7=9A=84=E9=9D=99=E6=80=81=E5=86=85=E5=AD=98=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day6/homework/h9.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/day6/homework/h9.c b/day6/homework/h9.c index b8af654..19c8625 100644 --- a/day6/homework/h9.c +++ b/day6/homework/h9.c @@ -15,33 +15,39 @@ // 全月应纳税额超过80000元 45 % // 13505 #include +#include // 用于 malloc 函数调用 -void *result(int, double *, int *); +// int outNums[2]; // 3.全局地址?为什么要全局地址 + +int *result(int, float *, int *); int main() { - int pretexIncome; // 税前收入 - double 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}; // 速算扣除数数组 + 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}; // 速算扣除数数组 printf("请输入你的税前收入: "); scanf("%d", &pretexIncome); - result(pretexIncome, rates, deducts); + int *res; + res = result(pretexIncome, rates, deducts); + printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", res[0], res[1]); - // int *res = result(pretexIncome, rates, deducts); - // printf("%d%d", res[0], res[1]); - // printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", result(pretexIncome, rates, deducts)[1], result(pretexIncome, rates, deducts)[0]); + free(res); // 释放手动分配的内存空间 return 0; } -void *result(int pretexIncomeArgs, double *rateArgs, int *deductArgs) +int *result(int pretexIncomeArgs, float *rateArgs, int *deductArgs) { int level; // 税率等级 int tex; // 缴纳税额 int aftertexIncome; // 税后收入 int diff = pretexIncomeArgs - 3500; // 征税差值 + // static int outNums[2]; // 1.静态地址?为什么要静态地址 (静态变量会放在全局区,仅会在程序结束时释放) + int *outNums = (int *)malloc(2 * sizeof(int *)); // 2.手动分配内存空间?为什么 + if (diff <= 0) level = 0; else if (diff > 0 && diff <= 1500) @@ -62,8 +68,8 @@ void *result(int pretexIncomeArgs, double *rateArgs, int *deductArgs) tex = diff * rateArgs[level] - deductArgs[level]; aftertexIncome = pretexIncomeArgs - tex; - printf("你最后的所得为 %d ,你所缴纳的税额为 %d\n", aftertexIncome, tex); + outNums[0] = aftertexIncome; + outNums[1] = tex; - // int outNums[2] = {(int *)aftertexIncome, (int *)tex}; - // return *outNums; // 返回结果数组 + return outNums; // 返回结果数组 } \ No newline at end of file