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

来源:互联网 发布:网络剧拍摄成本 编辑:程序博客网 时间:2024/05/17 03:17

第六章编程练习答案

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

//6.1键盘输入,遇到@停止并回显,除数字外,大写变小写,小写变大写(用ccypye函数)#include <iostream>#include <cctype>using namespace std;int main () {charch;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 () {doublearray[10];doublesum = 0;unsignedk = 0;cout << "input number:" << endl;while (k < 10 && cin >> array[k])sum += array[k++];doubleave = sum / k;unsignednum = 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;charch;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 {charFullname[30];// real namecharTitle[50];// job titlecharBopname[30];// secret BOP namePreferencepreference;};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}};boolflat = 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;charch;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 () {     doubletvarp;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 {stringname;doubleamount;};int main () {     cout << "输入捐赠人数:";unsignednum;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 () {     unsignedcntVowels = 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]);unsignednum=0;charch;while (fin.get(ch))++num;cout << "此文件总共有字符:"  << num-1 << endl;    // 读入EOF符,所以num-1num=0;}

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

//6.9对练习6.6,从文件中读取信息,第一行显示人数,剩下显示信息(文件第一行为人数)#include <iostream>#include <string>#include <fstream>using namespace std;struct Donor {stringname;doubleamount;};int main (int argc,char* argv[]) {     ifstream fin(argv[1]);unsignednum;fin >> num;Donor* constdonor = 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;} 

1 0
原创粉丝点击