c语言一道编程题!会的帮忙 谢谢

来源:百度知道 编辑:UC知道 时间:2024/07/01 05:26:44
某大型超市进行打折促销活动,消费金额(p)越高,折扣(d)越大标准如下
消费金额 折扣
P<100 0%
100小于等于p<200 5%
200小于等于p<500 10%
500小于等于p<1000 15%
p大于等于1000 20%
编程,从键盘输入消费金额,输出折扣率和实付金额(f)要求
用switch语句实现

#include<stdio.h>
main()
{int d;
float f,p;
printf("请输入消费金额:");
scanf("%f",&p);
switch(int(p/100.0))
{case 0:d=0;f=p;break;
case 1:d=5;f=p*0.95;break;
case 2:
case 3:
case 4:d=10;f=p*0.9;break;
case 5:
case 6:
case 7:
case 8:
case 9:d=15;f=p*0.85;break;
default:d=20;f=p*0.8;break;
}
printf("折扣是%d%%,金额是%f.\n",d,f);
while(1);
return 0;
}
我验证过了,完全符合要求,希望对你有所帮助!

#include <stdio.h>
main(){
float price;
int flag=1;
while(flag){
printf("请输入消费金额");
scanf(price,%f);
if(price<0)
{printf("对不起,您输入的数据有误,请重新输入 /n");
continue;}
else if(price<100)
{printf("折扣率是0%/n");
printf("您的应付金额为" & price,%f);
break;
}