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语言学习笔记——第九章

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

8-1.c

#include<stdio.h>
int main(void)
{
	long i;
	i=0;
	while(getchar()!=EOF)
		i++;
	printf("%ld\n",i);
	return 0;
}

8-2.c

#include<stdio.h>
int main(void)
{
	char now;
	int i=0;
	while((now=getchar())!=EOF)
	{
		if(now=='\n')
			printf("|\\n %d",now);
		else if(now=='\t')
			printf("|\\t %d",now);
		else if(now<' ')
			printf("|^%c %d",now+64,now);
		else 
			printf("|%c %d",now,now);
		i++;
		if(i==10)
		{
			i=0;
			printf("|\n");
		}
	}
	return 0;
}

8-3.c

#include&lt;stdio.h&gt;
int main(void)
{
	int big=0,small=0;
	char now;
	while((now=getchar())!=EOF)
	{
		if(now&gt;='a' && now&lt;='z')
			small++;
		if(now&gt;='A' && now &lt;= 'Z')
			big++;
	}
	printf("%d big\n%d small\n", big, small);
	return 0;
}

8-4.c

#include<stdio.h>
int main(void)
{
	long total=0;
	int number=0;
	int ifin=0;
	char now;
	while((now=getchar())!=EOF)
	{
		if(now<'A' || now > 'z')
		{
			if(ifin==1)
			{
				ifin=0;
				number++;
			}
		}
		else 
		{
			ifin=1;
			total++;
		}
	}
	if(ifin==1)
		number++;
	printf("You have input %d words, And each word has ave %ld letters.\n", number, total/number);
	return 0;
}

8-5.c

#include&lt;stdio.h&gt;
int main(void)
{
	int guess=50,min=0,max=100,i=0;
	char res;
	printf("Pick an integer from 1 to 100. I will try to guess ");
	printf("it.\n Respond with a y if my guess is right. And\n");
	printf("if it is wrong\nRespond with b if my guess is &gt; yours.\nRespond with s if my guess is &lt; yours.\n");
	while(1)
	{
		printf("Uh...is your number %d?\n",guess);
		scanf("%c",&res);
		switch (res)
		{
			case 'b':max=guess;
					break;
			case 's':min=guess;
					break;
			case 'y':i=1;
					break;
		}
		guess=(max-min)/2+min;
		if(i==1)
			break;
	}
	printf("I know I can do it!\n");
	return 0;
}

8-6.c

#include<stdio.h>
char getfirst()
{
	char ch;
	ch = getchar();
	while(ch == ' ' || ch == '\t' || ch=='\n')
		ch=getchar();
	return ch;
}
int main(void)
{
	putchar(getfirst());
	return 0;
}

8-7.c

#include&lt;stdio.h&gt;
void printtop(void);
int getswitch(void);
int main(void)
{
	int hours=0;
	char select;
	float monperh=0.0;
	while((select=getswitch())!='q')
	{
		if(select&lt;'a' || select&gt;'d')
			continue;
		switch(select)
		{	
			case 'a':
				monperh=8.75;
				break;
			case 'b':
				monperh=9.33;
				break;
			case 'c':
				monperh=10.00;
				break;
			case 'd':
				monperh=11.2;
		}
		printf("How many hours have you worked?");
		scanf("%d",&hours);
		printf("You will have $%2.2f\n",hours*monperh);
	}
	printf("Bye!");
	return 0;
}
void printtop(void)
{
	int a;
	for(a=0;a&lt;70;a++)
		putchar('*');
	putchar('\n');
}
char getswitch(void)
{
	char a;
	printtop();
	printf("Enter the char corresponding to the desired pay rate or action:\n");
	printf("a)$8.75/hr\t\tb)$9.33/hr\nc)$10.00/hr\t\t4d$11.20/hr\nq)quit\n");
	printtop();
	a=getchar();
	while(getchar()!='\n')
		continue;
	return a;
}

