C Primer Plus 第九章 函数 编程练习答案(自作)

// Page 237, 9.11编程练习

9.11.1. 设计一个函数min(x, y), 返回两个double类型值的较小值。

// 9.11.1

#include <stdio.h>
double min(double x, double y);
int main()
{
	printf("Please enter two real numbers that you want:\n");
	double x, y;
	scanf("%lf %lf", &x, &y);
	printf("min number = %lf\n", min(x, y));
	
	return 0;
}
double min(double x, double y)
{
	double ret;
	if(x > y)
		ret = y;
	else
		ret = x;
	return ret;
}

9.11.2 设计一个函数 chline(ch, i, j),打印指定的字符 j 行 i 列。

// 9.11.2

#include <stdio.h>
void chline(char ch, int a, int b);
int main()
{
	printf("Please enter the character that you want:\n");
	char ch;
	scanf("%c", &ch);
	printf("Then enter the numbers:\n");
	int i, j;
	scanf("%d %d", &j, &i);
	chline(ch, i, j);
	
	return 0;
	
}
void chline(char ch, int a, int b)
{
	char star[b][a];
	int i, j;
	for(i = 0; i < b; i++){
		for(j = 0; j < a; j++){
			star[i][j] = ch;
			printf("%c", star[i][j]);
		}
		putchar('\n');
	}
	return;
}

9.11.3 编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第一个整数指定一行中打印字符的次数,第二个整数指定打印指定字符的行数。(和上题一样,换个问法)

// 9.11.3

#include <stdio.h>
void func(char, int, int);
int main()
{
	char c;
	int i, j;
	printf("Please enter the character that you want:\n");
	scanf("%c", &c);
	printf("Please enter two integers that you want:\n");
	scanf("%d %d", &i, &j);
	
	func(c, i, j);
	
	return 0;
}
void func(char c, int i, int j)
{
	char star[j][i]; 
	
	int a, b;
	for(a = 0; a < j; a++){
		for(b = 0; b < i; b++){
			star[a][b] = c;
			printf("%c", star[a][b]);
		}
		putchar('\n');
	}
	return;
}

9.11.4 两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数。

// 9.11.4

#include <stdio.h>
double harmonic(double, double);
int main()
{
	double a, b;
	printf("Please enter two real numbers that you want:\n");
	scanf("%lf %lf", &a, &b);
	printf("harmonic mean = %lf\n", harmonic(a, b));
	
	return 0;
}
double harmonic(double a, double b)
{
	 a = 1.0 / a;
	 b = 1.0 / b;
	double average = (a + b) / 2.0;
	 average = 1.0 / average;
	
	return average;
}

9.11.5 编写并测试一个函数 large_of( ),该函数把两个double类型变量的值替换为较大的值。例如,larger_of(x, y)会把 x 和 y 中较大的值重新赋给两个变量。(指针)

// 9.11.5

#include <stdio.h>
void larger_of(double *, double *);
int main()
{
	double a, b;
	printf("Please enter 2 real numbers that you want:\n");
	scanf("%lf %lf", &a, &b);
	larger_of(&a, &b);
	printf("%lf %lf", a, b);
	
	return 0;
}
void larger_of(double *i, double *j)
{
	if(*i > *j)
		*j = *i;
	else
		*i = *j;
	return;
}

9.11.6 编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第一个变量,中间值放入第二个变量,最大值放入第三个变量。

// 9.11.6

#include <stdio.h>
void ptr(double *, double *, double *);
int main()
{
	double a = 1.1;
	double b = 2.2;
	double c = 3.3;
	
	ptr(&a, &b, &c);
	printf("a= %lf b=%lf c=%lf\n", a, b, c);
	
	return 0;
}
void ptr(double *i, double *j, double *k)
{
	*i = 4.4;
	*j = 5.5;
	*k = 6.6;
	
	return;
}

9.11.7 编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和c在字母表中的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回 -1 (输入字符,输出字母在字母表中的位置)

