c primer plus 第九章

//第一题 
#include<stdio.h>
double min(double x,double y);
int main()
{
	double number1,number2;
	scanf("%lf %lf",&number1,&number2);
	printf("The lesser of %f and %f is %f",number1,number2,min(number1,number2));	
}
double min(double x,double y)
{
	return (x<y)?x:y;
}
//第二题 
#include<stdio.h>
void chline(char c,int a,int b);
int main()
{
	char c;
	int line,column;
	printf("Please enter the character:\n");
	scanf("%c",&c);
	printf("Please enter the line and column:\n");
	scanf("%d %d",&line,&column);
	chline(c,column,line);
}
void chline(char c,int a,int b)
{
	int i,j;
	for(j=b;j>0;j--){
		for(i=a;i>0;i--){
			printf("%c",c);
		}
		printf("\n");
	}
}
//第三题和第二题 一样 
#include<stdio.h>
void chline(char c,int a,int b);
int main()
{
	char c;
	int line,column;
	printf("Please enter the character:\n");
	scanf("%c",&c);
	printf("Please enter the line and column:\n");
	scanf("%d %d",&line,&column);
	chline(c,column,line);
}
void chline(char c,int a,int b)
{
	int i,j;
	for(j=b;j>0;j--){
		for(i=a;i>0;i--){
			printf("%c",c);
		}
		printf("\n");
	}
}
//第四题 
#include<stdio.h>

double average(double n1,double n2);
int main()
{
	double number1,number2;
	scanf("%lf %lf",&number1,&number2);
	double harmonic_average;
	harmonic_average=average(number1,number2);
	
	printf("The harmonic_average of the %lf and %lf is %lf",number1,number2,harmonic_average);
}
double average(double n1,double n2)
{
	n1=1/n1;
	n2=1/n2;
	
	return 1/((n1+n2)/2);
}
//第五题 
#include<stdio.h>

void larger_of(double* x,double* y);
int main()
{
	double x,y;
	scanf("%lf %lf",&x,&y);
	printf("Before: %f %f\n",x,y);
	larger_of(&x,&y);
	printf("After: %f %f\n",x,y);
}
void larger_of(double* x,double* y)
{
	if(*x>*y){
		*y=*x;
	}else{
		*x=*y;
	}
}
//第六题 
#include<stdio.h>

void sort(double* x,double* y,double*z);
int main()
{
	double x,y,z;
	scanf("%lf %lf %lf",&x,&y,&z);
	printf("Before: %f %f %f\n",x,y,z);
	sort(&x,&y,&z);
	printf("After: %f %f %f\n",x,y,z);
}
void sort(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;
	}
}
//第七题
#include<stdio.h>
#include<ctype.h>
 
int isalpha(char ch);
int main()
{
	char ch;
	while((ch=getchar())!=EOF){
		if(ch=='\n'){
			printf("please enter another character");
			continue;
		}
		if(isalpha(ch)!=-1){
			printf("alpha %c is %d in alphabet\n",ch,isalpha(ch));
		}
	}
}
int isalpha(char ch)
{
	if(isupper(ch)){
		return ch-'A'+1;
	}else if(islower(ch)){
		return ch-'a'+1;
	}else{
		return -1;
	}
}
//第八题 
#include<stdio.h>
#include<math.h>
double power( double x,double 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;
}
double power( double x,double y )
{
	double t;
	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){
		t=pow( x,-y );
		return (1/t);
	}
}
//第九题 
#include<stdio.h>
 
double power(double x,double y);
int main()
{
    double x,y,result;
    scanf("%lf %lf",&x,&y);
    result=power(x,y);
    printf("%lf to the %lf is %lf",x,y,result);
}
double power(double x,double y)
{
    double t;
    if(y>0){    
        ;
        return (x*power(x,--y));
    }
    if(y==0){
        if(x==0){
            printf("0 to the 0 is underfined\n");
        }
        return 1;
    }
    if(y<0){
        
        t=1/x;
        return (t*power(x,++y));
    }
}
//第十题 
#include<stdio.h>
 
void to_base_n(int bas,int n);
int main()
{
	int bas,n;
	scanf("%d %d",&bas,&n);
	
	printf("conver %d to %d is\n",bas,n);
	to_base_n(bas,n);
	
}
void to_base_n(int bas,int n)
{
	int x;
	x=bas%n;
	if(bas>=n){
		to_base_n(bas/n,n);
	}
	printf("%d",x);
}
//第十一题 
#include<stdio.h>
void Fibonacci(int n);
int main()
{
	int n;
	scanf("%d",&n);
	Fibonacci(n);
}
void Fibonacci(int n)
{
	int i;
	int n1,n2;
	int t;
	n1=1;
	n2=1;
	
	for(i=1;i<=n;i++){
		if(i==1){
			printf("1 ");
		}else if(i==2){
			printf("1 ");
		}else{
			t=n1+n2;
			n1=n2;
			n2=t;
			printf("%d ",n2);
		}
		
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值