c语言 如何用switch语句编写一个有关商场购物金额优惠的选择程序

某百货公司为了促销,采用购物打折的办法。
(1) 在1000元以上者,按九五折优惠;
(2) 在2000元以上者,按九折优惠;
(3) 在3000元以上者,按八五折优惠;
(4) 在5000元以上者,按八折优惠。
编写c语言程序,输入购物款数,计算并输出优惠价。(要求用switch语句编写)

#include <stdio.h>

int main(int argc, char** argv)
{
int amount = 0;
scanf("%d", &amount);//输入顾客购买的总额
int status = amount/1000;
switch (status)
{
case 0: break;
case 1: amount = amount * 0.95; break;
case 2: amount = amount*0.90; break;
case 3: amount *= 0.85; break;
default: amount *= 0.80; break;
}
printf("%d\n", amount);//打印出打折后的总额

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-24
#include<stdio.h>

main()
{
double p;
while(scanf("%lf",&p) != EOF)
{
switch( (long)(p/1000) )
{
case 0: break;
case 1: p*= 0.95; break;
case 2: p*= 0.9; break;
case 3:
case 4: p*= 0.85; break;
default: p*=0.8; break;

}
printf("discount price = %lf\n\n",p);
}

}
相似回答