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

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

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


9_1

#include 

void selection_sort (int n, int a[n]);

int main (void)
{
	int n;
	
	printf ("How many numbers do you want to resevse? ");
	scanf ("%d", &n);
	
	//C99变长数组 
	int a[n];    
	
	printf ("Enter the number you want: ");
	for (int i = 0; i < n; i++) {
		scanf ("%d", &a[i]);
	} 
	
	selection_sort (n, a);
	
	printf ("In sorted order: ");
	for (int i = 0; i < n; i++) {
		printf ("%d ", a[i]);
	}
	
	return 0;
}

void selection_sort (int n, int a[n]) {
	
	int i, j, max = a[0];
	
	for (i = 0; i < n; i++) {
		if (max <= a[i]) {
			max = a[i];
			j = i;
		}
	}
	a[j] = a[n-1];
	a[n - 1] = max;
	
	//函数每执行一次, 就会将数组的长度减一 
	n--;
	if (n == 0) return;
	selection_sort (n, a);
}

9_2

#include 

float tax_ (float n);

int main (void) 
{
	float income;
	
	printf ("Enter the income value: ");
	scanf ("%f", &income);

	printf ("The tax to be paid is %.4f", tax_ (income));
	
	return 0;
}

float tax_ (float n) 
{
	
	float tax;
	
	if (n < 750.0f) {                     
		tax = n * 0.01;
	} else if (n < 2250.0f) {
		tax = 7.5f + (n - 750.0f) * 0.02;
	} else if (n < 3750.0f) {
		tax = 37.5f + (n - 2250.0f) * 0.03;
	} else if (n < 5250.0f) {
		tax = 82.5f + (n - 3750.0f) * 0.04;
	} else if (n < 7000.0f) {
		tax = 142.5f + (n - 5250.0f) * 0.05;
	} else tax = 230.0f + (n - 7000.0f) * 0.06;
	
	return tax;
}

9_3

#include 
#include 
#include 
#include 

#define N 10
#define WAY 4

void generate_random_walk(char walk[][10]);
void print_array(char walk[][10]);

int main(void)
{
	char walk[N][N];
	
	generate_random_walk(walk);
	
	print_array(walk);
	
	return 0;
	
}

void generate_random_walk(char walk[10][10])
{
	bool a[N][N] = {false};
	int i, j;
	
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			walk[i][j] = '.';
		}
	}
	
	srand((unsigned) time(NULL));
	int ways;
	i = 0; j = 0;
	walk[0][0] = 'A';
	a[0][0] = true;
	
	for (int m = 0; m < 25; ) {
		ways = rand() % WAY;        //余0表上, 1表下, 2表左, 3表右 
		
		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;
				walk[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;
				walk[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;
				walk[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;
				walk[i][j + 1] = 'B' + m;
				m++;
				j++;
				continue;
			}
		}  
	}
}

void print_array(char walk[10][10])
{
	int i, j;
	for (i = 0; i < N; i++) {
		for (j = 0; j < N; j++) {
			printf("%c ", walk[i][j]);
		}
		printf("\n");
	}
}
	

9_4

 #include 
 #include  
 #include 
 #include 
 
 void read_word(int counts[]);
 bool equal_array(int counts1[], int counts2[]);
 
 int main(void)
 {
 	int counts1[26] = {0};
 	int counts2[26] = {0};
 	
	read_word(counts1);
	read_word(counts2);

 	if (equal_array(counts1, counts2)) printf("The words are not anagrams.");
	else printf("The words are anagrams.");
 	
 	return 0;
 }
 
 void read_word(int counts[26])
 {
 	printf("Enter the word: ");
 	
 	char word[20];
 	for (int i = 0; i < 20; i++) {
 		word[i]  = getchar();
 		if (!isalpha(word[i]) && word[i] != '\n') {
 			printf("Illegal input!");
			exit(0); 
		 }
 		if (word[i] == '\n') break;
 		word[i] = tolower(word[i]);
 		counts[word[i] - 'a']++;
	 } 
 }
 
 bool equal_array(int counts1[26], int counts2[26])
{
	bool flag = false;
	
	for (int i = 0; i < 26; i++) {
		if (counts1[i] != counts2[i]) flag = true;
	}
	
	return flag;
	
 } 

9_5

#include 

void create_magic_square(int n, int magic_square[n][n]);
void print_magic_square(int n, int magic_square[n][n]); 

int main()
{
	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 n;
	scanf("%d", &n);
	int square[n][n];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			square[i][j] = 0;
		}
	}
	
	create_magic_square(n, square);
	print_magic_square(n, square); 
	
	return 0;
}

void create_magic_square(int n, int magic_square[n][n])
{
	int size = n;
	int (*square)[n];
	square = magic_square;
	
	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;
	}
 } 

void print_magic_square(int n, int magic_square[n][n])
{
	int i, j;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			printf("%3d", magic_square[i][j]);
		}
		printf("\n");
	}
}

9_6

#include 

int calculate(int x);

int main(void)
{
	int x; 
	
	printf("Enter the value of x: ");
	scanf("%d", &x);
	
	printf("The result is %d", calculate(x));
	
	return 0;
}

int calculate(int x)
{
	int result;
	
	result = ((((3 * x) * x - 5) * x - 1) * x + 7) * x - 6;
	
	return result;
}

9_7

#include 

int power(int, int);

int main(void)
{
	int x, n;
	
	printf("输入x和n的值:");
	scanf("%d%d", &x, &n);
	
	
	printf("结果:%d", power(x, n));
	
	return 0;
}

int power(int x, int n)
{
	if (n == 0) {
		return 1;
	} else if (n % 2 == 0 && n != 0) {
		return power(x, n / 2) * power(x, n / 2);	
	} else if (n % 2 != 0) {
		return x * power(x, n - 1);
	}
 } 

9_8

#include 
#include 
#include 
#include 
#include  //sleep()函数头文件 

int rool_dice(void);
bool play_game(void); 

int main(void)
{	
	char a;
	int count1 = 0, count2 = 0;
	
	do {
		if (play_game()) {
			printf("You lose!\n\n");
			count1++;
		} else {
			printf("You win!\n\n");
			count2++;
		}
		
		printf("Play again? ");
		a = getchar();
		getchar();
		printf("\n");
		if (a == 'y') {
			continue;
		} else if(a == 'n') {
			break;
		} else {
			printf("Illegal input!");
			break;
		}
				
	} while(1);
	
	printf("Wins: %d  Losses: %d ", count2, count1);

	return 0;
}

int rool_dice(void)
{
	//两个变量记录两个骰子的数值 
	int count1, count2;
	
	srand((unsigned) time(NULL));
	count1 = rand() % 6 + 1;
	count2 = rand() % 6 + 1;
	
	return count1 + count2;
	
}


bool play_game(void)
{
	int flag = false;
	int point;	//期待的值 
	int r;	//骰子的值 
	
	r = rool_dice();
	printf("You rolled: %d\n", r);
	if (r == 7 || r == 11) {
		flag = true;
		return flag;
	} else if (r == 2 || r == 3 || r == 12) {
		flag = true;
		return flag;	
	} else {
		point = r;
		printf("You point is %d\n", point);
		
		for( ; ;) {
			sleep(2);                           //可能是运行的时间太快了, 产生的随机数都是一样的,百度说延时一下 
			r = rool_dice();
			printf("You rolled: %d\n", r);
			if (r == point) {
				break;
			} else if (r == 7) {
				flag = true;
				break;
			} else {
			}
		}
		
		return flag;
	}

}

 

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