C Primer Plus (7.12) 編程練習

/*C Primer Plus (7.11) 3*/

1 #include<stdio.h>
 2 int main()
 3 {
 4 double weight,height;
 5 printf("Please enter your weight and height.\n");
 6 printf("Weight (pound):");
 7 scanf("%lf",&weight);
 8 printf("Height (inch):");
 9 scanf("%lf",&height);
10 //加入建立比較友好的人機交互
11 if (weight < 100 && height > 64)
12 if (height >= 72)
13 printf("You are very tall for your weight.\n");
14 else
15 printf("You are tall for your weight.\n");
16 else if (weight > 300 && height < 48)
17 printf("You are quite short for your weight.\n");
18 else
19 printf("Your weight is ideal.\n");
20 //減少沒有用的if判斷條件
21 return 0;
22 }
23 /*
24 輸出樣例
25 
26 Please enter your weight and height.
27 Weight (pound):99
28 Height (inch):65
29 You are tall for your weight.
30 
31 Please enter your weight and height.
32 Weight (pound):98
33 Height (inch):72
34 You are very tall for your weight.
35 
36 Please enter your weight and height.
37 Weight (pound):301
38 Height (inch):46
39 You are quite short for your weight.
40 
41 Please enter your weight and height.
42 Weight (pound):200
43 Height (inch):50
44 Your weight is ideal.
45 
46 */

/*C Primer Plus (7.11) 10*/

1 #include<stdio.h>
 2 int main()
 3 {
 4 char ch;
 5 
 6 while ((ch=getchar()) != '#')
 7 {
 8 if (ch != '\n')
 9 {
10 printf("Step 1\n");
11 if (ch == 'b')
12 break;
13 else if (ch !='c')
14 {
15 if (ch != 'h')
16 printf("Step 2\n");
17 printf("Step 3\n");
18 }
19 }
20 }
21 printf("Done.\n");
22 return 0;
23 }
24 /*
25 輸出樣例
26 
27 q
28 Step 1
29 Step 2
30 Step 3
31 c
32 Step 1
33 h
34 Step 1
35 Step 3
36 b
37 Step 1
38 Done.
39 
40 */

/*C Primer Plus (7.12) 1*/

1 #include<stdio.h>
 2 int main()
 3 {
 4 int space, linebreak, others;
 5 int realothers = 0;
 6 char ch;
 7 space = linebreak = others = 0;
 8 
 9 printf("Please enter some characters (# to quit).\n");
10 while ((ch = getchar()) != '#')
11 {
12 if (ch == ' ' ? space++ : others++ && ch == '\n' ? linebreak++ : others++);
13 }
14 realothers = others / 2;
15 printf("These are the number of characters required for statistics.\n");
16 printf("Space : %d" ,space);
17 printf("\nLinebreak : %d" ,linebreak);
18 printf("\nOthers: %d" ,realothers);
19 
20 return 0;
21 }
22 /*
23 輸出樣例
24 
25 Please enter some characters (# to quit).
26 Hello,My name is Coco.
27 Hello. My name is Mike !#
28 These are the number of characters required for statistics.
29 Space : 8
30 Linebreak : 1
31 Others: 38
32 
33 */

/*C Primer Plus (7.12) 2*/

1 #include<stdio.h>
 2 int main(void)
 3 {
 4 int i = 0;
 5 char ch;
 6 
 7 printf("Please enter some characters (# to quit):");
 8 while ((ch = getchar()) != '#')
 9 {
10 if (i++ % 8 == 0)
11 {
12 putchar('\n'); //每輸出8個字符的信息就進行一次換行操作
13 }
14 if (ch == '\n')
15 {
16 printf("\'\\n\' -> %2d ",ch);
17 }
18 else if (ch == '\t')
19 {
20 printf("\'\\t\' -> %2d ",ch);
21 }
22 else
23 {
24 printf("\'%c\' -> %2d ",ch,ch);
25 }
26 }
27 printf("\nDone.");
28 
29 return 0;
30 }
31 /*
32 輸出樣例
33 
34 Please enter some characters (# to quit):KurokiTomoko#
35 
36 'K' -> 75 'u' -> 117 'r' -> 114 'o' -> 111 'k' -> 107 'i' -> 105 'T' -> 84 'o' -> 111
37 'm' -> 109 'o' -> 111 'k' -> 107 'o' -> 111
38 Done.
39 
40 */

