C Primer Plus(第六版)第九章编程题答案参考

9-3的题目读起来好像和9-2是一样的意思?不清楚有什么区别[・_・?]

复习题-6

#include <stdio.h>
void alter(int *x, int *y);
int main(void) {
	int n, m;
	printf("Originally x and y = \n");
	scanf("%d%d", &n, &m);
	alter(&n, &m);
	printf("Now x = %d and y = %d.\n", n, m);
	return 0;
}
void alter(int *x, int *y) {
	int sum = *x + *y;
	int difference = 0;
	if (*x > *y) {
		difference = *x - *y;
	}
	else {
		difference = *y - *x;
	}
	*x = sum;
	*y = difference;
}

复习题-9

#include <stdio.h>
#define UPPER 4
#define LOWER 1
void menu(void);
int limit(int upper_limit, int lower_limit);
int main(void) {
	int choice;
	menu();//显示菜单
	choice = limit(UPPER, LOWER);
	while (choice != 4) {
		switch (choice) {
		case 1:
			printf("1) copy files\n");
			break;
		case 2:
			printf("2) move files\n");
			break;
		case 3:
			printf("3) remove files\n");
			break;
		default:
			break;
		}
		menu();//显示菜单
		choice = limit(UPPER, LOWER);
	}
	printf("Done!\n");
	return 0;
}
void menu(void) {
	printf("Please choose one of the following:\n");
	printf("1) copy files\t2) move files\n");
	printf("3) remove files\t4) quit\n");
	printf("Enter the number of your choice;\n");
	return;
}
int limit(int upper_limit, int lower_limit) {
	int choice;
	scanf("%d", &choice);
	if (choice<lower_limit || choice>upper_limit) {
		printf("Enter an integer from 1 to 4;\n");
		menu();
		limit(UPPER, LOWER);
	}
	else if (choice >= lower_limit && choice <= upper_limit) {
		return choice;
	}
	else {
		return 4;
	}
}

9-1

#include <stdio.h>
double dmin(double x, double y);
int main(void) {
	double x, y;
	double min = 0;
	printf("请输入两个double类型的数字:");
	scanf("%lf%lf", &x, &y);
	min = dmin(x, y);
	printf("较小值为%.3lf", min);
	return 0;
}
double dmin(double x, double y) {
	return x > y ? y : x;
}

9-2

//题意要求i是列,j是行,写反了,但问题不大
#include <stdio.h>
void chline(char ch, int i, int j);
int main(void) {
	char ch;
	int i, j;
	printf("请输入一个字符:");
	scanf("%c", &ch);
	printf("请输入行和列:");
	scanf("%d%d", &i, &j);
	chline(ch, i, j);
	return 0;
}
void chline(char ch, int i, int j) {
	for (int n = 1; n <= i; n++) {
		for (int m = 1; m <= j; m++) {
			putchar(ch);
		}
		putchar('\n');
	}
}

9-4

#include <stdio.h>
double harmonic_mean(double x, double y);
int main(void) {
	double x, y;
	double result = 0;
	printf("请输入两个浮点数:");
	scanf("%lf%lf", &x, &y);
	result = harmonic_mean(x, y);
	printf("调和平均数为:%.3lf", result);
	return 0;
}
double harmonic_mean(double x, double y) {
	double temp = 1.0 / x + 1.0 / y;
	return 2.0 / temp;

}

9-5

#include <stdio.h>
void larger_of(double *x, double *y);
int main(void) {
	double x, y;
	printf("请输入两个数:");
	scanf("%lf%lf", &x, &y);
	larger_of(&x, &y);
	printf("替换为较大的数为:%lf,%lf", x, y);
	return 0;
}
void larger_of(double* x, double* y) {
	*x = *x > *y ? *x : *y;
	*y = *x;
	return;
}

9-6

