c primer plus第九章

*1.设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单的驱动程序中>测试该函数。
 * */
#include<stdio.h>
double min(double x,double n);
int main(void)
{
printf("%.2f\n",min(3,6));
return 0;
}
double min(double x,double n)
{
return (x<n)?x:n;
}
* 2.设计一个函数chline(ch, i, j),打印指定的字符j行i列。在一个简单的驱动程序中>测试该函数。*/
#include<stdio.h>
void chline(char ch,int i,int j);
int main(void)
{
chline('n',3,5);
return 0;
}
void chline(char ch,int i,int j)
{
        int x=0,y=0;
        while(x<i)
        {
                while(y<j)
                {
                        putchar(ch);
                        y++;
                }
                putchar('\n');
                x++;
                y=0;
        }
        * 3.编写一个函数,接受3个参数:一个字符和两个整数。字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,第2个整数指定打印指定字符的行数。编写一个调用该函数的程序。*/
#include<stdio.h>
void chline(char ch,int a,int b);
int main(void)
{
chline('j',5,3);
	return 0;
}
void chline(char ch,int i,int j)
{
	int l=0,y=0;
	while(y<j)
	{
		while(l<i)
		{
		putchar(ch);
		l++;
		}
		putchar('\n');
		y++;
		l=0;
	}

}
* 4.两数的调和平均数这样计算:先得到两数的倒数,然后计算两个倒数的平均值,最后>取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平
均数。*/
#include<stdio.h>
double ave(double a,double b);
int main(void)
{
printf("%f\n",ave(6,9));
return 0;
}
double ave(double a,double b)
{
        double x;
        a=1/a;
        b=1/b;
        x=(a+b)/2;
        return (x=1/x);
}
/* 5.编写并测试一个函数larger_of(),该函数把两个double类型变量的值替换为较
大的值。例如, larger_of(x, y)会把x和y中较大的值重新赋给两个变量。*/
#include<stdio.h>
void larger_of(double *a,double* b);
int main(void)
{
        double a=5,b=7;
larger_of(&a,&b);
printf("%f\t%f\n",a,b);
return 0;
}
void larger_of(double * a,double * b)
{
        if(*a>*b)
                *b=*a;
        else
                *a=*b;

}
/* 6.编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第1个函数,中间值放入第2个变量,最大值放入第3个变量。*/
void doub(double *a,double *b,double *c);
#include<stdio.h>
int main(void)
{
double a=9,b=1,c=3;
doub(&a,&b,&c);
printf("%f\t%f\t%f\t\n",a,b,c);
return 0;
}
void doub(double *a,double *b,double *c)
{
	double min;
if((*a>*b&&*a>*c)&&(*b>*c))
{
	min=*c;
	*c=*a;
	*a=min;
}
if((*b>*a&&*b>*c)&&(*a>*c))
{
	min=*c;
	*c=*b;
	*b=*a;
	*a=min;
}
if((*a>*b&&*a>*c)&&(*b<*c))
{
	min=*b;
	*b=*c;
	*c=*a;
	*a=min;
}
}
/* 7.编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的数值位置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。*/
#include<stdio.h>
#include<ctype.h>
int chochar(int ch);
int main(void)
{
	int ch;
printf("%d\n",chochar(ch));
return 0;
}
int chochar(int ch)
{
	ch=getchar();
	if(isalpha(ch))
	{
	if(islower(ch))
		return (ch-'a'+1);
	if(isupper(ch))
		return (ch-'A'+1);
	}
	return -1;

}
* 8.6章的程序清单6.20中,power()函数返回一个double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告00次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。*/
#include<stdio.h>
double power(double num,unsigned b);
void print(void);
int main(void)	
{
double num;
unsigned int pow;
print();
while(scanf("%lf%u",&num,&pow)==2)
{
	printf("num==%f---pow==%u------power==%.2f\n",num,pow,power(num,pow));
print();
}
return 0;
}
double power(double num,unsigned pow)
{
	double p=1;
	
	if(num==0)
		return (p=0);
	if(pow==0)
		return (p=1);
	while(pow>0)
	{
		p*=num;
		pow--;
	}
	return p;

}
void print(void)
{
printf("请输入一个数的正整数密次数\n");
}

/9.使用递归函数重写编程练习8#include<stdio.h>
double power(double num,signed int pow);
int main(void)
{
double num;
signed int pow;
while(scanf("%lf%d",&num,&pow)==2)
{
printf("%.2f\n",power(num,pow));
}
return 0;
}
double power(double num,signed int pow)
{
	double p=1;
	if(num==0)
		return 0;
	if(pow==0)
		return 1;
	if(pow>0)
	p= power(num,pow-1)*num;
	return p;

}
  *10.为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n()函数接受两个在210范围内的参数,然后以第2个参数中指定的进制打印第1个参数的数值。例如,to_base_n(1298)显示的结果为201,也就是129的八进制数。在一个完整的程序中测试该函数
 * */
#include<stdio.h>
void to_base_n(unsigned int num,unsigned int base);
int main(void)
{
unsigned int num;
unsigned int base;
while(scanf("%u%u",&num,&base)==2)
	to_base_n(num,base);
return 0;
}
void to_base_n(unsigned int num,unsigned int base)
{
	if(base <2|| base>10)
	{
		printf("请输入2-10之间的数\n");
		return ;
	}
	unsigned int b;
	b=num%base;
	if(num>=base)
		to_base_n(num/base,base);
		putchar(b==1?'1':'0');
return ;

}                           
/*11.编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契数。*/
#include<stdio.h>
void fibonacci(unsigned int num);
int main(void)
{
unsigned int num;
while(scanf("%u",&num)==1)
	fibonacci(num);
return 0;
}
void fibonacci(unsigned int num)
{
	unsigned int x,i=1,f1=1,f2=1;
	while(i<=num)
	{
		if(i<=2)
			x=1;
		else
		{
	x=f1+f2;
	f1=f2;
		f2=x;
		}
	i++;
	printf("%d\t",x);
	}
	putchar('\n');
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值