8-8.c

#include&lt;stdio.h&gt;
char getchoice(void)
{
	char choice;
	printf("Enter the operation of your choice:\n");
	printf("a. add\t\ts. subtract\nm. multiply\t\td. divide\nq. quit\n");
	choice=getchar();
	toend();
	return choice;
}
toend(void)
{
	while(getchar()!='\n')
		continue;
}
float getnumber(void)
{
	char a[255];
	float num;
	while(scanf("%f",&num)!=1)
	{
		scanf("%s", a);
		printf("%s is not a number.\nPlease enter a number, such as 2.5, -1.78E8 or 3:");
	}
	return num;
}
int main(void)
{
	char choice;
	float firstnum,secondnum;
	while((choice=getchoice())!='q')
	{
		if(choice!='a' && choice!='s' && choice!='m' && choice!='d')
			continue;
		printf("Enter first number:");
		firstnum=getnumber();
		
		printf("Enter second number:");
		secondnum=getnumber();
		while(choice=='d' && secondnum==0)
		{
			printf("Enter a number other than 0:");
			secondnum=getnumber();
		}
		switch (choice)
		{
			case 'a':
				printf("%.6f + %.6f = %.6f\n", firstnum, secondnum, firstnum+secondnum);
				break;
			case 's':
				printf("%.6f - %.6f = %.6f\n", firstnum, secondnum, firstnum-secondnum);
				break;
			case 'm':
				printf("%.6f X %.6f = %.6f\n", firstnum, secondnum, firstnum*secondnum);
				break;
			case 'd':
				printf("%.6f / %.6f = %.6f\n", firstnum, secondnum, firstnum/secondnum);
				break;
		}
	}
	printf("Bye!\n");
	return 0;
}

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

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

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

7-1.c

#include<stdio.h>
int main(void)
{
	int blank,turn,others;
	char tmp;
	blank=turn=others=0;
	printf("Please input some string.End with #:");
	while((tmp=getchar())!='#')
	{
		if(tmp==' ')
			blank++;
		else if(tmp=='\n')
			turn++;
		else
			others++;
	}
	printf("\nYou have input %d blank, %d return, %d others.\n", blank, turn
	, others);
	return 0;
}

7-11.c

#include<stdio.h>
void printtop(void)
{
	int a;
	for(a=0;a<70;a++)
		putchar('*');
	putchar('\n');
}
int menu(void)
{
	int input;
	printtop();
	printf("What are you going to do?\na)caoxianjing:$1.25/pound\t\tb)tiancai:$0.65/pound\n");
	printf("c)huluobo:$0.89/pound\t\t\nq)Tell me how much I should pay\n");
	printtop();
	input=getchar();
	return input;
}
int main(void)
{
	int input;
	int a,b,c;
	float money;
	float yunshu;
	int zong;
	while((input=menu())!='q')
	{
		if(input<'a' || input>'c')
			continue;
		switch(input)
		{
			case 'a':
				printf("How many?");
				scanf("%d",&a);
				break;
			case 'b':
				printf("How many?");
				scanf("%d",&b);
				break;
			case 'c':
				printf("How many?");
				scanf("%d",&c);
				break;
		}
	}
	printf("caoxianjing:$1.25/pound\t%d pounds\t$%2.2f\n",a,a*1.25);
	printf("tiancai:$0.65/pound\t%d pounds\t$%2.2f\n",b,b*0.65);
	printf("huluobo:$0.89/pound\t%d pounds\t$%2.2f\n",c,c*0.89);
	money=a*1.25+b*0.65+c*0.89;
	if(money<100)
		printf("You need to pay $%2.2f!\n",money);
	else
	{
		money*=0.95;
		printf("You need to pay $%2.2f! Included 5%c off!\n",money,'%');
	}
	zong=a+b+c;
	if(zong<6)
		yunshu=3.5;
	else if(zong<20)
		yunshu=10;
	else 
		yunshu=8+0.1*zong;
	printf("If you need to yunshu,you need $%2.2f for it.\n",yunshu);
	printf("Total to pay $%2.2f\n",money+yunshu);
	return 0;
}

