c語言編程 if switch 語句編程序(拜托啦,很急啊!!!~)
企業發放的獎金根據利潤提成。利潤I低於或等於100,000元的,獎金可提成10% ;利潤高於100,000元,低於200,000元(100,0001,000,000元的部分按1%提成。從鍵盤輸入當月利潤I,求應發獎金總數。
要求分別用if語句和switch語句編程序。
最佳回答:
--------------------------------------------- if語句 --------------------------------------------- #include void main() { long i; double b,b1=0,b2=0,b4=0,b6=0,b10=0,other=0; printf("請輸入員工利潤:"); scanf("%ld",&i); b1=100000*0.1; /*利潤為10W時所得的獎金*/ b2=(200000-100000)*0.075+b1; /*利潤為20W時所得的獎金*/ b4=(400000-200000)*0.05+b2; /*利潤為40W時所得的獎金*/ b6=(600000-400000)*0.03+b4; /*利潤為60W時所得的獎金*/ b10=(1000000-600000)*0.015+b6; /*利潤為100W時所得的獎金*/ other=(i-1000000)*0.01+b10; if(i<=100000) { b=i*0.1; } else if(i<=200000 && i>100000) { b=(i-100000)*0.075+b1; } else if(i<=400000 && i>200000) { b=(i-200000)*0.05+b2; } else if(i<=600000 && i>400000) { b=(i-400000)*0.03+b4; } else if(i<=1000000 && i>600000) { b=(i-600000)*0.015+b6; } else if(i>1000000) { b=other; } printf("該員工所得獎金為:%.2f\n\n",b); } --------------------------------------------- switch語句 --------------------------------------------- #include void main() { long i; double b,b1=0,b2=0,b4=0,b6=0,b10=0,other=0; printf("請輸入員工利潤:"); scanf("%ld",&i); b1=100000*0.1; /*利潤為10W時所得的獎金*/ b2=(200000-100000)*0.075+b1; /*利潤為20W時所得的獎金*/ b4=(400000-200000)*0.05+b2; /*利潤為40W時所得的獎金*/ b6=(600000-400000)*0.03+b4; /*利潤為60W時所得的獎金*/ b10=(1000000-600000)*0.015+b6; /*利潤為100W時所得的獎金*/ other=(i-1000000)*0.01+b10; switch((i-1)/100000) { case 0:b=i*0.1;break; case 1:b=(i-100000)*0.075+b1; break; case 2: case 3:b=(i-200000)*0.05+b2; break; case 4: case 5:b=(i-400000)*0.03+b4; break; case 6: case 7: case 8: case 9:b=(i-600000)*0.015+b6;break; default:b=other; break; } printf("該員工所得獎金為:%.2f\n\n",b); }
查看更多答案>>