税级计算器自我分配
Tax bracket calculator self assignment
我的任务是以任何可能的方式创建一个税收计算器,最初是在不包括税级的情况下纳税。
我将如何调整此代码以改用循环或如何使其更高效?
#include <stdio.h>
#include <stdlib.h>
int main()
{
double earnings; //user income
double tax; //sum of total tax
printf("=====================<><>TAX CALCULATOR (INC TAX BRACKETS)<><>=====================\n How much have you earned: \n");
scanf("%lf", &earnings);
double tax1 = earnings -12000; //seperating each tax bracket
double tax2 = earnings - 20000;
double tax3 = earnings - 40000;
double tax4;
double tax5;
if (earnings >= 12000 && earnings <=19999){
tax = (tax1*0.1);
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
}
else if(earnings >= 20000 && earnings <=39999){
//printf("\n============tax2: %0.2f============\n",tax2);
//printf("\n============Tax2: %0.2f============\n",tax2*0.15);
tax = (tax1*0.1 + tax2*0.15);
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
}
else if(earnings >= 40000){
tax = (tax1*0.1 + tax2*0.15 + tax3*0.2);
//printf("\n============tax3: %0.2f============\n",tax3);
//printf("\n============Tax3: %0.2f============\n",tax3*0.2);
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
}
else{
tax = 0;
printf("You made %0.2f", earnings);
printf("Paying: %0.2f", tax);
}
return 0;
}
您可以简化您的条件,例如:
if (earnings < 12000) {
} else if (earnings < 20000) {
} else if (earnings < 40000) {
} else { // earnings >= 40000
}
您还可以减少“有时不需要”的预计算,合并对 printf 的连续调用并避免在任何地方重复它们,并将 运行-time 计算替换为编译时计算,如:
const double constTax1 = (20000 - 12000) * 0.1;
const double constTax2 = (40000 - 20000) * 0.25 + constTax1;
double tax = 0.0;
if (earnings < 12000) {
// Do nothing!
} else if (earnings < 20000) {
tax = (earnings - 12000) * 0.1;
} else if (earnings < 40000) {
tax = (earnings - 20000) * 0.25 + constTax1;
} else { // earnings >= 40000
tax = (earnings - 20000) * 0.45 + constTax2;
}
printf("\nYou've made: %0.2f\nPaying: %0.2f in tax", earnings - tax, tax);
幸运的是,编译器很可能会自己完成大部分优化。最大的性能提升将来自合并对 printf()
的调用(对 reduce/avoid 函数调用开销)。
请注意,编译器通常无法正确优化对 printf()
的调用,因为它位于不同的编译单元中(即使您使用 link 时间优化,也可能 printf()
在动态 linked 库中。
您可以执行类似以下操作,以避免多次写入相同的数字并更容易添加额外的税级。 (这几乎肯定不会让它变得更快,但我确实认为它可能更易于维护。)
const int nb = 3;
const double taxrate[nb] = {0.1, 0.15, 0.2};
const double taxcutoff[nb] = {12000, 20000, 40000};
double tax = 0;
for (int i = 0; i < nb && earnings > taxcutoff[i]; i++) {
tax += (earnings - taxcutoff[i]) * taxrate[i];
}
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
我相信以上内容与您的原始代码相同,但这并不是真正的税级的工作方式。您更有可能想要这样的东西:
const int nb = 3;
const double taxrate[nb] = {0.1, 0.15, 0.2};
const double taxcutoff[nb] = {12000, 20000, 40000};
double bracketearnings;
double tax = 0;
for (int i = 0; i < nb && earnings > taxcutoff[i]; i++) {
if (i == nb-1 || earnings < taxcutoff[i+1]) {
bracketearnings = earnings - taxcutoff[i];
}
else {
bracketearnings = taxcutoff[i+1] - taxcutoff[i];
}
tax += bracketearnings * taxrate[i];
}
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
编辑以按要求解释循环:在这两种情况下,我都使用 nb=3
来跟踪有多少税级。循环将遍历数组的每个项目(或直到“收入 > taxcutoff[I]”不成立)。
在第二个示例中,它检查下一个税级是否不存在 (i == nb-1
) 或收入是否“结束”在此税级中 (earnings < taxcutoff[i+1]
)。如果是这样,它使用收入减去当前税收截止作为 bracketearnings
,否则它使用下一个括号截止减去当前截止作为 bracketearnings
。在任何一种情况下,它都会将 bracketearnings
乘以当前税率并将其添加到 tax
。税收是一个 运行 总额,在循环的每次迭代中都会添加。
与您最初的想法相比的优势在于,如果您想添加另一个税级(或 100 个以上),您只需更改前 3 行,因为它们每一行的逻辑都是相同的。
我的任务是以任何可能的方式创建一个税收计算器,最初是在不包括税级的情况下纳税。 我将如何调整此代码以改用循环或如何使其更高效?
#include <stdio.h>
#include <stdlib.h>
int main()
{
double earnings; //user income
double tax; //sum of total tax
printf("=====================<><>TAX CALCULATOR (INC TAX BRACKETS)<><>=====================\n How much have you earned: \n");
scanf("%lf", &earnings);
double tax1 = earnings -12000; //seperating each tax bracket
double tax2 = earnings - 20000;
double tax3 = earnings - 40000;
double tax4;
double tax5;
if (earnings >= 12000 && earnings <=19999){
tax = (tax1*0.1);
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
}
else if(earnings >= 20000 && earnings <=39999){
//printf("\n============tax2: %0.2f============\n",tax2);
//printf("\n============Tax2: %0.2f============\n",tax2*0.15);
tax = (tax1*0.1 + tax2*0.15);
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
}
else if(earnings >= 40000){
tax = (tax1*0.1 + tax2*0.15 + tax3*0.2);
//printf("\n============tax3: %0.2f============\n",tax3);
//printf("\n============Tax3: %0.2f============\n",tax3*0.2);
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
}
else{
tax = 0;
printf("You made %0.2f", earnings);
printf("Paying: %0.2f", tax);
}
return 0;
}
您可以简化您的条件,例如:
if (earnings < 12000) {
} else if (earnings < 20000) {
} else if (earnings < 40000) {
} else { // earnings >= 40000
}
您还可以减少“有时不需要”的预计算,合并对 printf 的连续调用并避免在任何地方重复它们,并将 运行-time 计算替换为编译时计算,如:
const double constTax1 = (20000 - 12000) * 0.1;
const double constTax2 = (40000 - 20000) * 0.25 + constTax1;
double tax = 0.0;
if (earnings < 12000) {
// Do nothing!
} else if (earnings < 20000) {
tax = (earnings - 12000) * 0.1;
} else if (earnings < 40000) {
tax = (earnings - 20000) * 0.25 + constTax1;
} else { // earnings >= 40000
tax = (earnings - 20000) * 0.45 + constTax2;
}
printf("\nYou've made: %0.2f\nPaying: %0.2f in tax", earnings - tax, tax);
幸运的是,编译器很可能会自己完成大部分优化。最大的性能提升将来自合并对 printf()
的调用(对 reduce/avoid 函数调用开销)。
请注意,编译器通常无法正确优化对 printf()
的调用,因为它位于不同的编译单元中(即使您使用 link 时间优化,也可能 printf()
在动态 linked 库中。
您可以执行类似以下操作,以避免多次写入相同的数字并更容易添加额外的税级。 (这几乎肯定不会让它变得更快,但我确实认为它可能更易于维护。)
const int nb = 3;
const double taxrate[nb] = {0.1, 0.15, 0.2};
const double taxcutoff[nb] = {12000, 20000, 40000};
double tax = 0;
for (int i = 0; i < nb && earnings > taxcutoff[i]; i++) {
tax += (earnings - taxcutoff[i]) * taxrate[i];
}
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
我相信以上内容与您的原始代码相同,但这并不是真正的税级的工作方式。您更有可能想要这样的东西:
const int nb = 3;
const double taxrate[nb] = {0.1, 0.15, 0.2};
const double taxcutoff[nb] = {12000, 20000, 40000};
double bracketearnings;
double tax = 0;
for (int i = 0; i < nb && earnings > taxcutoff[i]; i++) {
if (i == nb-1 || earnings < taxcutoff[i+1]) {
bracketearnings = earnings - taxcutoff[i];
}
else {
bracketearnings = taxcutoff[i+1] - taxcutoff[i];
}
tax += bracketearnings * taxrate[i];
}
printf("\nYou've made: %0.2f", earnings - tax);
printf("\nPaying: %0.2f in tax", tax);
编辑以按要求解释循环:在这两种情况下,我都使用 nb=3
来跟踪有多少税级。循环将遍历数组的每个项目(或直到“收入 > taxcutoff[I]”不成立)。
在第二个示例中,它检查下一个税级是否不存在 (i == nb-1
) 或收入是否“结束”在此税级中 (earnings < taxcutoff[i+1]
)。如果是这样,它使用收入减去当前税收截止作为 bracketearnings
,否则它使用下一个括号截止减去当前截止作为 bracketearnings
。在任何一种情况下,它都会将 bracketearnings
乘以当前税率并将其添加到 tax
。税收是一个 运行 总额,在循环的每次迭代中都会添加。
与您最初的想法相比的优势在于,如果您想添加另一个税级(或 100 个以上),您只需更改前 3 行,因为它们每一行的逻辑都是相同的。