编程|记录——《C Primer Plus (第五版)》第十二章编程练习第1-8题

1.不使用全局变量,重写12.4的程序 。

# include int critic(void); int main(void) { int units = 0; printf("How many pounds to a firkin of butter?\n"); scanf("%d", &units); while(units != 56) { units = critic(); }printf("You must have looked it up!\n"); return 0; }int critic(void) { int units = 0; printf("No luck,chumy.Try again.\n"); scanf("%d", &units); return units; }

2.在美国通常以英里每加仑来计算油耗,在欧洲是以升每百公里来计算,下面是某程序的一部分,该 程序让用户选择一个模式(公制和美制),然后收集数据来计算油耗。
pe2a.c # include # include "pe2a.h"static int model = 0; static float distance = 0; static float fuel = 0; void set_mode(int mode) { model = mode; return; }void get_info(void) { extern int model; extern float distance; extern float fuel; if(0 == model) { printf("Enter distance traveled in kilometers: "); scanf("%f", &distance); printf("Enter fuel consumed in liters: "); scanf("%f", &fuel); } else if(1 == model) { printf("Enter distance traveled in kilometers: "); scanf("%f", &distance); printf("Enter fuel consumed in gallons: "); scanf("%f", &fuel); } }void show_info(void) { extern int model; extern float distance; extern float fuel; if(0 == model) { printf("Fuel consumption is %.2f liters per 100 km.\n", 100*fuel/distance); } else if(1 == model) { printf("Fuel consumption is %.2f miles per gallon.\n", fuel/distance); } }

pe2b.c # include # include "pe2a.h"int main(void) { int mode; printf("Enter 0 for metric mode, 1 for us mode: "); scanf("%d", &mode); while(mode >= 0) { set_mode(mode); get_info(); show_info(); printf("Enter 0 for metric mode, 1 for us mode"); printf("(-1 to quit): "); scanf("%d", &mode); } printf("Done.\n"); return 0; }

void set_mode(int mode); void get_info(void); void show_info(void);

3.重新设计练习二,使用自动变量,程序提供相同的用户界面,也就是说,要提示用户输入模式等。
# include float get_info(int); void show_info(int, float); int model = 0; float distance = 0; float fuel = 0; int main(void) { int model = 0; float unit_fuel = 0; printf("-----------------油耗计算-----------------\n"); printf("模式模式内容\n"); printf("0以升每百公里 \n"); printf("1以加仑每英里\n"); printf(" -1退出\n"); printf("------------------------------------------\n"); printf("Enter 0 for metric mode, 1 for us mode: "); scanf("%d", &model); while(model >= 0) { unit_fuel = get_info(model); show_info(model, unit_fuel); printf("\n-----------------油耗计算-----------------\n"); printf("模式模式内容\n"); printf("0以升每百公里 \n"); printf("1以加仑每英里\n"); printf(" -1退出\n"); printf("------------------------------------------\n"); printf("Enter 0 for metric mode, 1 for us mode"); printf("(-1 to quit): "); scanf("%d", &model); } printf("Done.\n"); return 0; }float get_info(int model) { float distance; float fuel; float unit_fuel; if(0 == model) { printf("Enter distance traveled in kilometers: "); scanf("%f", &distance); printf("Enter fuel consumed in liters: "); scanf("%f", &fuel); unit_fuel = 100*fuel/distance; } else if(1 == model) { printf("Enter distance traveled in kilometers: "); scanf("%f", &distance); printf("Enter fuel consumed in gallons: "); scanf("%f", &fuel); unit_fuel = fuel/distance; } return unit_fuel; }void show_info(int model, float unit_fuel) { if(0 == model) { printf("Fuel consumption is %.2f liters per 100 km.\n", unit_fuel); } else if(1 == model) { printf("Fuel consumption is %.2f miles per gallon.\n", unit_fuel); } }

3.编写一个函数,它返回函数自身被调用的次数,并在一个循环中测试
# include static int i; int f(int n); int main(void) { int i = 0; printf("请输入循环次数(输入0退出程序):"); scanf("%d", &i); while(i != 0) { printf("%d\n", f(i)); printf("请输入循环次数(输入0退出程序):"); scanf("%d", &i); } return 0; }int f(int n) { extern int i; i = 0; if(0 != n) { f(n-1); i++; } return i; }

