感谢 @墨瑾空城染青衣 提出的错误地方,现已修改。
C程序设计(第四版) 谭浩强 习题4.10
习题 4.10 企业发放的奖金根据利润提成。利润 I I I低于或等于100000元的,奖金可提0.1;利润高于100000元,低于200000( 100000 < I ≤ 200000 100000 \lt I \le 200000 100000<I≤200000)时,低于100000元的部分按10%提成,高于100000元的部分,可提成7.5%; 200000 < I ≤ 400000 200000 \lt I \le 400000 200000<I≤400000时,低于200000元的部分仍按上述办法提成(下同)。高于200000元的部分按5%提成; 400000 < I ≤ 600000 400000 \lt I \le 600000 400000<I≤600000元时,高于400000元的部分按3%提成; 600000 < I ≤ 1000000 600000 \lt I \le 1000000 600000<I≤1000000时,高于600000元的部分按1.5%提成; I > 1000000 I \gt 1000000 I>1000000时,超过1000000元的部分按1%提成。从键盘输入当月利润 I I I,求应发奖金总数。
要求:
(1)用if语句编程序;
(2)用switch语句编程序。
IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。
代码块
方法1:(用if语句)
#include <>
#include <>
int main(){
double i, bonus;
printf("Please enter profit: ");
scanf("%lf", &i);
//输入低于0的数字,报错并重新输入
while (i < 0){
printf("Error!\n");
printf("Please enter profit: ");
scanf("%lf", &i);
}
if (i <= 100000)
bonus = i * 0.1;
else if (i > 100000 && i <= 200000)
bonus = 10000 + (i - 100000) * 0.075;
else if (i > 200000 && i <= 400000)
bonus = 18500 + (i - 200000) * 0.05;
else if (i > 400000 && i <= 600000)
bonus = 37500 + (i - 400000) * 0.03;
else if (i > 600000 && i <= 1000000)
bonus = 43500 + (i - 600000) * 0.015;
else
bonus = 49500 + (i - 1000000) * 0.01;
printf("Bonus = %.2lf\n", bonus);
system("pause");
return 0;
}
方法2:(用switch语句)
#include <>
#include <>
int main(){
int i, s;
double bonus;
printf("Please enter profit: ");
scanf("%d", &i);
while (i < 0){
printf("Error!\n");
printf("Please enter profit: ");
scanf("%d", &i);
}
//通过分档给各档级别赋值
if (i <= 100000)
s = 1;
else if (i > 100000 && i <= 200000)
s = 2;
else if (i > 200000 && i <= 400000)
s = 3;
else if (i > 400000 && i <= 600000)
s = 4;
else if (i > 600000 && i <= 1000000)
s = 5;
else
s = 6;
//根据级别值执行相应语句
switch(s){
case 1: bonus = i * 0.1; break;
case 2: bonus = 10000 + (i - 100000) * 0.075; break;
case 3: bonus = 18500 + (i - 200000) * 0.05; break;
case 4: bonus = 37500 + (i - 400000) * 0.03; break;
case 5: bonus = 43500 + (i - 600000) * 0.015; break;
case 6: bonus = 49500 + (i - 1000000) * 0.01; break;
}
printf("Bonus = %.2lf\n", bonus);
system("pause");
return 0;
}
以上特别说明一下,如果按照教材上的switch实例,使用相同方法,比如通过定义一个s,使得s=i/100000,用switch(s)来实现分档,前两档是没有问题的,但是,在输入600000和1000000两个数值后,就会出现得到错误的分档结果。
方法3:使用函数的模块化设计
#include <>
#include <>
void input(); //定义输入函数
void bonus(int x); //定义奖金计算函数
int i; //定义全局变量,利润
int main()
{
input(); //调用输入函数
bonus(i); //调用奖金计算函数
system("pause");
return 0;
}
//输入函数
void input()
{
printf("Please enter profit: ");
scanf("%d", &i);
//一旦出现负值,报错并重新输入
while (i < 0){
printf("Error!\n");
input();
}
}
//奖金计算函数
void bonus(int x)
{
int s;
double bonus;
if (x <= 100000)
s = 1;
else if (x > 100000 && i <= 200000)
s = 2;
else if (x > 200000 && i <= 400000)
s = 3;
else if (x > 400000 && i <= 600000)
s = 4;
else if (x > 600000 && i <= 1000000)
s = 5;
else
s = 6;
switch(s){
case 1: bonus = x * 0.1; break;
case 2: bonus = 10000 + (x - 100000) * 0.075; break;
case 3: bonus = 18500 + (x - 200000) * 0.05; break;
case 4: bonus = 37500 + (x - 400000) * 0.03; break;
case 5: bonus = 43500 + (x - 600000) * 0.015; break;
case 6: bonus = 49500 + (x - 1000000) * 0.01; break;
}
printf("Bonus = %.2lf\n", bonus);
}