C primer plus 9.11

C-primer-plus-9-11

9.1.11

#include<stdio.h>
double min(double x,double y);
int main(void)
{
	float a;
	float b;
	printf("请输入两个数字:");
	scanf("%f%f",&a,&b); 
	min(a,b);
	return 0;
}
double min(double x,double y)
{
	if(x<=y)
	{
		printf("%f",x);
	}
	else
		printf("%f",y);
}

9.11.2

#include<stdio.h>
void chline(char ch,int j,int i);
int main(void)
{
	char ch; 
	int i;
	int j;
	printf("请输入您想打印的字符:");
	scanf("%c",&ch);
	getchar();
	printf("您想打印几行:");
	scanf("%d",&j);
	getchar();
	printf("您想打印几列:");
	scanf("%d",&i);
	chline(ch,j,i);
	return 0;
}
void chline(char ch,int j,int i)
{
	int m;
	int n;
	for(m=0;m<j;m++)
	{
		for(n=0;n<i;n++)
		{
			printf("%c",ch);
		}
		printf("\n");
	}
}

9.11.3

#include<stdio.h>
void chline(char ch,int j,int i);
int main(void)
{
	char ch;
	int j;//列 
	int i;//行 
	printf("请输入待打印字符:");
	scanf("%c",&ch);
	printf("请输入字符打印列数:");
	scanf("%d",&j);
	printf("请输入字符打印行数:");
	scanf("%d",&i); 
	chline(ch,j,i);
	return 0;
}
void chline(char ch,int j,int i)
{
	int m;
	int n;
	for(m=0;m<i;m++)
	{
		for(n=0;n<j;n++)
		{
			printf("%c",ch);
		}
		printf("\n");
	}
}

9.11.4

#include<stdio.h>
void harmonic_average(double i,double j); 
int main(void)
{
	double m;
	double n;
	printf("请输入两个浮点数:");
	scanf("%lf%lf",&m,&n);
	harmonic_average(m,n);
	return 0;
}
void harmonic_average(double i,double j)
{
	double k;
	i=1/i;
	j=1/j;
	
	k=(i+j)/2;
	k=1/k;
	
	printf("它们的调和平均数为:%lf",k);
}

9.11.5

#include<stdio.h>
void larger_of(double i,double j); 
int main(void)
{
	double m;
	double n;
	printf("请输入两个浮点数:");
	scanf("%lf%lf",&m,&n);
	
	larger_of(m,n);
	
	
	return 0;
}
void larger_of(double i,double j)
{
	if(i<j)
	{
		i=j;
	}	
	else
	{
		j=i;
	}
	printf("i=%lf,j=%lf",i,j);	
}

9.11.6

#include<stdio.h>
double rank(double *A,double *B,double *C);
int main(void)
{
	double M;
	double N;
	double K;
	printf("请输入三个浮点数,我们将按从小到大的顺序输出:");
	scanf("%lf%lf%lf",&M,&N,&K);
	rank(&M,&N,&K);
	printf("%lf %lf %lf",M,N,K);
	return 0;
}
double rank(double *A,double *B,double *C)
{
	double temp1=0;
	double temp2=0;
	double temp3=0;
	if(*A<*B)
	{
		if(*C<*A)
		{
			temp1=*C;
	        temp2=*A;
	        temp3=*B;
		}else if(*A<*C)
		{
			if(*C<*B)
			{
				temp1=*A;
		        temp2=*C;
		        temp3=*B;
			}
			else
			{
				temp1=*A;
		        temp2=*B;
		        temp3=*C;
			}
		}
	}
	if(*B<*A)
	{
		if(*C<*B)
		{
			temp1=*C;
	        temp2=*B;
	        temp3=*A;
		}else if(*B<*C)
		{
			if(*C<*A)
			{
				temp1=*B;
		        temp2=*C;
		        temp3=*A;
			}
			else
			{
				temp1=*B;
		        temp2=*A;
		        temp3=*C;
			}
		}
	}
	*A=temp1;
	*B=temp2;
	*C=temp3;
}

9.11.7

#include<stdio.h>
#include<ctype.h>
int numchara(char ch);
int main(void)
{
	char ch;
	
	while(scanf("%c",&ch)!=EOF)
	{
		printf("%d  ",numchara(ch));
	}
	return 0;
}
int numchara(char ch)
{
	int k;
	if(islower(ch))
	{
		k=ch-'a'+1;
	}
	else if(isupper())
	{
		k=ch-'A'+1;
	}
	else
	{
		k=-1;
	}
	return k;
}

9.11.8

// power.c -- raises numbers to integer powers
#include <stdio.h>
double power(double n, int p); // ANSI prototype
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);   // function call
        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)  // function definition
{
    double pow = 1;
    int i;
    
    if(n==0&&p==0)
    {
    	printf("0的0次幂未定义,因此把该值处理为1"); 
	}
    if(p>=0)
    {
    	for (i = 1; i <= p; i++)
        pow *= n;
	}
	else if(p<0)
	{
		for (i=-1; i>=p; i--)
        pow /= n;
	}
    
    return pow;                // return the value of pow
}

9.11.9

// power.c -- raises numbers to integer powers
#include <stdio.h>
double power(double n, int p); // ANSI prototype
double f(double n);
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);   // function call
        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)  // function definition
{
    if(n==0&&p==0)
    {
    	printf("0的0次幂未定义,因此把该值处理为1"); 
	}
	else if(n!=0&&p==0)
	{
		return 1;
	}
	else if(n==0&&p!=0)
	{
		return 0;
	}
	else if(n!=0&&p>0)
	{
		return power(n,p-1)*n;
	}
	else if(n!=0&&p<0)
	{
		return power(n,p-1)/n;
	}
}

9.11.10

#include<stdio.h>
#include<string.h>
void to_base_n(int m,int n);
int main(void)
{
	int m;//数字 
	int n;//进制 
	while(scanf("%d%d",&m,&n))
	{
		to_base_n(m,n);	
	}
	return 0;
}
void to_base_n(int m,int n)
{
	int p=1;
	int q=1;
	
	int nums[256];
	int i;
		
	for(i=0;p>0;i++)
	{
		p=m/n;
		q=m%n;
		m=p;
		nums[i]=q;
	}
	
	for(;i-1>=0;i--)
	{
		printf("%d",nums[i-1]);
	}
}

9.11.11

#include<stdio.h>
int Fibonacci(int n);
int main(void)
{
	int num;
	while(scanf("%d",&num))
	{
		printf("**%d**\n",Fibonacci(num));
	}
	return 0;
}
int Fibonacci(int n)
{
	int i=2;
	int a=1;
	int b=1;
	int Fibonacci;
	if(n<=i)
	{
		return 1;
	}
	else
	{
		for(i=3;i<=n;i++)
		{
			Fibonacci=a+b;	
			a=b;
			b=Fibonacci;
		}
		return Fibonacci;			
	}
}

完。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值