/*C Primer Plus (7.12) 3*/

1 #include<stdio.h>
 2 int main()
 3 {
 4 int num;
 5 int even,odd; //偶數的個數,奇數的個數
 6 int e_sum,o_sum;
 7 double e_value,o_value; //偶數和的平均值,奇數和的平均值
 8 even = odd = num = e_sum = o_sum = 0;
 9 e_value = o_value =0.0;
10 printf("Please enter some integer numbers.\n");
11 printf("The result your entered (0 to quit) : ");
12 while (scanf("%d",&num) == 1 && num)
13 {
14 (num % 2 == 0 ? (even++, e_sum += num) : (odd++, o_sum += num));
15 printf("Now you can enter again (0 to quit) : ");
16 }
17 printf("There are %d even numbers.\n",even);
18 if (even > 0)
19 {
20 e_value = e_sum / (double)even;
21 printf("The average of even numbers is : %.3lf\n",e_value);
22 }
23 printf("There are %d odd numbers.\n",odd);
24 if (odd > 0)
25 {
26 o_value = o_sum / (double)odd;
27 printf("The average of odd numbers is : %.3lf",o_value);
28 }
29 printf("\nDone.");
30 return 0;
31 }
32 /*
33 輸出樣例
34 
35 Please enter some integer numbers.
36 The result your entered (0 to quit) : 1
37 Now you can enter again (0 to quit) : 2
38 Now you can enter again (0 to quit) : 3
39 Now you can enter again (0 to quit) : 4
40 Now you can enter again (0 to quit) : 5
41 Now you can enter again (0 to quit) : 6
42 Now you can enter again (0 to quit) : 7
43 Now you can enter again (0 to quit) : 8
44 Now you can enter again (0 to quit) : 9
45 Now you can enter again (0 to quit) : 0
46 There are 4 even numbers.
47 The average of even numbers is : 5.000
48 There are 5 odd numbers.
49 The average of odd numbers is : 5.000
50 Done.
51 
52 */

/*C Primer Plus (7.12) 4*/

1 #include<stdio.h>
 2 int main()
 3 {
 4 char ch;
 5 int count1 = 0;
 6 int count2 = 0;
 7 printf("Please enter the text you want (enter '#' to quit).");
 8 printf("\nNow please enter : ");
 9 while ((ch = getchar()) != '#')
10 {
11 if (ch == '.')
12 {
13 putchar('!');
14 count1++;
15 }
16 else if (ch == '!')
17 {
18 printf("!!");
19 count2++;
20 }
21 else
22 {
23 putchar(ch);
24 }
25 }
26 printf("The number of times an exclamation mark "
27 "has been replaced with a period is : %d",count1);
28 printf("\nThe number of times an exclamation mark "
29 "is replaced by two exclamations is : %d",count2);
30 printf("\nDone.");
31 
32 return 0;
33 }
34 /*
35 輸出樣例
36 
37 Please enter the text you want (enter '#' to quit).
38 Now please enter : !!!!!.....
39 !!!!!!!!!!!!!!!
40 #
41 The number of times an exclamation mark has been replaced with a period is : 5
42 The number of times an exclamation mark is replaced by two exclamations is : 5
43 Done.
44 
45 */

/*C Primer Plus (7.12) 5*/

#include<stdio.h>
int main()
{
 char ch;
 int count1 = 0;
 int count2 = 0;
 printf("Please enter the text you want (enter '#' to quit).");
 printf("\nNow please enter : ");
 while ((ch = getchar()) != '#')
 {
 switch(ch)
 {
 case '.':
 {
 putchar('!');
 count1++;
 break;
 }
 case '!':
 {
 printf("!!");
 count2++;
 break;
 }
 default:
 {
 putchar(ch);
 }
 }
 }
 printf("The number of times an exclamation mark "
 "has been replaced with a period is : %d",count1);
 printf("\nThe number of times an exclamation mark "
 "is replaced by two exclamations is : %d",count2);
 printf("\nDone.");
 return 0;
}
/*
輸出樣例
Please enter the text you want (enter '#' to quit).
Now please enter : Hello, This is Coconut !
Hello, This is Coconut !!
My name is Coconut.
My name is Coconut!
#
The number of times an exclamation mark has been replaced with a period is : 1
The number of times an exclamation mark is replaced by two exclamations is : 1
Done.
*/

