CPrimerPlus学习(八):编程练习

//打开一个文件并显示该文件
#include <stdio.h>
#include <stdlib.h>       // 为了使用exit()

int main()
{
	int ch;
	FILE * fp;
	char fname[50];       // 储存文件名
	
	printf("Enter the name of the file: ");
	scanf("%s", fname);
	
	fp = fopen(fname, "r");   // 打开待读取文件
	
	if (fp == NULL)       // 如果失败
	{
		printf("Failed to open file. Bye\n");
		exit(1);         // 退出程序
	}
	
	// getc(fp)从打开的文件中获取一个字符
	while ((ch = getc(fp)) != EOF)
		putchar(ch);
		
	fclose(fp);          // 关闭文件
	
	return 0;
}

EOF

	//EOF在windows系统往往通过输入 ctrl+z来表达
	//但是仅仅这个这个是不够的,在此之前输入的前一个字符必须是换行符
	//如果没有换行符,输入ctrl+z后并不会识别为EOF

编程练习

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

后面的题遇到了很多堆栈错误 暂未解决
编程练习答案参考

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

#include <stdio.h> 
#include <ctype.h>

int main(void) 
{ 
	int ch;  
	int n = 0;   

	while ((ch = getchar()) != EOF)
	{
		if (isalnum(ch))
			n++;
	}
	printf("%d\n", n);   
	
	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 < ' ')
			switch (ch)
			{
			case '\n':
				printf("\\n");
				break;
			case '\t':
				printf("\\t");
				break;
			default:
				printf("^%c:%d", ch + 64, ch);
				break;
			}
		else
			printf("%c:%d ", ch, ch);
		i++;

		if (i % 10 == 0)
			putchar('\n');
	}

	return 0;
}

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

# include <stdio.h>
# include <ctype.h>

int main(void)
{
	int ch;
	int i = 0,j = 0;

	while ((ch = getchar()) != EOF)
	{
		if (isupper(ch))
			i++;
		else if (islower(ch))
			j++;
	}
	printf("%d %d\n", i, j);

	return 0;
}

/*
abcd123AB
^Z
2 4
*/

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

# include <stdio.h>
# include <ctype.h>

int main(void)
{
	int ch;
	float i = 0;
	float total = 0;
	float num = 0;

	while ((ch = getchar()) != EOF)
	{
		if(isalpha(ch))
		{
			i = i + 1;
		}
		else
		{
			num = num + 1;
			printf("%.2f\n", i);
			total = total + i;
			i = 0;
		}
	}
	printf("words:%.2f total:%.2f average:%.2f\n",num,total,total / num);

	return 0;
}

/*
nothing compares to you
7.00
8.00
2.00
3.00
^Z
words:4.00 total:20.00 average:5.00
*/

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

# include <stdio.h>
# include <ctype.h>

int main(void)
{
	int guess = 50;
	int upper = 100;
	int lower = 0;
	char ch;

	printf("Pick an integer from 1 to 100. I will try to guess it.\n");
	printf("Let's begin!\n");
	printf("Uh...is your number %d?(y/n)\n", guess);

	while ((ch = getchar()))		
	{
		if (!isalpha(ch))
			continue;		

		if (ch == 'y')
			break;			

		printf("bigger or smaller?(b/s): ");
		while ((ch = getchar()))
		{
			if (!isalpha(ch))
				continue;	

			switch (ch)
			{
			case 'b':	
				lower = guess;
				guess = guess + ((upper - lower) / 2);
				break;
			case 's':	
				upper = guess;
				guess = guess - ((upper - lower) / 2);
				break;
			default:	
				printf("bigger or smaller?(b/s): ");
				continue;
			}
			break;			
		}
		printf("is it %d?(y/n)\n", guess);
	}
	printf("OK!\n");

	return 0;
}

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

# include <stdio.h>
# include <ctype.h>

char get_first(void);

int main(void)
{
	char ch;

	while ((ch = get_first()) != EOF)
		putchar(ch);

	return 0;
}

char get_first(void)
{
	int ch;

	while (isblank(ch = getchar()));

	while (getchar() != '\n');

	return ch;
}

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

//堆栈问题 ‘q’ 为解决
#include <stdio.h>

#define BT 40
#define OT 1.5
#define T1 0.15
#define T2 0.2
#define T3 0.25
#define A1 300
#define A2 150
#define BW1 8.75
#define BW2 9.33
#define BW3 10.00
#define BW4 11.20

void menu();

int main(void)
{
	char ch;
	float t;
	float BW;
	float bw, tax, net;

	menu();

	printf("Your choice:");

	scanf("%s",&ch);

	while (ch != 'q') //输入q发生ch堆栈错误 暂未解决
	{
		if (ch > 96 && ch < 101)
		{
			switch (ch)
			{
			case 97:
				BW = BW1;
				break;
			case 98:
				BW = BW2;
				break;
			case 99:
				BW = BW3;
				break;
			case 100:
				BW = BW4;
				break;
			}

			printf("please enter your hours:");
			scanf("%f", &t);

			if (t > 0)
			{
				if (t <= BT)
					bw = t * BW;
				else
					bw = BT * BW + (t - BT) * OT * BW;

				if (bw < A1)
					tax = bw * T1;
				else if (bw > A1  && bw <= (A1 + A2))
					tax = A1 * T1 + (bw - A1) * T2;
				else
					tax = A1 * T1 + A2 * T2 + (bw - A1 - A2) * T3;

				net = bw - tax;

				printf("Wage is %.2f Taxex is %.2f Net is %.2f\n", bw, tax, net);
			}
			else
			{
				printf("Your hours too short\n");
				printf("Please re-enter!\n");
				/*menu();
				printf("Your choice:");
				scanf("%s", &ch);*/
			}
		}
		else
		{
			printf("Please re-enter!\n");
		}
		menu();
		printf("Your choice:");
		scanf("%s", &ch);
	}

	return 0;
}

