C Primer Plus 第六版 编程练习题个人总结 第九章

编程练习题个人总结 第九章

第九章

1.

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

#include <stdio.h>
double min(double,double);
int main(void){
	double a;
	double x,y;
	a=min(x, y);
	printf("%lf",a);
	}
double min(double,double){
	double x,y;
	printf("请输入两个值,空格隔开:\n");
	scanf("%lf %lf",&x,&y);
	if(x>y)
	{
		return y;
	}
	if(y>x)
	{
		return x;
	}
	}

2.

设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。
啊啊啊啊啊,书又翻译错了。。。。可以看看这位大神的,我不想写了
原文that prints the requested character in columns i through j .
大概就是打印从i列到j列

#include <stdio.h>
void math(char,int,int);
int main(void){	
	char ch;
	int i,j;
	printf("hang lie:");
	scanf("%d %d",&j,&i);
	printf("shuru zi fu:");
	math(ch, i, j);
	}
void math(char ch,int i,int j){	
	int x;
	for(x=0;x<j;x++)
		{
		ch=getchar();
		}
		for(x=j;x<=i;x++)
		{
			ch=getchar();
			putchar(ch);
		}
}

3.

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

#include <stdio.h>
void math(char,int,int);
int main(void){	
	char ch;
	int i,j;
	printf("输入要打印的字符:");
	scanf("%c",&ch);
	printf("输入要打印的行列(空格隔开)");
	scanf("%d %d",&i,&j);
	math(ch, i, j);
	}
void math(char ch,int i,int j){	
	int x;
	int y;
	for(x=0;x<i;x++)
	{
		for(y=0;y<j;y++)
		{
			printf("%c",ch);
		}
		printf("\n");
	}
}

4.

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

#include <stdio.h>
double math(double a,double b);
int main(void){	
	double  i,j;
	printf("输入两个参数(空格隔开)");
	scanf("%lf %lf",&i,&j);
	printf("%lf",math(i, j));
	}
double math(double a,double b){	
	return 1/((1/a+1/b)/2);
}

5.

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

#include<stdio.h>
void large_of(double *x,double *y){
	if(*x>*y)*y=*x;
	else *x=*y;
}
int main(void){
	double x,y;
	printf("请输入两个数");
	scanf("%lf %lf",&x,&y);
	large_of(&x, &y);
	printf("%lf %lf",x,y);
}

6.

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

#include<stdio.h>
void math(double*a,double*b,double*c);
int main(void){
	double x,y,z;
	printf("请输入3个数");
	scanf("%lf %lf %lf",&x,&y,&z);
	math(&x, &y, &z);
	printf("%lf %lf %lf",x,y,z);
}
void math(double*a,double*b,double*c){
	double m;
	if(*a>*b)
	{
		if(*a>*c)
		{
			m=*c;
			*c=*a;
			if(m>*b)
			{
				*a=*b;
				*b=m;
			}
			else
			{
				*b=*b;
				*a=m;
			}
		}
		else
		{
			m=*a;
			*a=*b;
			*b=m;
		}
	}
	else
	{
		if(*a>*c)
		{
			m=*b;
			*b=*a;
			*a=*c;
			*c=m;
		}
		else
		{
			if(*b>*c)
			{
				m=*c;
				*c=*b;
				*b=m;
			}
			else
			{
				*a=*a;
				*b=*b;
				*c=*c;
			}
		}
	}
}//这个循环判断写的丧心病狂,应该是不建议这么写的

7.

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

#include<stdio.h>
#include <ctype.h>
int math(char ch);
int main(void){
	char ch;
	printf("请输入");
	printf("%d",math(ch));
}
int math(char ch){
	while((ch=getchar())!=EOF)
	{
		if(!isalpha(ch))
		{
			return -1;
		}
		else if(isupper(ch)){return ch-64;}
		else
		{
			return ch-96;
		}
	}
}

8.

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

// power.c -- 计算数的整数幂
#include <stdio.h> 
double power(double n, int p);
 // ANSI函数原型
int main(void) {
	 double x, xpow;
	  int exp;
	 printf("Enter a number and the positive integer power"); 
	 printf(" to which\nthe number will be raised. Enter q");
	 printf(" 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; 
	int i;
	if(p>0)
	{
		for (i = 1; i <= p; i++)
		pow *= n;
	}
	else if(p<0)
	{
		for (i = 1; i <=-p; i++)
		pow *= 1/n;
	}
	else pow=1;
	return pow;          // 返回pow的值
}

9.

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

// power.c -- 计算数的整数幂
#include <stdio.h> 
double power(double n, int p);
 // ANSI函数原型
int main(void) {
	 double x, xpow;
	  int exp;
	 printf("Enter a number and the positive integer power"); 
	 printf(" to which\nthe number will be raised. Enter q");
	 printf(" 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)  // 函数定义
{ 
	if(p>=1)
	{
		return n*power(n, p-1);
	}
	else if(p<0)
	{
		return 1/n*power(n, p+1);
	}
	else return 1;
	            // 返回pow的值
}

10.

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

/* binary.c -- 以二进制形式打印制整数 */ 
#include <stdio.h> 
void to_binary_n(int n,int x); 
int main(void) 
{ 
	int number,xx;
	printf("Enter two integer (q to quit):\n"); 
	while (scanf("%d %d", &number,&xx) == 2) { 
		if(xx<=8&&xx>=2)
		{
		printf("Binary equivalent: "); 
		to_binary_n(number,xx); 
		putchar('\n'); 
		printf("Enter an integer (q to quit):\n");
		}
		else continue;
	} 
		printf("Done.\n"); 
		return 0;
} 

void to_binary_n(int n,int x) /* 递归函数 */ 
{ 
	int r;
	r = n % x; 
	if (n >= x) 
	to_binary_n(n/x,x); 
	printf("%d",r);
	return; 
}

11.

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

#include <stdio.h>
unsigned long Fibonacci(unsigned n);
int main(void){
	unsigned m=1,n=1;
	unsigned q;
	unsigned i,j;
	printf("请输入数列的长度:");
	scanf("%u",&j);
	for(i=1;i<j;i++)
	{
		q=m;
		printf("%u ",q);
		m=n;
		n=q+n;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值