企业发放的奖金根据利润提成。利润I低于或等于100000元的,奖金可提10%;利润高于100000元,低于或等于200000元(100000<I≤200000)时,低于或等于100000元的部分按10%提成,高于100000元的部分,可提成7.5%;200000<I<400000时,低于或等于200000元的部分仍按上述办法提成(下同)。高于200000元的部分按5%提成;400000<I<600000元时,高于400000元的部分按3%提成;600000<I<1000000时,高于600000元的部分按1.5%提
成:I>1000000时,超过1000000元的部分按1%提成。从键盘输入当月利润I,求应发奖金总数。
要求:
(1)用if语句编程序;
(2)用switch语句编程序。
(书上题目关于等号的区间说明有瑕疵,现在我把题目改对了)
(1)if语句:
#include<stdio.h>
int main()
{
double I,J;
printf("请输入当月利润:");
scanf("%lf",&I);
if(I<=100000)
J=I*0.1;
else if(I<=200000)
J=100000*0.1+(I-100000)*0.075;
else if(I<=400000)
J=100000*0.1+(200000-100000)*0.075+(I-200000)*0.05;
else if(I<=600000)
J=100000*0.1+(200000-100000)*0.075+(400000-200000)*0.05+(I-400000)*0.03;
else if(I<=1000000)
J=100000*0.1+(200000-100000)*0.075+(400000-200000)*0.05+(600000-400000)*0.03+(I-600000)*0.015;
else
J=100000*0.1+(200000-100000)*0.075+(400000-200000)*0.05+(600000-400000)*0.03+(1000000-600000)*0.015+(I-1000000)*0.01;
printf("应得的奖金是:%f\n",J);
return 0;
}
在VS2019下,需将源文件的scanf改为scanf_s:
#include<stdio.h>
int main()
{
double I, J;
printf("请输入当月利润:");
scanf_s("%lf", &I);
if (I <= 100000)
J = I * 0.1;
else if (I <= 200000)
J = 100000 * 0.1 + (I - 100000) * 0.075;
else if (I <= 400000)
J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (I - 200000) * 0.05;
else if (I <= 600000)
J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (I - 400000) * 0.03;
else if (I <= 1000000)
J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (600000 - 400000) * 0.03 + (I - 600000) * 0.015;
else
J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (600000 - 400000) * 0.03 + (1000000 - 600000) * 0.015 + (I - 1000000) * 0.01;
printf("应得的奖金是:%f\n", J);
return 0;
}
(2)switch语句:
#include<stdio.h>
int main()
{
double I,J;
printf("请输入当月利润:");
scanf("%lf",&I);
int branch=(int)(I/100000);
if(branch>10) branch=10;
switch(branch)
{
case 0:J=I*0.1;break;
case 1:J=100000*0.1+(I-100000)*0.075;break;
case 2:
case 3:J=100000*0.1+(200000-100000)*0.075+(I-200000)*0.05;break;
case 4:
case 5:J=100000*0.1+(200000-100000)*0.075+(400000-200000)*0.05+(I-400000)*0.03;break;
case 6:
case 7:
case 8:
case 9:J=100000*0.1+(200000-100000)*0.075+(400000-200000)*0.05+(600000-400000)*0.03+(I-600000)*0.015;break;
case 10:J=100000*0.1+(200000-100000)*0.075+(400000-200000)*0.05+(600000-400000)*0.03+(1000000-600000)*0.015+(I-1000000)*0.01;
}
printf("应得的奖金是:%f\n",J);
return 0;
}
在VS2019下,需将源文件的scanf改为scanf_s:
#include<stdio.h>
int main()
{
double I, J;
printf("请输入当月利润:");
scanf_s("%lf", &I);
int branch = (int)(I / 100000);
if (branch > 10) branch = 10;
switch (branch)
{
case 0:J = I * 0.1; break;
case 1:J = 100000 * 0.1 + (I - 100000) * 0.075; break;
case 2:
case 3:J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (I - 200000) * 0.05; break;
case 4:
case 5:J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (I - 400000) * 0.03; break;
case 6:
case 7:
case 8:
case 9:J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (600000 - 400000) * 0.03 + (I - 600000) * 0.015; break;
case 10:J = 100000 * 0.1 + (200000 - 100000) * 0.075 + (400000 - 200000) * 0.05 + (600000 - 400000) * 0.03 + (1000000 - 600000) * 0.015 + (I - 1000000) * 0.01;
}
printf("应得的奖金是:%f\n", J);
return 0;
}