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

// 设计一个函数民(x,y),返回两个double类型值的较小的值。在一个简单
// 的驱动程序中测试该函数
#include "stdio.h"
double min(int,int);
int main(void)
{
	int n,m;
	printf("Please enter two numbers.\n");
	while(scanf("%d %d",&n,&m)==2)
	{
		printf("The lesser of %d and %d id %f.\n",
			n,m,min(n,m));
		printf("Please enter two numbers again(q to quit)\n");
	}
	return 0;
}
double min(int n,int m)
{
	if (n<m)
		return n;
	else
		return m;
}

// 设计一个函数chline(ch,i,j),打印指定的字符j行i列。在一个简单
// 的驱动程序中测试该函数
#include "stdio.h"
#include "ctype.h"
void chline(char,int,int);
int main(void)
{
	char ch;
	int j,i;
	printf("Press enter to start\n");
	do
	{
		while(getchar()!='\n')
			continue;
		printf("What characters do you want\n");
		ch=getchar();
		while(getchar()!='\n')
			continue;
		printf("How many rows and columns of stars do you want\n");
		scanf("%d %d",&j,&i);
		chline(ch,j,i);
		printf("Do you want it again?(1 or 0)\n");
		scanf("%d",&j);
	}while(j==1);	
	return 0;
}
void chline(char ch,int j,int i)
{
	int h,l;
	for(h=1;h<=j;h++)
	{
		for (l=1;l<=i;l++)
			printf("%c",ch);
		printf("\n");
	}
}

(刚看题目有点懵,复制第二题代码,i和j互换)

// 编写一个函数,接受3个参数:一个字符和两个整数。字符参数时待打印的
// 字符,第1个整数指定一行打印字符的次数,第2个整数指定打印指定字符
// 行数。编写一个调用该函数的程序。
#include "stdio.h"
#include "ctype.h"
void chline(char,int,int);
int main(void)
{
	char ch;
	int j,i;
	printf("Press enter to start\n");
	do
	{
		while(getchar()!='\n')
			continue;
		printf("What characters do you want\n");
		ch=getchar();
		while(getchar()!='\n')
			continue;
		printf("How many rows and columns of stars do you want\n");
		scanf("%d %d",&j,&i);
		chline(ch,j,i);
		printf("Do you want it again?(1 or 0)\n");
		scanf("%d",&j);
	}while(j==1);	
	return 0;
}
void chline(char ch,int j,int i)
{
	int h,l;
	for(h=1;h<=i;h++)//i和j互换
	{
		for (l=1;l<=j;l++)//i和j互换
			printf("%c",ch);
		printf("\n");
	}
}

// 两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的
// 平均值,最后取计算结果的倒数.编写一个函数,接受两个double类型
// 的参数,返回这两个参数的调和平均数.
#include "stdio.h"
double average(double,double);
int main(void)
{
	double n,m;
	printf("Please enter two floating point numbers\n");
	while(scanf("%lf %lf",&n,&m)==2)
	{
		printf("%lf\n",average(n,m));
		printf("Please enter two floating point numbers again (q to quit)\n");		
	}
	return 0;
}
double average(double n,double m)
{
	double a,b;
	double sum1,sum2;
	a=1/n;
	b=1/m;
	sum1=(a+b)/2;
	sum2=1/sum1;
	return sum2;
}

// 编写并测试一个函数larger_of(),该函数把两个double类型变量的值
// 替换为较大的值。例如larger_of(x,y)会把x和y中较大的值重新赋给两
// 个变量。
#include "stdio.h"
double larger_of(double * u,double * v);
int main(void)
{
	double i,j;
	printf("Please enter two numbers.\n");
	scanf("%lf %lf",&i,&j);
	printf("i=%lf,j=%lf\n",i,j);
	larger_of(&i,&j);
	printf("Now i=%lf,j=%lf\n",i,j);
	return 0;
}
double larger_of(double * u,double * v)
{
	int temp;
	temp=*u;
	*u=*v;
	*v=temp;
}

// 编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小
// 的值放入第一个变量,中间值放入第二个变量,最大值放入第三个变量。
#include "stdio.h"
void large(double * u,double * v,double* y);
int main(void)
{
	double f,s,t;
	printf("Please enter three numbers\n");
	scanf("%lf %lf %lf",&f,&s,&t);
	printf("f=%lf,s=%lf,t=%lf\n",f,s,t);
	large(&f,&s,&t);
	printf("nowf=%lf,s=%lf,t=%lf\n",f,s,t);
	return 0;
}
void large(double * u,double * v,double* y)
{
	int temp;
	if (*v<*u)
	{
		temp=*u;
		*u=*v;
		*v=temp;
	}
	if (*y<*v)
	{
		temp=*v;
		*v=*y;
		*y=temp;
	}
	if (*v<*u)
	{
		temp=*u;
		*u=*v;
		*v=temp;
	}		
}

