导图社区 C Primer Plus 第六章编程练习
书籍C Primer Plus 第六章编程练习,便于理解课本,有助于期末考试复习和背诵。可收藏,亦可使用后补充知识点,完善属于自己的知识框架。
编辑于2022-07-20 22:43:51C Primer Plus 第六章编程练习
第一题
#include <stdio.h> int main() { char ch[26]; for(int i = 0; i < 26; i++) { ch[i] = 'a' + i; } for(int i = 0; i < 26; i++) { printf("%-2c", ch[i]); } return 0; }
第二题
#include <stdio.h> int main() { for(int i = 1; i <= 5; i++) { for(int j = 0; j < i; j++) { printf("$"); } printf("\n"); } return 0; }
第三题
#include <stdio.h> int main() { for(int i = 1; i <= 6; i++) { for(int j = 0; j < i; j++) { printf("%c", 'F' - j); } printf("\n"); } return 0; }
第四题
#include <stdio.h> int main() { char c = 'A'; for(int i = 0; i < 6; i++) { for(int j = 0; j <= i; j++) { printf("%c", c); c += 1; } printf("\n"); } return 0; }
第五题
#include <stdio.h> #define BEGIN 'A' int main() { printf("Please enter uppercase letters: "); char c; scanf("%c", &c); int num = c - BEGIN; for(int i = num; i >= 0; i--) { for(int j = i; j > 0; j--) { printf(" "); } int j; for(j = 0; j <= num - i; j++) { printf("%c", BEGIN + j); } for(j--; j > 0; j--) { printf("%c", BEGIN + j - 1); } printf("\n"); } return 0; }
第六题
#include <stdio.h> int main() { printf("Please enter the upper and lower limits of the form: "); int upper, lower; scanf("%d %d", &lower, &upper); for(;lower <= upper; lower++) { printf("%-9d%-9d%-9d\n", lower, lower * lower, lower * lower * lower); } return 0; }
第七题
#include <stdio.h> #include <string.h> int main() { char s[40]; scanf("%s", s); for(int i = strlen(s); i > 0; i--) { printf("%c", s[i - 1]); } printf("\n"); return 0; }
第八题
#include <stdio.h> int main() { printf("Please enter two floating point numbers: "); double fir, sec; while(scanf("%lf %lf", &fir, &sec) == 2) { double tmp = (fir - sec) / (fir * sec); printf("%f\n", tmp); printf("Please enter two floating point numbers: "); } printf("OK\n"); return 0; }
第九题
#include <stdio.h> double my_fun(double fir, double sec); int main() { printf("Please enter two floating point numbers: "); double fir, sec; while(scanf("%lf %lf", &fir, &sec) == 2) { double tmp = my_fun(fir, sec); printf("%f\n", tmp); printf("Please enter two floating point numbers: "); } printf("OK\n"); return 0; } double my_fun(double fir, double sec) { double tmp = (fir - sec) / (fir * sec); return tmp; }
第十题
#include <stdio.h> int main() { printf("Enter lower and upper integer limits: "); int lower, upper; scanf("%d %d", &lower, &upper); int sum = 0; for(int i = lower;i <= upper; i++) { sum += i * i; } printf("The sums of the squares from %d to %d is %d\n", lower * lower, upper * upper, sum); printf("Enter next set of limits: "); while(scanf("%d %d", &lower, &upper) == 2) { int sum = 0; for(int i = lower;i <= upper; i++) { sum += i * i; } printf("The sums of the squares from %d to %d is %d\n", lower * lower, upper * upper, sum); printf("Enter next set of limits: "); } printf("Done\n"); return 0; }
第十一题
#include <stdio.h> int main() { int a[8]; for(int i = 0; i < 8; i++) { scanf("%d", &a[i]); } for(int i = 8; i > 0; i--) { printf("%-5d", a[i - 1]); } return 0; }
第十二题
#include <stdio.h> #include <math.h> int main() { int cnt; while(scanf("%d", &cnt) == 1 && cnt > 0) { double fir = 1.0; double sec = 2.0; double sum1 = 1.0; double sum2 = 1.0; for(int i = 2; i < cnt; i++) { sum1 += fabs(fir) / sec; fir *= -1; sum2 += fir / sec; sec += 1; } printf("first: %f\n", sum1); printf("second: %f\n", sum2); } return 0; }
第十三题
#include <stdio.h> int main() { int a[8]; a[0] = 1; for(int i = 1; i < 8; i++) { a[i] = a[i - 1] * 2; } int i = 0; do{ printf("%-5d", a[i]); i++; } while(i < 8); return 0; }
第十四题
#include <stdio.h> int main() { printf("Please enter 8 values for the first array: "); double first[8]; for(int i = 0; i < 8; i++) { scanf("%lf", &first[i]); } double second[8]; second[0] = first[0]; for(int i = 1; i < 8; i++) { second[i] = second[i - 1] + first[i]; } for(int i = 0; i < 8; i++) { printf("%-5.1f ", first[i]); } printf("\n"); for(int i = 0; i < 8; i++) { printf("%-5.1f ", second[i]); } printf("\n"); return 0; }
第十五题
#include <stdio.h> int main() { char c[255]; char tmp; int i = 0; while(scanf("%c", &tmp) == 1 && tmp != '\n') { c[i] = tmp; i++; } for(i--; i >= 0; i--) { printf("%c", c[i]); } printf("\n"); return 0; }
第十六题
#include <stdio.h> #define DAPHNE 100.0 * 0.1 #define DEIRDRE 1.05 int main() { double Daphne = 100.0; double Deirdre = 100.0; int i = 0; while(Deirdre <= Daphne) { Deirdre *= DEIRDRE; Daphne += DAPHNE; i = i + 1; } printf("Investment values after %d years:\n", i); printf("Daphne: %-5.2f\n", Daphne); printf("Deirdre: %-5.2f\n", Deirdre); return 0; }
第十七题
#include <stdio.h> #define INTEREST 1.08 int main() { double earn = 100; int i = 0; while(earn > 0) { earn *= INTEREST; earn -= 10; i++; } printf("after %d years, Chuckie has taken all of the money!\n", i); return 0; }
第十八题
#include <stdio.h> #define DUNBAR 150 int main() { int fri_num = 5; int i = 1; while(fri_num < DUNBAR) { fri_num -= i; fri_num *= 2; printf("Week %d, Rabnud has %d friends.\n", i, fri_num); i++; } return 0; }