C Primer Plus(第六版)第十二章 编程练习答案

Posted 水番正文

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C Primer Plus(第六版)第十二章 编程练习答案相关的知识,希望对你有一定的参考价值。

如果说之前是在编程的基础上扩展,那么这次就是从线到面的第一步了!

有问题欢迎提问,有BUG欢迎提出!

本人用的编译器的DevC++ 多文件编译要创建项目 附一篇别人的博客

C语言(一) Dev-C++ 安装和创建项目_liu362732346的博客-CSDN博客_devc++新建项目

CH12 Code answer 1:

#include <stdio.h> 

int critic(int);

int main(void)
{
	int units;
	
	printf("How many pounds to a firkin of butter?\\n");
	scanf("%d",&units);
	while(units != 19)
		units = critic(units);
	printf("You must have looked it up\\n");
	
	return 0;	
}

int critic(int n)
{
	printf("No luck, my friend, Try again\\n");
	scanf("%d",&n);
	
	return n;
}

CH12 Code answer 2:

模块一

#include <stdio.h>
#include "pel2-2a.h"

static int mode, km;
static float fu;

void set_mode(int n) //直接用mode变量存储发来的值 弄了个set_mode(mode) 这样实际是创建了一个mode变量并没有用  
{
	if(n > 1)
	{
		printf("Invalid mode specified . Mode 1(US) used.\\n");
		mode = n;
	} 
	else if(n == 0) 
		mode = 0;
	else
		mode = 1;
}

void get_info(void)
{
	if(mode == 0)
	{
		printf("Enter distance traveled in kilometers:");
		scanf("%d", &km);
		printf("Enter fule consumed in liters:");
		scanf("%f", &fu);
	}
	else
	{
		printf("Enter distance traveled in miles:");
		scanf("%d", &km);
		printf("Enter fuel consumed in gallons:");
		scanf("%f", &fu);
	}
}

void show_info(void)
{
	if(mode == 0)
		printf("Fuel consumption is %.1f liters per 100 km.\\n",fu/km*100);
	else
		printf("Fuel consumption is %.1f miles per gallon.\\n",fu/km*1000);
}

模块二

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

模块三

#include <stdio.h>
#include "pel2-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;
}

CH12 Code answer 3:

模块一

#include <stdio.h>
#include "pel2-2a.h"

static int km;
static float fu;

int set_mode(int mode) 
{
	if(mode >= 1)
	{
		printf("Invalid mode specified . Mode 1(US) used.\\n");
		return 1;
	} 
	else
		return 0;
}

void get_info(int mode, float * k_m)
{
	if(mode == 0)
	{
		printf("Enter distance traveled in kilometers:");
		scanf("%f", &k_m[0]);
		printf("Enter fule consumed in liters:");
		scanf("%f", &k_m[1]);
	}
	else
	{
		printf("Enter distance traveled in miles:");
		scanf("%f", &k_m[0]);
		printf("Enter fuel consumed in gallons:");
		scanf("%f", &k_m[1]);
	}
}

void show_info(int mode, float * k_m)
{
	if(mode == 0)
		printf("Fuel consumption is %.1f liters per 100 km.\\n",k_m[1]/k_m[0]*100);
	else
		printf("Fuel consumption is %.1f miles per gallon.\\n",k_m[1]/k_m[0]*1000);
}

模块二

int set_mode(int);
void get_info(int, float *);
void show_info(int, float *);

模块三

#include <stdio.h>
#include "pel2-2a.h"

int main(void)
{
	int mode;
	float k_m[2] = {0};
	
	printf("Enter 0 for metric mode, 1 for US mode: ");
	scanf("%d", &mode);
	while(mode >= 0)
	{
		mode = set_mode(mode);
		get_info(mode, k_m);
		show_info(mode, k_m);
		printf("Enter 0 for metric mode, 1 for US mode");
		printf(" (-1 to quit): ");
		scanf("%d", &mode);
	}
	printf("Done!\\n");
	
	return 0;
}

CH12 Code answer 4:

#include <stdio.h>

static int count;

int To_Count(void);

int main(void)
{
	int i;
	
	for(i = 0; i < 10; i++)
		printf("This is the %d time\\n",To_Count());
	
	return 0;
}

int To_Count(void)
{
	return ++count;
}

CH12 Code answer 5:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

int main(void)
{
	int data[SIZE];
	int i, j, tmp;
	
	for(i = 0; i < SIZE; i++)
		data[i] = rand() % 10 + 1;
	for(i = 0; i < SIZE - 1; i++)
	{
		for(j = i + 1; j < SIZE; j++)
			if(data[i] <= data[j])
			{
				tmp = data[i];
				data[i] = data[j];
				data[j] = tmp;
			}
		printf("The %d is %d \\n", i + 1, data[i]);
	}	
	
	return 0;
}

CH12 Code answer 6:

r_drive

#include <stdio.h>

#define SIZE 10

extern void srand1(unsigned int x);
extern int rand1(void); 