需要用到 ctype.h 文件头 调用 isalpha() 函数

// 9.11.7

#include <stdio.h>
#include <ctype.h>
int table(char);
int main()
{
	char num;
	printf("Please enter the character that you want:\n");
	scanf("%c", &num);
	printf("character's location = %d", table(num));
	
	return 0;
}
int table(char num)
{
	int ret;
	if(isalpha(num) == 2){     // 小写字母判断为2
		ret = num - 'a' + 1;
	}else if(isalpha(num) == 1){ //  大写字母判断为1
		ret = num - 'A' + 1;
	}else ret = -1;
	
	return ret;
}

9.11.8 第六章的程序清单 6.20 中,power()函数返回一个double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。

// 9.11.8

#include <stdio.h>
double power(double, int);
int main()
{
	double x;
	int pow;
	printf("Please enter a real number and an integer that you want:\n");
	scanf("%lf %d", &x, &pow);
	printf("%lf to the power %d is %lf\n", x, pow, 	power(x, pow));
	
	return 0;
}
double power(double x, int pow)
{
	double number = 1.0;
	double ret;
	int i;
	if(pow > 0){
		for(i=0; i<pow; i++){
			number *= x;
			ret = number;
		}
	}else if(pow < 0){
			for(i=0; i<(-pow); i++){
			number *= x;
			ret = 1.0 / number;
		}
	}else{
		printf("Power equals to 0, it's not identified.\n");
		ret = 1;
	}
	
	return ret;
}

9.11.9 使用递归函数重写编程练习8

// 9.11.9

#include <stdio.h>
double power(double, int);
int main()
{
	double x;
	int pow;
	printf("Please enter a real number and an integer that you want:\n");
	scanf("%lf %d", &x, &pow);
	
	if(pow >= 0)
	printf("%lf to power %d is %lf\n", x, pow, power(x, pow));
	else
	printf("%lf to power %d is %lf\n", x, pow, (1.0 / power(x, pow)));
	
	return 0;
}
double power(double x, int pow)
{
	double ret;
	double num;
	if(pow != 0){
		if(pow > 0)
		ret = x * power(x, pow-1);	
		if(pow < 0)
		ret = x * power(x, pow+1);
	}else
		ret = 1;
	
	return ret;
}

9.11.10 为了让程序清单 9.8 中的 to_binary( ) 函数更通用,编写一个to_base_n ( ) 函数接受两个参数,且第二个参数在 2~10 范围内,然后以第 2 个参数中指定的进制打印第 1 个参数的数值。例如,to_base_n (129, 8) 显示的结果为201,也就是129的八进制数。在一个完整的程序中测试该函数。

// 9.11.10

#include <stdio.h>
void to_base_n(int, int);
int main()
{
	int num, base;
	printf("Please enter the number and the base that you want:\n");
	scanf("%d %d", &num, &base);
	printf("The number %d based on %d is:\n", num, base);
	putchar('\n');
	to_base_n(num, base);
	putchar('\n');
	
	return 0;
}
void to_base_n(int num, int base)
{
	int ret;
	ret = num % base;
	if(num >= base)
		to_base_n(num / base, base);
	printf("%d", ret);
	
	return;
}

9.11.11 编写并测试Fibonacci( )函数,该函数用循环代替递归计算斐波那契数。

// 9.11.11

#include <stdio.h>
void fibonacci(int);
int main()
{
	int n;
	printf("Please enter the number that you want:\n");
	scanf("%d", &n);
	fibonacci(n);
	
}
void fibonacci(int n)
{
	int fib[n] = {1, 1, };
	int i;
	for(i=0; i<n; i++){
		if(i < 2)
			printf("1 ");
		if(i >= 2){
			fib[i] = fib[i-1] + fib[i-2];
			printf("%d ", fib[i]);
	 }
	}
	return;
}

猜你喜欢

转载自blog.csdn.net/fengqy1996/article/details/123311086