导图社区 C Primer Plus 第七章编程练习
这是一篇关于C Primer Plus 第七章编程练习的思维导图
编辑于2023-01-04 21:40:54C Primer Plus 第七章编程练习
第一题
#include <stdio.h> int main() { char ch; int space, newLine, other; space = 0; newLine = 0; other = 0; while((ch = getchar()) != '#') { if(ch == ' ') { space++; } else if(ch == '\n') { newLine++; } else{ other++; } } printf("space: %d\n", space); printf("newLine: %d\n", newLine); printf("other: %d\n", other); return 0; }
第二题
#include <stdio.h> int main() { char ch; int cnt = 0; while((ch = getchar()) != '#') { cnt++; printf("%d: %-5c %d; ", cnt, ch, ch); if((cnt %= 8) == 0) { printf("\n"); } } return 0; }
第三题
#include <stdio.h> int main() { int n; int odd = 0; int even = 0; double aveOdd = 0; double aveEven = 0; while(scanf("%d", &n) == 1 && n != 0) { if((n % 2) == 0) { even++; aveEven += n; } else{ odd++; aveOdd += n; } } if(even != 0) { aveEven /= even; } if(odd != 0) { aveOdd /= odd; } printf("The even number entered by the user is %d, the average of these even numbers is %.2f.\n", even, aveEven); printf("The odd number entered by the user is %d, the average of these odd numbers is %.2f.\n", odd, aveOdd); return 0; }
第四题
#include <stdio.h> int main() { char ch; int cnt = 0; while((ch = getchar()) != '#') { if(ch == '?') { printf("!"); cnt++; } else if(ch == '!') { printf("!!"); cnt++; } else{ printf("%c", ch); } } printf("number of replacements: %d\n", cnt); return 0; }
第五题
#include <stdio.h> int main() { char ch; int cnt = 0; while((ch = getchar()) != '#') { switch(ch) { case '?': printf("!"); cnt++; break; case '!': printf("!!"); cnt++; break; default: printf("%c", ch); break; } } printf("number of replacements: %d\n", cnt); return 0; }
第六题
#include <stdio.h> int main() { char ch; char preCh; int cnt = 0; while((ch = getchar()) != '#') { if(ch == 'i' && preCh == 'e') { cnt++; } preCh = ch; } printf("the number of occurrences of \'ei\' is %d.\n", cnt); return 0; }
第七题
#include <stdio.h> #define BASIC 10.00 #define OVERTIME 1.5 #define RATE_300 0.15 #define RATE_150 0.2 #define RATE_OTHER 0.25 int main() { printf("Please enter the number of hours worked in a week: "); double time = 0; scanf("%lf", &time); double salary, tax, netIncome; if(time <= 40) { salary = time * BASIC; } else{ salary = 40 * BASIC + (time - 40) * OVERTIME * BASIC; } if(salary <= 300) { tax = salary * RATE_300; } else if(salary < 450) { tax = 300 * RATE_300 + (salary - 300) * RATE_150; } else{ tax = 300 * RATE_300 + 150 * RATE_150 + (salary - 450) * RATE_OTHER; } netIncome = salary - tax; printf("gloss salary: %-5.2f, tax: %-5.2f, net income: %-5.2f\n", salary, tax, netIncome); return 0; }
第八题
#include <stdio.h> #include <stdbool.h> #define BASIC1 8.57 #define BASIC2 9.33 #define BASIC3 10.00 #define BASIC4 11.20 #define QUIT "quit" #define OVERTIME 1.5 #define RATE_300 0.15 #define RATE_150 0.2 #define RATE_OTHER 0.25 int main() { for(int i = 0; i < 65; i++) { printf("*"); } printf("\n"); printf("Enter the number corresponding to the desired pay rate or action: \n"); printf("1) $%.2f/hr 2) $%.2f/hr\n", BASIC1, BASIC2); printf("3) $%.2f/hr 4) $%.2f/hr\n", BASIC3, BASIC4); printf("5) %s\n", QUIT); for(int i = 0; i < 65; i++) { printf("*"); } printf("\n"); double BASIC; int choice; bool flag = 0; while(scanf("%d", &choice) == 1) { switch(choice) { case 1: BASIC = BASIC1; break; case 2: BASIC = BASIC2; break; case 3: BASIC = BASIC3; break; case 4: BASIC = BASIC4; break; case 5: flag = 1; break; default: printf("Please enter the correct number\n"); break; } if(flag == 1) { printf("quit\n"); break; } printf("Please enter the number of hours worked in a week: "); double time = 0; scanf("%lf", &time); double salary, tax, netIncome; if(time <= 40) { salary = time * BASIC; } else{ salary = 40 * BASIC + (time - 40) * OVERTIME * BASIC; } if(salary <= 300) { tax = salary * RATE_300; } else if(salary < 450) { tax = 300 * RATE_300 + (salary - 300) * RATE_150; } else{ tax = 300 * RATE_300 + 150 * RATE_150 + (salary - 450) * RATE_OTHER; } netIncome = salary - tax; printf("gloss salary: %-5.2f, tax: %-5.2f, net income: %-5.2f\n", salary, tax, netIncome); } return 0; }
第十一题
#include <stdio.h> #include <stdbool.h> #define ARTICHOKE 2.05 #define BEET 1.15 #define CARROT 1.09 #define POUND1 6.5 #define POUND2 14.0 #define POUND3 0.5 int main() { for(int i = 0; i < 96; i++) { printf("*"); } printf("\n"); printf("Enter the character that corresponds to the type of vegetable you want to buy (press q to quit): \n"); printf("a: artichoke b: beet c: carrot q: quit\n"); for(int i = 0; i < 96; i++) { printf("*"); } printf("\n"); int arti = 0; int beet = 0; int carr = 0; char ch; while((ch = getchar()) != 'q') { int tmp; switch(ch) { case 'a': printf("Enter the number of pounds you want to buy: "); scanf("%d", &tmp); arti += tmp; break; case 'b': printf("Enter the number of pounds you want to buy: "); scanf("%d", &tmp); beet += tmp; break; case 'c': printf("Enter the number of pounds you want to buy: "); scanf("%d", &tmp); carr += tmp; break; case '\n': break; default: printf("Please enter the right character!\n"); break; } if(ch != '\n') { for(int i = 0; i < 96; i++) { printf("*"); } printf("\n"); printf("Enter the character that corresponds to the type of vegetable you want to buy (press q to quit): \n"); printf("a: artichoke b: beet c: carrot q: quit\n"); for(int i = 0; i < 96; i++) { printf("*"); } printf("\n"); } } double totArti = arti * ARTICHOKE; double totBeet = beet * BEET; double totCarr = carr * CARROT; double total = totArti + totBeet + totCarr; bool flag = 0; double discount; if(total >= 100) { discount = total * 0.05; flag = 1; } double ship = 0; int pound = arti + beet + carr; if(pound <= 0) { ship = 0; } else if(pound <= 5) { ship = POUND1; } else if(pound <= 20) { ship = POUND2; } else{ ship = POUND2 + (pound - 20) * POUND3; } printf("The selling price of artichoke is %.2f, beet is %.2f, carrot is %.2f.\n", ARTICHOKE, BEET, CARROT); printf("The ordered weight of artichoke is %d, beet is %d, carrot is %d.\n", arti, beet, carr); printf("The cost of buying artichokes is %.2f, beet is %.2f, carrot is %.2f.\n", totArti, totBeet, totCarr); printf("The total cost of the order is %.2f.\n", total); if(flag == 1) { printf("The discount is %.2f.\n", discount); } else{ printf("There is no discount.\n"); } printf("The shipping and packing money is %.2f.\n", ship); printf("The total cost of all is %.2f.\n", total + ship); return 0; }
第十题
#include <stdio.h> #include <stdbool.h> #define BASIC1 17850.0 #define BASIC2 23900.0 #define BASIC3 29750.0 #define BASIC4 14875.0 #define RATE1 0.15 #define RATE2 0.28 int main() { for(int i = 0; i < 79; i++) { printf("*"); } printf("\n"); printf("Enter the number that corresponds to the type of tax or action you want to pay: \n"); printf("1) single 2) householder 3) married 4) married but divorced 5) quit\n"); for(int i = 0; i < 79; i++) { printf("*"); } printf("\n"); int num; double BASIC; bool flag = 0; while(scanf("%d", &num) == 1) { double salary; double tax; switch(num) { case 1: BASIC = BASIC1; break; case 2: BASIC = BASIC2; break; case 3: BASIC = BASIC3; break; case 4: BASIC = BASIC4; break; case 5: flag = 1; break; default: printf("Please enter the correct number\n"); break; } if(flag == 1) { printf("quit!\n"); break; } printf("Please enter your salary: "); scanf("%lf", &salary); if(salary <= BASIC) { tax = salary * RATE1; } else{ tax = BASIC * RATE1 + (salary - BASIC) * RATE2; } printf("Tax is %.2f.\n", tax); for(int i = 0; i < 79; i++) { printf("*"); } printf("\n"); printf("Enter the number that corresponds to the type of tax or action you want to pay: \n"); printf("1) single 2) householder 3) married 4) married but divorced 5) quit\n"); for(int i = 0; i < 79; i++) { printf("*"); } printf("\n"); } return 0; }
第九题
#include <stdio.h> #include <stdbool.h> bool isPrime(int n); int main() { int num; printf("Please enter a number: "); scanf("%d", &num); for(int i = 2; i <= num; i++) { if(isPrime(i)) { printf("%d\n", i); } } return 0; } bool isPrime(int n) { for(int i = 2; (i * i) <= n; i++) { if(n % i == 0) { return false; } } return true; }