C语言程序设计现代方法第二版,第八章课后编程习题全部答案

自己练习时手写,难免会有些疏忽遗漏等各种各样问题,错误之处还请指出

但这些代码确实已通过编译,实现了书上的输出结果,还希望能给需要之人作为个小参考


8_1

#include 
#include 

int main (void)
{
	bool digit_seen[10] = {false}, digit_wr[10] = {false}, flag = false;
	int digit;
	long n;
	
	printf ("Enter a number: ");
	scanf ("%ld", &n);
	
	while (n > 0) {
		digit = n % 10;
		if (digit_seen[digit] == true) {
			flag = true;
			digit_wr[digit] = true;
		}
		digit_seen[digit] = true;
		n /= 10;
	}
	
	if (flag) {
		printf ("Repeated digits(s): ");
		for (int i = 0; i < 10; i++) {
			if (digit_wr[i]) printf ("%d ", i);
		} 
	}
	else 
		printf ("No repeated digit\n");
	
	return 0;
 } 

8_2

#define _CRT_SECURE_NO_WARNINGS

#include 
#include 

int main (void)
{
	int digit_wr[10] = {0};
	int digit;
	int n;
	
	printf ("Enter a number:");
	scanf ("%d", &n);
	
	while (n > 0) {
		digit = n % 10;
		digit_wr[digit]++;
		n /= 10;
	} 
	
	printf ("Digit:\t\t");
	for (int i = 0; i < 10; i++) {
		printf ("%3d", i);
	}
	
	printf ("\nOccurrences:\t");
	for (int i = 0; i < 10; i++) {
		printf ("%3d", digit_wr[i]);
	}

	printf("\n");
	system("pause");
	return 0;
 } 

8_3

#include 
#include 

/* * * * * * * * * * * * * * * * * * * * * * * * 
 *	连续输入一百以内数,当输入的数小于或等于0  * 
 *	时程序终止。                               * 
 * * * * * * * * * * * * * * * * * * * * * * * */

int main(void) 
{
	bool flag;
	int digit;
	int n;
	
	while (1) {
		//每次执行前将标志数组和标志变量初始化
		bool digit_seen[10] = {false};
		flag = false;				

		printf ("Enter a number: ");
		scanf ("%d", &n);

		//如果输入是0跳出循环,结束
		if (n <= 0) break;
		while (n > 0) {
			digit = n % 10;
			if (digit_seen[digit] == true) flag = true;
			digit_seen[digit] = true;
			n = n / 10;
		}
		if (flag) {
			printf("Repeated digit\n");
		}
		else {
			printf("No repeated digit\n");
		}
	}

	return 0;
}

8_4

#define _CRT_SECURE_NO_WARNINGS

#include 
#include 

#define SIZE  (int)(sizeof (a) / sizeof (a[0])) 
#define N 10

int main (void)
{
	int a[N], i;
	
	printf ("Enter %d numbers: ", N);
	for (i=0; i < SIZE; i++)
		scanf ("%d", &a[i]);
		
	printf ("In reverse order: ");
	for (i = SIZE - 1; i >= 0; i--)
		printf (" %d", a[i]);
	printf ("\n");
	
	printf("\n");
	system("pause");
	return 0;
}

8_5

#include 

#define NUM_RATES ((int)(sizeof (value) / sizeof (value[0])))
#define INITIAL_BALANCE 100.00

int main (void) 
{
	int i, low_rate, num_years, year, month;
	double value[5];
	
	printf ("Enter interst rate: ");
	scanf ("%d", &low_rate);
	printf ("Enter number of years: ");
	scanf ("%d", &num_years);
	
	printf ("\nYears");
	for (i = 0; i < NUM_RATES; i++) {
		printf ("%6d%%", low_rate + i);
		value[i] = INITIAL_BALANCE;
	}
	printf ("\n");
	
	for (year = 1; year <= num_years; year++) {
		printf ("%3d    ");
		for (i = 0; i < NUM_RATES; i++) {
			for (month = 1; month <= 12; month++) {
				value[i] += ((double)(low_rate + i) / 12) / 100.0 * value[i]; 
			}
			printf ("%7.2f", value[i]);
		}
		printf ("\n");
	}
	
	return 0;
}

