C primer plus (第6版)中文版 第九章答案

//第 1 题

#include<stdio.h>
double min( double x, double y )
{
	return ( x < y ? x : y);
}
int main()
{
	double x,y,result;
	scanf("%lf",&x);
	scanf("%lf",&y);
	result = min(x,y);
	printf("%lf",result);	
	return 0;
}
//第 2 题

#include <stdio.h>
void chline (char ch, int i, int j)
{
	int a,b;
	for( a=0 ; a<j ; a++ )
	{
		for( b=0 ; b<i ; b++ )
		{
			putchar(ch);
		}
		putchar('\n');
	}
}
int main()
{
	char ch;
	int i,j;
	scanf("%c",&ch);
	scanf("%d %d",&i,&j);
	chline( ch, i, j);	
	return 0;
}
//第 3 题

把第二题行和列换一下就行
//第 4 题

#include <stdio.h>
double average( double x , double y )
{
	double average;
	x = 1/x;//倒数 
	y = 1/y;
	average = (x+y)/2.0;
	average = 1/average;
	return average;
} 
int main()
{
	double x,y,aver;
	scanf("%lf %lf",&x,&y);
	aver = average(x,y);
	printf("%lf",aver);	
	return 0;
}
//第 5 题

#include<stdio.h>
void larger_of( double *a, double *b )
{
	int temp;
	temp = *a > *b ? *a : *b;
	*a = temp;
	*b = temp;
}
int main()
{
	double x,y;
	scanf("%lf %lf",&x,&y);
	printf("Before:%lf %lf\n",x,y);
	larger_of( &x, &y);
	printf("After:%lf %lf\n",x,y);
	
	return 0;
}
//第 6 题

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void sorting(double *a, double *b, double* c);
int main()
{
	double x,y,z;
	scanf("%lf %lf %lf",&x,&y,&z);
	printf("Before:%lf %lf %lf\n",x,y,z);
	sorting( &x, &y, &z);
	printf("After:%lf %lf %lf\n",x,y,z);
	
	return 0;
}
void sorting(double* a, double* b, double* c)
{
	int temp1,temp2,temp3;
	temp1 = *c >= (*a > *b ? *a : *b) ? *c : (*a > *b ? *a : *b);
	temp3 = *c <= (*a < *b ? *a : *b) ? *c : (*a < *b ? *a : *b);
	if (*a <= temp1 && *a >= temp3)
		temp2 = *a;
	else if (*b <= temp1 && *b >= temp3)
		temp2 = *b;
	else temp2 = *c;
	*a = temp1;
	*b = temp2;
	*c = temp3;
}
//第 7 题

#include <stdio.h>
int alpha(char ch)
{
	if (ch >= 'a' && ch <= 'z')
	{
		return (ch - 'a' + 1);
	}
	else if (ch >= 'A' && ch <= 'Z')
	{
		return (ch - 'A' + 1);
	}
	else
	{
		return -1;
	}
}
int main(void)
{
	char ch;
	int alp;
	while ((ch = getchar()) != EOF)
	{
		if ('\n' == EOF)
		{
			continue;
		}
		alp = alpha(ch);
		if (alp != -1)
		{
			printf("%c:%d\n", ch, alp);
		}
	}
	return 0;
}
第 8 题

#include<stdio.h>
#include<math.h>
double power( double x,double y )
{
	double temp;
	if( y>0 )
	{
		return pow( x,y );
	}
	if( y==0 )
	{
		if( 0==x )
		{
			printf("0 to the 0 is undefined\n");
		}
		
		return 1;
	}
	if( y<0 )
	{
		temp = pow( x,-y );
		return (1/temp);
	}
}
int main()
{
	double x,y,result;
	
	scanf("%lf %lf",&x,&y);
	result = power(x,y);
	printf("%lf to the %lf is %lf\n",x,y,result);
	
	return 0;
}
//第 9 题

#include <stdio.h>
double power(double x,double y)
{
	double temp;
	if( y>0 )
	{
		y--;
		return (x * power(x,y));
	}
	if( y==0 )
	{
		if( x==0 )
		{
			printf("0 to the 0 is undefined\n");
		}
		return 1;
	}
	if( y<0 )
	{
		y++;
		temp = 1/x;
		return (temp * power(x,y));
	}
}
int main()
{
	double x,y,result;
	
	scanf("%lf %lf",&x,&y);
	result = power(x,y);
	printf("%lf to the %lf is %lf\n",x,y,result);
	
	return 0;
}
//第 10 题

#include <stdio.h>
void to_base_n( int base, int n )
{
	int x;

	x = base % n;
	if( base>=n )
	{
		to_base_n(base / n, n);
	}

	printf("%d",x);
}
int main()
{
	int base,n;
	scanf("%d %d",&base,&n);
	printf("When we convert %d to %d,we get ",base,n);
	to_base_n(base,n);
	
	return 0;
}
//第 11 题

#include <stdio.h>
int Fibonacci(int n)
{
	int i;
	int x,y,z;
	for( i=1; i<=n; i++ )
	{
		if( i==1 )
		{
			x = 1;
			printf("%d ",x);
		}
		else if( i==2 )
		{
			y = 1;
			printf("%d ",y);
		}
		else
		{
			z = x+y;
			x = y;
			y = z;
			printf("%d ",z);
		}
	}
	putchar('\n');
}
int main()
{
	int n;
	scanf("%d",&n);
	Fibonacci(n);
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值