c primer plus 第六版 第七章编程练习答案
2019-07-29 00:42
351 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43531895/article/details/97621788
(编译环境 Microsoft Visual Studio 2019)
1.
[code]/*1.*/ #include<stdio.h> int main(void) { char ch; unsigned int s_count = 0; unsigned int l_count = 0; unsigned int o_count = 0; printf("Please enter some text.(end with #)\n"); while ((ch = getchar()) != '#') { if (ch == ' ') s_count++; else if (ch == '\n') l_count++; else o_count++; } printf("The number of spaces is %d\n", s_count); printf("The number of newline characters is %d\n", l_count); printf("The number of other characters is %d\n", o_count); return 0; }
2.
[code]/*2.*/ #include<stdio.h> int main(void) { char ch; unsigned int count = 0; printf("Please enter some text.(end with #)\n"); while ((ch = getchar()) != '#') { if (ch == '\n') continue; printf("%4c-%-4hhu ",ch,ch); count++; if (count % 8 == 0) putchar('\n'); } printf("\n"); return 0; }
3.
[code]/*3.*/ #include<stdio.h> int main(void) { int integer; unsigned int e_number = 0; unsigned int o_number = 0; unsigned int e_sum = 0; unsigned int o_sum = 0; printf("Please enter some integer.(0 to quit)\n"); scanf_s("%d", &integer); while (integer != 0) { if (integer % 2 == 0) { e_number++; e_sum += integer; } else { o_number++; o_sum += integer; } scanf_s("%d", &integer); } if (e_number == 0) printf("The even number is 0 and the average even number is 0\n"); else printf("The even number is %u and the average even number is %.2f\n", e_number, (float)e_sum / e_number); if (o_number == 0) printf("The odd number is 0 and the average odd number is 0\n"); else printf("The odd number is %u and the average odd number is %.2f\n", o_number, (float)o_sum / o_number); return 0; }
4.
[code]/*4.*/ #include<stdio.h> int main(void) { char ch; unsigned int count = 0; printf("Please enter some text.(end with #)\n"); while ((ch = getchar()) != '#') { if (ch == '.') { ch = '!'; count++; } else if (ch == '!') { putchar(ch); count++; } putchar(ch); } printf("\nA total of %u replacements.\n", count); return 0; }
5.
[code]/*5.*/ #include<stdio.h> int main(void) { char ch; unsigned int count = 0; printf("Please enter some text.(end with #)\n"); while ((ch = getchar()) != '#') { switch (ch) { case '.': ch = '!'; count++; break; case '!': putchar(ch); count++; break; default: break; } putchar(ch); } printf("\nA total of %u replacements.\n", count); return 0; }
6.
[code]/*6.*/ #include<stdio.h> int main(void) { char ch; char ch_pre = 0; unsigned int count = 0; printf("Please enter some text.\n"); while ((ch = getchar()) != '#') { if (ch == 'e') ch_pre = ch; if (ch_pre == 'e' && ch == 'i') count++; } printf("The count of 'ei' is %u\n", count); return 0; }
7.
[code] /*7.*/ #include<stdio.h> #define BASE_PAY 10 #define BASE_TIME 40 #define OVER_PAY 15 #define TAX_1 300 #define TAX_2 150 #define TAX_1_RATE 0.15f #define TAX_2_RATE 0.2f #define TAX_OT_RATE 0.25f int main(void) { unsigned int time; float wage,tax; printf("Please enter the number of hours you work a week.\n"); scanf_s("%u", &time); if (time <= BASE_TIME) wage = time * (float)BASE_PAY; else wage = BASE_TIME * (float)BASE_PAY + (time - BASE_TIME) * OVER_PAY; if (wage < TAX_1) tax = wage * TAX_1_RATE; else if (wage < TAX_2) tax = TAX_1 * TAX_1_RATE + (wage - TAX_1) * TAX_2_RATE; else tax = TAX_1 * TAX_1_RATE + TAX_2 * TAX_2_RATE + (wage - TAX_2) * TAX_OT_RATE; printf("Total wage: %.2f\n", wage); printf("Tax: %.2f\n", tax); printf("Net income: %.2f\n", wage - tax); return 0; }
8.
[code]/*8.*/ #include<stdio.h> #define BASE_TIME 40 #define OVER_PAY 15 #define TAX_1 300 #define TAX_2 150 #define TAX_1_RATE 0.15f #define TAX_2_RATE 0.2f #define TAX_OT_RATE 0.25f float level(void); void star(void); int main(void) { unsigned int time; float wage, tax, salary; while(1) { if ((salary = level()) == 0) break; printf("Please enter the number of hours you work a week.\n"); scanf_s("%u", &time); if (time <= BASE_TIME) wage = time * (float)salary; else wage = BASE_TIME * (float)salary + (time - BASE_TIME) * OVER_PAY; if (wage < TAX_1) tax = wage * TAX_1_RATE; else if (wage < TAX_2) tax = TAX_1 * TAX_1_RATE + (wage - TAX_1) * TAX_2_RATE; else tax = TAX_1 * TAX_1_RATE + TAX_2 * TAX_2_RATE + (wage - TAX_2) * TAX_OT_RATE; printf("Total wage: %.2f\n", wage); printf("Tax: %.2f\n", tax); printf("Net income: %.2f\n", wage - tax); } return 0; } float level(void) { const float level_1 = 8.75f; const float level_2 = 9.33f; const float level_3 = 10.00f; const float level_4 = 11.20f; float salary; int flag; _Bool cycle; do { cycle=0; 3ff7 star(); printf("\nEnter the number corresponding to the desired pay rate or action.\n\n"); printf("1) $8.75/hr\t\t2) $9.33/hr\n\n"); printf("3) $10.00/hr\t\t4) $11.20/hr\n\n"); printf("5) quit\n\n"); star(); printf("Your choice: "); scanf_s("%d", &flag); switch (flag) { case 1: salary = level_1; break; case 2: salary = level_2; break; case 3: salary = level_3; break; case 4: salary = level_4; break; case 5: salary = 0; printf("bye\n"); break; default: printf("Please enter right number.\n"); cycle = 1; break; } }while(cycle==1); return salary; } void star(void) { printf("********************************************************************\n"); }
9.
[code]/*9.*/ #include<stdio.h> int main(void) { unsigned int value, per, i; _Bool prime; printf("Please enter a positive integer: "); scanf_s("%u", &value); printf("Prime number:\n"); for (per = 2; per <= value; per++) { for (prime = 1, i = 2; i * i <= per; i++) { if (per % i == 0) prime = 0; } if (prime == 1) printf("%u ", per); } putchar('\n'); return 0; }
10.
[code]/*10.*/ #include<stdio.h> #define TAX_S 17580 #define TAX_H 23900 #define TAX_MC 29750 #define TAX_MD 14875 #define TAX_RATE_B 0.15f #define TAX_RATE_O 0.28f void star(void); int main(void) { unsigned short type; unsigned int tax_base; float income, tax; _Bool flag = 1; _Bool re; do { do { re = 0; printf("*******************************\n"); printf("Please choose your tax type.\n"); printf("1. single\n2. householder\n"); printf("3. married,common\n4. married,divorced\n"); printf("5. quit\n\n"); printf("Your choice: "); scanf_s("%hu", &type); switch (type) { case 1: tax_base = TAX_S; break; case 2: tax_base = TAX_H; break; case 3: tax_base = TAX_MC; break; case 4: tax_base = TAX_MD; break; case 5: flag = 0; printf("bye"); break; default: printf("\nPlease enter right number.\n"); re = 1; break; } } while (re); if (flag) { printf("Please enter your income: "); scanf_s("%f", &income); if (income <= tax_base) tax = income * TAX_RATE_B; else tax = tax_base * TAX_RATE_B + (income - tax_base) * TAX_RATE_O; printf("Your tax is %.2f\n\n", tax); } } while (flag); putchar('\n'); return 0; }
11.
[code]/*11.*/ #include<stdio.h> #define ARTICHOKE 2.05f #define BEET 1.15f #define CARROT 1.09f #define DISCOUNT_M 100 #define DISCOUNT_R 0.05 #define FARE_P_1 5 #define FARE_P_2 20 #define FARE_1 6.5 #define FARE_2 14 #define FARE_OT 0.5 int main(void) { char goods, ch; float a_pound = 0; float b_pound = 0; float c_pound = 0; float pound, total_pound; float fare, total_cost; float discount = 0; float a_cost, b_cost, c_cost; _Bool re; do { re = 0; printf("*****************************************\n"); printf("Please select the goods you need.\n"); printf("a. artichoke\nb. beet\nc. carrot\n\n"); printf("Your choice: "); goods = getchar();//输入时会把回车的换行符放在缓冲区中,下一次getchar()或scanf(%c)会读取到换行符 getchar();//所以每次输入字符时候都要用一个getchar()将换行符读取掉 switch (goods) { case 'a': printf("Artichoke weight(pound): "); scanf_s("%f", £); getchar(); a_pound += pound; break; case 'b': printf("Beet weight(pound): "); scanf_s("%f", £); getchar(); b_pound += pound; break; case 'c': printf("Carrot weight(pound): "); scanf_s("%f", £); getchar(); c_pound += pound; break; default: printf("\nPlease enter the correct letter.\n"); re = 1; break; } if (re != 1) { printf("\nDo you want to select another goods?\n"); printf("Y.yes\t\tN.no\n"); while ((ch = getchar()) != 'Y' && ch != 'N') { getchar(); printf("Please enter correct letter.\n"); } if (ch == 'Y') { getchar(); re = 1; } else printf("\nThank you for purchase.\n\n"); } } while (re == 1); a_cost = a_pound * ARTICHOKE; b_cost = b_pound * BEET; c_cost = c_pound * CARROT; total_cost = a_cost + b_cost + c_cost; total_pound = a_pound + b_pound + c_pound; if (total_cost >= DISCOUNT_M) discount = DISCOUNT_M * DISCOUNT_R; if (total_pound <= 5) fare = 6.5f; else if (total_pound <= 20) fare = 14.0f; else fare = 14.0f + (total_pound - 14) * 0.5f; printf("%30s %15s %15s %15s\n","Goods:","Artichoke","Beet","Carrot"); printf("%30s %15s %15s %15s\n","Price(/pound):","2.05","1.15","1.09"); printf("%30s %15.2f %15.2f %15.2f\n", "Ordered weight(pound):",a_pound, b_pound, c_pound); printf("%30s %15.2f %15.2f %15.2f\n", "Cost:",a_cost, b_cost, c_cost); printf("%30s %.2f\n", "Total cost:", total_cost); printf("%30s %.2f\n", "Discount:", discount); printf("%30s %.2f\n", "Freight and packing charges:", fare); printf("%30s %.2f\n", "Total payment:",total_cost - discount + fare); return 0; }
相关文章推荐
- C++ primer Plus(第六版)中文版 第七章 函数——C++的编程模块 编程练习答案
- C ++ Primer Plus 第六版 第七章编程练习答案
- C++ Primer Plus第六版 第七章 编程练习答案
- C++ PRIMER PLUS (第六版) 中文版 第七章编程练习答案
- C primer plus 第六版 第七章 编程练习 答案
- C Primer Plus (第六版)中文版 第七章 编程练习答案
- C++ Primer Plus 第六版 学习笔记 第七章 编程练习答案
- C primer plus 第六版 第五章 编程练习 答案
- C++ primer plus第六版课后编程练习答案:6.3
- c primer plus 第六版 第九章编程练习答案
- C++primer plus第六版课后编程练习答案2.6
- C++primer plus第六版课后编程练习答案7.1
- C++primer plus第六版课后编程练习答案8.7
- C++primer plus第六版课后编程练习答案10.4
- C Primer Plus (第六版)中文版 第十四章 编程练习答案
- C Primer Plus (第六版) 第十三章_编程练习答案
- C primer plus 第七章 C控制语句-分支和跳转 编程练习个人答案
- C ++ Primer Plus 第六版 第十一章编程练习答案
- C++ primer Plus(第六版)中文版 第八章 函数探幽 编程练习答案
- C++ Primer Plus第五版 第七章 编程练习答案