/*C Primer Plus (7.12) 6*/

#include<stdio.h>
int main()
{
 int count = 0;
 char ch;
 char prev; //讀取的前一個字符
 printf("Please enter some characters ('#' to quit):");
 prev = '#'; //前一個字符為“#”的時候會停止(用於識別結束符號)
 while ((ch = getchar()) != '#')
 {
 if(prev == 'e' && ch == 'i')
 count++;
 prev = ch;
 }
 printf("There %d ei in this sentence.",count);
 return 0;
}
/*
輸出樣例
Please enter some characters ('#' to quit):Receive your eieio award.#
There 3 ei in this sentence.
*/

/*C Primer Plus (7.12) 7*/

#include<stdio.h>
#define BASIC_SALARY 10.00
#define EXTRA_WORK 1.5
#define NORMAL_TAX 0.15
#define EXTRA_TAX 0.20
#define OTHER_TAX 0.25
int main()
{
 double worktime = 0.0;
 double salary,tax,netincome;
 salary = tax = netincome = 0.0;
 printf("Please enter your "
 "working hours in a week : ");
 while (scanf("%lf",&worktime) != 1 || worktime <= 0)
 {
 while (getchar() != '\n') continue;
 printf("Please enter a right number( >= 0 ).");
 }
 salary = worktime > 40 ? (40.00 * BASIC_SALARY) + (1.5 * (worktime - 40)) * BASIC_SALARY : worktime * BASIC_SALARY;
 if (salary <= 300)
 {
 tax = 300.00 * NORMAL_TAX;
 netincome = salary - tax;
 }
 else if (salary <= 450)
 {
 tax = 300.00 * NORMAL_TAX + (salary - 300.00) * EXTRA_TAX;
 netincome = salary - tax;
 }
 else
 {
 tax = 300.00 * NORMAL_TAX + 150.00 * EXTRA_TAX + (salary - 450.00) * OTHER_TAX;
 netincome = salary - tax;
 }
 printf("There is your salary, tax and net income information.\n");
 printf("Salary : %.3lf",salary);
 printf("\nTax : %.3lf",tax);
 printf("\nNet income : %.3lf",netincome);
 return 0;
}
/*
輸出樣例
Please enter your working hours in a week : 300
There is your salary, tax and net income information.
Salary : 4300.000
Tax : 1037.500
Net income : 3262.500
Please enter your working hours in a week : 450
There is your salary, tax and net income information.
Salary : 6550.000
Tax : 1600.000
Net income : 4950.000
Please enter your working hours in a week : 521.73
There is your salary, tax and net income information.
Salary : 7625.950
Tax : 1868.988
Net income : 5756.963
*/

/*C Primer Plus (7.12) 8*/

#include<stdio.h>
#include<stdbool.h>
#define EXTRA_WORK 1.5
#define NORMAL_TAX 0.15
#define EXTRA_TAX 0.20
#define OTHER_TAX 0.25
void quit ();
void menu ();
void Salary (double Bsalary , double worktime);
int choice = 0;
double worktime = 0.0;
int main()
{
 while (true)
 {
 menu ();
 switch(choice)
 {
 case 1 :
 {
 Salary(8.75,worktime);
 break;
 }
 case 2 :
 {
 Salary(9.33,worktime);
 break;
 }
 case 3 :
 {
 Salary(10.00,worktime);
 break;
 }
 case 4 :
 {
 Salary(11.20,worktime);
 break;
 }
 case 5 :
 {
 quit();
 printf("Done.");
 return 0;
 }
 }
 }
}
void quit()
{
 printf("\t\t\n************************************************\t\t\n");
 printf("|| ||");
 printf("\n|| ||");
 printf("\n|| Thank you to use this programme! ||");
 printf("\n|| ||");
 printf("\n|| ||");
 printf("\t\t\n************************************************\t\t\n");
}
void menu()
{
 printf("\t\t\n*****************************************************************\t\t\n");
 printf("Enter the number corresponding to the desired pay rate or action:\n");
 printf("1) $8.75/hr 2) $9.33/hr");
 printf("\n3) $10.00/hr 4) $11.20/hr\n");
 printf("5) quit");
 printf("\t\t\n*****************************************************************\t\t\n");
 printf("Please enter your options: ");
 scanf("%d",&choice);
 while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
 {
 printf("Please enter the right choice:");
 scanf("%d",&choice);
 }
}
void Salary(double Bsalary , double worktime)
{
 double tax,netincome,salary;
 salary = tax = netincome = 0.0;
 printf("Please enter your working hours in a week : ");
 while (scanf("%lf",&worktime) != 1 || worktime <= 0)
 {
 while (getchar() != '\n') continue;
 printf("Please enter a right number( >= 0 ).");
 }
 salary = worktime > 40 ? (40.00 * Bsalary) + (1.5 * (worktime - 40)) * Bsalary : worktime * Bsalary;
 if (salary <= 300)
 {
 tax = 300.00 * NORMAL_TAX;
 netincome = salary - tax;
 }
 else if (salary <= 450)
 {
 tax = 300.00 * NORMAL_TAX + (salary - 300.00) * EXTRA_TAX;
 netincome = salary - tax;
 }
 else
 {
 tax = 300.00 * NORMAL_TAX + 150.00 * EXTRA_TAX + (salary - 450.00) * OTHER_TAX;
 netincome = salary - tax;
 }
 printf("There is your salary, tax and net income information.\n");
 printf("Salary : %.3lf",salary);
 printf("\nTax : %.3lf",tax);
 printf("\nNet income : %.3lf",netincome);
 }
