C Primer Plus 第六版编程练习第九章答案和记录一些学习历程

1、设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单的驱动程序中测试该函数。

这道题不难。
首先:如何返回两个数之间的最小值呢,我们想到前两章学的条件运算符(唯一的一个三元运算符)。
然后:设计一个驱动程序。我们想设计程序提示输入不断循环,那么外层就要有一个循环语句,进入条件是成功输入即可。跳出循环就使用q字符。

/* Programming Exercise 9-1 */ 

#include <stdio.h>
 
double min (double x, double y);//声明函数
 
int main (void)
{
    double x, y;
 
    printf ("Enter two numbers of double(q to quit): ");
    while (scanf ("%lf %lf", &x, &y) == 2)
    {
    	 printf("The smaller number is %f.\n", min(x,y));
    	 printf("Next two values (q to quit): ");
    }
    printf("bye!");
 
    return 0;
}
 
double min (double a, double b)//判断较小值 
{
    return a<b ? a : b;
} 

运行结果:
在这里插入图片描述

2、 设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。

首先:照常设计一下打印字符的函数,循环打印行列,那么我们就用for嵌套。
然后:驱动程序如题1一般设计即可。(这里有个注意的点就是,这里是混合数值和字符输入,要注意getchar()会读取字符,包括空白;空格,制表符和换行符。而scanf()不会)

/* Programming Exercise 9-2 */ 

#include <stdio.h> 
void chLine(char ch, int i, int j);//声明函数 
int main(void)     
{     
	char ch;     
	int col, row;  
	        
	printf("Enter a character (# to quit): ");     
	while ( (ch = getchar()) != '#')     
	{         
		if (ch == '\n')//清空缓冲区中的换行符             
		continue;         
		printf("Enter number of columns and number of rows: ");         
		if (scanf("%d %d", &col, &row) != 2)             
			break;         
		chLine(ch, col, row);         
		printf("\nEnter next character (# to quit): ");     
	}     
	printf("Bye!\n");            
	
	return 0;
}
void chLine(char ch, int i, int j)//定义函数 
{     
	int col, row; 
    for (row = 0; row < j ; row++)//设置行     
	{        
		for (col = 0; col < i; col++)//设置列             
			putchar(ch);         
		putchar('\n');     
	}     
	return; 
}

运行结果:
在这里插入图片描述

3、编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。

和第二题一样,就不做了。

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

和第一题差不多。

/* Programming Exercise 9-4 */ 

#include <stdio.h>
 
double concoction (double a, double b);//定义函数 
 
int main (void)
{
    double x, y;
 
    printf ("Enter two numbers of double(q to quit): ");
    while (scanf ("%lf %lf", &x, &y) == 2)
    {
    	 printf("The concoction number is %f.\n", concoction(x,y));
    	 printf("Next two values (q to quit): ");
    }
    printf("bye!");
 
    return 0;
}
 
double concoction (double a, double b)//求两数的调和平均数 
{
    return 1/((1/a +1/b)/2);
} 

运行结果:

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

首先:要用函数修改主调函数中变量的数值,对数值进行处理的话,被调函数只能返回一个值。
其次: 第一种方法可以用函数计算两个变量数值最大的,然后返回给主调函数,令x=y=a。但是如果要返回俩个数值的话就无法做到了。
第二种方法我们可以使用指针。直接修改x y所在地址中存储的值。这种通用性更好。

/* Programming Exercise 9-5 */ 

#include <stdio.h>
 
void larger_of(double *p1, double *p2); 

int main(void)     
{     
	double x, y; 
	         
	printf("Enter two numbers (q to quit): ");     
	while (scanf("%lf %lf", &x, &y) == 2)     
	{         
		larger_of(&x, &y);         
		printf("The modified values are %f and %f.\n", x, y);         
		printf("Next two values (q to quit): ");    
	}     
	printf("Bye!\n");
	            
	return 0; 
}
 
void larger_of(double *p1, double *p2) //判断两个数哪个大 
{     
	*p1= *p2 = (*p1 > *p2 ? *p1 : *p2); 
}
 

运行结果:
在这里插入图片描述

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

和上一题差不多。
核心算法:就是对三个数进行排序,最多需要比较三次。第一次比较,把较小的那个数放到中间的笼子里,较大的在和第三个数比较,情况1:如果也比第三个小,顺序确定。情况二:如果比第三个数大,把这个数放到第3个笼子里,再比较一次其他两个数,即可确定顺序。

/* Programming Exercise 9-6 */ 

#include <stdio.h>
 
void larger_of(double *p1, double *p2, double *p3); //声明函数 

int main(void)     
{     
	double x, y, z; 
	         
	printf("Enter three numbers (q to quit): ");     
	while (scanf("%lf %lf %lf", &x, &y, &z) == 3)     
	{         
		larger_of(&x, &y, &z);         
		printf("The minimal number: %f  meduim number:  %f and maximal number:%f.\n", x, y, z);         
		printf("Next three values (q to quit): ");    
	}     
	printf("Bye!\n");
	            
	return 0; 
}
 
