小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. C/C++
  5. 正文

C++ Primer Plus(第五版)第6章编程题答案

2018年10月26日 1602点热度 0人点赞 1条评论

第1题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 1
#include<iostream>
#include<cctype>
using namespace std;
int main() {
	cout << "Enter text for analysis and type @"
		"to terminate input.\n";
	char ch;
	cin.get(ch);
	while (ch != '@') {
		if (!isdigit(ch))
		{
			if (islower(ch))
				ch = toupper(ch);
			else if (isupper(ch))
				ch = tolower(ch);
			cout << ch;

		}
		cin.get(ch);
	}

	system("pause");
	return 0;
}
//错误代码,判断如果是数字执行continue的话就会循环判断字符是不是等于@,第一个字符就无限循环。
//else同样。
//while (ch != '@') {
//	if (isdigit(ch))
//		continue;
//	else if (isalpha(ch))
//	{
//		cout << ch << endl;
//		if (islower(ch))
//			ch = toupper(ch);
//		else if (isupper(ch))
//			ch = tolower(ch);
//		cout << ch;
//	}
//	else
//		continue;
//}

第2题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 2
#include<iostream>
#include<cctype>
const int SIZE = 10;
using namespace std;
int main() {
	double donation[10];
	double average;
	int countbig = 0;
	int count=0;
	double sum = 0.0;
	cout << "Enter 10 numbers.If you want to quit, just input @:" << endl;
	for (int i = 0; i < SIZE; i++) {
		cin >> donation[i];
		if (cin.fail())
			break;
		else {
			sum += donation[i];
			++count;
		}
	}
	average = sum / count;
	cout << "The average number is: " << average << endl;
	for (int i = 0; i < count; i++) {
		if (donation[i] > average)
			++countbig;
	}
	cout << countbig << " numbers bigger than average.\n";
	
	system("pause");
	return 0;
}

第3题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 3
#include<iostream>
#include<cctype>
using namespace std;
void showmenu();
int main() {
	showmenu();
	char choice;
	cin.get(choice);
	while ((choice != 'c') && (choice != 'p') && (choice != 't') && (choice != 'g'))
	{
		cout << "Please enter a c,p,t,g: ";
		//cin.get(choice);
		cin.ignore();
	}
	switch (choice)
	{
	case 'c':
		cout << "This is carnivore's answer.\n";
		break;
	case 'p':
		cout << "This is pianist's answer.\n";
		break;
	case 't':
		cout << "A maple is a tree.\n";
		break;
	case 'g':
		cout << "This is game's answer.\n";
		break;
	default:
		break;
	}
	system("pause");
	return 0;
}

void showmenu() {
	cout << "Please enter one of the following choices.\n"
		"c)carnivore            p)pianist\n"
		"t)tree                 g)game\n";
}

第4题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 4
#include<iostream>
#include<cctype>
using namespace std;
//函数
void showmenu();
void dis_by_name();
void dis_by_title();
void dis_by_bop();
void dis_by_pre();
//定义常量
const int strsize = 30;
const int NUM = 5;
//定义结构体
struct bop {
	char fullname[strsize];
	char title[strsize];
	char bopname[strsize];
	int preference;//0=fullname,1=title,2=bopname
};
bop people[5] = {
	{
		"Wimp Macho",
		"BOSS",
		"WM",
		0
	},
	{
		"Raki Rhodes",
		"Manager",
		"Junior Programmer",
		2
	},
	{
		"Celia Laiter",
		"MIPS",
		"CL",
		1
	},
	{
		"Hoppy Hipman",
		"Analyst Trainee",
		"AT",
		1
	},
	{
		"Pat Hand",
		"Student",
		"LOOPY",
		2
	}
};
char ch;
//开始主函数
int main() {
	showmenu();
	cin >> ch;
	while ((ch != 'a') && (ch != 'b') && (ch != 'c') && (ch != 'd') && (ch != 'q'))
	{
		showmenu();
		//cin.get(choice);
		cin.ignore();
	}
	while (ch != 'q') {
		switch (ch)
		{
		case 'a':
			dis_by_name();
			break;
		case 'b':
			dis_by_title();
			break;
		case 'c':
			dis_by_bop();
			break;
		case 'd':
			dis_by_pre();
			break;
		default:
			break;
		}
		cout << "Next choice:" << endl;
		cin >> ch;
		while ((ch != 'a') && (ch != 'b') && (ch != 'c') && (ch != 'd') && (ch != 'q'))
		{
			showmenu();
			//cin.get(choice);
			cin.ignore();
		}

	}
	cout << "Bye!" << endl;
	system("pause");
	return 0;
}