/*
輸出樣例
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5) quit
*****************************************************************
Please enter your options: 2
Please enter your working hours in a week : 600
There is your salary, tax and net income information.
Salary : 8210.400
Tax : 2015.100
Net income : 6195.300
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5) quit
*****************************************************************
Please enter your options: 1
Please enter your working hours in a week : 325.44
There is your salary, tax and net income information.
Salary : 4096.400
Tax : 986.600
Net income : 3109.800
*****************************************************************
Enter the number corresponding to the desired pay rate or action:
1) $8.75/hr 2) $9.33/hr
3) $10.00/hr 4) $11.20/hr
5) quit
*****************************************************************
Please enter your options: 5
************************************************
|| ||
|| ||
|| Thank you to use this programme! ||
|| ||
|| ||
************************************************
Done.
*/

/*C Primer Plus (7.12) 9*/

1 #include<stdio.h>
 2 int Prime (int x)
 3 {
 4 int index = 0;
 5 for (index = 2; index < x; index++)
 6 {
 7 if (x % index == 0)
 8 return 0;
 9 }
10 return 1;
11 }
12 
13 int main()
14 {
15 int num;
16 printf("Please enter a number (<= 0 to quit) : ");
17 while (scanf("%d",&num) ==1 && num > 0)
18 {
19 if (num == 1)
20 {
21 printf("This number is not a prime.");
22 }
23 else
24 {
25 printf("These are prime numbers less than %d : ",num);
26 for (int index = 2; index < num; index++)
27 if (Prime(index))
28 {
29 printf("%-5d",index);
30 }
31 }
32 printf("\nNow you can enter again : ");
33 }
34 printf("That's all.");
35 
36 return 0;
37 }
38 /*
39 輸出樣例
40 
41 Please enter a number (<= 0 to quit) : 15
42 These are prime numbers less than 15 : 2 3 5 7 11 13
43 Now you can enter again : 40
44 These are prime numbers less than 40 : 2 3 5 7 11 13 17 19 23 29 31 37
45 Now you can enter again : 0
46 That's all.
47 
48 */

/*C Primer Plus (7.12) 10*/

