写一个用if- else语句和switch语句计算税金的c++程序
时间: 2023-04-08 19:05:29 浏览: 196
以下是用if-else语句和switch语句计算税金的C程序:
使用if-else语句:
#include <stdio.h>
int main() {
float income, tax;
printf("请输入您的收入:");
scanf("%f", &income);
if (income <= 0) {
printf("您输入的收入不合法!\n");
} else if (income <= 5000) {
tax = 0;
} else if (income <= 10000) {
tax = (income - 5000) * 0.05;
} else if (income <= 20000) {
tax = 250 + (income - 10000) * 0.1;
} else if (income <= 40000) {
tax = 1250 + (income - 20000) * 0.15;
} else if (income <= 60000) {
tax = 3750 + (income - 40000) * 0.2;
} else if (income <= 80000) {
tax = 7750 + (income - 60000) * 0.25;
} else {
tax = 14750 + (income - 80000) * 0.3;
}
printf("您应缴纳的税金为:%.2f元\n", tax);
return 0;
}
使用switch语句:
#include <stdio.h>
int main() {
float income, tax;
printf("请输入您的收入:");
scanf("%f", &income);
if (income <= 0) {
printf("您输入的收入不合法!\n");
} else {
switch ((int)income / 10000) {
case 0:
tax = 0;
break;
case 1:
tax = (income - 5000) * 0.05;
break;
case 2:
case 3:
tax = 250 + (income - 10000) * 0.1;
break;
case 4:
case 5:
tax = 1250 + (income - 20000) * 0.15;
break;
case 6:
case 7:
tax = 3750 + (income - 40000) * 0.2;
break;
default:
tax = 7750 + (income - 60000) * 0.25;
break;
}
printf("您应缴纳的税金为:%.2f元\n", tax);
}
return 0;
}
阅读全文