《C primer plus》ch12答案

《C primer plus》ch12答案

目录

###/Programming Exercise12-1/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*Programming Exercise12-1*/
#include <stdio.h>
int critic(void);
int main()
{
	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;
	printf("No luck,chummy.Try again.\n");
	scanf("%d",&units);
	return units;
}

###/Programming Exercise12-2/

####pe12-2b.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 
#include "pe12-2a.h"
#include <stdio.h>
void set_mode(int *pm)
{
	extern int prev_mode;//外部变量
	if (*pm!=METRIC && *pm!=US)
	{
		printf("Invalid mode specified.Mode %d will be used\n",prev_mode);
		*pm=prev_mode;//mode需要利用上一个mode
	}
	prev_mode=*pm;//储存当前的mode值,作为下一次的前值
}
void get_info(int mode,double *pd,double *pf)
{
	if(mode==METRIC)
	{
		printf("_______METRIC MODE_________\n");
		printf("Enter distane traveled in km : ");
		scanf("%lf",pd);
		printf("Enter fuel consumed in litter : ");
		scanf("%lf",pf);
	}
	else if(mode==US)
	{
		printf("__________US MODE_________\n");
		printf("Enter distane traveled in miles : ");
		scanf("%lf",pd);
		printf("Enter fuel consumed in gallon : ");
		scanf("%lf",pf);
	}
}
void show_info(int mode,double dist,double fuel)
{
	if(mode==METRIC)
		printf("Fuel consumption is %lf litters per 100 km.\n",(fuel/(dist*100)));
	else
		printf("Fuel consumption is %lf miles per gallon km.\n",dist/fuel);
}

####pe12-2a.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
#include "pe12-2a.h"
#include <stdio.h>
int prev_mode=METRIC;//储存上一个mode,文件作用域,外部链接
int main()
{
	int mode;
	double dist;
	double fuel;
	printf("Enter 0 for metric mode,1 for US mode: ");
	scanf("%d",&mode);
	while(mode>=0)
	{
		set_mode(&mode);//检查并设置合适的mode值
		get_info(mode,&dist,&fuel);//要求用户输入
		show_info(mode,dist,fuel);//计算并显示结果
		printf("Enter 0 for metric mode,1 for US mode ");
		printf("(-1 to quit):");
		scanf("%d",&mode);
	}
	printf("Done\n");
	return 0;
}

####pe12-2b.h

1
2
3
4
5
6
 
#define METRIC 0
#define US 1
void set_mode(int *pm);//需要通过指针修改mode
void get_info(int mode,double *pd,double *pf);//需要通过指针修改dist和fuel
void show_info(int mode,double dist,double fuel);//只需调用不需要修改

###/Programming Exercise12-3/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
/*Programming Exercise12-3*/
#include <stdio.h>
#define METRIC 0
#define US 1
int set_mode(int mode,int *prev);//需要通过指针修改mode
void get_info(int mode,double *pd,double *pf);//需要通过指针修改dist和fuel
void show_info(int mode,double dist,double fuel);//只需调用不需要修改
int main()
{
	int mode;
	double dist;
	double fuel;
	int prev_mode=METRIC;//储存上一个mode,文件作用域,外部链接
	printf("Enter 0 for metric mode,1 for US mode: ");
	scanf("%d",&mode);
	while(mode>=0)
	{
		mode=set_mode(mode,&prev_mode);//检查并设置合适的mode值
		get_info(mode,&dist,&fuel);//要求用户输入
		show_info(mode,dist,fuel);//计算并显示结果
		printf("Enter 0 for metric mode,1 for US mode ");
		printf("(-1 to quit):");
		scanf("%d",&mode);
	}
	printf("Done\n");
	return 0;
}
int set_mode(int mode,int *prev)
{
	if (mode!=METRIC && mode!=US)
	{
		printf("Invalid mode specified.Mode %d will be used\n",*prev);
		mode=*prev;//mode需要利用上一个mode
	}
	*prev=mode;//储存当前的mode值,作为下一次的前值
	return mode;
}
void get_info(int mode,double *pd,double *pf)
{
	if(mode==METRIC)
	{
		printf("_______METRIC MODE_________\n");
		printf("Enter distane traveled in km : ");
		scanf("%lf",pd);
		printf("Enter fuel consumed in litter : ");
		scanf("%lf",pf);
	}
	else if(mode==US)
	{
		printf("__________US MODE_________\n");
		printf("Enter distane traveled in miles : ");
		scanf("%lf",pd);
		printf("Enter fuel consumed in gallon : ");
		scanf("%lf",pf);
	}
}
void show_info(int mode,double dist,double fuel)
{
	if(mode==METRIC)
		printf("Fuel consumption is %lf litters per 100 km.\n",(fuel/(dist*100)));
	else
		printf("Fuel consumption is %lf miles per gallon km.\n",dist/fuel);
}