void showmenu() {
	cout << "Please enter one of the following choices.\n"
		"a)display by name				b)display by title\n"
		"c)display by bopname			d)display by preference\n"
		"q)game\n";
}
void dis_by_name() {
	for (int i = 0; i < NUM; ++i)
	{
		cout << people[i].fullname << endl;
	}
}
void dis_by_title() {
	for (int i = 0; i < NUM; ++i)
	{
		cout << people[i].title << endl;
	}
}
void dis_by_bop() {
	for (int i = 0; i < NUM; ++i)
	{
		cout << people[i].bopname << endl;
	}
}
void dis_by_pre() {
	for (int i = 0; i < NUM; ++i) {
		if(people[i].preference==0)
			cout << people[i].fullname << endl;
		else if(people[i].preference==1)
			cout<< people[i].title << endl;
		else
			cout << people[i].bopname << endl;
	}
}

第5题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 5
#include<iostream>
const double level1 = 0.1;
const double level2 = 0.15;
const double level3 = 0.2;
using namespace std;
int main() {
	double tvarp;
	double cus;
	cout << "Please input your money: ";
	cin >> tvarp;
	while (cin.good() && (tvarp > 0))
	{
		if (tvarp <= 5000)
			cus = 0.0;
		else if (tvarp > 5000 && tvarp <= 15000)
			cus = (tvarp - 5000)*level1;
		else if (tvarp > 15000 && tvarp <= 35000)
			cus = 10000 * level1 + (tvarp - 15000)*level2;
		else if (tvarp > 35000)
			cus = 10000 * level1 + 20000 * level2 + (tvarp - 35000)*level3;
		cout << "您需要交税" << cus << " tvarp.\n";
		cout << "Please input your money: ";
		cin >> tvarp;
	}
	cout << "Bye\n";

	system("pause");
	return 0;
}

第6题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 6
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
struct donation
{
	string name;
	double money;

};
int main() {
	int num;
	int count = 0;
	cout << "Please input the number of donationer: ";
	cin >> num;
	cin.get();//吃掉换行符
	donation *donaer = new donation[num];
	for (int i = 0; i < num; i++)
	{
		cout << "Enter the name: ";
		getline(cin, donaer[i].name);
		cout << "Enter money: ";
		cin >> donaer[i].money;
		cin.get();//吃掉换行符
	}
	cout << "****************************" << endl;
	cout << "Grand Patrons" << endl;
	cout << "****************************" << endl;
	for (int i = 0; i < num; i++) {
		if (donaer[i].money > 10000) {
			++count;
			cout << "Name: " << donaer[i].name << endl;
			cout << "Donate Money: " << donaer[i].money << endl;
			cout << endl;
		}
	}
	if (count == 0)
	{
		cout << "None" << endl;
	}
	count = 0;
	cout << "****************************" << endl;
	cout << "Patrons" << endl;
	cout << "****************************" << endl;
	for (int i = 0; i < num; i++) {
		if (donaer[i].money <= 10000) {
			++count;
			cout << "Name: " << donaer[i].name << endl;
			cout << "Donate Money: " << donaer[i].money << endl;
			cout << endl;
		}
	}
	if (count == 0)
	{
		cout << "None" << endl;
	}
	delete[] donaer;
	system("pause");
	return 0;
}

