#include <stdio.h>
#define yangji 2.05
#define beet 1.15
#define carrot 1.09
#define discount 0.05
#define first_rate 6.5
#define second_rate 14
#define third_rate 0.5
int main(void)
{
double pound,n, discount_money, total_money, freight;
char type;
double total_weight = 0.0;
double yangji_weight = 0.0;
double beet_weight = 0.0;
double carrot_weight = 0.0;
printf("please enter the goods you want to (q to quit): ");
while ((type = getchar()) != 'q')
{
switch (type)
{
case 'a':
printf("please enter the pound you want to buy: ");
scanf("%lf",&n);
yangji_weight += n;
total_weight += n;
break;
case 'b':
printf("please enter the pound you want to buy: ");
scanf("%lf",&n);
beet_weight += n;
total_weight += n;
break;
case 'c':
printf("please enter the pound you want to buy: ");
scanf("%lf",&n);
carrot_weight += n;
total_weight += n;
break;
}
printf("please enter another goods your want to : ");
}
total_money = yangji * yangji_weight + beet * beet_weight + carrot * carrot_weight;
if (total_money > 100)
discount_money = total_money * discount ;
if (total_weight < 5)
freight = first_rate;
else if (total_weight < 20)
freight = second_rate;
else
freight = second_rate + third_rate*(total_weight - 20.0);
printf("the price of goods are yangji : %.1f beet : %.2f carrot : %.2f \n",yangji, beet, carrot);
printf("your order form are yangji : %.f beet : %.1f carrot : %.1f \n",yangji_weight, beet_weight, carrot_weight);
printf("the total price is %.1f\n",total_money);
printf("the discount is %.1f\n",discount_money);
printf("the freight is %.1f\n",freight);
printf("the finally money is %.3f",total_money + freight - discount_money);
return 0;
}
第一次写完的版本,没有出错但是出现一个问题:
please enter the goods you want to (q to quit): a
please enter the pound you want to buy: 5please enter another goods your want to : please enter another goods your want to :
这边出现了调用两次的printf,一开始不明白为什么。后来看到答案明白了 ,原来我在输出5后 按下了Enter键来表示输入完毕,而Enter键的信息‘\n’,就就随之存在了栈区,之前我的做法是在while 循环的最后加上 scanf()来更新栈区的内容。
而答案的版本更好 ,在while 的开头使用了 if( type == “\n”) continue; 这样就将那一次Enter键的值给跳过了,还有在答案中加了一句:
while (getchar ()!= ‘\n’)
continue;
这是用来跳过输入种类那行的剩余部分,因为getchar()是将输入的字符串一个一个的读取,用这个程序就可以值接收首字母。
假设输入asd 只会接收a,这样避免了误输入;以后的学习中对这种细节要注意!
#include <stdio.h>
#define yangji 2.05
#define beet 1.15
#define carrot 1.09
#define discount 0.05
#define first_rate 6.5
#define second_rate 14
#define third_rate 0.5
int main(void)
{
double pound,n, discount_money, total_money, freight;
char type;
double total_weight = 0.0;
double yangji_weight = 0.0;
double beet_weight = 0.0;
double carrot_weight = 0.0;
printf("please enter the goods you want to (q to quit): ");
while ((type = getchar()) != 'q')
{
if ( type == '\n') //用来跳过输入重量时所按下的Enter键
continue;
while (getchar() !='\n')
continue; // 用来跳过输入行的剩余部分
switch (type)
{
case 'a':
printf("please enter the pound you want to buy: ");
scanf("%lf",&n);
yangji_weight += n;
total_weight += n;
break;
case 'b':
printf("please enter the pound you want to buy: ");
scanf("%lf",&n);
beet_weight += n;
total_weight += n;
break;
case 'c':
printf("please enter the pound you want to buy: ");
scanf("%lf",&n);
carrot_weight += n;
total_weight += n;
break;
}
printf("please enter another goods your want to : ");
}
total_money = yangji * yangji_weight + beet * beet_weight + carrot * carrot_weight;
if (total_money > 100)
discount_money = total_money * discount ;
if (total_weight < 5)
freight = first_rate;
else if (total_weight < 20)
freight = second_rate;
else
freight = second_rate + third_rate*(total_weight - 20.0);
printf("the price of goods are yangji : %.1f beet : %.2f carrot : %.2f \n",yangji, beet, carrot);
printf("your order form are yangji : %.f beet : %.1f carrot : %.1f \n",yangji_weight, beet_weight, carrot_weight);
printf("the total price is %.1f\n",total_money);
printf("the discount is %.1f\n",discount_money);
printf("the freight is %.1f\n",freight);
printf("the finally money is %.3f",total_money + freight - discount_money);
return 0;
}