C primer plus 8.11

C primer plus 8.11

8.11.1

#include<stdio.h>
int main(void)
{
	char ch;//用户输入 
	int i;//程序输出
	
	while((ch=getchar())!=EOF)
	{
		i++;
	}
	printf("%d",i);
	
	return 0; 
}

8.11.2

#include<stdio.h>
int main(void)
{
	char ch;//用户输入 
	int i=0;//数值数
	int k;//换行判定 
		
	while((ch=getchar())!=EOF)
	{
		if(ch=='\n')
		{
			printf("\\n");
			printf("%d ",ch);
			i++;
		}
		else if(ch=='\t')
		{
			printf("\\t");
			printf("%d ",ch);
			i++;
		}
		else if(ch<65)
		{
			printf("^%c",ch+64);
			printf("%d ",ch);
			i++;
		}	
		else
		{
			printf("%c",ch);
			printf("%d ",ch);
			i++;
		}
		
		k=i%10;
		if(k==0)
		{
			printf("\n");
		}
		
	}
	return 0;
}

8.11.3

#include<stdio.h>
#include<ctype.h>
int main(void)
{
	char ch;//输入 
	int i;//大写字母
	int j;//小写字母计数
	
	
	while((ch=getchar())!=EOF)
	{
		
		if(isupper(ch)!=0)
		{
			i++;
		}
		if(islower(ch)!=0)
		{
			j++;
		}	
	} 
	printf("大写字母有%d个,小写字母有%d个",i,j);	
	return 0;
}

[外链图片转存失败(img-uxT4NID4-1569394926032)(C:\Users\wyz41\Desktop\3.PNG)]

8.11.4

#include<stdio.h>
#include<ctype.h>
int main(void)
{
	char ch;
	int i=0;//字母计数 
	int j=0;//单词计数 
	double k;
	char space=' ';
	ch=getchar();
	while(ch!='EOF')
	{
		while(ch==space)
		{
			ch=getchar();
		}
		j++;
		while(isalpha(ch))
		{
			i++;
			ch=getchar();
		}		
	}	
	k=i/j;
	printf("平均每个单词的字母数为:%.2f",k);
	return 0;
} 

[外链图片转存失败(img-2k7wqLgD-1569394926033)(C:\Users\wyz41\Desktop\4.PNG)]

8.11.5

/* guess.c -- an inefficient and faulty number-guesser */
#include <stdio.h>
int main(void)
{
    int guess=50;
    char ch;
    int m=100; 
    int n=0;
    
    printf("Pick an integer from 1 to 100. I will try to guess ");
    printf("it.\nRespond with a r if my guess is right and with");
    printf("\nan b if it is big and with");
    printf("\nan s if it is small.\n");
    printf("Uh...is your number %d?\n", guess);
    scanf("%c",&ch);
	getchar();
    while(ch!='r')
    {
    	
    	if(ch=='b')
    	{
    		m=guess;
    		n=n;
		}
		else
		{
			m=m;
			n=guess;
		}
		guess=(m+n)/2;
		printf("Uh...is your number %d?\n", guess);
		scanf("%c",&ch);
		getchar();
	} 
	printf("I knew I could do it!\n");
    return 0;
}

8.11.6

#include<stdio.h>
char get_first(void);
int main(void)
{
	printf("请输入一串字符,我们将返回第一个非空白字符");
	printf("%c",get_first());
	return 0;
}
char get_first(void)
{
    char ch;
    char space=' ';
    while((ch=getchar())==space)
    {
    	
	}
    return ch;
}

8.11.7

#include<stdio.h>
#define A 8.75//薪资等级1
#define B 9.33//薪资等级2 
#define C 10.00//薪资等级3 
#define D 11.20//薪资等级4 