8_6

#include 
#include 

int main (void) 
{
	char mess[100];
	int i = 0, j;
	
	printf ("Enter message: ");
	while (((mess[i]) = getchar () ) != '\n') {
		i++;
	}
	
	printf ("In B1FF-speak: ");
	for (j = 0; j < i; j++) {
		if ((mess[j] <= 122) && (mess[j] >= 97)){
			mess[j] = toupper (mess[j]);
		}
		
		switch (mess[j]) {
			case 'A': printf ("4"); break;
			case 'B': printf ("8"); break;
			case 'E': printf ("3"); break;
			case 'I': printf ("1"); break;
			case 'O': printf ("0"); break;
			case 'S': printf ("5"); break;
			default : printf ("%c", mess[j]); break;
		}
	}
	printf ("!!!!!!!!!!");
	
	return 0;
	
}

8_7

#include 

#define N 5

int main(void)
{
	int a[N][N] = {0};
	int i, j;
	 
	for (i = 0; i < N; i++) {
		printf("Enter row %d:  ", i + 1);
		for (j = 0; j < N; j++) {
			scanf("%d", &a[i][j]);
		} 
	}
	
	int sum;
	
	printf("Row totals:  ");
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			sum += a[i][j];
		}
		printf("%d ", sum);
		sum = 0;
	}
	
	printf("\nColumn totals:  ");
	for (j = 0; j < N; j++) {
		for (i = 0; i < N; i++) {
			sum += a[i][j];
		}
		printf("%d ", sum);
		sum = 0;
	}
	
	return 0;
 } 

8_8

#include 

#define N 5

int main(void)
{
	int a[N][N];
	int i, j;
	
	for (i = 0; i < N; i++) {
		printf("输入学生%d的五门成绩:", i + 1);
		for (j = 0; j < N; j++) {
			scanf("%d", &a[i][j]);
		}
	}
	printf("\n");
	
	int sum = 0;
	float average = 0.0f;
	
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			sum += a[i][j];
		}
		average = (float) sum / N;
		printf("学生%d的总成绩和平均分分别为%d和%.2f\n", i, sum, average);
		sum = 0;
	}
	printf("\n");
	
	int max, mix;
	sum = 0;
	average = 0.0f;
	
	for (j = 0; j < N; j++) {
		max = a[0][j];
		mix = a[0][j];
		for (i = 0; i < N; i++) {
			if (max < a[i][j]) max = a[i][j];
			if (mix > a[i][j]) mix = a[i][j];
			sum += a[i][j];
		}
		average = (float) sum / N;
		printf("学科%d的平均分是%.2f,最高分是%d,最低分是%d\n", j, average, max, mix);
		sum = 0;
	}
	
	
	return 0;
}

8_9

#include 
#include 
#include 
#include 

#define N 10
#define WAY 4