#include <stdio.h>
void max(double* x, double* y, double* z);
int main(void) {
	double x, y, z;
	printf("请输入三个数:");
	scanf("%lf%lf%lf", &x, &y, &z);
	max(&x, &y, &z);
	printf("最大数为%.3f,中间为%.3lf,最小为%.3lf", x, y, z);
	return 0;
}
void max(double* x, double* y, double* z) {
	double temp = 0;
	if (*x < *y) {
		temp = *x;
		*x = *y;
		*y = temp;
	}
	if (*y < *z) {
		temp = *y;
		*y = *z;
		*z = temp;
	}
	if (*x < *z) {
		temp = *x;
		*x = *z;
		*z = temp;
	}
	return;

}

9-7

#include <stdio.h>
#include <ctype.h>
int letter(char ch);
int main(void) {
	char ch;
	int ad = 0;
	int count = 0;
	printf("请输入字符:");
	while ((ch = getchar())!='\n') {
		count++;
		ad = letter(ch);
		printf("%c-%d\t", ch, ad);
		if (count == 8) {
			printf("\n");
		}
	}
	return 0;
}
int letter(char ch) {
	if (isalpha(ch)) {
		char ch2 = tolower(ch);
		int ad = ch2 - 'a';
		return ad;
	}
	else {
		return -1;
	}
}

9-8

#include <stdio.h>
double power(double n, int p);
int main(void) {
	double x, xpow;
	int exp;
	printf("Enter a number and the integer power to witch\n");
	printf("the number will be raised. Enter q to quit.\n");
	while (scanf("%lf%d", &x, &exp) == 2) {
		xpow = power(x, exp);
		printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
		printf("Enter next pair of numbers or q to quit.\n");
	}
	printf("Hope you enjoyed this power trip —— bye!\n");
	return 0;
}
double power(double n, int p) {
	double pow = 1;
	if (n == 0 && p != 0) {
		return 0;
	}
	if (p < 0) {
		int p2 = -p;
		for (int i = 1; i <= p2; i++) {
			pow *= n;
		}
		pow = 1.0 / pow;
	}
	else if (p == 0) {
		return 1;
	}
	else if (p > 0) {
		for (int j = 1; j <= p; j++) {
			pow *= n;
		}
	}
	return pow;
}

9-9

#include <stdio.h>
double power(double n, int p);
int main(void) {
	double x, xpow;
	int exp;
	printf("Enter a number and the integer power to witch\n");
	printf("the number will be raised. Enter q to quit.\n");
	while (scanf("%lf%d", &x, &exp) == 2) {
		xpow = power(x, exp);
		printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
		printf("Enter next pair of numbers or q to quit.\n");
	}
	printf("Hope you enjoyed this power trip —— bye!\n");
	return 0;
}
double power(double n, int p) {
	double pow = 1;
	if (n == 0 && p != 0) {
		return 0;
	}
	if (p < 0) {
		pow = n * power(n, (-p) - 1);
		pow = 1.0 / pow;
	}
	else if (p > 0) {
		pow = n * power(n, p - 1);
	}
	else if (p == 0) {
		return 1;
	}
	return pow;
}

9-10

#include <stdio.h>
void to_base_n(unsigned long n, int b);
int main(void) {
	unsigned long number;
	int b;
	printf("Enter an integer (q to quit):\n");
	while (scanf("%lu%d", &number,&b) == 2) {
		printf("%d进制的%lu为:", b, number);
		to_base_n(number, b);
		putchar('\n');
		printf("Enter an integer (q to quit):\n");
	}
	printf("Done!\n");
	return 0;
}
void to_base_n(unsigned long n, int b) {
	int r;
	r = n % b;
	if (n >= b) {
		to_base_n(n / b, b);
	}
	printf("%d", r);
	return;
}

9-11

#include <stdio.h>
unsigned long Fibonacci(unsigned long n);
int main(void) {
	unsigned long n;
	printf("Enter an integer (q to quit):");
	while (scanf("%lu", &n) == 1) {
		printf("%lu\n", Fibonacci(n));
		printf("Enter an integer (q to quit):\n");
	}
	printf("Done!\n");
	return 0;
}
unsigned long Fibonacci(unsigned n) {
	long prov = 0;
	long current = 0;
	long next = 1;
	for (int i = 1; i < n; i++) {
		prov = current;
		current = next;
		next = prov + current;
	}
	return next;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值