// 编写一个函数。从标准输入中读取字符,直到遇到文件结尾。程序要报告
// 每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。
// 例如,c和C在字母表中的位置都是3.合并一个函数,以一个字符作为参
// 数,如果该字符是一个字母则返回一个数值位置,否则返回-1.
#include "stdio.h"
int letter(char);
int main(void)
{
	char ch;
	int i;
	printf("Please enter something.\n");
	while((ch=getchar())!=EOF)
	{
		i=letter(ch);
		if (i==-1)
			printf("%c is not a letter\n",ch);
		else
			printf("%c is a letter,In the %d position\n",ch,i);
		printf("\n");
	}
	return 0;
}
int letter(char ch)
{
	int i;
	if (ch>='a'&&ch<='z')
	{
		i=ch-'a'+1;
		return i;
	}
	else if (ch>='A'&&ch<='Z')
	{
		i=ch-'A'+1;
		return i;
	}
	else
		return -1;
}

// 改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0.任何
// 数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1).要使用
// 一个循环。并在程序中测试该函数。
#include "stdio.h"
double power(double n,int p);
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");
	}
	return 0;
}
double power(double n,int p)
{
	double pow =1;
	int i,o=1;
	if (n!=0&&p==0)
		return pow;
	else if(p<0)
		o=-1;
	else if (n==0&&p!=0)
	{
		pow=0;
		return pow;
	}
	else if(n==0&&p==0)
	{
		printf("The 0 power of 0 is undefined, which is treated as 1 at here\n");
		return pow;
	}
	for ( i = 1; i <=p*o; i++)
			pow*=n;
	if(o==-1)		
		pow=1/pow;
	return pow;
}

// 使用递归函数重写编程练习8.
	#include "stdio.h"
	double power(double n,int p);
	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)
		{
			if(exp>0)
			xpow=power(x,exp);
			else
			{
				xpow=power(x,-exp);
				xpow=1/xpow;
			}
			printf("%.3g to the power %d is %.5g\n",x,exp,xpow);
			printf("Enter next pair of numbers or q to quit.\n");
		}
		return 0;
	}
	double power(double n,int p)
	{
		double pow =1;
		int i=1,o=1;
		if (n!=0&&p==0)
			return pow;
		else if(p<0)
			o=-1;
		else if (n==0&&p!=0)
		{
			pow=0;
			return pow;
		}
		else if(n==0&&p==0)
		{
			printf("The 0 power of 0 is undefined, which is treated as 1 at here\n");
			return pow;
		}
		 pow = n * power(n, p * o-1);
		return pow;
	}

// 为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n()
// 函数接受两个参数,且第二个参数在2~10范围内,然后以第2个参数中指定的
// 进制打印1个参数的数值。例如,to_base_n(129,8)显示的结果201,也就
// 是129的八进制数。在一个完整的程序中测试该函数.
#include "stdio.h"
void binary(unsigned long,unsigned long);
int main(void)
{
	unsigned long i,j;
	printf("Enter an integer(q to quit)\n");
	while(scanf("%lu %lu",&i,&j)==2)
	{
		if (j<2||j>10)
		{
			printf("please enter 2~10\n");
			continue;
		}
		printf("jidgbkdfsjg\n");
		binary(i,j);
		putchar('\n');
		printf("Enter an integer(q to quit)again\n");
	}
	return 0;
}
void binary(unsigned long i,unsigned long j)
{
	int r;
	r=i%j;
	if (i>=j)
		binary(i/j,j);
	printf("%d",r);
	return;	
}

十一

// 编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契数.
#include "stdio.h"
int Fibonacci(int n);
int main(void)
{
	int n,m;
	printf("Please enter a integer\n");
	while(scanf("%d",&n)==1)
	{
		m=Fibonacci(n);
		printf("%d\n",m);
		printf("Please enter a integer again\n");
	}
	return 0;
}
int Fibonacci(int n)
{
	if (n==1||n==2)
		return 1;			
	if(n>2)
		return Fibonacci(n-1)+Fibonacci(n-2);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X在学了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值