当前位置: 代码迷 >> C语言 >> [求助]switch的问题
  详细解决方案

[求助]switch的问题

热度:327   发布时间:2007-02-04 11:50:06.0
[求助]switch的问题
要求是在 I<= 100000时,i=I*0.1,100000< I <= 200000时,i = 100000 * 0.1 + (I - 100000) * 0.075,200000< I <= 400000时,i = 100000 * 0.1 + 100000 * 0.075 + (I - 200000) * 0.05,400000< I <= 600000时,i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (I - 400000) * 0.03,600000<I <= 1000000时,i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (I - 600000)*0.015,用switch语句来做。
我做这个有问题,如果大家有更好的办法,或者能改正这个都行,谢谢了

#include<stdio.h>
void main()
{
int I, x;
float i;
printf("Please enter the profit: \n");
scanf("%d", &I);
x = I/100000;
switch(x)
{
case'0': printf("The praise of the year is %f\n", i = I * 0.1);
break;
case'1': printf("The praise of the year is %f\n", i = 100000 * 0.1 + (I - 100000) * 0.075);
break;
case'2': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + (I - 200000) * 0.05);
break;
case'3': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + (I - 200000) * 0.05);
break;
case'4': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (I - 400000) * 0.03);
break;
case'5': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (I - 400000) * 0.03);
break;
case'6': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (I - 600000)*0.015);
break;
case'7': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (I - 600000)*0.015);
break;
case'8': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (I - 600000)*0.015);
break;
case'9': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (I - 600000)*0.015);
break;
case'10': printf("The praise of the year is %f\n", i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (I - 600000)*0.015);
break;
}
}
搜索更多相关的解决方案: switch  

----------------解决方案--------------------------------------------------------
case 0 不是case '0'
后者表示的是0的ASCII码,好象是48
----------------解决方案--------------------------------------------------------
恩,我弄错了,现在改成这样的,应该要简洁得多。
#include<stdio.h>
void main()
{
int I, x;
float i;
printf("Please enter the profit: \n");
scanf("%d", &I);
x = (I - 1)/100000;
switch(x)
{
case 0: i = I * 0.1;
break;
case 1: i = 100000 * 0.1 + (I - 100000) * 0.075;
break;
case 2:
case 3: i = 100000 * 0.1 + 100000 * 0.075 + (I - 200000) * 0.05;
break;
case 4:
case 5: i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + (I - 400000) * 0.03;
break;
case 6:
case 7:
case 8:
case 9: i = 100000 * 0.1 + 100000 * 0.075 + 200000 * 0.05 + 200000 * 0.03 + (I - 600000)*0.015;
break;
}
printf("The praise of the year is %f\n", i);
}

[此贴子已经被作者于2007-2-4 12:29:57编辑过]


----------------解决方案--------------------------------------------------------
  相关解决方案