###/Programming Exercise12-4/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
/*Programming Exercise12-4*/
#include <stdio.h>
void call_time();
int times=0;
int i;
int main(int argc, char const *argv[])
{
	for(i=0;i<10;i++)
	{
		call_time();
	}
	printf("%d\n",times);
	return 0;
}

void call_time()
{
	times++;
}		
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
###/*Programming Exercise12-5*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
int main(int argc, char const *argv[])
{
	int i,j;
	int num[SIZE];
	int temp;/*用于交换的中间容器*/
	srand(time(NULL));
	for(i=0;i<SIZE;i++)
	{
		num[i]=rand()%10;/*生成0-10的随机数,储存在一个数组中等待排序
	}
	for(i=0;i<SIZE;i++)/*打印原始数据*/
	{
		printf("%d ", num[i]);
	}
	printf("\n");
	for(i=0;i<SIZE;i++)/*冒泡排序*/
	{
		for(j=SIZE-1;j>i;j--)
		{
			if (num[j]>num[j-1])
			{
				temp=num[j-1];
				num[j-1]=num[j];
				num[j]=temp;
			}

		}
	}
	for(i=0;i<SIZE;i++)
	{
		printf("%d ", num[i]);
	}
	printf("\n");
	return 0;
}

###/Programming Exercise12-6/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
/*Programming Exercise12-6*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
	int num;
	int temp[10]={0};
	int i;
	int total=0;
	srand(time(NULL));
	for(i=0;i<1000;i++)
	{
		num=rand()%10;
		temp[num]++;/*用产生的数做索引,并把相应位置的数+1*/
	}
	for(num=0;num<10;num++)
	{
		printf("%d :%d\n", num,temp[num]);
		total+=temp[num];
	}
	printf("\n");
	printf("total:%d\n",total );
	return 0;
}

###/Programming Exercise12-7/

####12-7.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>
#include <time.h>	
#include "diceroll.h"

int main(int argc, char const *argv[])
{
	int roll;
	int dice,sides,sets;
	int i;
	srand((unsigned int)time(NULL));
	printf("Enter the number of sets;q to stop.\n");
	while(scanf("%d",&sets)==1 && sets>0) 
	{	
		printf("How many sides and how many dice?\n");
		scanf("%d %d",&sides,&dice);
		printf("Here are %d sets of %d %d-sided throw.\n   ",sets,sides,dice );
		for(i=0;i<sets;i++)
		{
			roll=roll_n_dice(dice,sides);
			printf("%d ", roll);
		}
		printf("\n");
		printf("Enter the number of sets;q to stop.\n");
	}
	return 0;
}

####/diceroll.c/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*diceroll.c*/
#include <stdio.h>
#include <stdlib.h>	
#include "diceroll.h"
static int rollem(int sides)
{
	int roll;
	roll=rand()%sides+1;//产生1-sides的随机数
	return roll;
}
int roll_n_dice(int dice,int sides)
{
	int i;
	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(i=0;i<dice;i++)
		total+=rollem(sides);
	return total;
}

####/diceroll.h/

1
2
/*diceroll.h*/
int roll_n_dice(int dice,int sides);

###/Programming Exercise12-8/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*Programming Exercise12-8*/
#include <stdio.h>
#include <stdlib.h>
int * make_array(int elem,int val);
void show_array(const int ar[],int n);
int main()
{
	int *pa;
	int size;
	int value;
	printf("Enter the number of elements: ");
	scanf("%d",&size);
	while(size>0) 
	{
		printf("Enter the initialization value: ");
		scanf("%d",&value);
		pa=make_array(size,value);//如果没有分配成功返回NULL
		if(pa)//分配成功
		{
			show_array(pa,size);
				free(pa);//使用malloc就要free
		}
		printf("Enter the number of elements(<1 to stop)\n");
		scanf("%d",&size);
	}
	printf("Done.\n");
	return 0;
}
int * make_array(int elem,int val)
{
	int *p;
	int *start;
	int i;
	p=(int *)malloc(sizeof(int)*elem);//分配elem个int大小的空间
	start=p;//保存指针初始位置
	for(i=0;i<elem;i++)
	{
		*(p++)=val;//指针移动,所以要保存初始指针位置
	}
	return start;//返回数组首地址
}
void show_array(const int ar[],int n)//使用const防止修改数组
{
	int i;
	for(i=0;i<n;i++)
	{
		if (i%8==0)
			printf("\n");//每个8个数字进行一次换行
		printf("%d ", ar[i]);
	}
	printf("\n");
}

源代码:

github repo


本文采用中国大陆版CC协议发布

作者保留以下权利:

  1. 署名(Attribution):必须提到原作者。
  2. 非商业用途(Noncommercial):不得用于盈利性目的。
  3. 禁止演绎(No Derivative Works):不得修改原作品, 不得再创作。
    新浪微博 @XX含笑饮砒霜XX