C++ Primer Plus(第六版)第六章编程练习答案

1.请编写一个程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列)

#include<iostream>
#include<cctype>
using namespace std;

int main() {
	char ch;
	cout << "Please enter string: " << endl;
	while (cin >> ch && ch != '@') {
		if (!isdigit(ch)) {
			if (isupper(ch)) {
				ch = ch + 32;
				cout << ch;
			}
			else if (islower(ch)) {
				ch = ch - 32;
				cout << ch;
			}		
		}
	}
	cout << endl;

	system("pause");
	return 0;
}

2.编写一个程序,最多将10个donation值读入到一个double数组中(如果您愿意,也可以使用模板类array)。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

#include<iostream>
using namespace std;

const int MAX = 10;

int main() {
	double arr[MAX];
	double number;
	double total = 0;
	int i = 0;
	cout << "Please enter a number: ";

	while (i < MAX && cin >> number) {
		arr[i] = number;
		total += number;
		i++;
		if (i<MAX)
			cout << "Please enter a number: ";
	}

	double average = total / i;
	cout << "The average of those numbers is " << average << endl;
	int high = 0;
	for (int j = 0; j < i; j++) {
		if (arr[j] > average)
			high++;
	}
	cout << "The number of higher than average is " << high << endl;

	system("pause");
	return 0;
}

3.编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提供用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
Please enter a c , p , t , or g: q
Please enter a c , p , t , or g: t
A maple is a tree.

#include<iostream>
#include<string>
using namespace std;

int main() {
	char ch;
	string c = "carnivore";
	string p = "pianist";
	string t = "tree";
	string g = "game";

	//cout << "Please enter one of the following choices:\n";
	//cout << "c) " << c << "        " << "p) " << p << "\n";
	//cout << "t) " << t << "        " << "g) " << g << "\n";
	//while (cin >> ch) {
	//	switch (ch) {
	//		case 'c':cout << "A maple is a " << c << endl;
	//				 break;
	//		case 'p':cout << "A maple is a " << p << endl;
	//			     break;
	//		case 't':cout << "A maple is a " << t << endl;
	//			     break;
	//		case 'g':cout << "A maple is a " << g << endl;
	//			     break;
	//		default:cout << "Please enter a c, p, t or g: ";
	//	}
	//}

	cout << "Please enter one of the following choices:\n";
	cout << "c) " << c << "        " << "p) " << p << "\n";
	cout << "t) " << t << "        " << "g) " << g << "\n";
	while (cin >> ch && ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') {
		cout << "Please enter a c, p, t or g: ";
	}
	switch (ch) {
	    case 'c':cout << "A maple is a " << c << endl;
			break;
		case 'p':cout << "A maple is a " << p << endl;
			break;
		case 't':cout << "A maple is a " << t << endl;
			break;
		case 'g':cout << "A maple is a " << g << endl;
			break;
	}

	system("pause");
	return 0;

}

4. 加入Benevolent Order of Programmer后,在BOP大会上,人们便可以通过加入者的真实姓名、头衔或秘密BOP姓名来了解他(她)。请编写一个程序,可以使用真实姓名、头衔、秘密姓名或成员偏好来列出成员。编写该程序时,请使用下面的结构:

/ Benevolent Order of Programmers name structure 
struct bop{ 
char fullname[strsize]; // real name 
char title[strsize];    // job title 
char bopname[strsize];  // secret BOP name 
int preference;         // 0 = fullname , 1 = title , 2 = bopname 
}; 

该程序创建一个由上述结构组成的小型数组,并将其初始化为适当的值。另外,该程序使用一个循环,让用户在下面的选项中进行选择:

a. display by name         b. display by title 
c. display by bopname      d. display by preference 
q. quit 

注意,“display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:

Benevolent Order of Programmers Report 
a. display by name         b. display by title 
c. display by bopname      d. display by preference 
q. quit 
Enter your choice: a 
Wimp Macho 
Raki Rhodes 
Celia Laiter 
Hoppy Hipman 
Pat Hand 
Next choice: d 
Wimp Macho 
Junior Programmer 
MIPS 
Analyst Trainee 
LOOPY 
Next choice: q 
Bye!
#include<iostream>
using namespace std;