7-2.c

#include<stdio.h>
int main(void)
{
	int num,ch;
	num=ch=0;
	printf("Please input some strings:");
	while((ch=getchar())!='#')
	{
		num++;
		if((num%8)!=0)
			printf("|%c %d", ch, ch);
		else
			printf("\n");
	}
	return 0;
}

7-3.c

#include<stdio.h>
int main(void)
{
	int all_ou,all_ji,num_ou,num_ji,now;
	all_ou=all_ji=num_ou=num_ji=0;
	printf("Input some integers,end with 0:");
	while((scanf("%d",&now)!=-1) && (now!=0))
	{
		if(now%2==0)
		{

			all_ou+=now;
			num_ou++;
		}
		else
		{
			all_ji+=now;
			num_ji++;
		}
	}
	printf("You have input %d ou and ave is %2.2f.\n", num_ou,(float)all_ou/(float)num_ou);
	printf("You have input %d ji and ave is %2.2f.\n", num_ji,(float)all_ji/(float)num_ji);
	return 0;
}

7-4.c

#include<stdio.h>
int main(void)
{
	int times,now;
	times=0;
	while((now=getchar())!='#')
	{
		if(now=='.')
		{
			times++;
			putchar('!');
		}
		else if(now=='!')
		{
			times++;
			printf("!!");
		}
		else 
			putchar(now);
	}
	printf("\nI have repleaced for %d times!\n",times);
	return 0;
}

7-6.c

#include<stdio.h>
int main(void)
{
	char before=0,now;
	int times=0;
	printf("Please input some strings,end with #:");
	while((now=getchar())!='#')
	{
		if(before=='e' && now=='i')
			times++;
		before=now;
	}
	printf("\nYou have input %d ei.\n", times);
	return 0;
}

7-7.c

#include<stdio.h>
#define base 10
#define add 1.5
#define qian300 0.15
#define hou150 0.2
#define yuxia 0.25
int main(void)
{
	int hours,money=0;
	printf("How many hours do you woke a week?");
	scanf("%d",&hours);
	if(hours>40)
		hours+=(hours-40)*add;
	money=hours*base;
	if(money<300 || money==300)
		money=money*(1-qian300);
	else if(money<450 || money==450)
		money=money-300*qian300-(money-300)*hou150;
	else
		money=money-300*qian300-150*hou150-(money-450)*yuxia;
	printf("You will have %d $ a week.", money);
	return 0;
}

7-8.c

#include<stdio.h>
void printtop(void);
int getswitch(void);
int main(void)
{
	int hours,select=0;
	float monperh=0.0;
	while((select=getswitch())!=5)
	{
		if(select<1 || select>5)
			continue;
		switch(select)
		{	
			case 1:
				monperh=8.75;
				break;
			case 2:
				monperh=9.33;
				break;
			case 3:
				monperh=10.00;
				break;
			case 4:
				monperh=11.2;
		}
		printf("How many hours have you worked?");
		scanf("%d",&hours);
		printf("You will have $%2.2f\n",hours*monperh);
	}
	printf("Bye!");
	return 0;
}
void printtop(void)
{
	int a;
	for(a=0;a<70;a++)
		putchar('*');
	putchar('\n');
}
int getswitch(void)
{
	int a;
	printtop();
	printf("Enter the number corresponding to the desired pay rate or action:\n");
	printf("1)$8.75/hr\t\t2)$9.33/hr\n3)$10.00/hr\t\t4)$11.20/hr\n5)quit\n");
	printtop();
	scanf("%d",&a);
	return a;
}

7-9.c