1 #include<stdio.h>
 2 #include<stdbool.h>
 3 #define Choice1 17850
 4 #define Choice2 23900
 5 #define Choice3 29750
 6 #define Choice4 14875
 7 #define TAX1 0.15
 8 #define TAX2 0.28
 9 void MENU();
 10 void QUIT();
 11 double TAX_PLAN1 (double money, double Ctax);
 12 double TAX_PLAN2 (double money1, double Ctax1);
 13 double TAX_PLAN3 (double money2, double Ctax2);
 14 double TAX_PLAN4 (double money3, double Ctax3);
 15 int choice = 0;
 16 double income = 0.0;
 17 double tax = 0.0;
 18 double tax1 = 0.0;
 19 double tax2 = 0.0;
 20 double tax3 = 0.0;
 21 
 22 int main()
 23 {
 24 while (true)
 25 {
 26 MENU();
 27 
 28 switch (choice)
 29 {
 30 case 1 :
 31 {
 32 TAX_PLAN1 (income, tax);
 33 break;
 34 }
 35 case 2 :
 36 {
 37 TAX_PLAN2 (income, tax1);
 38 break;
 39 }
 40 case 3 :
 41 {
 42 TAX_PLAN3 (income, tax2);
 43 break;
 44 }
 45 case 4 :
 46 {
 47 TAX_PLAN4 (income, tax3);
 48 break;
 49 }
 50 case 5 :
 51 {
 52 QUIT();
 53 return 0;
 54 }
 55 }
 56 }
 57 
 58 }
 59 void MENU()
 60 {
 61 printf("************************************************************");
 62 printf("\nPlease enter your choice.\n");
 63 printf("(1) Single (2) Head of household\n");
 64 printf("(3) Married, jointly owned (4) Married, divorced\n");
 65 printf("(5) Quit\n");
 66 printf("************************************************************\n");
 67 printf("Please enter your choice : ");
 68 scanf("%d",&choice);
 69 while (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5)
 70 {
 71 printf("Please enter the right choice:");
 72 scanf("%d",&choice);
 73 }
 74 }
 75 
 76 void QUIT()
 77 {
 78 printf("\t\t\n************************************************\t\t\n");
 79 printf("|| ||");
 80 printf("\n|| ||");
 81 printf("\n|| Thank you to use this programme! ||");
 82 printf("\n|| ||");
 83 printf("\n|| ||");
 84 printf("\t\t\n************************************************\t\t\n");
 85 }
 86 
 87 double TAX_PLAN1 (double money, double Ctax)
 88 {
 89 double income, tax;
 90 income = tax = 0;
 91 printf("Please enter your income (<= 0 to quit) : ");
 92 while (scanf("%lf",&income) == 1 && income > 0)
 93 {
 94 if (income <= Choice1)
 95 {
 96 tax = income * TAX1;
 97 printf("This is your tax : %g$\n",tax); break;
 98 }
 99 else
100 {
101 tax = (Choice1 * TAX1)+(income - Choice1) * TAX2;
102 printf("This is your tax : %g$\n",tax); break;
103 }
104 }
105 }
106 
107 double TAX_PLAN2 (double money1, double Ctax1)
108 {
109 double income, tax1;
110 income = tax1 = 0;
111 printf("Please enter your income (<= 0 to quit) : ");
112 while (scanf("%lf",&income) == 1 && income > 0)
113 {
114 if (income <= Choice2)
115 {
116 tax1 = income * TAX1;
117 printf("This is your tax : %g$\n",tax1); break;
118 }
119 else
120 {
121 tax1 = (Choice2 * TAX1) + (income - Choice2) * TAX2;
122 printf("This is your tax : %g$\n",tax1); break;
123 }
124 }
125 }
126 
127 double TAX_PLAN3 (double money2, double Ctax2)
128 {
129 double income, tax2;
130 income = tax2 = 0;
131 printf("Please enter your income (<= 0 to quit) : ");
132 while (scanf("%lf",&income) == 1 && income > 0)
133 {
134 if (income <= Choice3)
135 {
136 tax2 = income * TAX1;
137 printf("This is your tax : %g$\n",tax2); break;
138 }
139 else
140 {
141 tax2 = (Choice3 * TAX1)+(income - Choice3) * TAX2;
142 printf("This is your tax : %g$\n",tax2); break;
143 }
144 }
145 }
146 
147 double TAX_PLAN4 (double money3, double Ctax3)
148 {
149 double income, tax3;
150 income = tax3 = 0;
151 printf("Please enter your income (<= 0 to quit) : ");
152 while (scanf("%lf",&income) == 1 && income > 0)
153 {
154 if (income <= Choice4)
155 {
156 tax3 = income * TAX1;
157 printf("This is your tax : %g$\n",tax3); break;
158 }
159 else
160 {
161 tax3 = (Choice4 * TAX1)+(income - Choice4) * TAX2;
162 printf("This is your tax : %g$\n",tax3); break;
163 }
164 }
165 }
166 /*
167 輸出樣例
168 
169 ************************************************************
170 Please enter your choice.
171 (1) Single (2) Head of household
172 (3) Married, jointly owned (4) Married, divorced
173 (5) Quit
174 ************************************************************
175 Please enter your choice : 1
176 Please enter your income (<= 0 to quit) : 20000
177 This is your tax : 3279.5$
178 ************************************************************
179 Please enter your choice.
180 (1) Single (2) Head of household
181 (3) Married, jointly owned (4) Married, divorced
182 (5) Quit
183 ************************************************************
184 Please enter your choice : 6
185 Please enter the right choice:2
186 Please enter your income (<= 0 to quit) : 35415
187 This is your tax : 6809.2$
188 ************************************************************
189 Please enter your choice.
190 (1) Single (2) Head of household
191 (3) Married, jointly owned (4) Married, divorced
192 (5) Quit
193 ************************************************************
194 Please enter your choice : 5
195 
196 ************************************************
197 || ||
198 || ||
199 || Thank you to use this programme! ||
200 || ||
201 || ||
202 ************************************************
203 
204 */

