编写c语言程序,输入购物款数,计算并输出优惠价。(要求用switch语句编写)

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

#include <stdio.h>
int main()
{
float totalprice=0,level=0; //总的消费额,打折等级标志
scanf("%d",&totalprice);
if(totalprice<=1000) level=1; //一共分为五等,不同等级,对应不同的优惠策略。
else if(totalprice>1000 && totalprice<=2000) level=2;
else if(totalprice>2000 && totalprice<=3000) level=3;
else if(totalprice>3000 && totalprice<=5000) level=4;
else level=5;
switch(level) //一共分为五等,不同等级,对应不同的优惠策略。
{
case 1: printf("%f",totalprice);break;
case 2: printf("%f",totalprice*0.95);break;
case 3: printf("%f",totalprice*0.90);break;
case 4: printf("%f",totalprice*0.85);break;
default: printf("%f",totalprice*0.80);break;
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答