C primer plus第12章编程练习答案

    技术2025-05-01  31

    编程练习答案是我学习过程中所敲,作为一个学渣,答案是以完成习题和易理解为导向,很少有一些五彩缤纷的算法。程序仅作为交流,如有错误与不足还请指出。 所用软件:VS2019

    题号

    12.9.112.9.212.9.312.9.412.9.512.9.612.9.712.9.812.9.9

    12.9.1

    /* global.c -- 使用外部变量 */ #include <stdio.h> void critic(int * p); int main(void) { int units = 0; /* 可选的重复声明 */ printf("How many pounds to a firkin of butter?\n"); scanf_s("%d", &units); while (units != 56) critic(&units); printf("You must have looked it up!\n"); return 0; } void critic(int* p) { /* 删除了可选的重复声明 */ printf("No luck, my friend. Try again.\n"); scanf_s("%d", p); }

    12.9.2

    // main函数 #include <stdio.h> #include "pe12-2a.h" int main(void) { int mode; printf("Enter 0 for metric mode, 1 for US mode: "); scanf_s("%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_s("%d", &mode); } printf("Done.\n"); return 0; } //.文件 #ifndef PE12_2A_H #define PE12_2A_H void set_mode(int mode); void get_info(void); void show_info(void); #endif //.c文件 #include "pe12-2a.h" #include "stdio.h" int mode = 0;//模式 int distance = 0;//距离 double fuel = 0;//燃料 void set_mode(int m_mode) { if (m_mode == 0 || m_mode == 1) mode = m_mode; else//不是0或者1 { printf("Invalid mode specified.Mode %d used.\n",mode); } } void get_info(void) { if (mode == 0) { printf("Enter distance traveled in kilometers : "); scanf_s("%d", &distance); printf("Enter fuel consumed in liters : "); scanf_s("%lf", &fuel); } else { printf("Enter distance traveled in miles : "); scanf_s("%d", &distance); printf("Enter fuel consumed in gallons :"); scanf_s("%lf", &fuel); } } void show_info(void) { if (mode == 0) { printf("Fuel consumption is %.2lf liters per 100 km.\n", fuel / (double)distance * 100.0); } else printf("Fuel consumption is %.1lf miles per gallon.\n", distance / fuel); }

    12.9.3

    //main函数 #include <stdio.h> #include "pe12-2a.h" int main(void) { int mode; printf("Enter 0 for metric mode, 1 for US mode: "); scanf_s("%d", &mode); while (mode >= 0) { get_set_show(mode); printf("Enter 0 for metric mode, 1 for US mode"); printf(" (-1 to quit): "); scanf_s("%d", &mode); } printf("Done.\n"); return 0; } #ifndef PE12_2A_H #define PE12_2A_H void get_set_show(int mode); #endif #include "pe12-2a.h" #include "stdio.h" void get_set_show(int mode) { static now_mode = 0; int distance; double fuel; if (mode == 0 || mode == 1) now_mode = mode; else//不是0或者1 printf("Invalid mode specified.Mode %d used.\n", now_mode); if (now_mode == 0) { printf("Enter distance traveled in kilometers : "); scanf_s("%d", &distance); printf("Enter fuel consumed in liters : "); scanf_s("%lf", &fuel); printf("Fuel consumption is %.2lf liters per 100 km.\n", fuel / (double)distance * 100.0); } else { printf("Enter distance traveled in miles : "); scanf_s("%d", &distance); printf("Enter fuel consumed in gallons :"); scanf_s("%lf", &fuel); printf("Fuel consumption is %.1lf miles per gallon.\n", distance / fuel); } }

    12.9.4

    #include "stdio.h" int test(void); int main(void) { int num = 0; for (int i = 0;i < 50;i++) num = test(); printf("该函数被调用了%d次", num); return 0; } int test(void) { static int num = 0; num++; printf("这是该函数第%d次被调用\n",num); return num; }

    12.9.5

    #include "stdio.h" #include "stdlib.h" int main(void) { int temp; int num[100]; for (int i = 0;i < 100;i++) num[i] = rand() % 10 + 1; for(int i = 0;i < 100;i++)//冒泡排序 for (int j = 0;j < 100 - i - 1;j++) { if (num[j+1] > num[j]) { temp = num[j]; num[j] = num[j+1]; num[j + 1] = temp; } } for (int i = 0;i < 100;i++) { printf("%d\n", num[i]); } return 0; }

    12.9.6

    #include "stdio.h" #include "stdlib.h" int main(void) { int count[10][10] = {0};//计数 10组 每组0-9 int num[10][1000];//数据 10组 每组1000 for(int i = 0;i < 10;i++) for (int j = 0;j < 1000;j++) num[i][j] = rand() % 10 + 1; for (int i = 0;i < 10;i++) { for (int j = 0;j < 1000;j++) { switch (num[i][j]) { case 1:count[i][0] += 1;break; case 2:count[i][1] += 1;break; case 3:count[i][2] += 1;break; case 4:count[i][3] += 1;break; case 5:count[i][4] += 1;break; case 6:count[i][5] += 1;break; case 7:count[i][6] += 1;break; case 8:count[i][7] += 1;break; case 9:count[i][8] += 1;break; case 10:count[i][9] += 1;break; default:exit(EXIT_FAILURE); } } } for (int i = 0;i < 10;i++) { for (int j = 1;j < 11;j++) { printf("第%d组%d出现的次数为%d\n", i, j, count[i][j]); } } return 0; }

    12.9.7

    //main函数 #include <stdio.h> #include <stdlib.h> /* 为库函数 srand() 提供原型 */ #include <time.h> /* 为 time() 提供原型 */ #include "diceroll.h" /* 为roll_n_dice()提供原型, 为roll_count变量 提供声明 */ int main(void) { int sets; int sides, dice; srand((unsigned int)time(0)); /* 随机种子 */ printf("Enter the number of sets; enter q to stop :"); while (scanf_s("%d", &sets) == 1 && sets != 'q' ) { printf("How many sides and how many dice?"); scanf_s("%d%d", &sides,&dice); printf("Here are %d sets of %d %d-sided throws.\n", sets, sides, dice); for (int i = 0;i < sets;i++) { printf("%d ", roll_n_dice(sides, dice)); } printf("How many sets? Enter q to stop:"); } printf("done!\n"); return 0; } //.h文件 #ifndef _DICEROLL_H #define _DICEROLL_H extern int roll_count; int roll_n_dice(int dice, int sides); #endif //.c文件 #include "diceroll.h" #include <stdio.h> #include <stdlib.h> /* 提供库函数 rand()的原型 */ int roll_count = 0; /* 外部链接 */ static int rollem(int sides) /* 该函数属于该文件私有 */ { int roll; roll = rand() % sides + 1; ++roll_count; /* 计算函数调用次数 */ return roll; } 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; }

    12.9.8

    #include <stdio.h> #include "stdlib.h" 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_s("%d", &size) == 1 && size > 0) { printf("Enter the initialization value: "); scanf_s("%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* elem_ptr = (int*)malloc(sizeof(int) * elem);//分配内存 for (int i = 0;i < elem;i++)//赋值 { elem_ptr[i] = val; } return elem_ptr; } void show_array(const int ar[], int n) { for (int i = 0;i < n;i++) { if (i % 8 == 0 && i != 0) printf("\n"); printf("%d", ar[i]); } printf("\n"); }

    12.9.9

    #include "stdio.h" #include "string.h" #include "stdlib.h" int main(void) { int word_num = 0; printf("How many words do you wish to enter ?\n"); scanf_s("%d", &word_num); char** num_ptr = (char **)malloc(word_num * sizeof(char*)); printf("Enter %d words now :\n",word_num); for (int i = 0;i < word_num;i++) { char word_temp[100];//临时储存单词 scanf("%s", word_temp);//读取字符串 char* word_ptr = (char *)malloc(sizeof(char) * strlen(word_temp) + 1);//分配内存给单词 for (int j = 0;j < sizeof(char) * strlen(word_temp) + 1;j++)//字符串放到动态内存中 +1很重要要把\0放进去 { word_ptr[j] = word_temp[j]; } //单词的地址给指针数组 num_ptr[i] = word_ptr; } printf("Here are your words:\n"); for (int i = 0;i < word_num;i++) { printf("%s\n", num_ptr[i]); } return 0; }
    Processed: 0.012, SQL: 9