第八章 编程练习(未完……)

版权声明:本文为博主原创文章,未经博主允许不得转载! https://blog.csdn.net/weixin_42839965/article/details/89790445

下面的一些程序要求输入以EOF终止。如果你的操作系统很难或根本无法使用重定向,
请使用一些其他的测试来终止输入,如读到&字符时停止。
1、编写一个程序,统计在读到文件结尾之前读到的字符数。
编译时,换行开头Ctrl+Z,读取文件的EOF

//编译时,换行开头Ctrl+Z,读取文件的EOF,vs2015
#include <stdio.h>
int main(void)
{
	int ch;
	int count=0;

	printf("请输入字符:\n");
	while ((ch = getchar()) != EOF)
		count++;

	printf("一共输入了%d个字符!\n",count);
	return 0;
}

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

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

#include<stdio.h>
#include<ctype.h>
int main(void)
{
	char ch;
	int count1 = 0, count2 = 0;//count1,count2分别表示小写、大写字母的个数

	printf("请输入字符流:");
	while ((ch = getchar()) != EOF)
	{
		if (islower(ch))
			count1++;
		if (isupper(ch))
			count2++;
	}
	printf("小写字母是%d个,大写字母是%d个\n", count1, count2);
	return 0;
}

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

#include<stdio.h>
#include<stdbool.h>
#include<ctype.h>
int main(void)
{
	bool danci = false;
	char ch;
	float count1 = 0, count2 = 0;//count1,count2分别代表字母数、单词数

	printf("请输入单词:");
	while ((ch = getchar()) != EOF)
	{
		if (isalpha(ch))
			count1++;
		if (isalpha(ch) && !danci)
		{
			count2++;
			danci = true;
		}
		if (isspace(ch))
			danci = false;
	}
	printf("单词数是%.2lf,字母数是%.2lf\n", count2, count1);
	printf("平均单词字母数是%.2lf\n", count1 / count2);
	return 0;
}

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

#include<stdio.h>
int main(void)
{
	int high = 100;
	int low = 1;
	int guess=(high+low)/2;
	char response;

	printf("Pick an integer from 1 to 100.I will try to guess");
	printf("it.\nRespond with a y if my guess is right,with");
	printf("\nan h if it is high,with an l if it is low.\n");
	printf("Uh……is your number %d?\n",guess);
	while ((response = getchar()) != 'y')
	{
		if (response == '\n')
			continue;//if语句好像不用也可以????
			switch (response)
			{
			case 'l':
				low = guess +1;
				break;
			case 'h':
				high = guess - 1;
				break;
			default:
				printf("Option error!\n");
			}
		guess = (high + low) / 2;
		printf("Well,then,is it %d?\n", guess);
		printf("Please re-enter l/h or y:");
	}
	printf("I knew I coulde do it!\n");
	return 0;
}

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

#include<stdio.h>
char get_first(void);
int main(void)
{
	char choice;
	printf("Input character:");
	choice = get_first();
	printf("The first non-blank character is:%c\n", choice);
	printf("Done!\n");
	return 0;
}
char get_first(void)
{
	char ch;
	while ((ch = getchar())==' ')
		continue;
	return ch;
}

7、用字符代替数字标记菜单的选项,用q代替5作为结束输入的标记。
修改第7章编程练习题8,修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。
使用switch完成工资等级选择。运行程序后,显示菜单应该类似这样:


Enter the number corresponding to the desired pay rate or action:
1)$8.75/hr 2)$9.33/hr
3)$10.00/hr 4)$11.20/hr
5)quit


如果选择1~4其中的一个数字,程序应询问用户工作的小时数。程序要通过循环运行,
除非用户输入q。如果输入1~4之外的数字,程序应提醒用户输入正确的选项,然后
再重复显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率。

#include<stdio.h>
char get_choice(void);
void menu(void);
float get_number(void);
char get_first(void);
int main(void)
{
	float first, second;
	char op,choice;
	float result;

	menu();
	//while ((choice = getchar()) != 'q')
	while ((choice = get_choice()) != 'q')
	{
		printf("Enter first number:");
		first = get_number();
		printf("Enter second number:");
		second = get_number();
		switch (choice)
		{
		case 'a':
			result = first + second;
			op = '+';
			break;
		case 's':
			result = first - second;
			op = '-';
			break;
		case 'm':
			result = first * second;
			op = '*';
			break;
		case 'd':
			while (second == 0)
			{
				printf("Enter a number other than 0:");
				second = get_number();
			}
			result = first / second;
			op = '/';
			break;
		default:
			printf("Program error!\n");
			//printf("Re-enter the operation of your choice(q to quit):");
			//choice = getchar();
			//continue;
			break;
		}
		printf("%.2lf%c%.2lf=%.2lf\n", first, op, second, result);
		menu();
		getchar();//还是不太懂为什么要加这条语句
	}

	printf("Done!\n");
	return 0;
}
void menu(void)
{
	printf("***********************************\n");
	printf("Enter the operation of your choice:\n");
	printf("a.add		s.subtract\n");
	printf("m.multiply	d.divide\n");
	printf("q.quit\n");
	printf("***********************************\n");
}
char get_choice(void)
{
	int ch;
	printf("Enter the operation of your choice(q to quit):");
	ch = get_first();
	//while ((ch != 'a' || ch != 's' || ch != 'm' || ch != 'd') && ch != 'q')
	//因为判断条件错误了,所以一直没弄出来
	while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')

	{
		printf("Re-enter the operation of your choice(q to quit):");
		ch = get_first();
	}
	return ch;
}
char get_first(void)
{
	char ch;
	ch = getchar();
	while (getchar() != '\n')
		continue;
	return ch;
}

float get_number(void)
{
	float index;
	char ch;
	while (scanf_s("%f", &index) != 1)
	{
		while ((ch = getchar()) != '\n')
			putchar(ch);
		printf(" is not a number.\n");
		printf("Please the a number, such as 2.5,-1.78E8, or 3:");
	}
	return index;
}

猜你喜欢

转载自blog.csdn.net/weixin_42839965/article/details/89790445