#include<stdio.h>
int ifshu(int a)
{
	int b=0,c=1;
	for(b=2;b<a;b++)
	{
		if(a%b==0)
		{
			c=0;
			break;
		}
	}
	return c;
}
int main(void)
{
	int i,now;
	printf("Please input an integer:");
	while(scanf("%d",&i)==-1)
		continue;
	for(now=2;now<(i+1);now++)
	{
		if(ifshu(now)==1)
			printf("%d ", now);
	}
	printf("\nBye!\n");
	return 0;
}

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

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

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

6-1.c

#include<stdio.h>
int main(void)
{
	char letter[27]="abcdefghijklmnopqrstuvwxyz";
	int a;
	for(a=0 ; a<26; a++)
		printf("%c", letter[a]);
	return 0;
}

6-10.c

#include&lt;stdio.h&gt;
int main(void)
{
	int integer[8];
	int a,b;
	printf("Please input 8 integers:");
	for(a=0;a&lt;8;a++)
		scanf("%d",&integer[a]);
	for(a=7;a&gt;-1;a--)
		printf("%d ",integer[a]);
	return 0;
}

	

6-11.c

#include&lt;stdio.h&gt;
float run1(int towhere);
float run2(int towhere);
int main(void)
{
	float a;
	printf("To:");
	scanf("%f", &a);
	printf("%f\n",run1(a));
	printf("%f\n",run2(a));
	return 0;
}
float run1(int towhere)
{
	float a,b=1.0;
	for (a=2.0;a&lt;=towhere;a++)
		b+=1.0/a;
	return b;
}
float run2(int towhere)
{
	float a,b=1.0;
	for (a=2.0;a&lt;=towhere;a++)
		b+=(((int)a-1)%2==0?1:-1)*(1.0/a);
	return b;
}


6-12.c

#include<stdio.h>
long pow(int num, int times);
int main(void)
{
	int number[8];
	int a;
	for(a=0;a<8;a++)
	{
		number[a]=pow(2,a+1);
	}
	a=0;
	do
		printf("%d\n", number[a]);
	while (++a<8);
	return 0;


}
long pow(int num, int times)
{
	int a;
	long b=1;
	for(a=1;a<=times;a++)
	{
		b*=num;
	}
	return b;
}

6-2.c

#include<stdio.h>
int main(void)
{
	int a,b;
	for (a=1;a<=5;a++)
	{
		for (b=1;b<=5;b++)
			printf("$");
		printf("\n");
	}
	return 0;
}

6-3.c

#include<stdio.h>
int main(void)
{
	int a;
	char b;
	for (a=1;a<=6;a++)
	{
		for (b='F';b>='A';b--)
			printf("%c", b);
		printf("\n");
	}
	return 0;
}

6-4.c

#include&lt;stdio.h&gt;
int main(void)
{
	int blank,howlong,a,b,c;
	char towhere,mid;
	printf("Tell me to go where:");
	scanf("%c",&towhere);
	howlong=(towhere - 'A')*2+1;
	for (a=1;a&lt;=(towhere - 'A'+1);a++)
	{
		blank=towhere-'A'-a+1;
		for(b=1;b&lt;=blank;b++)
			printf(" ");
		for (b=0;b&lt;a-1;b++)
			printf("%c",b+'A');
		printf("%c",'A'+a-1);
		for (c=a-1;c&gt;0;c--)
			printf("%c",'A'+c-1);
		for(b=1;b&lt;=blank;b++)
			printf(" ");
		printf("\n");
	}

	return 0;

}

6-5.C

#include&lt;stdio.h&gt;
int main(void)
{
	int from,to;
	int a;
	printf("From:");
	scanf("%d", &from);
	printf("To:");
	scanf("%d",&to);
	for (a=from;a&lt;=to;a++)
	{
		printf("%d %d %d\n",a,a*a,a*a*a);
	}
	return 0;
}
		

6-6.c

