C Primer Plus 第六版---编程练习8

下面的一些程序要求输入以EOF终止。如果你的操作系统很难或根本无法使用重定向,请使用一些其他的测试来终止输入,如读到&字符时停止。

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

include<stdio.h>
int main(void)
{
	int i = 0;
	while (getchar() != EOF)
		i++;
	printf("字符数量为%d\n", i);
	return 0;
}

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;
	int i = 0;
	while ((ch = getchar()) != EOF)
	{
		if (ch > ' ')
			printf("%c\t", ch);
		else if (ch == '\n')
			printf("\\n\t");
		else if (ch == '\t')
			printf("\\t\t");
		else
			printf("^%c\t", ch + 64);
		printf("%d\t", ch);
		i++;
		if (ch == '\n' || i == 10)
		{
			printf("\n");
			i = 0;
		}
	}
	return 0;
}

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

#include<stdio.h>
int main(void)
{
	char ch;
	int uppercase = 0;
	int lowercase = 0;

	while ((ch = getchar()) != EOF)
	{
		if (ch >= 'A' && ch <= 'Z')
			uppercase++;
		if (ch >= 'a' && ch <= 'z')
			lowercase++;
	}
	printf("uppercase letters 是%d,lowercase letters 是%d\n", uppercase, lowercase);
	return 0;
}

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

#include<stdio.h>
int main(void)
{
	char ch;
	int word = 0;
	int character = 0;
	int space = 1;

	printf("输入一些单词\n");
	while ((ch = getchar()) != EOF)
	{
		if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			character++;
			if (space)
			{
				word++;
				space = 0;
			}
		}
		else
			space = 1;
	}
	printf("the average characters of %d words is %d.\n", word, character / word);
	return 0;
}

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

#include<stdio.h>
int main(void)
{
	char ch;
	int low = 1;
	int high = 100;
	int guess = (low + high) / 2;
	printf("输入一个在1~100范围内的整数,我会尝试猜一下。\n"
		"如果我猜对了,请回我一个\"y\""
		"如果我猜高回我一个\"y\""
		"如果我猜低回我一个\"l\"");
	printf("嗯~你输入的数字是 %d?\n", guess);
	while ((ch = getchar()) != 'y')
	{
		if (ch == 'h')
		{
			high = guess;
			guess = (low + high) / 2;
		}
		else if (ch == 'l')
		{
			low = guess;
			guess = (low + high) / 2;
		}
		else if (ch == '\n')
			continue;
		else
		{
			printf("请输入 y, h 或 l");
			continue;
		}
		printf("输入一个在1~100范围内的整数,我会尝试猜一下。\n"
			"如果我猜对了,请回我一个\"y\""
			"如果我猜高回我一个\"y\""
			"如果我猜低回我一个\"l\"");
		printf("嗯~你输入的数字是 %d?\n", guess);
	}
	printf("我知道我能做到!\n");
		return 0;
}

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

#include<stdio.h>
char get_first(void);
int main(void)
{
	char ch;
	printf("输入一些词以测试\n");
	ch = get_first();
	printf("%c\n", ch);
	return 0;
}
char get_first(void)
{
	char ch;
	ch = getchar();
	while (cj == ' ' || ch == '\t' || ch == '\n')
		ch = getchar();
	while (getchar() != '\n')
		continue;
	return ch;
}

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

#include<stdio.h>
#define TIME 40.0
#define PAY1 8.75
#define PAY2 9.33
#define PAY3 10.00
#define PAY4 11.20
#define ADD 1.5
#define RATE1 0.15
#define RATE2 0.2
#define RATE3 0.25
#define BREAK1 300
#define BREAK2 450
#define TAX1 (BREAK1 * RATE1)
#define TAX2 TAX1+(RATE2 * (BREAK2 - BREAK1))

int main()
{
	float time, wage, tax, income, pay;
	char ch;
	printf("'*' * 30\n");
	printf("enter the character corresponding to the desired pay rate or action:\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("'*' * 30\n");
	while ((ch = getchar()) != 'q')
	{
		if (ch < 'a' || ch > 'd')
		{
			printf("enter the correct option\n");
			printf("'*' * 30\n");
			printf("enter the character corresponding to the desired pay rate or action:\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("'*' * 30\n");
			continue;
		}
		switch (ch)
		{
		case 'a':
			pay = PAY1;
			break;
		case 'b':
			pay = PAY2;
			break;
		case 'c':
			pay = PAY3;
			break;
		case 'd':
			pay = PAY4;
			break;
		}
		printf("enter the working time(hour)\n");
		scanf("%f", &time);
		while (getchar() != '\n')
			continue;
		if (time <= TIME)
			wage = pay * time;
		else
			wage = pay * (TIME + ADD * (time - TIME));
		if (wage <= BREAK1)
			tax = RATE1 * wage;
		else if (wage > BREAK1 && wage < BREAK2)
			tax = TAX1 + RATE2 * (wage - BREAK1);
		else
			tax = TAX2 + RATE3 * (wage - BREAK2);
		income = wage - tax;
		printf("The wage is $%.2f, tax is $%.2f, net income is $%.2f\n", wage, tax, income);
		printf("\nEnter the character corresponding to the desired pay rate or action:\n");
	}
	return 0;
}

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 an 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.

a. add        s. subtract

m. multiply     d. divide

q. quit

a

Enter first number: 22 .4

Enter second number: one

one is not an 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>
float get_float(void);
void option(void);
char choice(void);
char get_first(void);
int main(void)
{
        float first_number, second_number;
        char ch;

        option();
        while ((ch = choice()) != 'q')
        {
                printf("Enter first number: ");
                first_number = get_float();
                printf("Enter second number: ");
                second_number = get_float();
                switch (ch)
                {
                        case 'a':
                                printf("%g + %g = %g\n", first_number, second_number, first_number + second_number);
                                break;
                        case 's':
                                printf("%g - %g = %g\n", first_number, second_number, first_number - second_number);
                                break;
                        case 'm':
                                printf("%g * %g = %g\n", first_number, second_number, first_number * second_number);
                                break;
                        case 'd':
                                while (second_number == 0)
                                {
                                        printf("Enter a number other than 0: ");
                                        second_number = get_float();
                                }
                                printf("%g / %g = %g\n", first_number, second_number, first_number / second_number);
                                break;
                }
                option();
        }
        printf("Bye.\n");

        return 0;
}

void option(void)
{
        printf("Enter the operation of your choice:\n");
        printf("a. add             s. subtract\n");
        printf("m. multiply        d. divide\n");
        printf("q. quit\n");
}

float get_float(void)
{
        float number;
        char ch;

        while (scanf("%f", &number) != 1)
        {
                while ((ch = getchar()) != '\n')
                        putchar(ch);
                printf(" is not an number.\n"
                                "Please enter a number, such as 2.5, -1.78E8, or 3: ");
        }

        return number;
}

char choice(void)
{
        char ch;

        ch = get_first();
        while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
        {
                printf("please enter a, s, m, d or q\n");
                ch = get_first();
        }

        return ch;
}
char get_first(void)
{
        char ch;

        ch = getchar();
        while (ch == ' ' || ch == '\n' || ch == '\t')
                ch = getchar();
        while (getchar() != '\n')
                continue;

        return ch;
}

猜你喜欢

转载自blog.csdn.net/fangshuo_light/article/details/123782043