const int strsize = 20;
const int human_num = 5;

struct bop {
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;
};

int main() {
	bop human[human_num] = { {"Wimp Macho", "Teacher","Captain America", 0},
							 {"Raki Rhides", "Junior Programmer", "Raytheon", 1},
							 {"Celia Laiter", "Doctor","MIPS",2},
						 	 {"Hoppy Hipman","Analyst Trainee","Black widow", 1},
						 	 {"Pat Hand","Student", "LOOPY", 2} };
	cout << "Benevolent Order of Programmers Report\n";
	cout << "a. display by name     b.display by title\n";
	cout << "c. display by bopname  d. display by preference\n";
	cout << "q. quit\n";

	char ch;
	cout << "Enter your choice: ";
	while (cin >> ch && ch != 'q') {
		switch (ch) {
			case 'a':
			{
				for (int i = 0; i < human_num; i++) {
					cout << human[i].fullname << endl;
				}
				break;
			}
			case 'b':
			{
				for (int i = 0; i < human_num; i++)
					cout << human[i].title << endl;
				break;
			}
			case 'c':
			{
				for (int i = 0; i < human_num; i++)
					cout << human[i].bopname << endl;
				break;
			}
			case 'd':
			{
				for (int i = 0; i < human_num; i++) {
					if (human[i].preference == 0)
						cout << human[i].fullname << endl;
					else if (human[i].preference == 1)
						cout << human[i].title << endl;
					else
						cout << human[i].bopname << endl;
				}
				break;
			}
		}
		cout << "Next choice: ";
	}
	cout << "Bye!" << endl;

	system("pause");
	return 0;
}

5. 在Neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:
5000 tvarps: 不收税
5001~15000 tvarps: 10%
15001~35000 tvarps: 15%
35000 tvarps 以上: 20%
例如,收入为38000 tvarps时,所得税为5000 * 0.00 + 10000 * 0.10 + 20000 * 0.15 + 3000 * 0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。

#include<iostream>
using namespace std;

const float Rate0 = 0;
const float Rate1 = 0.1;
const float Rate2 = 0.15;
const float Rate3 = 0.2;

int main() {
    float income, tax;
	cout << "Please enter your income(enter postive number and not number to quit):";
	while (cin >> income && income > 0)
	{
		if (income < 5000)
			tax = 0;
		else if (income < 15000)
			tax = 5000 * Rate0 + (income - 5000) * Rate1;
		else if (income < 35000)
			tax = 5000 * Rate0 + 10000 * Rate1 + (income - 15000)*Rate2;
		else
			tax = 5000 * Rate0 + 10000 * Rate1 + 20000 * Rate2 + (income - 35000) * Rate3;
		cout << "The tax that you should pay is " << tax << endl;
		cout << "Please enter your income(enter postive number and not number to quit):";
	}

	system("pause");
	return 0;
}

6. 编写一个程序,记录捐助给“维护合法权利团体”的资金。该程序要求用户输入捐献者数目,然后要求用户输入每一个捐献者的姓名和款项。这些信息被储存在一个动态分配的结构数组中。每个结构有两个成员:用来储存姓名的字符数组(或string对象)和用来储存款项的double成员。读取所有的数据后,程序将显示所有捐款超过10000的捐款者的姓名及其捐款数额。该列表前应包含一个标题,指出下面的捐款者是重要捐款人(Grand Patrons)。然后,程序将列出其他的捐款者,该列表要以Patrons开头。如果某种类别没有捐款者,则程序将打印单词“none”。该程序只显示这两种类别,而不进行排序。

#include<iostream>
#include<string>
using namespace std;

struct information {
	string name;
	double money;
};

void show_grand(information * info, int number);
void show_other(information * info, int number);