第7题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 7
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main() {
	string word;
	char ch;
	int yuan = 0;
	int fu = 0;
	int others = 0;
	cin >> word;
	while (word != "q") {
		ch = word[0];
		if (isalpha(ch))
		{
			if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'
				|| ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
				++yuan;
			else
				++fu;
		}
		else
			++others;
		cin >> word;
	}
	cout << yuan << " words beginning with vowels." << endl;
	cout << fu << " words beginning with consonants." << endl;
	cout << others << " others." << endl;

	system("pause");
	return 0;
}

第8题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 8
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
const int MAXSIZE = 50;
int main() {
	char filename[MAXSIZE];
	ifstream inFile;
	cout << "Please enter the file's name: ";
	cin.getline(filename, MAXSIZE);
	inFile.open(filename);
	if (!inFile.is_open()) {
		cout << "Open this file error."<<endl;
		exit(EXIT_FAILURE);
	}
	char ch;
	int count = 0;
	inFile >> ch;
	while (inFile.good()) {
		++count;
		inFile >> ch;
	}
	if (inFile.eof()) {
		cout << "Have reached end of this file.\n";
	}
	else if (inFile.fail()) {
		cout << "Input terminated by data mismatch.\n";
	}
	else
		cout << "unknown reason to stop.\n";
	if (count == 0) {
		cout << "No data in this file.\n";
	}
	else {
		cout << "There are " << count << " characters in this file.\n";
	}
	inFile.close();
	system("pause");
	return 0;
}

第9题:

//C++ Primer Plus Edition 5
//Chapter 6 Homework 9
#include<iostream>
#include<cctype>
#include<string>
#include<fstream>
using namespace std;
const int MAXSIZE = 50;
struct donation
{
	string name;
	double money;

};
int main() {
	char filename[MAXSIZE];
	ifstream inFile;
	cout << "Please enter the file's name: ";
	cin.getline(filename, MAXSIZE);
	inFile.open(filename);
	if (!inFile.is_open()) {
		cout << "Open this file error." << endl;
		exit(EXIT_FAILURE);
	}
	int num;
	int count = 0;
	inFile >> num;
	inFile.get();//吃掉换行符
	donation *donaer = new donation[num];
	for (int i = 0; i < num; i++)
	{
		getline(inFile, donaer[i].name);
		inFile >> donaer[i].money;
		inFile.get();//吃掉换行符
	}
	cout << "****************************" << endl;
	cout << "Grand Patrons" << endl;
	cout << "****************************" << endl;
	for (int i = 0; i < num; i++) {
		if (donaer[i].money > 10000) {
			++count;
			cout << "Name: " << donaer[i].name << endl;
			cout << "Donate Money: " << donaer[i].money << endl;
			cout << endl;
		}
	}
	if (count == 0)
	{
		cout << "None" << endl;
	}
	count = 0;
	cout << "****************************" << endl;
	cout << "Patrons" << endl;
	cout << "****************************" << endl;
	for (int i = 0; i < num; i++) {
		if (donaer[i].money <= 10000) {
			++count;
			cout << "Name: " << donaer[i].name << endl;
			cout << "Donate Money: " << donaer[i].money << endl;
			cout << endl;
		}
	}
	if (count == 0)
	{
		cout << "None" << endl;
	}
	delete[] donaer;
	inFile.close();
	system("pause");
	return 0;
}

 

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: C++ 代码
最后更新:2018年10月26日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
“超”最终版官网上线,发布进入最终倒计时 2010 S.V Beijing Travel 22:"New Life"Plan-理解包容·明白自己 已修:Python语言程序设计【北京理工大学】[2018-08-21] 马云卸任CEO演讲:明天起生活将是我的工作 个人学习笔记整理 vivo2020校招提前批开发笔试试题
标签聚合
Python 高中 鸟哥的linux私房菜 学习 leetcode 生活 python学习 Java 算法 linux
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 9 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号