C语言学习笔记——第九章

9-1.c

#include<stdio.h>
double min(double x, double y);
int main(void)
{
	min(1.1,2.1);
	return 0;
}
double min(double x,double y)
{
	return x>y?y:x;
}

9-10.c

#include &lt;stdio.h&gt;
long Fibonacci(int a)
{
	int i;
	long one=1,two=1,ret=1;
	if(a&lt;=2)
		return 1;
	for (i=2;i&lt;a;i++)
	{
		ret=one+two;
		one=two;
		two=ret;
	}
	return ret;
}
int main(void)
{
	int a=1;
	while(a)
	{
		scanf("%d",&a);
		printf("%ld\n",Fibonacci(a));
	}
	return 0;
}

9-2.c

 #include<stdio.h>
 chline(char ch, int i,int j)
 {
	int a;
	for(a=0;a<j;a++)
	 {
		 if(a<i)
			 putchar(' ');
		 else
			 putchar(ch);
	 }
 }
		 
 int main(void)
 {
	 chline('a',10,100);
	 return 0;
 }

9-3.c

 #include<stdio.h>
 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(void)
 {
	 chline('a',10,100);
	 return 0;
 }

9-4.c

#include&lt;stdio.h&gt;
double xiejun(double a, double b)
{
	return 1/(((1/a)+(1/b))/2);
}
int main(void)
{
	printf("&f",xiejun(3,5));
	return 0;
}

9-5.c

#include&lt;stdio.h&gt;
larger_of(double * x, double * y)
{
	if(*x&gt;*y)
		*y=*x;
	else
		*x=*y;
}
int main(void)
{
	double a,b;
	a=1;
	b=10;
	larger_of(&a,&b);
	printf("%f %f",a,b);
	return 0;
}

	

9-6.c

#include&lt;stdio.h&gt;
int getseq(char ch)
{
	if(ifletter(ch)==1)
	{
		tosmall(&ch);
		return ch-96;
	}
	else
		return -1;
}
int ifletter(char ch)
{
	if((ch&gt;='A' && ch&lt;='Z') || (ch&gt;='a' && ch&lt;='z'))
		return 1;
	else
		return 0;
}
tosmall(char * ch)
{
	if(*ch&lt;'a')
		*ch=*ch + 32;
}
int main(void)
{
	char ch;
	while((ch=getchar())!='\n')
		printf("%d\n",getseq(ch));
	return 0;
}

9-7.c

#include<stdio.h>
double power(double a, int b)
{
	double ret=1;
	int now;
	if(a==0)
		ret=0;
	else if(b>0)
	{
		for(now=1;now<=b;now++)
			ret*=a;
	}
	else if(b<0)
	{
		b*=-1;
		for(now=1;now<=b;now++)
			ret*=a;
		ret=1/ret;
	}
	else 
		ret=1;
	return ret;
	
}
int main(void)
{
	printf("%f\n",power(2,8));
	printf("%f\n",power(0,8));
	printf("%f\n",power(2,0));
	printf("%f\n",power(2,-8));
	return 0;
}

9-8.c

#include<stdio.h>
double power(double a,int b)
{
	double ret=1;
	if(a==0)
		ret=0;
	else if(b==0)
		ret=1;
	else if(b>0)
	{
		ret=a*power(a,b-1);
	}
	else if(b<0)
	{
		b*=-1;
		ret=1/power(a,b);
	}
	return ret;
}
int main(void)
{
	printf("%f\n",power(2,8));
	printf("%f\n",power(0,8));
	printf("%f\n",power(2,0));
	printf("%f\n",power(2,-8));
	return 0;
}

9-9.c

#include&lt;stdio.h&gt;
void to_base_n(int a,int b)
{
	int c;
	if(b&lt;1 || b&gt;10)
		printf("Error!");
	else
	{
		c=a%b;
		if (a&gt;=b)
			to_base_n(a/b,b);
		putchar('0'+c);
	}
}
int main(void)
{
	int a,b;
	scanf("%d %d", &a, &b);
	to_base_n(a,b);
	return 0;
}
		

原创文章,转载请注明: 转载自YXZ’s Blog

本文链接地址: C语言学习笔记——第九章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>