int main(void)
{
	int count;
	unsigned seed;
	int num[SIZE] = {0};
	
	printf("Please enter your choice for seed.\\n");
	while(scanf("%d", &seed) == 1)
	{
		srand1(seed);
		for(count = 0; count < 1000; count++)
			num[rand1()-1]++;
	
		for(count = 0; count < SIZE; count++)
		{
			printf("This %d had %d time.\\n", count + 1, num[count]);
			num[count] = 0;
		}
			
		printf("Please enter next seed (q to quit):\\n"); 
	}
	
	return 0;	
} 

s_and_r

static unsigned long int next = 1;

int rand1(void)
{
	next = next * 1103515245 + 12345;
	return (unsigned int) (next / 65535) % 10 + 1;	//输出1到10 
}

void srand1(unsigned int seed)
{
	next = seed;
}

CH12 Code answer 7:

Manydice

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "diceroll.h"

int main(void)
{
	int dice, roll;
	int sides, sets;
	int i;
	
	srand((unsigned int) time(0));		//随机种子 
	printf("Enter the number of sets; enter q to stop.\\n"); 
	while(scanf("%d", &sets) == 1 && sets > 0)
	{
		printf("How many sides and how many dice?\\n");
		scanf("%d", &sides);
		scanf("%d", &dice);
		
		printf("Here are %d sets of %d %d-sided throws.\\n", sets, dice, sides);
		for(i = 0; i < sets; i++)
			printf("%d ",roll_n_dice(dice, sides));
		
		printf("\\nHow many sets? Enter q to stop.\\n");
	}
	
	printf("The rollem() function was called %d times.\\n",roll_count);
	printf("GOOD FORTUNE TO YOU!\\n"); 
	
	return 0;
} 

/*		if((status = scanf("%d", &dice)) != 1)	//错误处理 
		{
			if(status == EOF)
				break;
			else
			{
				printf("You should have entered an integer.");
				printf(" Let's begin again.\\n'");
				while(getchar() != '\\n')
					continue;
				printf("How many sides?Enter 0 to stop.\\n");
				continue;
			}	
		}
*/	

diceroll.h

extern int roll_count;

int roll_n_dice(int dice, int sides);

diceroll.c

#include "diceroll.h"
#include <stdio.h>
#include <stdlib.h>

int roll_count = 0;

static 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");
	}
	
	for(d = 0; d < dice; d++)
		total += rollem(sides);
	
	return total;
}

彩蛋!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
	srand((unsigned int) time(0));
	int num = rand() % 100 + 1;
	int n;
	
	printf("猜猜这是什么数字!");
	scanf("%d", &n); 
	while(num != n)
	{
		printf("再试试!"); 
		scanf("%d", &n);
	}
	printf("你一定作弊了!"); 
	
	return 0;			
} 

CH12 Code answer 8:

模块一

#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!");
	
	return 0;
} 

模块二

#include <stdlib.h>

int make_array(int elem, int value)
{
	int * pa;
	int i;
	
	pa = (int *) malloc(elem * sizeof(int));
	for(i = 0; i < elem; i++)
		pa[i] = value;
	
	return pa;
}

void show_array(const int ar[], int n)
{
	int i;
	
	for(i = 0; i < n; i++)
	{
		printf("%d ",ar[i]);
		if( (i+1) % 8 == 0 )
			printf("\\n");
	}
		
}

CH12 Code answer 9:

我分成了两个模块打,一个模块也行,但按这张的教的感觉一个项目不错。

模块一

#include <stdio.h>
#include <string.h>

char ** Creat(int);
char * To_Ar(char *, int);

int main(void)
{
	int i, j;
	int size;
	char str[20];
	char * *pc;
	
	printf("How many words do you wish to enter?");
	while( scanf("%d",&size) == 1 && size > 0 )
	{
		pc = Creat(size);
		printf("Enter %d words now:\\n", size);
		for(j = 0; j < size; j++)
		{
			scanf("%s", str);
//			printf("+++%s+++ %d", str, strlen(str)); 
			pc[j] = To_Ar(str, strlen(str));
		}
		printf("Here give your words:\\n");
		for(i = 0; i < size; i++)
			puts(pc[i]);
		printf("How many words do you wish to enter?(<1 to quit)");
	}
	
	return 0;	
} 

模块二

#include <stdlib.h>

char ** Creat(int n)
{
	char * *pc;
	pc = (char **) malloc(n * sizeof(char *));
	
	return pc;
}

char * To_Ar(char * str, int n)
{
	char * ptr;
	int i;
	
	ptr = (char *) malloc((n + 1) * sizeof(char)); //多一个元素与用来放结束符号 '\\0' 
	for( i = 0; i < n; i++)
		ptr[i] = str[i];
	ptr[n] = '\\0';
	
	return ptr;
}

以上是关于C Primer Plus(第六版)第十二章 编程练习答案的主要内容,如果未能解决你的问题,请参考以下文章

C Primer Plus(第六版)第十四章 编程练习答案

C Primer Plus(第六版)第十五章 编程练习答案

C Primer Plus(第六版)第十一章 编程练习答案

C Primer Plus(第六版)第十七章 编程练习答案

C Primer Plus(第六版)第十三章 编程练习答案

C Primer Plus(第六版)第十六章 编程练习答案