#include<stdio.h>
int main(void)
{
	char string[255];
	int max,where;
	printf("Please input a sentense:");
	scanf("%s",string);
	max=strlen(string) - 1;
	for (where=max;where>=0;where--)
	{
		putchar(string[where]);
	}
	return 0;
}

6-7.c

#include&lt;stdio.h&gt;
float getfloat();
int main(void)
{
	float a,b;
	a=getfloat();
	b=getfloat();
	printf("%f\n",(a-b)/(a*b));
	return 0;

}
float getfloat()
{
	float a=0.0;
	printf("Please input a float:");
	while(scanf("%f",&a) != 1);
	return a;
}

6-9.c

#include&lt;stdio.h&gt;
long dorun(int lower,int upper);
int main(void)
{
	int lower,upper=0;
	printf("Enter lower and upper integer limits:");
	scanf("%d %d",&lower,&upper);
	while (lower&lt;=upper)
	{
		printf("The sums of the squares form %d to %d is %ld.\n",lower,upper,dorun(lower,upper));
		printf("Enter next set of limits:");
		scanf("%d %d",&lower,&upper);
	}
	printf("Done.\n");
	return 0;
}
long dorun(int lower,int upper)
{
	int a=0;
	long c=0;
	for (a=lower;a&lt;=upper;a++)
	{
		c+=a*a;
	}
	return c;
}


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

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

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

5-1.c

#include<stdio.h>
const int n60 = 60;
int main(void)
{
	int i_input, i_min, i_hour;
	printf("Change min into hour+min.\n");
	printf("Input <=0 to stop.\n");
	printf("Input a minute:");
	scanf("%d", &i_input);
	while(i_input > 0)
	{
		i_hour = i_input / n60;
		i_min = i_input % n60;
		printf("%d min is %d hour(s) and %d i_minute(s).\n", i_input, i_hour, i_min);
		printf("Input a minute:");
		scanf("%d", &i_input);
	}
	printf("Done!");
	return 0;
}

5-2.c

#include<stdio.h>
int main(void)
{
	int i, num;
	printf("Please input an int:");
	scanf("%d", &num);
	i = 0;
	while(i++ < 11)
	{
		printf("%d ", num + i-1);
	}
	printf("\nDone!\n");
	return 0;
}

5-3.c

#include<stdio.h>
const int n7 = 7;
int main(void)
{
	int i_input, i_min, i_hour;
	printf("Change days into weeks+days.\n");
	printf("Input <=0 to stop.\n");
	printf("Input a days:");
	scanf("%d", &i_input);
	while(i_input > 0)
	{
		i_hour = i_input / n7;
		i_min = i_input % n7;
		printf("%d day is %d week(s) and %d day(s).\n", i_input, i_hour, i_min);
		printf("Input a day:");
		scanf("%d", &i_input);
	}
	printf("Done!");
	return 0;
}

5-5.c

#include<stdio.h>
int main(void)
{
	int i, i_num=0, towhere;
	printf("Input an int:");
	scanf("%d", &towhere);
	printf("How much is 1+2+3+...+%d?.\n", towhere);
	i = 0;
	while(i++ < towhere)
	{
		i_num = i_num+i;
	}
	printf("Oh, it's %d.\n", i_num);
	return 0;
}

5-6.c

#include<stdio.h>
int main(void)
{
	int i, i_num=0, towhere;
	printf("Input an int:");
	scanf("%d", &towhere);
	printf("How much is 1*1+2*2+...+%d*%d?\n", towhere, towhere);
	i = 0;
	while(i++ < towhere)
	{
		i_num = i_num+i*i;
	}
	printf("Oh, it's %d.\n", i_num);
	return 0;
}

5-7.c

#include<stdio.h>
float sqr(float num);
int main(void)
{
	float f_num;
	printf("Input a float:");
	scanf("%f", &f_num);
	printf("%f * %f is %f",f_num, f_num, sqr(f_num));
	return 0;
}
float sqr(float num)
{
	return num*num;
}

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

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