新手提问C语言的问题

来源:百度知道 编辑:UC知道 时间:2024/07/06 13:20:25
设置运费为p,货物重为w,距离为s,折扣为d,总运费为
f=p*w*s*(1-d)

折扣计算标准:
s<250 没有折扣
250≤s<500 2%折扣
500≤s<1000 5%折扣
1000≤s<2000 8%折扣
2000≤s<3000 10%折扣
3000≤s 15%折扣

求总运费f

#include<stdio.h>
#include<math.h>
void main()
{int s;
double d,f,p,w;
printf("please enter p,w,s\n");
scanf("%lf,%lf,%d\n",&p,&w,&s);

if(s>=3000) d=0.15;
else if(s>=2000&&s<3000) d=0.1;
else if(s>=1000&&s<2000) d=0.08;
else if(s>=500&&s<1000) d=0.05;
else if(s>=250&&s<500) d=0.02;
else d=0;

f=p*w*s*(1-d);
printf("total:");
printf("%10.2f",f);
}

这是我的代码,,没报错,但是总价没办法显示出来,求解惑,谢谢拉

因为这句
scanf("%lf,%lf,%d\n",&p,&w,&s);
首先要删掉最后的\n。因为scanf在看到\n时会试着跳过所有空字符,所以你的回车都被它吃了,直到遇到一个非空字符为止。也就是说,你输完所有的数以后,scanf会开始吃空格(回车也算),直到你输入一个其他字符(任何非空字符,不一定是\n)。

改成
scanf("%lf,%lf,%d",&p,&w,&s);
你运行程序,然后输入数字时应该输入
数字,数字,数字
包括其中的逗号。

我试过你的代码了..不知为什么还要输入一个数才有输出??

呵呵,你粗心了:
scanf("%lf,%lf,%d\n",&p,&w,&s);
改成:
scanf("%lf,%lf,%d",&p,&w,