C语言程序设计现代方法_第二版,CH9第九章编程题(Programming_Projects)

CH9_1

#include <stdio.h>

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

int main(void)
{
    int a[100] = { 0 };
    int i, count = 0;

    printf("Enter a series integers: ");
    scanf("%d", &a[0]);
    count++;
    for (i = 1; i < 100 && (getchar() != '\n'); i++) {
        scanf("%d", &a[i]);
        count++;
    }

    printf("Unsorted array: ");
    for (i = 0; i < count; i++)
        printf("%d ", a[i]);

    selection_sort(a, count);

    printf("\nSorted array: ");
    for (i = 0; i < count; i++)
        printf("%d ", a[i]);

    return 0;
}

void selection_sort(int a[], int n)
{
    if (n <= 1) {
        return;
    }

    int t;
    
    for (int i = n - 2; i >= 0; i--) {
        if (a[n - 1] < a[i]) {
            t = a[n - 1];
            a[n - 1] = a[i];
            a[i] = t;
        }
    }       
    selection_sort(a, n - 1);
}

//下面为非递归函数
//void selection_sort(int a[], int n)
//{
//    int t;
//    for (int j = n - 1; j > 0; j--) {
//        for (int i = j-1; i >= 0; i--) {
//            if (a[j] < a[i]) {
//                t = a[j];
//                a[j] = a[i];
//                a[i] = t;
//            }
//        }
//    }
//    
//}

CH9_2

#include <stdio.h>

double tax(int income);

int main(void)
{
    int income;

    printf("Enter the amount of income: ");
    scanf("%d", &income);
    printf("Tax due: $%.2f", tax(income));

    return 0;
}

double tax(int income)
{
    int tax_due;

    if (income <= 750)
        tax_due = income * 0.01;
    else if (income <= 2250)
        tax_due = 7.50 + ((income - 750) * 0.02);
    else if (income <= 3750)
        tax_due = 37.50 + ((income - 2250) * 0.03);
    else if (income <= 5250)
        tax_due = 82.50 + ((income - 3750) * 0.04);
    else if (income <= 7000)
        tax_due = 142.50 + ((income - 5250) * 0.05);
    else
        tax_due = 230.00 + ((income - 7000) * 0.06);

    return tax_due;
}

CH9_3

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

#define N 10	//方形矩阵边长

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

int main(void)
{
	char a[N][N];
	
	generate_random_walk(a);

	print_array(a);

	return 0;
}

void generate_random_walk(char walk[10][10])
{
	bool key[N][N] = { true };
	int i, j, it, jt;
	int way;
	int step = 0;
	int temp;
	bool full = false;

	for (i = 0; i <= N - 1; i++) {	//初始化方阵
		for (j = 0; j <= N - 1; j++) {
			walk[i][j] = 46;
		}
	}
	i = j = 0;
	temp = walk[i][j] = 'A';

	srand((unsigned)time(NULL));

	while (1) {
		step++;
		it = i;	//i,j缓存变量;试错之后方便回到原位
		jt = j;

		do {
			if (key[it][jt + 1] == 1 && key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && key[it - 1][jt] == 1 ||
				key[it][jt + 1] == 1 && key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && it == 0 ||
				key[it][jt + 1] == 1 && key[it][jt - 1] == 1 && it == N - 1 && key[it - 1][jt] == 1 ||
				key[it][jt + 1] == 1 && jt == 0 && key[it + 1][jt] == 1 && key[it - 1][jt] == 1 ||
				jt == N - 1 && key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && key[it - 1][jt] == 1 ||
				key[it][jt + 1] == 1 && key[it + 1][jt] == 1 && it == 0 && jt == 0 ||
				key[it][jt - 1] == 1 && key[it - 1][jt] == 1 && it == N - 1 && jt == N - 1 ||
				key[it][jt + 1] == 1 && key[it - 1][jt] == 1 && it == N - 1 && jt == 0 ||
				key[it][jt - 1] == 1 && key[it + 1][jt] == 1 && it == 0 && jt == N - 1)
			{	//if判断此时是否处于被包围状态
				full = true;
				break;
			}
			i = it;
			j = jt;
			way = rand() % 4;
			switch (way) {
			case 0: ++i; break;
			case 1: ++j; break;
			case 2: --i; break;
			case 3: --j; break;
			default: printf("ERROR"); return; break;	//纠错语句,可删去
			}
		} while ((i < 0) || (i > N - 1) || (j < 0) || (j > N - 1) || (key[i][j] == true));

		if (step >= 26 || full == true) {	//满足退出条件时,打印已经走完的step		
			printf("%d step\n\n", step);	//显示共走了几步
			return;
		}

		key[i][j] = true;
		temp = walk[i][j] = temp + 1;
	}
}

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

	return;	
}