void larger_of(double *p1, double *p2, double *p3) //给三个数排序 
{     
	double t;
	if (*p1 > *p2)
    {
        t = *p1;
        *p1 = *p2;
        *p2 = t;
    }
    if (*p1 > *p3)
    {
        t = *p1;
        *p1 = *p3;
        *p3 = t;
    }
    if (*p2 > *p3)
    {
        t = *p2;
        *p2 = *p3;
        *p3 = t;
    }
	

运行结果:
在这里插入图片描述

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

涉及文件的暂时空置。。

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

核心算法:
首先: 令一个变量为底数一个变量为幂。if 幂为0时 ( if 底数为0 报告未定义, else 该底数次幂为0)
其次: else if 幂大于0,循环累乘即可 。
最后: else 幂小于0, 取反循环累乘取倒数即可。

/* Programming Exercise 9-8 */ 
#include <stdio.h> 

double power(double a, int b);  //声明函数
 
int main(void) 
{   
	double x, xpow;   
	int n; 
	
   	printf("Enter a number and the integer power to which\nthe number will be raised.(Enter q to quit.)\n");   
	while (scanf("%lf%d", &x, &n) == 2)   
	{        
		xpow = power(x,n);               
		printf("%.3g to the power %d is %.5g\n", x, n, 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 a, int b) //函数定义 
{   
	double pow = 1;   
	int i; 
	     
	if (b == 0)//如果次幂为0  
	{       
		if (a == 0)//0的0次幂           
		printf("0 to the 0 undefined; using 1 as the value\n");       
		pow = 1.0;   
	}   
	else if (a == 0)//除0以外的0次幂       
		pow = 0.0;
	else if (b > 0) //正数幂          
		for(i = 1; i <= b; i++)        
		pow *= a;   
	else //负数幂  
		for(i = 1; i <= -b; i++)        
		pow *= a;    
		pow = 1.0 / pow; 
	  
	return pow;                  //返回幂数值 
}

运行结果:
在这里插入图片描述

9、使用递归函数重写编程练习8。

同上:递归用于:幂小于0取反后在带入。

/* Programming Exercise 9-9 */ 
#include <stdio.h> 
double power(double a, int b);  //声明函数
int main(void) 
{   
	double x, xpow;   
	int n; 
   	printf("Enter a number and the integer power");   
	printf(" to which\nthe number will be raised. Enter q");   
	printf(" to quit.\n");   
	while (scanf("%lf%d", &x, &n) == 2)   
	{        
		xpow = power(x,n);       /* function call           */        
		printf("%.3g to the power %d is %.5g\n", x, n, 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 a, int b) //函数定义 
{   
	double pow = 1;   
	int i; 
	     
	if (b == 0)//如果次幂为0  
	{       
		if (a == 0)//0的0次幂           
		printf("0 to the 0 undefined; using 1 as the value\n");       
		pow = 1.0;   
	}   
	else if (a == 0)//除0以外的0次幂       
	pow = 0.0;   
	else if (b > 0)       
	for(i = 1; i <= b; i++)        
	pow *= a;   
	else   //负数幂      
	pow = 1.0 / power(a, - b); //使用递归 
	  
	return pow;                  //返回幂数值 
}

运行结果:
在这里插入图片描述

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

**核心算法:**转换为几进制,先对这个数取余,得到它的“个位”。然后再相除得到它的“十位”。 再把“十位”又取余得到真正的十位。溢出的十位再相除得到“百位”。如此迭代即可。

/* Programming Exercise 9-10 */ 
#include <stdio.h> 
void to_base_n(int x, int base);//声明转换进制函数 

int main(void) 
{   
	int number;   
	int b;   
	int count;      
	
	printf("Enter an integer (q to quit):\n");   
	while (scanf("%d", &number) == 1)   
	{      
		printf("Enter number base (2-10): ");      
		while ((count = scanf("%d", &b))== 1 &&  (b < 2 || b > 10))//误输入2-10以外的数      
		{          
			printf("base should be in the range 2-10: ");      
		}      
		if (count != 1) //未成功读取         
			break;      
		printf("Base %d equivalent: ", b);      
		to_base_n(number, b);      
		putchar('\n');      
		printf("Enter an integer (q to quit):\n");   
	}   
	printf("Done.\n");
	   
	return 0; 
} 
void to_base_n(int x, int base)//定义转换进制函数  
{   
 	int r;
	  
   	r = x % base;   
	if (x >= base)     
		to_base_n(x / base, base); 
	putchar('0' + r);//把r转换为数字字符。putchar(48 + r)也可以。因为'0'的ascii码是48。   
	
}

运行结果:
在这里插入图片描述

11、编写并测试一个函数FIbonacci(),在该函数中使用循环代替递归完成斐波纳契数列的计算。

核心算法:
首先:考虑特殊情况:第一位和第二位。
然后:从第三位开始,令他为t = a + b;等于前两位相加。
最后:令第一位保存第二位的值,丢弃第一位本身的值。第二位保留第三位t的值。循环即可。

/* Programming Exercise 9-11 */ 
#include <stdio.h>
 
void fibonacci(int x);//声明斐波纳契函数 

int main(void) 
{   
	int number;    
	    
	printf("Enter an integer (q to quit):\n");   
	while (scanf("%d", &number) == 1 )   
	{      
		if (number <= 0) 
			printf("Enter an integer which greater than 0:\n");
		else if (number == 1)
			printf("the 1 fibonacci number is 1.\n");
		else 
		{
			printf("the 1 fibonacci number is 1.\n");
			printf("the 2 fibonacci number is 1.\n");
			fibonacci(number);
		}
		printf("Enter a next integer (q to quit):\n");	  
	}   
	   
	return 0; 
} 
void fibonacci(int x)//定义斐波纳契函数 
{
	int count;
	int a = 1;
	int b = 1;
	int t; 
	for (count = 2; count < x; count++)
		{
				t = a + b;
				a = b;
				b = t;
				printf("the %d fibonacci number is %d.\n", count + 1, t);
		} 
}

运行结果:
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值