#define b 1.5
#define c1 0.15
#define c2 0.2
#define c3 0.25
int main(void)
{
	char n;//用户选择12345
	float level;//薪资等级 
	float hour;//用户输入值,小时数
	float total_wage;//工资总额
	float taxes;//税金
	float net_income;//小时数
		
	printf("****************************************************************\n");
	printf("请选择您的薪资等级:\n");
	printf("a) $8.75/hr                     b) $9.33/hr                     \n");
	printf("c) $10.00/hr                    d) $11.20/hr                    \n");
	printf("q)quit                                                          \n");
	printf("****************************************************************\n");
	scanf("%c",&n);	
	while(n!='q')
	{
		switch(n)
		{
			case 'a':
				level=A;
				break;
			case 'b':
				level=B;
				break;
			case 'c':
				level=C;
				break;
			case 'd':
				level=D;
				break;		
		}
		
		printf("请输入您一周工作的小时数:"); 
		scanf("%f",&hour);
		if(hour<40)
		{
			total_wage=hour*level;
		}
		else
		{
			total_wage=40*level+(hour-40)*b*level;
		}	
			
		if(total_wage<=300)
		{
			taxes=total_wage*c1;
		}
		else if(total_wage<=450)
		{
			taxes=300*c1+(total_wage-300)*c2;
		}
		else
		{
			taxes=300*c1+150*c2+(total_wage-450)*c3;
		}
		
		net_income=total_wage-taxes; 
		 
		printf("工资总额:%f\n",total_wage);
		printf("税金:%f\n",taxes);
		printf("净收入:%f\n\n\n",net_income);	
		
		printf("****************************************************************\n");
		printf("请选择您的薪资等级:\n");
		printf("a) $8.75/hr                     b) $9.33/hr                     \n");
		printf("c) $10.00/hr                    d) $11.20/hr                    \n");
		printf("q)quit                                                          \n");
		printf("****************************************************************\n");
		scanf("%c",&n);	
				
	}					
	return 0;
}

8.11.8

#include<stdio.h>
int main(void)
{
	char a,s,m,d,q;
	char ch;//用户输入-运算方法 
	float num1;
	float num2;
	float out_put;
	char k;
	printf("Enter the operation of your chioce:\n");
	printf("a.add             s.subtract       \n");
	printf("m.multiply        d.divide         \n");
	printf("q.quit                             \n");
	scanf("%c",&ch);
	getchar();
	while(ch!='q')
	{
		while(ch=='a'||ch=='s'||ch=='m'||ch=='d')
		{
			printf("Enter first number:");
			while(scanf("%f",&num1)!=1)
			{
				getchar();
				printf("one is not an number.\n");
				printf("Please enter a number:");	
			}
			getchar();
			printf("Enter second number:");
			while(scanf("%f",&num2)!=1)
			{
				printf("one is not an number.\n");
				printf("Please enter a number:");	
			}		
			switch(ch)
			{
				case 'a':
					out_put=num1+num2;
					k='+';
					break;
				case 's':
					out_put=num1-num2;
					k='-';
					break;
				case 'm':
					out_put=num1*num2;
					k='*';
					break;
				case 'd':
					while(num2==0)
					{
						printf("Enter a number other than 0:");
							while(scanf("%f",&num2)!=1)
							{
								printf("one is not an number.\n");
								printf("Please enter a number:");
								getchar();	
							}			
					}
					out_put=num1/num2;
					k='/';
					break;
			}
			printf("%f%c%f=%.2f\n",num1,k,num2,out_put);
			
			printf("Enter the operation of your chioce:\n");
			printf("a.add             s.subtract       \n");
			printf("m.multiply        d.divide         \n");
			printf("q.quit                             \n");
			getchar();
			scanf("%c",&ch);
			getchar();						
		}
		while(ch!='q'&&ch!='a'&&ch!='s'&&ch!='m'&&ch!='d')
		{
			printf("It is wrong,please enter a right character:");
			scanf("%c",&ch);
		    getchar();
		}
	}
	printf("bye!");
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值