C primer plus第六版 第九章答案

//9.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;
}
//9.2
#include<stdio.h>
void chline (char ch, int i, int j)
{
	int x,y;
	for( x=0 ; x<j ; x++ )
	{
		for( y=0 ; y<i ; y++ )
		{
			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;
}
//9.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;
}
//9.5
#include<stdio.h>
void larger_of( double *x, double *y )
{
	if( *x>*y )
	{
		*y = *x;
	}
	else
	{
		*x = *y;
	}
}
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;
}
//9.6
#include<stdio.h>
void sorting(double *x,double *y,double *z)
{
	double temp = 0.0;
	if( *x>*y )
	{
		temp = *x;
		*x = *y;
		*y = temp;
	}
	if( *x>*z )
	{
		temp = *x;
		*x = *z;
		*z = temp;
	}
	if( *y>*z )
	{
		temp = *y;
		*y = *z;
		*z = temp;
	}
}
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;
}
//9.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()
{
	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;
}
//9.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.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;
}
//9.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;
}
//9.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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章