void menu()
{
	printf("Enter the number corresponding to the desired pay rate or action:\n");
	printf("a) $8.75  / hr\n");
	printf("b) $9.33  / hr\n");
	printf("c) $10.00 / hr\n");
	printf("d) $11.20 / hr\n");
	printf("q) quit\n");
}


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.

//堆栈问题 ‘q’ 为解决
#include <stdio.h>
#include <ctype.h>

void menu();

int main(void)
{
	char ch;
	float a, b;
	float results;

	menu();
	printf("your choice:");
	scanf("%s", &ch);

	do 
	{
		printf("Enter first number: ");
		scanf("%f", &a);
		printf("Enter second number: ");
		scanf("%f", &b);

		if (b == 0)
		{
			printf("Please re-enter!\n");
			printf("Enter second number: ");
			scanf("%f", &b);
		}

		switch(ch)
		{
		case 'a':
			results = a + b;
			break;
		case 'b':
			results = a - b;
			break;
		case 'c':
			results = a * b;
			break;
		case 'd':
			results = a / b;
			break;
		}
		printf("results:%.2f\n", results);

		menu();
		printf("your choice:");
		scanf("%s", &ch);

	} while (ch != 'q');
	printf("Bye!\n");

	return 0;
}

void menu()
{
	printf("Enter the operation of your choice:\n");
	printf("a) add\n");
	printf("b) subtract\n");
	printf("c) multiply\n");
	printf("d) divide\n");
	printf("q) quit\n");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很抱歉,我作为AI语言模型无法提供cprimerplus第六版答案pdf的下载链接或资源。建议您通过正规途径购买或借阅相关书籍,以合法合规的方式获取答案和学习资料。同时,我们也提醒您注意版权保护,避免侵权行为。谢谢理解。 ### 回答2: 对于C++初学者来说,理解和掌握语言的基础知识是非常重要的。而对于一本好的教材来说,它的教学内容必须得清晰易懂,有系统的组织和解释,配合良好的例子方能达到良好的教学效果。 “C Primer Plus”就是一本讲解C++语言基础知识的书,特别适合初学者使用。在书中,作者清晰地介绍了C++的各种语法特性、数据类型、函数和指针等重要概念,并且通过大量的例子来帮助读者更好地理解和掌握这些知识点。 然而,在学习过程中,很多同学会遇到一些棘手的问题。比如说,遇到一些难以理解的概念或者概述,或者需要更多的习题来帮助加深对知识点的理解。此时,一份好的 “C Primer Plus第六版答案PDF”资料会起到很大的帮助作用。 这份资料可以提供一些示例问题的解答、习题的详细答案及解析,有助于同学们更好地掌握和应用所学内容。同时,这份答案也可以帮助同学们在学习过程中迅速发现和解决问题,节约时间和精力。 使用这份资料的同时,我们也要明确,这份答案的主要作用是辅助学习,帮助理解和掌握重点知识。因此,在使用这份答案的过程中,我们也应该注意加强自己的思考和理解能力,避免过度依赖答案,而忽略了对知识点本身的深入理解。 总而言之,“C Primer Plus第六版答案PDF”为初学者提供了一个理解和掌握C++基础知识的辅助工具。使用这份答案可以更好地理解和应用所学知识,同时也需要注意加强自己的思考和理解能力。 ### 回答3: cprimerplus第六版答案pdf是一份对于C语言初学者非常有价值的参考材料,该文档收集了《c primer plus》这本书的所有习题答案,能够帮助读者更好地学习和理解本书中介绍的C语言知识。 该答案pdf包含了所有章节的习题答案,涵盖了从基础语法到高级应用的各种知识点。通过仔细地分析每道题的解答过程,读者可以更深入地理解C语言的语法和思维模式,从而更直观地掌握这门编程语言。 此外,cprimerplus第六版答案pdf还提供了一些非常实用的技巧和方法,帮助读者更好地应用C语言进行编程。例如,文档中提供了关于指针、数组、结构体等语言特性的详细解释,帮助读者了解这些知识点在实际编程中的应用场景。 对于初学者而言,cprimerplus第六版答案pdf可以作为一本非常权威的参考书籍,为C语言学习者提供了非常全面和详细的答案解析。通过认真阅读和实践,读者可以快速掌握C语言,进而深入应用各种高级的编程技术。 综合来看,cprimerplus第六版答案pdf是一份非常优质的学习资料,不仅能够帮助初学者更好地理解C语言,也能为有一定编程基础的人提供非常有价值的参考。如果你正在学习C语言,那么这份答案pdf是一份你不能错过的资料。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值