c++ primer plus(第6版)中文版 第六章编程练习答案

第六章编程练习答案

6.1键盘输入,遇到@停止并回显,除数字外,大写变小写,小写变大写(用ccypye函数)

//6.1键盘输入,遇到@停止并回显,除数字外,大写变小写,小写变大写(用ccypye函数)
#include <iostream>
#include <cctype>
using namespace std;

int main () 
{
	char	ch;
	while (cin.get(ch) && ch != '@') 
	{
		if (isdigit(ch))
			continue;
		else if (islower(ch)) 
			ch = (char)toupper(ch);
		else if (isupper(ch)) 
			ch = (char)tolower(ch);
		cout << ch;
	}
} 

6.2最多将10个数值读入一个double数组中,遇到非数字停止,计算平均值和比平均值大的个数

//6.2最多将10个数值读入一个double数组中,遇到非数字停止,计算平均值和比平均值大的个数
#include <iostream>
using namespace std;

int main () 
{
	double	array[10];
	double	sum = 0;
	unsigned	k = 0;
	cout << "input number:" << endl;
	while (k < 10 && cin >> array[k])
		sum += array[k++];
	double	ave = sum / k;
	unsigned	num = 0;
	for (unsigned i = 0 ; i < k; ++i)
		if (array[i] > ave)
			++num;
	cout << "平均值为" << ave << ",超过平均值的有" << num << "个。" << endl;
} 

6.3编写一个菜单驱动的模型,提供4个选项,使用switch进行简单操作

//6.3编写一个菜单驱动的模型,提供4个选项,使用switch进行简单操作
#include <iostream>
using namespace std;

int main () 
{     
	cout << "Please enter one of the following choices: " << endl;
	cout << "c) carnivore\t" << "p) pianist" << endl;
	cout << "t) tree\t\t" << "g) game" << endl;
	char	ch;
	while (cin >> ch) {
		switch (ch) {
			case 'c': 
					cout << "carnivore. " << endl;
				break;
			case 'p': 
					cout << "pianist. " << endl;
				break;
			case 't': 
					cout << "A maple is a tree." << endl;
				break;
			case 'g': 
					cout << "game. " << endl;
				break;
			default:
				cout << "Please enter a c, p, t, or g: ";
				break;
		}
	}
} 

6.4编写一个BOP程序,人们可通过姓名,职位,秘密名字和偏好来列出成员

//6.4编写一个BOP程序,人们可通过姓名,职位,秘密名字和偏好来列出成员(应用c++11标准:auto和范围for循环)
#include <iostream>
using namespace std;

enum Preference {fullname, title, bopname};
struct Bop {
	char		Fullname[30];	// real name
	char		Title[50];			// job title
	char		Bopname[30];	// secret BOP name
	Preference	preference;
};

int main () 
{ 
	Bop Bops[] = {	{"Yang Yang", "chinamobile", "yangyang.gnu", fullname},{"xiao wang", "microsoft", "xiaowang", title},
							{"xiao liu", "IBM", "xiaoliu", title}, {"xiao zhang", "Huawei", "xiaozhang", bopname}	};
	bool	flat = true;
	while (flat) 
	{
		cout << "Benevolent Order of Programmers Report" << endl;
		cout << "a. display by name" << "\t" << "b. display by title" << endl;
		cout << "c. display by bopname" << "\t" << "d. display by preference" << endl;
		cout << "q. quit" << endl;
		char	ch;
		if (!(cin >> ch)) 
		{
			flat = false;
			break;
		}
		switch (ch) 
		{
			case 'a':
				for (const auto& e :Bops) {
					cout << e.Fullname << endl;
				};
			break;
			case 'b': 
				for (const auto& e : Bops) 
					cout << e.Title << endl;
			break;
			case 'c': 
				for (const auto& e : Bops) 
					cout << e.Bopname << endl;
			break;
			case 'd': 
				for (const auto& e : Bops)
				 {
					if (fullname == e.preference) 
						cout << e.Fullname << endl;
					else if (title == e.preference) 
						cout << e.Title<< endl;
					else if (bopname == e.preference)
						cout << e.Bopname<< endl;
					else ;
				}
			break;
			case 'q': 
				cout << "Bye! " << endl;
				flat = false;
			break;
			default: 
				cout << "Error! " << endl;
			break;
		}
	}
} 

6.5输入用户收入,按下图的税收方法计算所得税,当输入不是正数时退出

//6.5输入用户收入,按下图的税收方法计算所得税,当输入不是正数时退出
	// |-----|----------|--------------------|-----...
	//   0% (5K)  10% (1.5W)      15%      (3.5W)  20%
#include <iostream>
using namespace std;

