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

#include<iostream>#include<cctype>//prototypes for character functionsusing namespace std;int main(){char ch;//定义一个char类型变量cout << "Enter the character,and type @"<<endl;cin.get(ch);while (ch != '@ '){if (isdigit(ch))//当输入的是数字时{cin.get(ch);//输出数字continue;}else if (islower(ch))//当输入的是小写字母时{ch = toupper(ch);//小写 -> 大写}else if (isupper(ch))//当输入的是大写字母时{ch = tolower(ch);//大写 -> 小写}cout << ch ;cin.get(ch);}system("pause");return 0;}//Enter the character, and type @//@//@//china//CHINA//////CHINA//china

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

#include<iostream>#include<array>using namespace std;int main(){array<double, 10> donation;//使用array模板定义数组长度为10double input;int counter = 0;double average, sum = 0;int bigger = 0;//sum ->和 average ->平均值 counter ->元素数 bigger->大于平均值的个数cout << "Enter the double numerical:";cin >> input;while (input != 0 && counter < 10){donation[counter++] = input;cout << "No." << counter << " Data input to Array." << endl;cout << "Enter the double numerical.";cin >> input;}//通过while循环输入数据,当输入非数字时或超过10个元素时推出循环for (int i = 0; i < counter; i++){sum += donation[i];//十个数的总和}average = sum / counter;for (int i = 0; i < counter; i++){if (donation[i] > average)bigger++;}//通过遍历计算大于平均值的元素个数cout << "The Average is " << average << " and " << bigger;cout << " data bigger than average." << endl;system("pause");return 0;}//Enter the double numerical : 10//No.1 Data input to Array.//Enter the double numerical.20//No.2 Data input to Array.//Enter the double numerical.30//No.3 Data input to Array.//Enter the double numerical.40//No.4 Data input to Array.//Enter the double numerical.50//No.5 Data input to Array.//Enter the double numerical.60//No.6 Data input to Array.//Enter the double numerical.70//No.7 Data input to Array.//Enter the double numerical.80//No.8 Data input to Array.//Enter the double numerical.90//No.9 Data input to Array.//Enter the double numerical.100//No.10 Data input to Array.//Enter the double numerical.110//The Average is 55 and 5 data bigger than average.//请按任意键继续. . .

3. 编写一个菜单驱动程序的雏形。该程序显示一个提供4个选项的菜单——每个选项用一个字母标记。如果用户使用有效选项之外的字母进行响应,程序将提示用户输入一个有效的字母,直到用户这样做为止。然后,该程序使用一条switch语句,根据用户的选择执行一个简单操作。该程序的运行情况如下:

Please enter one of the following choices:

c) carnivore            p) pianist

t) tree                     g) game

f

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>using namespace std;void showmenu();//showmenu()函数声明int main(){char choice;showmenu();cin.get(choice);//显示菜单,并读取用户输入,保存至choice变量中while (choice != 'c' && choice != 'p' && choice != 't' && choice != 'g'){cin.get();cout << "Please enter a,c,p,t,or g ";cin.get(choice);}switch (choice){case'c':break;case'p':break;case't':cout << "Asample is a tree." << endl;case'g':break;}system("pause");return 0;}void showmenu(){cout << "Please enter one of the following choice:\n";cout << "c) carnivore \t\t\t p) pianist\n";cout << "t) tree\t\t\t\t g)game\n";}//showmenu()函数只负责菜单传递信息的输出//Please enter one of the following choice ://c) carnivore                     p) pianist//t) tree                          g)game//f//Please enter a, c, p, t, or g q//Please enter a, c, p, t, or g t//Asample is a tree.//请按任意键继续. . .

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

