2019.1.30《C Primer Plus》拖了一天写完的第八章编程练习答案

第八章的文件重定向输入真是难学……
用之前玩的Ubuntu16.04虚拟机捣鼓捣鼓,才勉强知道是咋回事
昨天看完了第八章除了重定向以外的部分
今天拖了一天,把第八章课后题写了……

8.11.1

设计一个程序,统计在读到文件结尾之前读取的字符数

#include<stdio.h>
int main(void)
{
	int count=0,ch;
	
	while((ch=getchar())!='&'){
		count++;
	}
	printf("%d",count);
	
	return 0;
}

还在想要不要用isspace()函数,不过空白字符也是字符吧?

8.11.2

编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。程序要打印每个输入的字符及其相应的ASCII十进制值。
注意,在ASCII序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。如果非打印字符是换行符制表符,则分别打印\n或\t。否则,使用控制字符表示法。例如,ASCII的1是Ctrl+A,可显示为^A。注意,A的ASCII值是Ctrl+A的值加上64。其他非打印字符也有类似的关系。除每次遇到换行符打印新的一行之外,每行打印10对值。
(注意:不同的操作系统其控制字符可能不同。)

#include<stdio.h>
int main(void)
{
	int ch,i=0;
	while((ch=getchar())!=EOF){
		if(ch=='\n'){
			putchar('\\');
			putchar('n');
			printf(" %d\n",ch);//遇到换行符需要打印新的一行,所以i要清零 
			i=0;
		}
		else if(ch=='\t'){
			putchar('\\');
			putchar('t');
			printf(" %d\t",ch);
		}
		else if(ch<' '){
			putchar('^');
			putchar(ch+64);//划重点,其他非打印字符也有类似的关系 
			printf("%d\t",ch);
		}
		else{
			printf("%c %d\t",ch,ch);
		}
		if(++i==8){
			putchar('\n');
			i=0;
		}
	}
	return 0;
}

这题目是真难懂……

8.11.3

编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。该程序要报告输入中的大写字母和小写字母的个数。假设大小写字母数值是连续的。或者使用ctype.h库中合适的分类函数更方便

#include<stdio.h>
int main(void)
{
	int ch,up=0,low=0;
	while((ch=getchar())!=EOF&&ch!='#'){
		if('A'<=ch&&ch<='Z')
			up++;
		else if('a'<=ch&&ch<='z')
			low++;
	}
	printf("大写字母:%d\n小写字母:%d\n",up,low);
	
	return 0;
}

8.11.4

编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告平均每个单词的字母数。不要把空白统计为单词的字母。实际上,标点符号也不应该统计,但是现在暂时不同考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()函数)

#include<stdio.h>
#include<ctype.h>
int main(void)
{
	int ch,count=0,words=0,inword=0;
	double aver;
	while((ch=getchar())!=EOF){
		if(!isspace(ch)&&!inword){
			inword=1;
			words++;
		}
		if(isalpha(ch))
			count++;
		if(isspace(ch)&&inword)
			inword=0;
	}
	aver=(double)count/(double)words;
	printf("%.2lf\n",aver);
	
	return 0;
}

写到这题,回去翻第七章了?
我是不是太菜了?

8.11.5

修改程序清单8.4的猜数字程序,使用更智能的猜测策略。例如,程序最初猜50,询问用户是猜大了、猜小了还是猜对了。如果猜小了,那么下一次猜测的值应是50和100中值,也就是75。如果这次猜大了,那么下一次猜测的值应是50和75的中值,等等。使用二分查找(binary search)策略,如果用户没有欺骗程序,那么程序很快就会猜到正确的答案

#include<stdio.h>
char get_first(void);
int main(void)
{
	int guess=50,up_lim=100,low_lim=1,ch;
	
	printf("Pick an integer from 1 ti 100.I will try to guess ");
	printf("it.\nRespon with a y if my guess is right an with");
	printf("\na b if it is too big,or enter s if it too small.\n");
	printf("Uh...is your number %d?\n",guess);
	while((ch=get_first())!='y'){
		if(ch=='b'){
			up_lim=guess;
			guess=(low_lim+guess)/2;
			printf("Well,then,is it %d?\n",guess);
		}
		else if(ch=='s'){
			low_lim=guess;
			guess=(up_lim+guess)/2;
			printf("Well,then,is it %d?\n",guess);
		}
		else
			printf("Please enter again.\n");
	}
	printf("I know I can do it!\n");
	
	return 0;
}
char get_first(void)
{
	int ch;
	ch=getchar();
	while(getchar()!='\n')
		continue;
	return ch;
}

不知道会不会有隐式转换的问题……
没进行多次测试……
有问题一定要在下面留言啊

8.11.6

修改程序清单8.8中的get_first()函数,让该函数返回读取的第1个非空白字符,并在一个简单的程序中测试

char get_firstnotspace(void)
{
	int ch;
	while((ch=getchar())==' '||ch=='\t'||ch=='\n')
		continue;
	while(getchar()!='\n')
		continue;
	return ch;
}

就是修改出来的自定义函数,不能单独运行……
不过这个函数挺有用的,应该记一下……

8.11.7

修改第7章的编程练习8,用字符代替数字标记菜单的选项。用q代替5作为结束输入的标记

