学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出!
1.不使用全局变量,重写程序清单12.4。
#include <stdio.h>
void critic(int* units)
{
printf("No luck, my friend. Try again.\n");
scanf("%d", 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;
}
2.在美国,通常以英里/加仑来计算油耗;在欧洲,以升/100 公里来计 算。下面是程序的一部分,提示用户选择计算模式(美制或公制),然后接 收数据并计算油耗。
// pe12-2b.c
// 与 pe12-2a.c 一起编译
#include <stdio.h>
#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; }
下面是是一些输出示例:
Enter 0 for metric mode, 1 for US mode: 0
Enter distance traveled in kilometers: 600
Enter fuel consumed in liters: 78.8
Fuel consumption is 13.13 liters per 100 km.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 1
Enter distance traveled in miles: 434
Enter fuel consumed in gallons: 12.7
Fuel consumption is 34.2 miles per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): 3
Invalid mode specified. Mode 1(US) used.
Enter distance traveled in miles: 388
Enter fuel consumed in gallons: 15.3
Fuel consumption is 25.4 miles per gallon.
Enter 0 for metric mode, 1 for US mode (-1 to quit): -1
Done.
如果用户输入了不正确的模式,程序向用户给出提示消息并使用上一次 输入的正确模式。请提供pe12-2a.h头文件和pe12-2a.c源文件。源代码文件应 定义3个具有文件作用域、内部链接的变量。一个表示模式、一个表示距 离、一个表示消耗的燃料。get_info()函数根据用户输入的模式提示用户输入 相应数据,并将其储存到文件作用域变量中。show_info()函数根据设置的模 式计算并显示油耗。可以假设用户输入的都是数值数据。
#include <stdio.h>
#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;
}
3.重新设计编程练习2,要求只使用自动变量。该程序提供的用户界面 不变,即提示用户输入模式等。但是,函数调用要作相应变化。
#include <stdio.h>
#include "pe12-2a.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;
}
4.在一个循环中编写并测试一个函数,该函数返回它被调用的次数。
#include <stdio.h>
static int count = 0;
int counter(void)
{
return ++count;
}
int main(void)
{
int i, j;
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;
}
5.编写一个程序,生成100个1~10范围内的随机数,并以降序排列(可 以把第11章的排序算法稍加改动,便可用于整数排序,这里仅对整数排 序)。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define LEN 100
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)
{
printf("\n");
}
}
printf("\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;
}
6.编写一个程序,生成1000个1~10范围内的随机数。不用保存或打印 这些数字,仅打印每个数出现的次数。用 10 个不同的种子值运行,生成的 数字出现的次数是否相同?可以使用本章自定义的函数或ANSI C的rand()和 srand()函数,它们的格式相同。这是一个测试特定随机数生成器随机性的方 法。
#include <stdio.h>
#include <stdlib.h>
#define N 10
#define LEN 1000
int 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;
}
7.编写一个程序,按照程序清单12.13输出示例后面讨论的内容,修改该 程序。使其输出类似: Enter the number of sets;
enter q to stop : 18
How many sides and how many dice? 6 3
Here are 18 sets of 3 6-sided throws.
12 10 6 9 8 14 8 15 9 14 12 17 11 7 10
13 8 14 H
ow many sets?
Enter q to stop: q
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int rollem(int sides)
{
return rand() % sides + 1;
}
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;
}
8.下面是程序的一部分:
// pe12-8.c
#include <stdio.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("%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; }
提供make_array()和show_array()函数的定义,完成该程序。make_array() 函数接受两个参数,第1个参数是int类型数组的元素个数,第2个参数是要赋 给每个元素的值。该函数调用malloc()创建一个大小合适的数组,将其每个 元素设置为指定的值,并返回一个指向该数组的指针。show_array()函数显 示数组的内容,一行显示8个数。
#include <stdio.h>
#include <stdlib.h>
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(0);
}
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)
{
printf("\n");
}
}
printf("\n");
return;
}
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;
}
9.编写一个符合以下描述的函数。首先,询问用户需要输入多少个单 词。然后,接收用户输入的单词,并显示出来,使用malloc()并回答第1个问 题(即要输入多少个单词),创建一个动态数组,该数组内含相应的指向 char的指针(注意,由于数组的每个元素都是指向char的指针,所以用于储 存malloc()返回值的指针应该是一个指向指针的指针,且它所指向的指针指向char)。在读取字符串时,该程序应该把单词读入一个临时的char数组, 使用malloc()分配足够的存储空间来储存单词,并把地址存入该指针数组 (该数组中每个元素都是指向 char 的指针)。然后,从临时数组中把单词 拷贝到动态分配的存储空间中。因此,有一个字符指针数组,每个指针都指 向一个对象,该对象的大小正好能容纳被储存的特定单词。下面是该程序的 一个运行示例:
How many words do you wish to enter? 5
Enter 5 words now:
I enjoyed doing this exerise
Here are your words: I
enjoyed
doing
this
exercise
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 256
int 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));
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;
}
else
{
printf("Memory allocation failed!\n");
exit(EXIT_FAILURE);
}
return 0;
}