C语言程序设计现代方法(第二版)Chapter 9

本文涵盖了多种编程任务的实现,包括数组排序的快速选择排序,税率计算的函数设计,随机步法的实现,判断变位词的算法,创建幻方的逻辑以及多项式计算和简化幂函数的方法。此外,还介绍了一个基于掷骰子的简单游戏,展示了如何使用随机数进行游戏逻辑的模拟。
摘要由CSDN通过智能技术生成

1. 数组排序

#include <stdio.h>

int 
selection_sort (int n, int book[n]) 
{  //n 记录数组长度,由用户输入 
	
	int i, j; 
	int max = book[0];
	
	for (i = 0; i < n; i ++) 
	{
		if (max <= book[i]) 
		{
			max = book[i];
			j = i;
		}
	}
	book[j] = book[n-1];
	book[n - 1] = max;
	n --;
	
	if (n == 0) 
	{
		return;
	}
	
	selection_sort (n, book); //再次调用,递归 
}

2. 编写 计算税率函数

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;
}

3. 随机步法函数

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
 
#define N 10
#define DIRECTIONS 4
#define ALPHABET 26

void 
generagte_random_walk(char walk[N][N])
{
	bool m[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 direction;
	int alpha;
	
	m[0][0] = true;
	walk[0][0] = 'A';
	
	i = 0;
	j = 0;
	 
	for (alpha = 0; alpha < ALPHABET - 1; ) 
	{
		 //余0表上, 1表下, 2表左, 3表右 
		direction = rand() % DIRECTIONS;      
		
		switch(direction) 
		{
			case 0:
				if (i - 1 < 0) //向上出界 
				{
	    			continue;
	    		} 
		    	else if (m[i - 1][j] == true) //向上已有字母 
				{
		    		if ((m[i + 1][j] == true&&m[i][j - 1] == true&&m[i][j + 1] == true) //四周字母
		      			|| j - 1 < 0&&m[i + 1][j] == true&&m[i][j + 1] == true //向后出界,且向下,向前有字母 
		      			|| j - 1 < 0&&i + 1 > 9&&m[i][j + 1] == true //向后,向下出界,且向前有字母 
		    			|| j + 1 > 9&&m[i + 1][j] == true&&m[i][j - 1] == true //向前出界,且向下,向后以后字母 
	    				|| j + 1 > 9&&i + 1 > 9&&m[i][j - 1] == true) //向前,向下出界,且向后有字母 
	    				{
		    				continue;
	    				}

		     	} 
				else 
	  			{				
	    			m[i - 1][j] = true; //向上占位 
     				walk[i - 1][j] = 'B' + alpha;
     				
					alpha ++;
	    			i --;
	      		
				  	continue;
		       	} 
				break;
		       	
		   	case 1:
		       	if (i + 1 > 9) //向下出界 
		       	{
		      		continue;
					}
				else if (m[i + 1][j] == true)  //向下已有字母 
				{
					if ((m[i - 1][j] == true&&m[i][j - 1] == true&&m[i][j + 1] == true) //四周字母 
				    	|| j - 1 < 0&&m[i - 1][j] == true&&m[i][j + 1] == true //向后出界,且向前,向上有字母 
				   		|| j - 1 < 0&&i - 1 < 0&&m[i][j + 1] == true //向后,向上出界,且向前有字母 
			    		|| j + 1 > 9&&i - 1 < 0&&m[i][j - 1] == true //向前,向上出界,且向后有字母
			   			|| j + 1 > 9&&m[i -1][j] == true&&m[i][j - 1] == true) //向前出界,且向后,向上有字母
		 	 		{
						continue; 
					} 
				}
				else
				{
					m[i + 1][j] = true; //向下占位
					walk[i + 1][j] = 'B' + alpha; 
					
					alpha ++;
					i ++;
						
					continue;
				}
				break;
				
			case 2:
				if (j - 1 < 0) //向后出界
				{
					continue; 
				}
				else if (m[i][j - 1] == true) //向后已有字母 
				{
					if ((m[i - 1][j] == true&&m[i + 1][j] == true&&m[i][j + 1] == true) //四周有字母
					    || i - 1 < 0&&m[i][j + 1] == true&&m[i + 1][j] == true //向上出界,且向前,向下有字母
						|| i - 1 < 0&&j + 1 > 9&&m[i + 1][j] == true //向上,向前出界,且向下有字母
						|| i + 1 > 9&&j + 1 > 9&&m[i - 1][j] == true //向下,向前出界,且向上有字母
						|| i + 1 > 9&&m[i - 1][j] == true&&m[i][j + 1] == true) //向下出界,且向上,向前有字母
						{
							continue;
						} 
	    		}
				else
				{
					m[i][j - 1] = true;
					walk[i][j - 1] = 'B' + alpha;
					
					alpha ++;
					j --;
				} 
				break;
			
			case 3:
				if (j + 1 > 9) //向前出界 
				{
					continue; 
				}
				else if (m[i][j + 1] == true) //向前已有字母
				{
					if ((m[i - 1][j] == true&&m[i + 1][j] == true&&m[i][j - 1] == true) //四周有字母 
			     		|| i - 1 < 0&&m[i][j - 1] == true&&m[i + 1][j] == true //向上出界,且向后,向下有字母
						|| i - 1 < 0&&j - 1 < 0&&m[i + 1][j] == true //向上,向后出界,且向下有字母
						|| i + 1 > 9&&j - 1 < 0&&m[i - 1][j] == true //向下,向后出界,且向上有字母
						|| i + 1 > 9&&m[i - 1][j] == true&&m[i][j - 1] == true) //向下出界,且向后,向上有字母 
					{
						continue;
					}
				}
				else 
				{
					m[i][j + 1] = true;
					walk[i][j + 1] = 'B' + alpha;
					
					alpha ++;
					j ++;
				}
				break;
		}
	}
}

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

int main(void)
{
	char walk[N][N];
	
	generagte_random_walk(walk);
	printf_array(walk);
	
	return 0;
}

4. 变位词

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>

void 
read_word(int consts[26])
{
	char alpha[20];  //存入字符 
	
	int i;
	int n;
	for (i = 0; i < 20; i ++) //循环记录 
	{
		alpha[i] = getchar(); 
		
		if ((alpha[i] < 'A'||alpha[i] > 'z')&&alpha[i] != '\n')
		{
			printf("Errow!");
		}
		
		if (alpha[i] == '\n')
		{
			break;
		}
		n = toupper(alpha[i]) - 'A';
		consts[n] ++; //记录字母次数 
	}
}

bool
print_array(int consts1[26], int consts2[26])
{
	int i;
	int judge = false;

	for (i = 0; i < 26; i ++)
	{
		if (consts1[i] != consts2[i])
		{
     		judge = true;
		}
	}
	
	return judge;
}

int main(void)
{
	int book1[26] = {0};
	int book2[26] = {0};
	
	read_word(book1);
	
	read_word(book2);

	if (print_array(book1, book2) == true)
	{
		printf("The words are not anagrams");
	}
	else
	{
		printf("The words are anagrams");
	}
	
	return 0; 
}

5. 幻方

#include <stdio.h>

void
create_magic_square(int n, int magic_square[n][n])
{
	int length;
	length = n;
	
	int i, j;
	for (i = 0; i < length; i ++)
	{
		for (j = 0; j < length; j ++)
		{
			magic_square[i][j] = 0;
		}
	}
	
	i = 0; j = length / 2;
	int sum = 1;
	
	for (; ;)
	{
		if (magic_square[i][j] == 0)
		{
			magic_square[i][j] = sum;
		
			if (i == 0&&j == length - 1)
	    	{
    			i = 1;
    			sum ++;
    			magic_square[i][j] = sum;
    			
	    	}
		}
		else
		{
			i = i + 2;
       		j = j - 1;
	    	magic_square[i][j] = sum;
		}
		sum ++;
		
		i --; j ++;
		
		if (i < 0)
		{
			i = length - 1;
		}
		if (j > length - 1)
		{
			j = 0;
		}
		
		if (sum > length * length)
		{
			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");
	}
}

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

6. 多项式计算

#include <stdio.h>

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

int main(void)
{
	int x;
	
	scanf("%d", &x);
	
	printf("%d", x_function(x));
	
	return 0;
}

7.  简化power 函数

#include <stdio.h>

int
power(int x, int n)
{
	if (n == 0)
	{
		return 1;
	}
	else if (n % 2 == 1)
	{
		return x * power(x, n - 1);
	}
	else 
	{
		int i;
		for (i = 0; i < n / 2; i ++)
		{
			x = x * x;
		}
		
		return x;
	}
}

int main(void)
{
	int x, n;
	
	scanf("%d %d", &x, &n);
	
	printf("%d", power(x, n));
	
	return 0;
}

8. 骰子游戏

#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <ctype.h>

int
roll_dice(void)
{
	int rand1, rand2;
	
	srand((unsigned)time(NULL));
	
	rand1 = rand() % 6 + 1;
	rand2 = rand() % 6 + 1;
	
	return rand1 + rand2;
}

bool
play_game(void)
{
	int goal = 0; 
	int result = 0;
	
	result = roll_dice();
	
	if (result == 7||result == 11)
	{
		return true;
	}
	else if (result == 2||result == 3||result == 12)
	{
		return false;
	}
	else
	{
		goal = result;
		return goal;
	}
}

int main(void)
{
			
	int win = 0;
	int lose = 0;
	int reminder = 0;
	char sign;
	
	while(1)
	{
		printf("You rolled %d \n", roll_dice());
		
		play_game();

		printf("%d", play_game());
		
		if (play_game() == true)
		{
			win ++;
			
			printf("You win ! \n");
			
			printf("Play again?");
			scanf("%c", &sign);
			if (toupper(sign) == 'Y')
			{
				continue;
			}
			else
			{
				break;
			}
		}
		else if (play_game() == false)
		{
			lose ++;
	    	
			printf("You lose ! \n");
			
			printf("Play again?");
			scanf("%c", &sign);
			if (toupper(sign) == 'Y')
			{
				continue;
			}
			else
			{
				break;
			}
		}
		else
		{
			if (reminder == play_game())
			{
				win ++;
				
				printf("Play again?");
	     		scanf("%c", &sign);
		    	if (toupper(sign) == 'Y')
		    	{
		    		continue;
	    		}
	     		else
	    		{
		    		break;
		    	}
			}
			reminder = play_game();	
		}	
	}
	printf("Wins: %d \t lose: %d", win, lose);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神秘的企鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值