5.编写产生100个1到10范围的随机数,并且以降序排序。
# include # include void init(int *rands); void descending_sort(int *rands); void show(int *rands); int main(void) { int rands[100] = {0}; init(rands); descending_sort(rands); show(rands); return 0; }void init(int *rands) { int num; for(int i = 0; i < 100; i++) { num = rand() % 10 + 1; rands[i] = num; } }void descending_sort(int *rands) { int num; for(int i = 0; i < 99; i++) { for(int j = i; j < 99; j++) { if(rands[i] < rands[j+1]) { num = rands[i]; rands[i] = rands[j+1]; rands[j+1] = num; } } } }void show(int *rands) { for(int i = 0; i < 100; i++) { printf("%3d ", rands[i]); if(0 == (i+1) % 10) printf("\n"); } }

6 编写产生1000个1到10范围的随机数,并统计每个数产生的次数 .
# include # include int main(void) { int num; int randomness[10] = {0}; //数组装1-10出现的次数for(int i = 0; i < 1000; i++) { num = rand() % 10 + 1; switch (num) { case 1: randomness[0] += 1; break; case 2: randomness[1] += 1; break; case 3: randomness[2] += 1; break; case 4: randomness[3] += 1; break; case 5: randomness[4] += 1; break; case 6: randomness[5] += 1; break; case 7: randomness[6] += 1; break; case 8: randomness[7] += 1; break; case 9: randomness[8] += 1; break; case 10: randomness[9] += 1; break; } } for(int j= 0; j < 10; j++) printf("%3d 出现的次数 %d\n", j+1, randomness[j]); return 0; }

7.编写一个程序,该程序与我们在显示程序清单12.13输出之后所讨论的修改程序具有相同的表现,也就是说,输出应像下面这样:
Enter the number of sets,enter q to stop.
How many sides and how many dice?
Here are 18 sets of 3 6-sided throws:
11 7 10 11 10 15 13 9 12 13 11 9 8 12 8
16 7 9
Enter the number of sets,enter q to stop.
*/
# include # include # include int roll_count = 0; int rollem(int sides); int roll_n_dice(int dice, int sides); int main(void) { int dice, roll; int sides; int num; int i; srand((unsigned int) time(0)); printf("Enter the number of sets: enter q to stop."); scanf("%d", &num); printf("How many sides?\n"); while(scanf("%d", &sides) == 1 && sides > 0) { printf("How many dice?\n"); scanf("%d", &dice); printf("Here are %d sets of %d %d-sides throws.", num, sides, dice); for(i = 0; i < num; i++) { roll = roll_n_dice(dice, sides); printf("%d", roll); } printf("\nHow many sides? Enter 0 to stop.\n"); } printf("The rollem() function was called %d times.\n", roll_count); printf("GOOD FORTUNE TO YOU!\n"); return 0; }int rollem(int sides) { int roll; roll = rand() % sides +1; ++roll_count; return roll; }int roll_n_dice(int dice, int sides) { int d; int total = 0; if(sides < 2) { printf("Need at least 2 sides.\n"); return -2; } if(dice < 1) { printf("Need at least 1 die.\n"); return -1; } for(d = 0; d < dice; d++) { total += rollem(sides); }return total; }

【编程|记录——《C Primer Plus (第五版)》第十二章编程练习第1-8题】8.下面是某程序的一部份,给出函数make_array()和show_array()的定义以使程序完整。函数make_array()接受两个参数。
第一个是int数组的元素个数,第二个是要赋给每个元素的值。函数使用malloc()来创建一个适当大小的数组,把每个元素设定 为指定的值,并返回一个数组指针,函数show_array()以8个数一行的格式显示数组的内容。
# include # include # include int *make_array(int elem, int val); void show_array(const int arr[], int n); int main(void) { int * pa; int size; int value; printf("Enter the number of elements: "); scanf("%d", &size); while(size > 0) { printf("Enter the intitialization value: "); scanf("%d", &value); pa = make_array(size, value); if (pa) { show_array(pa, size); free(pa); } printf("Enter the number of elements(<1 to quit): "); scanf("%d", &size); } printf("Done.\n"); return 0; }int * make_array(int elem, int val) { int * p; p = (int *)malloc(sizeof(int) * elem); for(int i = 0; i < elem; i++) { p[i] = val; }return p; }void show_array(const int arr[], int n) { for(int i = 0; i < n; i++) { printf("%d", arr[i]); if(0 == (i+1) % 8) printf("\n"); } }

    推荐阅读