#include <stdio.h>
#define BASE_SALARY 1000
#define OVERTIME 40
#define SCALE 1.5
#define BREAK1 300.0
#define BREAK2 150.0
#define TAX1 .15
#define TAX2 .2
#define TAX3 .25
int main()
{
unsigned short hours;
float salary;
printf("enter your work hours for a week:\n");
while(scanf("%hu", &hours)==1)
{
if(hours<OVERTIME)
salary = (float)hours*BASE_SALARY;
else
salary = (OVERTIME + (hours-OVERTIME)*SCALE)*BASE_SALARY;
if(salary<BREAK1)
salary *= (1-TAX1);
else if(salary>=BREAK1 && salary<=(BREAK1+BREAK2))
salary = BREAK1*(1-TAX1) + (salary-BREAK1)*(1-TAX2);
else if(salary>BREAK1+BREAK2)
salary = BREAK1*(1-TAX1) + BREAK2*(1-TAX2) + (salary-BREAK1-BREAK2)*(1-TAX3);
printf("your salary is $%0.2f.\n", salary);
}
return 0;
}
enter your work hours for a week:
1
your salary is $787.50.
39
your salary is $29287.50.
40
your salary is $30037.50.
45
your salary is $35662.50.
#include <stdio.h>
#define ARTICHOKE 2.05
#define SUGARBEET 1.15
#define CARROT 1.09
#define DISCOUNT .05
#define FREIGHT1 6.5
#define FREIGHT2 14
#define FREIGHT3 .5
int main()
{
char ch;
float artichoke=0, sugarbeet=0, carrot=0, artichoke_buffer=0, sugarbeet_buffer=0, carrot_buffer=0, totalWeight=0;//重量
float artichokePrice=0, sugarbeetPrice=0, carrotPrice=0, totalPrice=0;//价格
printf("enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)\n");
while((ch=getchar())!='q')
{
switch(ch)
{
case 'a':
{
printf("enter the pounds of the artichoke:\n");
scanf("%f", &artichoke_buffer);
artichoke += artichoke_buffer;
artichokePrice = artichoke*ARTICHOKE;
printf("artichoke weight:%.2f price: $%.2f\n", artichoke, artichokePrice);
printf("enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)\n");
break;}
case 'b':
{printf("enter the pounds of the sugarbeet:\n");
scanf("%f", &sugarbeet_buffer);
sugarbeet += sugarbeet_buffer;
sugarbeetPrice = sugarbeet*SUGARBEET;
printf("sugarbeet weight:%.2f price: $%.2f\n", sugarbeet, sugarbeetPrice);
printf("enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)\n");
break;}
case 'c':
{
printf("enter the pounds of the carrot:\n");
scanf("%f", &carrot_buffer);
carrot += carrot_buffer;
carrotPrice = carrot*CARROT;
printf("carrot weight:%.2f price: $%.2f\n", carrot, carrotPrice);
printf("enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)\n");
break;}
case '\n':
break;
default:
printf("only a, b, c, and q are accepted. Try again!\n");
}
}
totalPrice = artichokePrice + sugarbeetPrice + carrotPrice;
totalWeight = artichoke + sugarbeet + carrot;
if(totalPrice>=100)
{
totalPrice *= (1-DISCOUNT);
printf("Discount: %2.0f%%\n", DISCOUNT*100);
}
if(totalWeight<=5)
{
totalPrice += FREIGHT1;
printf("Freight: $%.2f\n", FREIGHT1);
}
else if(totalWeight>5 && totalWeight<=20)
{totalPrice += FREIGHT2;
printf("Freight: $%.2f\n", FREIGHT2);}
else if(totalWeight>20)
{totalPrice = totalPrice + FREIGHT2 + (totalWeight-20)*FREIGHT3;
printf("Freight: $%.2f\n\n", FREIGHT2 + (totalWeight-20)*FREIGHT3);}
printf("unit price(per pound) weight(pound) price\n");
printf("artichoke: $%-0.2f %-0.2f $%-0.2f\n", ARTICHOKE, artichoke, artichokePrice);
printf("sugarbeet: $%-0.2f %-0.2f $%-0.2f\n", SUGARBEET, sugarbeet, sugarbeetPrice);
printf("carrot: $%-0.2f %-0.2f $%-0.2f\n", CARROT, carrot, carrotPrice);
printf("\nTotal price: $%.2f\nThanks for your coming!\n", totalPrice);
return 0;
}
通过这次练习,我发现
- case语句不需要用花括号括起来,加不加花括号都不影响程序结果,所以这里有点像python等语言咯,不依靠花括号明确划清段落,之前还真是根本没想过这个问题,要不要在case的多条语句加个花括号括起来,通过这个练习明白了,不需要,下一个case相当于上一个case的结束。
- 确实要先分析清楚,然后写点伪代码,具体要设置几个变量,程序结构啥的,我这次就是和大多数人一样,一上来就直接开始编程,结果不断增加变量,还修改了好多次,比如之前在case语句里用了while,结果发现根本不符合题目要求,也是在编程过程中才发现换行符得处理一下
- 由于while一直在等字符输入,而每个case里面是输入的浮点数,所以不会影响,但是换行符和其他字母等字符,会影响到while,所以我在default里说了只支持那几个字母,并且对于换行符的输入多加了一个case,直接break跳过,很像continue的功能。
- 输出的对齐,还花了点时间
- 价钱加个美元符号
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
a
enter the pounds of the artichoke:
10
artichoke weight:10.00 price: $20.50
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
b
enter the pounds of the sugarbeet:
1
sugarbeet weight:1.00 price: $1.15
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
c
enter the pounds of the carrot:
13
carrot weight:13.00 price: $14.17
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
a
enter the pounds of the artichoke:
23
artichoke weight:33.00 price: $67.65
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
q
Freight: $27.50
unit price(per pound) weight(pound) price
artichoke: $2.05 33.00 $67.65
sugarbeet: $1.15 1.00 $1.15
carrot: $1.09 13.00 $14.17
Total price: $110.47
Thanks for your coming!
有折扣的
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
a
enter the pounds of the artichoke:
100
artichoke weight:100.00 price: $205.00
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
a
enter the pounds of the artichoke:
120
artichoke weight:220.00 price: $451.00
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
b
enter the pounds of the sugarbeet:
23
sugarbeet weight:23.00 price: $26.45
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
c
enter the pounds of the carrot:
1
carrot weight:1.00 price: $1.09
enter a, b, c to enter the weight of artichoke, sugarbeet, carrot:(enter q to quit)
q
Discount: 5%
Freight: $126.00
unit price(per pound) weight(pound) price
artichoke: $2.05 220.00 $451.00
sugarbeet: $1.15 23.00 $26.45
carrot: $1.09 1.00 $1.09
Total price: $580.61
Thanks for your coming!