C Primer Plus 第六版(中文版)第十二章(完美修订版)编程练习答案
//本博主所写的代码仅为阅读者提供参考;
//若有不足之处请提出,博主会尽所能修改;
//附上课后编程练习题目;
//若是对您有用的话请点赞或分享提供给它人;



//12.9 - 1.c
#include void critic(int *units);int main(void)
{int units;printf("How many pounds to a firkin of butter?\n");scanf("%d", &units);while (units != 56){critic(&units);}printf("You must have looked it up!\n");return 0;
}void critic(int *units)
{printf("No luck, my friend. Try again.\n");scanf("%d", units);return;
}
//---------------------------------------------------------------
//注意:要分成多个编译单元进行同时编译;
//pe12-2b.c
#include
#include "pe12-2a.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;
}
//pe12-2a.h
void set_mode(int n);
void get_info(void);
void show_info(void);
//pe12-2a.c
#include
#include "pe12-2a.h"static int mode;
static double range;
static double fuel;void set_mode(int n)
{if (n > 1){printf("Invalid mode specified. Mode %s used.\n",(0 == mode) ? "0(metric)" : "1(US)");}else{mode = n;}return;
}void get_info(void)
{if (0 == mode){printf("Enter distance traveled in kilometers: ");}else{printf("Enter distance traveled in miles: ");}scanf("%lf", &range);if (0 == mode){printf("Enter fuel consumed in liters: ");}else{printf("Enter fuel consumed in gallons: ");}scanf("%lf", &fuel);return;
}void show_info(void)
{if (0 == mode){printf("Fuel consumption is %.2lf liters per 100km.\n",(fuel / range) * 100);}else{printf("Fuel consumption is %.1lf miles per gallon.\n",range / fuel);}return;
}
//---------------------------------------------------------------
//注意:要分成多个编译单元进行同时编译;
//pe12-3b.c
#include
#include "pe12-3a.h"int main(void)
{int temp, mode;double range, fuel;printf("Enter 0 for metric mode, 1 for US mode: ");scanf("%d", &mode);temp = mode;while (mode >= 0){set_mode(&mode, &temp);get_info(temp, &range, &fuel);show_info(temp, range, fuel);printf("Enter 0 for metric mode, 1 for US mode");printf(" (-1 to quit): ");scanf("%d", &mode);}printf("Done.\n");return 0;
}
//pe12-3a.h
void set_mode(int *mode, int *n);
void get_info(int mode, double *range, double *fuel);
void show_info(int mode, double range, double fuel);
//pe12-3a.c
#include
#include "pe12-3a.h"void set_mode(int *mode, int *n)
{if (*mode > 1){printf("Invalid mode specified. Mode %s used.\n",(0 == *n) ? "0(metric)" : "1(US)");}else{*n = *mode;}return;
}void get_info(int mode, double *range, double *fuel)
{if (0 == mode){printf("Enter distance traveled in kilometers: ");}else{printf("Enter distance traveled in miles: ");}scanf("%lf", range);if (0 == mode){printf("Enter fuel consumed in liters: ");}else{printf("Enter fuel consumed in gallons: ");}scanf("%lf", fuel);return;
}void show_info(int mode, double range, double fuel)
{if (0 == mode){printf("Fuel consumption is %.2lf liters per 100 km.\n",(fuel / range) * 100);}else{printf("Fuel consumption is %.1lf miles per gallon.\n",range / fuel);}return;
}
//---------------------------------------------------------------
//12.9 - 4.c
#include static int count = 0;int counter(void);int main(void)
{int i, j, k;printf("Please enter a integer: ");scanf("%d", &i);for (j = 1; j <= i; j++){printf("count = %d\n", counter());}printf("The function called %d times.\n", count);return 0;
}int counter(void)
{return ++count;
}
//-------------
//12.9 - 5.c
#include
#include
#include
#define LEN 100void sort(int a[], int n);
void show_array(const int a[], int n);int main(void)
{int i, a[LEN];srand((unsigned int)time(0));for (i = 0; i < LEN; i++){a[i] = rand() % 10 + 1;}printf("Array:\n");show_array(a, LEN);sort(a, LEN);printf("After sorting:\n");show_array(a, LEN);return 0;
}void sort(int a[], int n)
{int i, j, t;for (i = 0; i < n - 1; i++){for (j = i + 1; j < n; j++){if (a[i] < a[j]){t = a[j];a[j] = a[i];a[i] = t;}}}return;
}void show_array(const int a[], int n)
{int i;for (i = 0; i < n; i++){printf("%-3d ", a[i]);if (0 == (i + 1) % 10){putchar('\n');}}putchar('\n');return;
}
//-------------
//12.9 - 6.c
#include
#include
#define N 10
#define LEN 1000int main(void)
{int i, temp, a[N + 1];unsigned int seeds;for (seeds = 1; seeds <= N; seeds++){printf("Time #%d:\n", seeds);srand(seeds);for (i = 0; i < N + 1; i++){a[i] = 0;}for (i = 0; i < LEN; i++){temp = rand() % 10 + 1;a[temp]++;}for (i = 1; i < N + 1; i++){printf("%-3d appeared %d times.\n", i, a[i]);}printf("Total random numbers: %d\n\n", LEN);}return 0;
}
//-------------
//12.9 - 7.c
#include
#include
#include int rollem(int sides);int main(void)
{int dice, count, roll;int sides;int set, sets;srand((unsigned int)time(0));printf("Enter the number of sets.\nEnter q to stop: ");while (scanf("%d", &sets) == 1){printf("How many sides and how many dice?\n");printf("Please two integers: ");if (scanf("%d %d", &sides, &dice) != 2){puts("Not integers -- terminating input loop.");break;}printf("Here are %d sets of %d %d-sided throws.\n", sets, dice, sides);for (set = 0; set < sets; set++){for (roll = 0, count = 0; count < dice; count++){roll += rollem(sides);}printf("%-3d", roll);if (0 == (set + 1) % 8){putchar('\n');}}printf("\nHow many sets? Enter q to stop: ");}puts("GOOD FORTUNE TO YOU!\n");return 0;
}int rollem(int sides)
{return rand() % sides + 1;
}
//-------------
//12.9 - 8.c
#include
#include int *make_array(int elem, int val);
void show_array(const int ar[], int n);int main(void)
{int *pa;int size;int value;printf("Enter the number of elements: ");while (scanf("%d", &size) == 1 && size > 0){printf("Enter the initialization 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): ");}printf("Done.\n");return 0;
}int *make_array(int elem, int val)
{int i;int *pt;pt = (int *)malloc(elem * sizeof(int));if (NULL == pt){printf("Memory allocation failed!\n");exit(EXIT_FAILURE);}printf("Output %d numbers:\n", val);for (i = 0; i < elem; i++){pt[i] = val;}return pt;
}void show_array(const int ar[], int n)
{int i;for (i = 0; i < n; i++){printf("%d ", ar[i]);if (0 == (i + 1) % 8){putchar('\n');}}putchar('\n');return;
}
//-------------
//12.9 - 9.c
#include
#include
#include
#define LEN 256int main(void)
{char **pt;int i, n, length;static char temp[LEN];printf("How many words do you wish to enter? ");scanf("%d", &n);if ((pt = (char **)malloc(n * sizeof(char *))) != NULL){printf("Enter %d words now:\n", n);for (i = 0; i < n; i++){scanf("%255s", temp);length = strlen(temp) + 1;pt[i] = (char *)malloc(length * sizeof(char));//↑使用malloc分配足够的存储空间来存储单词;if (NULL == pt[i]){printf("Memory allocation failed!\n");exit(EXIT_FAILURE);}strcpy(pt[i], temp);//↑从临时数组中把单词拷贝到动态分配的存储空间中;}printf("Here are your words:\n");for (i = 0; i < n; i++){puts(pt[i]);free(pt[i]);pt[i] = NULL;}free(pt);pt = NULL;//↑指针仍然指向malloc分配的存储空间;//↑因此令指针指向NULL后防止内存滥用;}else{printf("Memory allocation failed!\n");exit(EXIT_FAILURE);}return 0;
}
//-------------
//----------------------------2020年4月4日 -------------------------------
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