int main() {
	int number;
	cout << "Please enter the number of people: ";
	cin >> number;
	cin.get();
	information * info = new information[number];
	for (int i = 0; i < number; i++)
	{
		cout << "Please enter the " << i + 1 << "-th name: ";
		getline(cin, info[i].name);
		cout << "Please enter the " << i + 1 << "-th money:";
		cin >> info[i].money;
		cin.get();
	}

	cout << "Patrons" << endl;
	show_grand(info, number);
	show_other(info, number);

	system("pause");
	return 0;
}

void show_grand(information * info, int number)
{
	cout << "Grand Patrons:" << endl;
	int count_grand = 0;
	for (int i = 0; i < number; i++)
	{

		if (info[i].money > 10000)
		{
			cout << info[i].name << "\t" << info[i].money << endl;
			count_grand++;
		}
	}
	if (count_grand == 0)
		cout << "none" << endl;
}

void show_other(information * info, int number)
{
	cout << "other Patrons:" << endl;
	int count_other = 0;
	for (int i = 0; i < number; i++)
	{

		if (info[i].money <= 10000)
		{
			cout << info[i].name << "\t" << info[i].money << endl;
			count_other++;
		}
	}
	if (count_other == 0)
		cout << "none" << endl;
}

7. 编写一个程序,它每次读取一个单词,直到用户只输入q。然后,该程序指出有多少个单词以元音打头,有多少个单词以辅音打头,还有多少个单词不属于这两类。为此,方法之一是,使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过了isalpha()测试的单词,使用if或switch语句来确定哪些以元音打头。该程序的运行情况如下:
Enter words (q to quit) :
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others

#include<iostream>
using namespace std;

int main()
{
	char str[20];
	int number_vowel = 0;
	int number_consonants = 0;
	int number_others = 0;
	cout << "Enter words (q to quit):" << endl;
	while (cin >> str)
	{
		if (strcmp(str, "q") == 0)
			break;
		char ch = str[0];
		if (isalpha(ch))
		{
			switch (ch)
			{
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				number_vowel++;
				break;
			default:
				number_consonants++;
			}
		}
		else
			number_others++;
	}
	cout << number_vowel << " words beginning with vowels\n";
	cout << number_consonants << " words beginning with consonants\n";
	cout << number_others << " others\n";
	
	system("pause");
	return 0;
}

8. 编写一个程序,它打开一个文件,逐个字符的读取该文件,直到到达文件末尾,然后指出该文件包含多少个字符。

9. 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:
4
Sam Stone
2000
Freida Flass
100500
Tammy Tubbs
5000
Rich Raptor
55000

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1) 本套课程针对高校大学生系统学习C语言而录制,从0基础入门讲起,循序渐进,通俗易懂,同时适用于计算机系及非计算机系的同学。通过学习可以帮助大家掌握C语言本质,轻松面对C语言全国二级考试,并达到能独立完成中型C项目、C游戏的水平;2) 多数高校都开设了C语言课程,网上C语言课程也很多,但普遍存在两个问题: (1) 授课方式单一,大多是照着代码念一遍, 对刚刚接触编程的同学来说,感觉晦涩难懂 (2) 授课内容过度注重语法,没有项目实战支撑,造成课程枯燥无趣。本课程针对上述问题进行了改进 (1) 授课方式采用语法讲解+内存布局示意图+项目的方式,让课程生动有趣好理解 (2) 系统全面的讲解了C语言的核心技术点,还配套录制了《全国计算机二级C语言真题精讲》,让学员在掌握C语言编程的同时,还能轻松面对二级考试;3) 课程详细内容: 常量-变量、分支语句、循环语句、操作符和表达式、函数(库函数、自定义函数、递归调用 )、数组(一维数 组、二维数组、数组作为函数参数、指针数组)、指针(指针和指针类型、二级指针和多级指针、指针表达式解析、指针运算、数 组指针、函数指针、回调函数)、调试技巧、程序环境和预处理(翻译环境、运行环境、预定义符号、#define和#undef、宏和函 数、条件编译、文件包含)、 数据在内存中的存储、内存块分配、static、字符函数和字符串函数、自定义类型(结构体、枚举 、联合)、动态内存管理、文件操作(i/o常量、i/o函数、流)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值