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

1 Response

  1. Kiana 2017 年 3 月 16 日 / 上午 1:21

    This info is the cat’s paasjma!

发表评论

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

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