CH4_4

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

void read_word(int counts[26]);
bool equal_array(int counts1[26], int counts2[26]);

int main(void)
{
	int ch1[26] = { 0 };
	int ch2[26] = { 0 };
	
	printf("Enter first word: ");
	read_word(ch1);
	printf("Enter Second word: ");
	read_word(ch2);

	if (equal_array(ch1, ch2)) {
		printf("The words are anagrams.\n");
	}
	else {
		printf("The words are not anagrams.\n");
	}

	return 0;
}

void read_word(int counts[26])
{
	char CH;

	while ((CH = getchar()) != '\n') {
		if (isalpha(CH)) {
			CH = toupper(CH);
			counts[CH - 'A']++;
		}
	}

	return;
}

bool equal_array(int counts1[26], int counts2[26])
{
	int key = 0;

	for (int i = 0; i < 26; i++) {
		if (counts1[i] - counts2[i] == 0) {
			key++;
		}
	}

	return key == 26;
}

CH9_5

#include <stdio.h>
#define N 5	//VS2019不支持变长数组,利用宏给出大小

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

int main(void)
{
	int a[N][N] = { 0 };

	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: %d\n", N);

	create_magic_square(N, a);
	print_magic_square(N, a);

	return 0;
}

void create_magic_square(int n, int magic_square[][N])
{
	int n_, m_;
	int n_next, m_next;

	m_ = 0;
	n_ = (n - 1) / 2;

	for (int i = 1; i <= n * n; i++) {
		magic_square[m_][n_] = i;

		if (m_ == 0) {
			m_next = n - 1;
		}
		else {
			m_next = m_ - 1;
		}

		if (n_ == n - 1) {
			n_next = 0;
		}
		else {
			n_next = n_ + 1;
		}

		if (magic_square[m_next][n_next] != 0) {
			m_++;
		}
		else {
			m_ = m_next;
			n_ = n_next;
		}
	}

	return;
}

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

	return;
}

CH9_6

#include <stdio.h>

int function(int x);

int main(void)
{
    int x;

    printf("Enter x: ");
    scanf("%d", &x);
    printf("%d", function(x));

    return 0;
}

int function(int x)
{
    return 3 * (x * x * x * x * x) + 2 * (x * x * x * x) - 5 * (x * x * x) - x * x + 7 * x - 6;
}

CH9_7

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

int power(int x, int n);

int main(void)
{
    int x;
    int n;

    printf("Enter x and n: ");
    scanf("%d %d", &x, &n);
    printf("%d\n", power(x, n));

    return 0;
}

int power(int x, int n)
{
    if (x == 0 && n == 0) {
        printf("ERROR\n");
        exit (0);
    }
    else if (x == 0) {
        return 0;
    }
    else if (n == 0) {
        return 1;
    }
    else if (n % 2 == 0) {
        int t = power(x, n / 2);
    //引入中间变量t可大大加快运算速度,原因?
        return t * t;
    }
    else {
        int t = power(x, n - 1);
        return x * t;
    }
}

CH9_8

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

int roll_dice(void);
bool play_game(void);

int main(void)
{
    int wins = 0;
    int losses = 0;
    int ch;

    srand((unsigned)time(NULL));
    //注意此处的srand函数与rand函数要进行一定时间的分割,表现为中间间隔多段语句
    //目的是令生成的随机数在短时间内不要重复,否则程序会迅速终止,难以体现随机掷骰子
    //此外,还可以利用sleep函数在67行进行延时得到同样效果

    do {
        if (play_game()) {
            printf("You win!\n\n");
            wins++;
        }
        else {
            printf("You lose!\n\n");
            losses++;
        }
        printf("Play again?");
        ch = getchar();
        getchar();  //扫走回车键,防止影响循环判断
        printf("\n");

    } while (toupper(ch) == 'Y');

    printf("Wins: %d\tLosses: %d",wins, losses);

    return 0;
}

int roll_dice(void)
{
    int m, n;
    m = rand() % 6 + 1;
    n = rand() % 6 + 1;
    return m + n;
}

bool play_game(void)
{
    int sum;
    int point;
    
    printf("You rolled: %d\n", point = roll_dice());
    if (point == 7 || point == 11) {
        return true;
    }
    else if (point == 2 || point == 3 || point == 12) {
        return false;
    }
    else {
        printf("You point is: %d\n", point);
    }

    while (1) {
        printf("You rolled: %d\n", sum = roll_dice());
        if (sum == point) {
            return true;
        }
        else if (sum == 7) {
            return false;
        }
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值