/*C Primer Plus (7.12) 11*/

1 #include<stdio.h>
 2 #include<ctype.h>
 3 #define Pr_artichoke 2.05 //洋薊的價格(美元/磅)
 4 #define Pr_beet 1.15 //甜菜的價格(美元/磅)
 5 #define Pr_carrot 1.09 //胡蘿蔔的價格(美元/磅)
 6 #define Discount 0.05 //折扣
 7 #define Under5 6.5 //低於或等於5磅的運輸和包裝費
 8 #define Under20 14.00 //5-20磅的運輸和包裝費
 9 #define Basic20 14.00 //20磅的運輸和包裝費
 10 #define Extratax 0.50 //超過20磅每重一磅就增加0.5$
 11 #define Bweight 20.00 //20磅
 12 
 13 void MENU();
 14 
 15 char choice;
 16 
 17 int main()
 18 {
 19 double P_artichoke = 0.0; //洋薊的磅數
 20 double P_beet = 0.0; //甜菜的磅數
 21 double P_carrot = 0.0; //胡蘿蔔的磅數
 22 double weight = 0.0; //重量統計
 23 double T_weight = 0.0; //總重量
 24 double M_artichoke = 0.0; //洋薊的總價格
 25 double M_beet = 0.0; //甜菜的總價格
 26 double M_carrot = 0.0; //胡蘿蔔的總價格
 27 double P_total = 0.0; //三種蔬菜花費的錢
 28 double R_total = 0.0; //算過折扣和包裝費之後的錢
 29 double P_discount = 0.0; //折扣
 30 double P_trans_and_package = 0.0; //運輸和包裝費的錢
 31 
 32 MENU();
 33 
 34 while ((choice = tolower(getchar())) != 'q') //ctype函數(tolower 大寫字符返回小寫字符)
 35 {
 36 if (isspace(choice)) //檢測空白符
 37 {
 38 continue;
 39 }
 40 while (getchar() != '\n')
 41 {
 42 continue;
 43 }
 44 switch (choice)
 45 {
 46 case 'a' :
 47 {
 48 printf("Enter pounds of artichokes : ");
 49 scanf("%lf",&weight);
 50 P_artichoke += weight;
 51 break;
 52 }
 53 case 'b' :
 54 {
 55 printf("Enter pounds of beets : ");
 56 scanf("%lf",&weight);
 57 P_beet += weight;
 58 break;
 59 }
 60 case 'c' :
 61 {
 62 printf("Enter pounds of carrots : ");
 63 scanf("%lf",&weight);
 64 P_carrot += weight;
 65 break;
 66 }
 67 default :
 68 {
 69 printf("%c is not a valid choice.\n",choice);
 70 printf("Please enter a positive choice : \n");
 71 }
 72 }
 73 MENU();
 74 }
 75 
 76 M_artichoke = Pr_artichoke * P_artichoke; //洋薊的購買的總價錢
 77 M_beet = Pr_beet * P_beet; //甜菜購買的總價錢
 78 M_carrot = Pr_carrot * P_carrot; //胡蘿蔔購買的總價錢
 79 P_total = M_artichoke + M_beet + M_carrot; //三種蔬菜在一起的總價錢
 80 T_weight = P_artichoke + P_beet + P_carrot; //三種蔬菜在一起的總重量
 81 
 82 if (P_total >= 100.0) //打折的費用
 83 {
 84 P_discount = P_total * Discount;
 85 }
 86 else
 87 {
 88 P_discount = 0.0;
 89 }
 90 
 91 if (T_weight <= 0.0)
 92 {
 93 P_trans_and_package = 0.0;
 94 }
 95 else if (T_weight < 5.0) //計算包裝和運輸費的費用
 96 {
 97 P_trans_and_package = Under5;
 98 }
 99 else if (T_weight < 20.0)
100 {
101 P_trans_and_package = Under20;
102 }
103 else
104 {
105 P_trans_and_package = Basic20 + Extratax * (T_weight - Bweight);
106 }
107 R_total = P_total + P_trans_and_package - P_discount;
108 
109 printf("\n\n**********************************************************************\n");
110 //蔬菜的報價(美元/磅)
111 printf("The price of artichokes is : $%.2lf per pound.\n",Pr_artichoke);
112 printf("The price of beets is : $%.2lf per pound.\n",Pr_beet);
113 printf("The price of carrots is : $%.2lf per pound.\n",Pr_carrot);
114 //購買蔬菜分開的價格以及一共的價格和購買的磅數
115 printf("You bought %.2lf pounds artichokes. "
116 "And the price is : %.2lf$\n",P_artichoke,M_artichoke);
117 printf("You bought %.2lf pounds beets. "
118 "And the price is : %.2lf$\n",P_beet,M_beet);
119 printf("You bought %.2lf pounds carrots. "
120 "And the price is : %.2lf$\n",P_carrot,M_carrot);
121 printf("The total weight is : %.2lf pound.\n",T_weight);
122 printf("The total cost of vegetables : %.2lf$\n",P_total);
123 printf("Discount : %.2lf$\n",P_discount);
124 printf("The price of transport and package : %.2lf$\n",P_trans_and_package);
125 printf("The last money spent is : %.2lf$\n",R_total);
126 printf("**********************************************************************\n");
127 
128 return 0;
129 }
130 
131 void MENU()
132 {
133 printf("**********************************************************************\n");
134 printf("Enter letters to complete the response.\n");
135 printf("(a) Buy artichokes (b) Buy beets\n");
136 printf("(c) Buy carrots\n");
137 printf(" Enter q to quit.\n");
138 printf("**********************************************************************\n");
139 printf("Please enter your choice : ");
140 }
141 /*
142 輸出樣例
143 
144 **********************************************************************
145 Enter letters to complete the response.
146 (a) Buy artichokes (b) Buy beets
147 (c) Buy carrots
148 Enter q to quit.
149 **********************************************************************
150 Please enter your choice : a
151 Enter pounds of artichokes : 96.5
152 **********************************************************************
153 Enter letters to complete the response.
154 (a) Buy artichokes (b) Buy beets
155 (c) Buy carrots
156 Enter q to quit.
157 **********************************************************************
158 Please enter your choice : v
159 v is not a valid choice.
160 Please enter a positive choice :
161 **********************************************************************
162 Enter letters to complete the response.
163 (a) Buy artichokes (b) Buy beets
164 (c) Buy carrots
165 Enter q to quit.
166 **********************************************************************
167 Please enter your choice : b
168 Enter pounds of beets : 66.7
169 **********************************************************************
170 Enter letters to complete the response.
171 (a) Buy artichokes (b) Buy beets
172 (c) Buy carrots
173 Enter q to quit.
174 **********************************************************************
175 Please enter your choice : c
176 Enter pounds of carrots : 123.64
177 **********************************************************************
178 Enter letters to complete the response.
179 (a) Buy artichokes (b) Buy beets
180 (c) Buy carrots
181 Enter q to quit.
182 **********************************************************************
183 Please enter your choice : q
184 
185 
186 **********************************************************************
187 The price of artichokes is : $2.05 per pound.
188 The price of beets is : $1.15 per pound.
189 The price of carrots is : $1.09 per pound.
190 You bought 96.50 pounds artichokes. And the price is : 197.82$
191 You bought 66.70 pounds beets. And the price is : 76.70$
192 You bought 123.64 pounds carrots. And the price is : 134.77$
193 The total weight is : 286.84 pound.
194 The total cost of vegetables : 409.30$
195 Discount : 20.46$
196 The price of transport and package : 147.42$
197 The last money spent is : 536.25$
198 **********************************************************************
199 
200 */

 

作者:KurokiTomoko原文地址:https://www.cnblogs.com/NoldorFromMiddleEarth/p/17087833.html

%s 个评论

要回复文章请先登录注册