int main () 
{     
	double	tvarp;
	double array[4] = {5000,10000,20000}; //存放每个间断内最多的钱
	while (cin >> tvarp && tvarp >= 0) {
		if (tvarp <= 5000){
			array[0] = tvarp;
			array[1] = array[2] = array[3] = 0;
		} 
		else if ((tvarp -= 5000) <= 10000) {
			array[1] = tvarp;
			array[2] = array[3] = 0;
		} 
		else if ((tvarp -= 10000) <= 20000) {
			array[2] = tvarp;
			array[3] = 0;
		} 
		else 
			array[3] = tvarp - 20000;
		cout << "税收明细如下:" 
			 << array[0] << " * 0% + " << array[1] << " * 10% + "
			 << array[2] << " * 15% + " << array[3] << " * 20% "
			 << " = " << array[0] * 0 + array[1] * 0.1 + array[2] * 0.15 + array[3] * 0.2
			 << endl;
	}
} 

6.6编写一个捐款程序,记录有姓名和钱数两个成员,并依据捐款是否大于10000分为两类显示人数

//6.6编写一个捐款程序,记录有姓名和钱数两个成员,并依据捐款是否大于10000分为两类显示人数
#include <iostream>
#include <string>
using namespace std;

struct Donor {
	string	name;
	double	amount;
};

int main () 
{     
	cout << "输入捐赠人数:";
	unsigned	num;
	cin >> num;
	Donor* const donor = new Donor [num];	
	for (unsigned i = 0; i < num; ++i) {
		cout << "输入捐赠人姓名:";
		cin >> donor[i].name;
		cout << "输入捐赠金额:";
		cin >> donor[i].amount;
	}
	cout << "荣誉捐赠者:" << endl;
	for (unsigned i = 0; i < num; ++i) 
		if (donor[i].amount >= 10000) 
			cout << donor[i].name << endl;
	cout << "普通捐赠者:" << endl;
	for (unsigned i = 0; i < num; ++i) 
		if (donor[i].amount < 10000) 
			cout << donor[i].name << endl;
	delete [] donor;
} 

6.7读取一些词,遇到q停止,输出元音开头和辅音开头单词,以及其它的个数

//6.7读取一些词,遇到q停止,输出元音开头和辅音开头单词,以及其它的个数
#include <iostream>
#include <cctype>
using namespace std;

int main () 
{     
	unsigned	cntVowels = 0, cntConsonants = 0, cntOthers = 0;
	cout << "输入单词,字母q结束:\n";
	string word;
	while (cin >> word && "q" != word) {
		char& first_char = word[0];
		if (!isalpha(first_char))
			++cntOthers;
		else if 	('a' == first_char || 'A' == first_char || 
					'e' == first_char || 'E' == first_char || 
					 'i' == first_char || 'I' == first_char || 
				    'o' == first_char || 'O' == first_char || 
					'u' == first_char || 'U' == first_char	) 
			++cntVowels;
		 else 
			++cntConsonants;
	}
	cout << "元音开头的单词" << cntVowels << "个、辅音单词" << cntConsonants << "个、其他" << cntOthers << "个" << endl;
} 

6.8打开一个文件,逐字读取,输出文件字符个数

//6.8打开一个文件,逐字读取,输出文件字符个数
#include <iostream>
#include <fstream>
using namespace std;

int main (int argc,char* argv[]) 
{
	ifstream fin(argv[1]);
	unsigned	num=0;
	char	ch;
	while (fin.get(ch))
		++num;
	cout << "此文件总共有字符:"  << num-1 << endl;    // 读入EOF符,所以num-1
	num=0;
}

6.9对练习6.6,从文件中读取信息,第一行显示人数,剩下显示信息(文件第一行为人数)

//6.9对练习6.6,从文件中读取信息,第一行显示人数,剩下显示信息(文件第一行为人数)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct Donor {
	string	name;
	double	amount;
};

int main (int argc,char* argv[]) 
{     
	ifstream fin(argv[1]);
	unsigned	num;
	fin >> num;
	Donor* const	donor = new Donor [num];	

	for (unsigned i = 0; i < num; ++i) 
	{
		fin.get();
		getline(fin, donor[i].name);
		fin >> donor[i].amount;
	}
	cout << "荣誉捐赠者:" << endl;
	for (unsigned i = 0; i < num; ++i) 
		if (donor[i].amount >= 10000) 
			cout << donor[i].name << endl;
	cout << "普通捐赠者:" << endl;
	for (unsigned i = 0; i < num; ++i) 
		if (donor[i].amount < 10000) 
			cout << donor[i].name << endl;
	delete [] donor;
} 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值