#include<stdio.h>
#define overtime 40//超过40h为加班时间
#define ratio 1.5//加班工时为1.5倍 
#define tax1 0.15//第一部分税率 
#define tax2 0.2//第二部分税率 
#define tax3 0.25//第三部分税率 
#define break1 300//税率第一个分界点(收入)
#define break2 450//税率第二个分界点(收入)
char get_firstnotspace(void); 
int main(void)
{
	float salary;
	int a;
	printf("************************************************************************************\n"
			"Enter the number corresponding to the desired pay rate or action:\n"
			"a) $8.75/hr\t\tb) $9.33/hr\nc) $10.00/hr\t\td) $11.20/hr\nq) quit\n"
			"************************************************************************************\n");
	while((a=get_firstnotspace())!='q'){
		switch(a)
		{
			case 'a':
				salary=8.75;
				break;
			case 'b':
				salary=9.33;
				break;
			case 'c':
				salary=10.00;
				break;
			case 'd':
				salary=11.20;
				break;
			default:
				printf("Please enter a correct option.\n");
		
		}
		if(a=='a'||a=='b'||a=='c'||a=='d'){
			float time,origin,total,tax,after_tax;
			printf("Please enter your working hours.\n");
			scanf("%f",&time);
			origin=time;
			
			if(time>overtime)
			time=(time-overtime)*ratio+overtime;
			
			total=time*salary;//计算税前薪水 
			if(total>=0&&total<=break1){
				tax=total*tax1;
				after_tax=total-tax;
			}
			else if(total>break1&&total<=break2){
				tax=break1*tax1+(total-break1)*tax2;
				after_tax=total- tax;
			}
			else {
				tax=break1*tax1+(break2-break1)*tax2+(total-break2)*tax3;
				after_tax=total-tax;
			}
			printf("Hours\tTotal wages\tTax\tAfter-tax income\n"
					"%.2f\t%.2f\t\t%.2f\t%.2f\n\n\n",origin,total,tax,after_tax);
//					"\nEnter the number corresponding to the desired pay……(Enter '5' to quit)\n"
				printf("************************************************************************************\n"
				"Enter the number corresponding to the desired pay rate or action:\n"
				"a) $8.75/hr\t\tb) $9.33/hr\nc) $10.00/hr\t\td) $11.20/hr\nq) quit\n"
				"************************************************************************************\n");
		}
//		getchar();//读取在摁回车时产生的\n,若无这个,则会被循环读取并赋值给a... 
	}
	return 0;
}

char get_firstnotspace(void)
{
	int ch;
	while((ch=getchar())==' '||ch=='\t'||ch=='\n')
		continue;
	while(getchar()!='\n')
		continue;
	return ch;
}

用上新函数之后,果然好用多了……

8.11.8

编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。
获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。
该程序只接受菜单提供的选项。
程序使用float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。
进行除法运算时,如果用户输入 0 作为第 2 个数(除数),程序应提示用户重新输入一个新值。
该程序的一个运行示例如下:
(加粗标记为用户输入)
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
a
Enter first number: 22.4//书上这块排版有问题
Enter second number: one
one is not a number.
Please enter a number, such as 2.5, -1.78E8, or 3: 1
22.4 + 1 = 23.4
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
d
Enter first number:18.4
Enter second number: 0
Enter a number other than 0:0.2
18.4 / 0.2 = 92
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
q
Bye.

#include<stdio.h>
double get_num(void);
char get_first(void);
int main(void)
{
	double first,second;
	int ch;
	printf("Enter the operation of your choice:\n"
			"a.add\t\ts.subtract\nm.multiply\td.divide\nq.quit\n");
	while((ch=get_first())!='q'){
		 if(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd')
        {
            printf("You must input only a ,s, m, d or q:");
            continue;
        }
        printf("Enter the first number:");
        first=get_num();
        printf("Enter the second number:");
        second=get_num();
	    if(ch=='d'){
	        while(second==0){
	            printf("Enter a number other than 0:");
	            second=get_num();
	        }
	    }
	    switch(ch){
	    	case 'a':
	    		printf("%lf + %lf = %lf\n",first,second,first+second);//不知道要保留几位小数… 
	    		break;
	    	case 's':
	    		printf("%lf - %lf = %lf\n",first,second,first-second);
	    		break;
	    	case 'm':
	    		printf("%lf * %lf = %lf\n",first,second,first*second);
	    		break;
	    	case 'd':
	    		printf("%lf / %lf = %lf\n",first,second,first/second);
	    		break;
		}
		printf("Enter the operation of your choice:\n"
			"a.add\t\ts.subtract\nm.multiply\td.divide\nq.quit\n");
	}
	printf("Bye.");
	return 0;
}

double get_num(void)
{
	double input;
	char ch;
	while(scanf("%lf",&input)!=1){
		while((ch=getchar())!='\n')
			putchar(ch);
		printf(" is not a number.\nPlease enter anumber,"
				"such as 2.5,-1.78E8,or 3:");
	}
	return input;
}

char get_first(void)
{
	int ch;
	while((ch=getchar())==' '||ch=='\t'||ch=='\n')
		continue;
	while(getchar()!='\n')
		continue;
	return ch;
}

模块化之后压力不算很大……感觉还行……
看着也不乱……

希望这个假期能学完指针和一些简单的常用算法……
明天开第九章
ε=ε=ε=( ̄▽ ̄)

2019年1月30日23点28分

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值