int main(void)
{
	bool a[N][N] = {false};
	char b[N][N];
	int i, j;
	
	//初始化表 
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			b[i][j] = '.';
		}
	}
	
	srand((unsigned) time(NULL));
	int ways;
	i = 0; j = 0;
	b[0][0] = 'A';
	a[0][0] = true;
	
	for (int m = 0; m < 25; ) {
		 //余0表上, 1表下, 2表左, 3表右 
		ways = rand() % WAY;      
		
		if (ways == 0) {
			//如果往上走出表格了,continue重新循环试试别的方向 
			if (i - 1 < 0) {
				continue;
			} 
			//如果往上走有字母了, 进行判断 
			else if (a[i - 1][j] == true){
				//如果四面都是字母,退出 
				if ((a[i + 1][j] == true && a[i][j - 1] == true && a[i][j + 1] == true)
				// 考虑左边和左下角情况 
					|| j - 1 < 0 && a[i + 1][j] == true && a[i][j + 1] == true
					|| j - 1 < 0 && i + 1 > 9 && a[i][j + 1] == true
				// 右边和右下角
					|| j + 1 > 9 && a[i + 1][j] == true && a[i][j - 1] == true
					|| j - 1 < 0 && i + 1 > 9 && a[i][j + 1] == true
				//下边
					|| i + 1 > 9 && a[i][j + 1] == true && a[i][j - 1] == true) break;
				//如果都不是,重新选方向 
				continue;
			} else {				
				a[i - 1][j] = true;
				b[i - 1][j] = 'B' + m;
				m++;
				i--;
				continue;
			}
		} 
		
		if (ways == 1) {
			if (i + 1 > 9) {
				continue;
			} else if (a[i + 1][j] == true){
				if (a[i - 1][j] == true && a[i][j - 1] == true && a[i][j + 1] == true
				//右边和右上角 
					|| j + 1 > 9 && a[i][j - 1] == true && a[i - 1][j] == true
					|| j + 1 > 9 && i - 1 < 0 && a[i][j - 1] == true
				//左边 
					|| j - 1 < 0 && a[i - 1][j] == true && a[i][j + 1] == true
				//上边 
					|| i - 1 < 0 && a[i][j + 1] == true && a[i][j - 1] == true) break;
				continue;
			} else {				
				a[i + 1][j] = true;
				b[i + 1][j] = 'B' + m;
				m++;
				i++;
				continue;
			}
		} 
		
		if (ways == 2) {
			if (j - 1 < 0) {
				continue;
			} else if (a[i][j - 1] == true){
				if (a[i + 1][j] == true && a[i - 1][j] == true && a[i][j + 1] == true
				// 考虑下边和右下角情况 
					|| i + 1 > 9 && a[i - 1][j] == true && a[i][j + 1] == true
					|| j + 1 > 9 && i + 1 > 9 && a[i - 1][j] == true
				// 上边和右上角
					|| i - 1 < 0 && a[i + 1][j] == true && a[i][j + 1] == true
					|| j + 1 > 9 && i - 1 < 0 && a[i][j + 1] == true
				// 右边
					|| j + 1 > 9 && a[i - 1][j] == true && a[i + 1][j] == true) break;
				continue;
			} else {				
				a[i][j - 1] = true;
				b[i][j - 1] = 'B' + m;
				m++;
				j--;
				continue;
			}
		} 
		
		if (ways == 3) {
			if (j + 1 > 9) {
				continue;
			} else if (a[i][j + 1] == true){
				if (a[i + 1][j] == true && a[i][j - 1] == true && a[i - 1][j] == true
				//左边和左下角 
					|| j - 1 < 0 && a[i - 1][j] == true && a[i + 1][j] == true
					|| j - 1 < 0 && i + 1 > 9 && a[i - 1][j] == true
				//下边 
					|| i + 1 > 9 && a[i - 1][j] == true && a[i][j - 1] == true
				//上边 
					|| i - 1 < 0 && a[i][j - 1] == true && a[i + 1][j] == true) break;
				continue;
			} else {				
				a[i][j + 1] = true;
				b[i][j + 1] = 'B' + m;
				m++;
				j++;
				continue;
			}
		} 
	}
	
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			printf("%c ", b[i][j]);
		}
		printf("\n");
	}
	
	
	return 0;
}

8_10

#include 
#include 

int main(void)
{
	const int lea[8] = {480, 583, 679, 767, 840, 945, 1140, 1305};
	const int arr[8] = {616, 712, 811, 900, 968, 1075, 1280, 1438};
	
	int hours, minutes;
	int time;
	
	printf ("Enter a 24-hour time:");
	scanf ("%d:%d", &hours, &minutes);
	//将时间换算成分钟 
	time = hours * 60 + minutes;
	
	int mix = 1440, m;
	
	//找到距离最近的时间,并记录其下标 
	for (int i = 0; i < 8; i++) {
		if (fabs(lea[i] - time) < mix) {
			mix = fabs(lea[i] - time);
			m = i;
		}
	}
	
	int hour, minute;
	//判断是上午还是下午并输出 
	if (lea[m] / 60 > 12) {
		if (arr[m] / 60 > 12) {
			printf("CLosest departure time is %d:%.2d p.m.", lea[m] / 60 - 12, lea[m] % 60);
			printf(", arriving at %d:%.2d p.m.", arr[m] / 60 -12, arr[m] % 60);
		}
	} else {
		if (arr[m] / 60 > 12) {
			printf("CLosest departure time is %d:%.2d a.m.", lea[m] / 60, lea[m] % 60);
			printf(", arriving at %d:%.2d p.m.", arr[m] / 60 -12, arr[m] % 60);
		} else {
			printf("CLosest departure time is %d:%.2d a.m.", lea[m] / 60, lea[m] % 60);
			printf(", arriving at %d:%.2d p.m.", arr[m] / 60, arr[m] % 60);
		}
	}
	
	return 0;
 } 