// Benevolent Order of Programmer 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 Programmer 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>#include<cstring>using namespace std;const int strsize = 40;const int usersize = 5;//Benevolent Order of Programmer 姓名结构体struct bop{char fullname[strsize];//真实姓名char title[strsize];//头衔char bopname[strsize];//秘密BOP姓名int preference;//0 = fullname,1 = title;2 = bopname};bop bop_user[usersize] ={{"Wimp Macho","Programmer","MIPS",0},{"Raki Rhodes","Junior Programmer",1},{"Celia Laiter","","MIPS",2},{"Hoppy Hipman","Analyst Trainee","",1},{"Pat Hand","","LOOPY",2},};//定义常量,定义结构体初始化bop数组信息void showmenu();//showmenu()函数声明void print_by_name();//打印姓名函数声明void print_by_pref();//打印偏好函数声明void print_by_title();//打印头衔函数声明void print_by_bopname();//打印秘密姓名函数声明void create_info();int main(){char choice;//create_info();showmenu();cout << "Enter your choice:";cin.get(choice);//显示菜单读取用户输入while (choice != 'q'){switch (choice){case 'a':print_by_name();break;case 'b':print_by_title();break;case 'c':print_by_bopname();break;case 'd':print_by_pref();break;default:cout << "Please enter character a ,b ,c ,d , or q:" << endl;}//通过switch语句对应的函数进行显示cout << "Next choice:";cin.get(choice);}//将switch语句放在while循环中,可以反复循环给下那个功能cout << "Bye!" << endl;system("pause");return 0;}void showmenu(){cout << "a. display by name\t\t b. display by title" << endl;cout << "c. display by bopname\t\t d. display by preference" << endl;cout << "q. quit" << endl;}//显示菜单void print_by_name(){for (int i = 0; i < usersize; i++){if (bop_user[i].fullname == 0)break;elsecout << bop_user[i].fullname << endl;}}void print_by_pref(){for (int i = 0; i < usersize; i++){if (bop_user[i].fullname == 0)break;else{switch (bop_user[i].preference){case 0:cout << bop_user[i].fullname << endl;break;case 1:cout << bop_user[i].title << endl;break;case 2:cout << bop_user[i].bopname << endl;break;}}}}void print_by_title(){for (int i = 0; i < usersize; i++){if (bop_user[i].fullname == 0)break;else{cout << bop_user[i].title << endl;break;}}}void print_by_bopname(){for (int i = 0; i < usersize; i++){if (bop_user[i].fullname == 0)break;else{cout << bop_user[i].bopname << endl;break;}}}void create_info(){for (int i = 0; i < usersize; i++){cout << "Enter the user's full name:";cin.getline(bop_user[i].fullname, strsize);cout << "Enter the user's title:";cin.getline(bop_user[i].title, strsize);cout << "Enter the user's bopname:";cin.getline(bop_user[i].bopname, strsize);cout << "Enter the user's preference:";cin >> bop_user[i].preference;cout << "Next...(f for finished):";cin.get();if (cin.get() == 'f') break;}}//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 : Please enter character a, b, c, d, or q ://Next choice : d//Wimp Macho//Raki Rhodes//MIPS//Analyst Trainee//LOOPY//Next choice : Please enter character a, b, c, d, or q ://Next choice : q//Bye!//请按任意键继续. . .

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;int main(){float tax, salary;cout << "Please enter your salary:";cin >> salary;while (salary > 0){if (salary < 5000){tax = 0;}else if (salary <= 15000){tax = (salary - 5000) * 0.10;}else if (salary <= 35000){tax = 10000 * 0.10 + (salary - 15000) * 0.15;}else if (salary > 35000){tax = 10000 * 0.10 + 20000 * 0.15 + (salary - 30000) * 0.20;}cout << "Your salary is " << salary << " trarps,and you should pay ";cout << tax << " trarps of tax." << endl;cout << "Enter your salary:";cin >> salary;}cout << "Bye!" << endl;system("pause");return 0;}//Please enter your salary : 4000//Your salary is 4000 trarps, and you should pay 0 trarps of tax.//Enter your salary : 15000//Your salary is 15000 trarps, and you should pay 1000 trarps of tax.//Enter your salary : 25000//Your salary is 25000 trarps, and you should pay 2500 trarps of tax.//Enter your salary : 40000//Your salary is 40000 trarps, and you should pay 6000 trarps of tax.//Enter your salary : 0//Bye!//请按任意键继续...

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