8_11

#include 

int main (void)
{
	int ch, i = 0;
	char c[15];
	
	printf ("Enter phone number: ");
	
	//输入字符直到为回车为止 
	while ((ch = getchar ()) != '\n') {
		//输入的如果是字母,换成数字 
		if (ch <= 'Z' && ch >= 'A') {
			switch (ch) {
				case 65: case 66: case 67:
					c[i] = '2';
					break;
				case 68: case 69: case 70:
					c[i] = '3';
					break;
				case 71: case 72: case 73:
					c[i] = '4';
					break;
				case 74: case 75: case 76:
					c[i] = '5';
					break;
				case 77: case 78: case 79: 
					c[i] = '6';
					break;
				case 81: case 82: case 83: case 80:
					c[i] = '7';
					break;
				case 84: case 85: case 86: case 87:
				 	c[i] = '8';
					break;
				case 88: case 89: case 90:
					c[i] = '9';
					break; 
			}
			i++;
			continue;
		}
		
		c[i] = ch;
		i++;
	}
	
	printf("In numeric form : "); 
	for (int j = 0; j < i; j++) {
		printf("%c", c[j]);
	}
	
	return 0;
}

8_12

#include 
//头文件ctpye中包含toupper()函数 
#include 

int main(void)
{
	//建立整数型数组模拟每个字母所代表的面值 
	const int a[26] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
	int sum = 0;
	char ch;
	
	printf("Enter a word: ");
	
	while ((ch = getchar()) != '\n') {
		if (toupper(ch) < 'A' || toupper(ch) > 'Z') {
			printf("Illegal input\n");
			break;
		} else {
			sum += a[toupper(ch) - 'A'];
		}	
	}
	
	printf("Scrabble value: %d", sum);
	
	
	return 0;
}

8_13

#include 

int main(void)
{
	char a[20];
	char ch1, ch2;
	
	printf ("Enter a first and last name: ");
	scanf ("%c", &ch1);
	
	//循环输入直到空格为止 
	while ((getchar()) != ' ')
		;
	
	int i = 0;	
	while ((ch2 = getchar()) != '\n') {
		a[i] = ch2;
		i++;
	}
	
	printf("You enered the name: ");
	for (int j = 0; j < i; j++) {
		printf("%c", a[j]);
	} 
	printf(", %c.", ch1);
	
	return 0;
 } 


8_14

#include 

int main(void)
{
	char sen[100];
	int i;
	
	printf("Enter a sentence: ");
	for (i = 0; i < 100; i++) {                                     //建立字符数字sen并输入句子 
		sen[i] = getchar();
		if (sen[i] == '!' || sen[i] == '.' || sen[i] == '?') break; //遇到句末符号break跳出循环(i++并没有执行),此时i指向末尾的符号 
	}
	
	int pri = i;								//记录下句子的长度,以后用于输出	
	char sen1[100];								//建立一个数组用于存改变顺序后的句子 (一个一个单词地复制) 
	int j = 0;
	int over, start;						    //start为每次开始复制时的数组下标,over为复制结束时的 
	for (i = i - 1; i >= 0; i--, j++) {         //从i-1的位置往前找空格,i-1表示先不处理句末符号 
		over = i;								//下次一个单词的复制就从i(i此时已经i-1)处结束 
		while (sen[i] != ' ' && i > 0) i--;		//遇到空格或者i<=0时,停止寻找 
		start = i + 1;							//开始一个单词的复制,下标应该在空格后一个 
		if (i == 0) start--;					//这个if只针对已经复制到原句的第一个单词的情况,start需要从零开始故减1 
		for (; start <= over; start++, j++) {
			sen1[j] = sen[start];				//复制单词 
		}
		sen1[j] = ' ';                          //每个单词后加个空格 
	}
	
	sen1[j - 1] = sen[pri];						//之后就是sen1数组的输出了! 
	printf("Reversal of sentence: ");
	for (int m = 0; m < pri + 1; m++) {
		printf("%c", sen1[m]);
	}
		
	return 0;
}

8_15

#include 

int main(void)
{	
	//sen为原句子 
	char sen[80], sen1[80];
	int n;
	
	//初始化数组
	int i;
	printf("Enter message to be encrypted: ");
	for (i = 0; ; i++) {               
		sen[i] = getchar();
		if (sen[i] == '\n') break;
	} 
	
	printf("Enter shift amout (1~25): ");
	scanf("%d", &n);
	
	printf("Encrypted message: ");
	for (int j = 0; j < i + 1; j++) {
		if (sen[j] <= 'z' && sen[j] >= 'a') {
			printf("%c", ((sen[j] - 'a') + n) % 26 + 'a');
		} else if (sen[j] <= 'Z' && sen[j] >= 'A') {
			printf("%c", ((sen[j] - 'A') + n) % 26 + 'A');
		} else {
			printf("%c", sen[j]);
		}
	}
	
	return 0;
 } 

8_16

 #include 
 #include  
 #include 
 
 int main(void)
 {
 	//定义一个整数数组用来记录每个字母出现的次数。第一输入单词,
	//对应数组元素加一,第二次输入减一,若最后每个元素都为零则 为变为词 
 	int num[26] = {0};
 	char word[20];
 	
 	int i;
 	
 	printf("Enter first word: ");
 	for (i = 0; i < 20; i++) {
 		word[i]  = getchar();
 		if (!isalpha(word[i]) && word[i] != '\n') {
 			printf("Illegal input!");
			return 0; 
		 }
 		if (word[i] == '\n') break;
 		word[i] = tolower(word[i]);
 		num[word[i] - 'a']++;
	 }
 	
 	printf("Enter second word: ");
 	for (i = 0; i < 20; i++) {
 		word[i]  = getchar();
 		if (!isalpha(word[i]) && word[i] != '\n') {
 			printf("Illegal input!");
			return 0; 
		 }
 		if (word[i] == '\n') break;
 		word[i] = tolower(word[i]);
 		num[word[i] - 'a']--;
	 }
	 
	bool flag = false;
	for (i = 0; i < 26; i++) {
		if (num[i]) flag = true;
	}
 	
 	if (flag) printf("The words are not anagrams.");
 	else printf("The words are anagrams.");
 	
 	return 0;
  } 

8_17

#include 

int main(void)
{
	printf("This is program creates a magic square of a specified size.\n");
	printf("The size must be an odd number between 1 and 99.\n");
	printf("Enter size of magic square: ");
	
	int size;
	scanf("%d", &size);
	
	int square[size][size];
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			square[i][j] = 0;
		}
	}
	int i = 0, j = size / 2, num = 1;
	
	for (; ; ) {
		if (square[i][j] == 0) {
			square[i][j] = num;
			i = i + size - 1; j++;
			i = i % size;
			j = j % size;		
		} else {
			i++; i++; j = j + size - 1;
			i = i % size;
			j = j % size;		  
			square[i][j] = num;
			i = i + size - 1; j++;	
			i = i % size;
			j = j % size;
		}
		num++;
		
		if (num > size * size) break;
	}

	for (i = 0; i < size; i++) {
		for (j = 0; j < size; j++) {
			printf("%3d", square[i][j]);
		}
		printf("\n");
	}
	
	return 0;
 } 

 

你可能感兴趣的:(C语言程序设计现代方法第二版,第八章课后编程习题全部答案)