#include<iostream>#include<string>using namespace std;struct patrons {string full_name;double fund;};//捐款人基本信息的结构体int main(){int patrons_number;patrons *ppatrons;cout << "How many patrons ? ";cin >> patrons_number;cin.get();ppatrons = new patrons[patrons_number];//建立动态数组int id = 0;bool empty = true;cout << "Starting to input patron's info:";while (id < patrons_number){cout << "Enter the full name of patrons: ";getline(cin, ppatrons[id].full_name);cout << "Enter the fuud of " << ppatrons[id].full_name << " : ";cin >> ppatrons[id].fund;cin.get();id++;cout << "Continue to input,or press (f) to finished.";if (cin.get() == 'f')break;}//建立捐款人名单cout << "Grand Patrons" << endl;for (int i = 0; i < patrons_number; i++){if (ppatrons[i].fund >= 1000) {cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl;empty = false;}}//查询Grand Patrons名单,如果empty为true,输出NONEif (empty)cout << "NONE" << endl;empty = false;cout << "Patrons" << endl;for (int i = 0; i < patrons_number; i++){if (ppatrons[i].fund < 1000) {cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl;}}if (empty)cout << "NONE" << endl;system("pause");return 0;}//How many patrons ? 2//Starting to input patron's info:Enter the full name of patrons: LI MING//Enter the fuud of LI MING : 5000//Continue to input, or press(f) to finished.//Enter the full name of patrons : LI HUA//Enter the fuud of LI HUA : 1000//Continue to input, or press(f) to finished.f//Grand Patrons//LI MING : 5000//LI HUA : 1000//Patrons//请按任意键继续. . .

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 consotants

2 others

#include<iostream>#include<cctype>using namespace std;int main(){char words[40];int vowel, consonant, others;vowel = consonant = others = 0;cout << "Enter words(q to quit):" << endl;cin >> words;while (strcmp(words, "q") != 0){if (!isalpha(words[0])){others++;}//统计非字母开头的单词else {switch (words[0]){case 'a':case 'e':case 'i':case 'o':case 'u':vowel++;//统计以元音字母开头的单词break;default:consonant++;//统计非元音字母开头的单词}}cin >> words;}cout << vowel << " words beginning with vowel." << endl;cout << consonant << " words beginning with consonants." << endl;system("pause");return 0;}//Enter words(q to quit) ://The 12 awesome oxen ambled//quitely across 15 meters of lawn//q//5 words beginning with vowel.//4 words beginning with consonants.//请按任意键继续. . .

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

#include<iostream>#include<fstream>using namespace std;int main(){ifstream fin;//定义文件输入流string file_name;cout << "Enter the file name:";getline(cin, file_name);fin.open(file_name);if (!fin.is_open()) {cout << "Error to open file." << endl;exit(EXIT_FAILURE);}char read_char;int char_counter = 0;while (!fin.eof()) {fin >> read_char;char_counter++;}cout << "The file " << file_name << " contains " << char_counter << " characters." << endl;fin.close();//关闭文件system("pause");return 0;}

建立txt文件如下所示:

9. 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。即该文件类似于下面:

4

Sam Stone

2000

Freida Flass

100500

Tammy Tubbs

5000

Rich Raptor

55000

#include <iostream>#include <fstream>#include<string>using namespace std;struct patrons {string full_name;double fund;};//表示捐款人信息的结构体int main(){ifstream fin;string file_name;cout << "Enter the file name:";getline(cin, file_name);fin.open(file_name);if (!fin.is_open()){cout << "Error to open file:" << endl;exit(EXIT_FAILURE);}int patrons_number;patrons *ppatrons;int id = 0;bool empty = true;fin >> patrons_number;if (patrons_number <= 0){exit(EXIT_FAILURE);}ppatrons = new patrons[patrons_number];fin.get();//读取人数,创建动态数组while (!fin.eof() && id < patrons_number){getline(fin, ppatrons[id].full_name);cout << "Read Name: " << ppatrons[id].full_name << endl;fin >> ppatrons[id].fund;cout << "Read fund: " << ppatrons[id].fund << endl;fin.get();id++;}//循环读取捐款人信息,也可以使用for循环fin.close();//关闭文件cout << "Grand Patrons" << endl;for (int i = 0; i < patrons_number; i++){if (ppatrons[i].fund >= 10000) {cout << ppatrons[i].full_name << " ; " << ppatrons[i].fund << endl;empty = false;}}if (empty)cout << "NONE" << endl;empty = false;cout << "Patrons" << endl;for (int i = 0; i < patrons_number; i++){if (ppatrons[i].fund < 10000) {cout << ppatrons[i].full_name << " : " << ppatrons[i].fund << endl;}}if (empty) cout << "NONE" << endl;system("pause");return 0;}

建立txt文件如下所示:

C++ Primer Plus(第六版)第6章 编程练习答案详解相关推荐

  1. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  2. C Primer Plus第六版第七章编程题目与参考答案⭐

    1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数.换行符数和所有其他字符的数量. #include <stdio.h> #define STOP '#' #define SP ...

  3. C++ Primer Plus(第六版)第4章 编程练习答案详解

    第四章 1. 编写一个C++程序,如下述输出示例所示的那样请求并显示信息: What is your first name? Betty Sue What is your last name? Yew ...

  4. C++primer plus第六版第四章编程题代码

    1. #include "stdafx.h" #include <iostream> #include <string> #include <vect ...

  5. C Primer Plus第六版第四章编程题目与参考答案⭐

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. #include <stdio.h>int main() {char firstname[20] ...

  6. 跟着c++Primer Plus学编程--- 4.13编程练习答案详解】

    跟着c++Primer Plus学编程--- 4.13编程练习答案详解 1.编写一个C++程序,如下述输出事例所示那样请求并显示信息. 2.修改程序清单4.4,使用C++ string 类而不是 ch ...

  7. 《C Prime Plus》(第六版) 第03章 编程练习 7 英寸转换成厘米

    C Prime Plus 第六版 编译器版本:Mac Xcode 11.6  第03章 数据和C 编程练习 7 7. 1英寸相当于2.54厘米.编写一个程序,提示用户输入身高(/英寸),然后以厘米为单 ...

  8. 《C Prime Plus》(第六版) 第03章 编程练习 8 品脱/盎司/大汤勺/茶勺单位换算

    C Prime Plus 第六版 编译器版本:Mac Xcode 11.6  第03章 数据和C 编程练习 8 在美国的体积测量系统中,1品脱等于2杯,1杯等于8盎司,1盎司等于2大汤勺,1大汤勺等于 ...

  9. C Primer Plus(第六版)第三章 数据和C

    笔记记录 1.float 类型可以储存带小数的数字. 2.printf()中使用%f来处理浮点值.%.2f中的.2用于精确控制输出,指定输出的浮点数只显示小数点后面两位. 3.scanf()函数用于读 ...

最新文章

  1. 【云周刊】第178期:阿里云以生态联盟推动全球市场,牵手Bolloré集团全球合作...
  2. 解决PLSQL Developer 9连接oracle10g出现乱码
  3. LOJ#6048. 「雅礼集训 2017 Day10」数列(线段树)
  4. 查看电脑重启日志_系统日志看硬盘故障图文教程,电脑日志查看磁盘硬盘坏道问题方法...
  5. LeetCode 678. 有效的括号字符串(栈)
  6. phpcms v9 在当前栏目下获取父栏目与当前栏目的名称与连接
  7. java的scanner使用步骤
  8. mybaits 学习
  9. 数论基础——欧拉函数(一)(模板)
  10. nacos cloud 配置中心中 修改properties格式内容并发布然后在浏览器页面刷新无效果
  11. Selenium 导航操作 Navigating
  12. android 打卡统计日历表,GitHub - lw1243925457/clickApp: 一个日常事务打卡和统计的APP,用于日常任务记录、任务所需时间记录、任务花费时间统计显示...
  13. 动态规划-背包问题(1)
  14. 中文实时语音识别引擎
  15. Maven打包Resource资源文件损坏解决
  16. v5行为验证使用介绍(三)- 程序接入流程
  17. R语言计算KS值、绘制KS曲线
  18. 开源与标准协同发展研究报告(2022)
  19. 学习OpenCV3:Cmake编译OpenCV时无法下载ffmpeg、xfeatures2d等文件
  20. 小程序对火锅店的发展利好及部分可行性内容设计

热门文章

  1. JVM老生代增长过快问题排查
  2. 【线性代数】标准正交矩阵与Gram-Schmidt正交化
  3. Oracle11g安装教程、配置实例、监听、客户端程序详解_Windows篇
  4. java在电脑上的用法,java怎么安装,java安装后怎么使用
  5. CSDN博客去除图片水印
  6. 获取 COM 服务器进程 ID 的 3 种方法
  7. 推荐一个文本编辑器-PilotEdit
  8. 1.5小时,一键部署Oracle 11GR2 RAC 集群
  9. Android挂断电话以及Java Class Loader
  10. 移动IP的作用及意义