声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便。

(具体方式参见第3章模板)

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

  1. #include <iostream>
  2. #include <cctype>
  3. using namespace std;
  4. void cprimerplus_exercise_6_1()
  5. {
  6. char letter;
  7. cout << "Please input letters:";
  8. cin >> letter;
  9. cin.get();
  10.  
  11. while ( letter != '@')
  12. {
  13. if (isdigit(letter))
  14. {
  15. cin.get(letter);
  16. }
  17. else
  18. {
  19. if (islower(letter))
  20. {
  21. letter = toupper(letter);
  22. }else if (isupper(letter))
  23. {
  24. letter = tolower(letter);
  25. }
  26.  
  27. cout << letter;
  28. cin >> letter;
  29. cin.get();
  30. }
  31.  
  32. }
  33.  
  34. }

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

  1. #include <iostream>
  2. #include <cctype>
  3. using namespace std;
  4. void cprimerplus_exercise_6_2()
  5. {
  6. double donation[10];
  7. double sum = 0.0;
  8. double average = 0.0;
  9. double tmp;
  10. int i = 0;
  11. int cnt = 0;
  12.  
  13. while ( cin >> tmp && i < 10)
  14. {
  15. donation[i] = tmp;
  16. sum +=donation[i];
  17. ++i;
  18. }
  19.  
  20. if ( i != 0)
  21. {
  22. average = sum / i;
  23. }
  24.  
  25. for (int j = 0; j < i; j++)
  26. {
  27. if (donation[j] > average)
  28. {
  29. ++cnt;
  30. }
  31. }
  32.  
  33. cout << "The average is:" << average << endl;
  34. cout << "There are " << cnt << " numbers are above the average!"<< endl;
  35.  
  36. }

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

  1. #include <iostream>
  2.  
  3. using namespace std;
  4. void cprimerplus_exercise_6_3()
  5. {
  6. cout << "Please enter one of the following choices:" << endl\
  7. <<"c) carnivore \tp) pianist \nt) tree \tg) game" << endl;
  8.  
  9. cout << "Please enter a c, p, t, or g:";
  10.  
  11. char letter;
  12. cin >> letter;
  13.  
  14. while ( letter != 'c' && letter != 'p' && letter != 't' && letter != 'g')
  15. {
  16. cout << "Please enter a c, p, t, or g:";
  17. cin >> letter;
  18. }
  19. switch (letter)
  20. {
  21. case 'c':
  22. cout << "A maple is a carnivore";
  23. break;
  24. case 'p':
  25. cout << "A maple is a pianist";
  26. case 't':
  27. cout << "A maple is a tree";
  28. break;
  29. case 'g':
  30. cout << "A maple is a game";
  31. break;
  32. default:
  33. break;
  34. }
  35. }

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

//Benevolent Order of Programmers name structure

Struct bop

{

char fullname[strsize];

char title[strsize];

char bopname[strsize];

int preference;

};

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

A. Display by name B. Display by title C. Display by bopname

D. Display by preference Q. quit

  1. #include <iostream>
  2.  
  3. using namespace std;
  4. void cprimerplus_exercise_6_4()
  5. {
  6. const int strsize = 20;
  7. const int mennum = 3;
  8. struct bop
  9. {
  10. char fullname[strsize]; //real name
  11. char title[strsize]; //job title
  12. char bopname[strsize]; //secret BOP name
  13. int preference; // 0 = fullname, 1 = title, 2 = bopname
  14. };
  15.  
  16. bop member[mennum] = {
  17. { "thm", "leader", "sb", 0},
  18. { "cgf", "sb", "sb", 1},
  19. { "th", "dsb", "ssb", 2} };
  20. cout << "Enter your choice!";
  21. char ch;
  22. cin.get(ch);
  23. while (cin >> ch && ch != 'q')
  24. {
  25. switch (ch)
  26. {
  27. case 'a':
  28. for (int i = 0; i < mennum; i++)
  29. cout << member[i].fullname << endl;
  30. break;
  31. case 'b':
  32. for (int i = 0; i < mennum; i++)
  33. cout << member[i].title << endl;
  34. break;
  35. case 'c':
  36. for (int i = 0; i < mennum; i++)
  37. cout << member[i].bopname << endl;
  38. break;
  39. case 'd':
  40. for (int i = 0; i < mennum; i++)
  41. {
  42. if (member[i].preference == 0)
  43. {
  44. cout << member[i].fullname << endl;
  45. }else if (member[i].preference = 1)
  46. {
  47. cout << member[i].title << endl;
  48. }else if (member[i].preference = 2)
  49. {
  50. cout << member[i].bopname << endl;
  51. }
  52. }
  53. break;
  54. default:
  55. break;
  56. }
  57. cout << "Next choice:";
  58. }
  59. cout << "Bye!\n";
  60.  
  61. }

5. 在neutronia王国,货币单位是tvarp,收入所得税的计算方式如下:

5000 tvarps:不收税

5001~15000 tvarps:10%

15001~35000 tvarps:15%

35000 tvarps以上:10%

如:收入38000 tvarps,所得税:5000*0.0+10000*0.1+20000*0.15+3000*0.2;

  1. #include <iostream>
  2.  
  3. using namespace std;
  4. void cprimerplus_exercise_6_5()
  5. {
  6. cout << "please input your income:";
  7. double income, revenue;
  8. while (cin >> income && income >= 0)
  9. {
  10. if (income <= 5000)
  11. {
  12. revenue = 0.0;
  13. }else if ( income > 5000 && income <= 15000)
  14. {
  15. revenue = (income - 5000) * 0.1;
  16. }else if( income > 15000 && income < 35000)
  17. {
  18. revenue = 5000 * 0.00 + 10000 * 0.10 + (income -20000) * 0.15;
  19. }else if( income > 35000)
  20. {
  21. revenue = 5000 * 0.00 + 10000 * 0.10 + + 20000 * 0.15 + (income -35000) * 0.15;
  22. }
  23.  
  24. cout << "your revenue is:" << revenue << endl;
  25. cout << "please enter your income:";
  26.  
  27. }
  28. }

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

  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. void cprimerplus_exercise_6_6()
  6. {
  7. int num;
  8. cout << "please input the donate num:";
  9. cin >> num;
  10. cin.get();
  11.  
  12. struct patron
  13. {
  14. string name;
  15. double money;
  16. };
  17.  
  18. patron *ps = new patron[num];
  19.  
  20. for (int i = 0; i < num; ++i)
  21. {
  22. cout <<"please input the "<< i+1 <<"th patron name:";
  23. getline(std::cin, ps[i].name);
  24. cout << "please input the " << i+1 << "th patron money:";
  25. cin >> ps[i].money;
  26. cin.get();
  27. }
  28. int cnt = 0, snt = 0;
  29. cout << "Grand Patrons:" << endl ;
  30. for (int i = 0; i < num; i++)
  31. {
  32. if (ps[i].money >= 10000)
  33. {
  34. cout << ps[i].name << '\t' <<ps[i].money << endl;
  35. ++cnt;
  36. }
  37.  
  38. }
  39. if ( cnt == 0)
  40. {
  41. cout << "none";
  42. }
  43.  
  44. cout << "\nPatrons:" << endl;
  45. for (int i = 0; i < num; i++)
  46. {
  47. if (ps[i].money < 10000)
  48. {
  49. cout << ps[i].name << '\t' << ps[i].money << endl;
  50. ++snt;
  51. }
  52.  
  53. }
  54. if ( snt == 0)
  55. {
  56. cout << "none";
  57. }
  58.  
  59. delete []ps;
  60.  
  61. }

7.编写一个程序,他每次读取一个单词,直到用户只输入q。然后,该程序指出有多少单词以元音打头,有多少个单词以辅音打头,还有多少单词不属于这两类。为此,方法之一是使用isalpha()来区分以字母和其他字符打头的单词,然后对于通过salpha()测试的单词,使用if或switch语句来确定哪些是以元音打头。

  1. #include <iostream>
  2. #include <string>
  3. #include <ctype.h>
  4.  
  5. using namespace std;
  6. void cprimerplus_exercise_6_7()
  7. {
  8. string word;
  9. cout << "Enter words (q to quit):";
  10. int cnt = 0;
  11. int vowel = 0, constant = 0, other = 0;
  12. while (cin >> word && !word.empty())
  13. {
  14. if(isalpha(word[0]))
  15. {
  16. if (word[0] == 'q' && word.length() == 1)
  17. break;
  18. else if( word[0] == 'a' || word[0] == 'e' || word[0] == 'i' || word[0] == 'o' || word[0] == 'u')
  19. {
  20. ++vowel;
  21. }else
  22. {
  23. ++constant;
  24. }
  25. }else
  26. {
  27. ++other;
  28. }
  29. }
  30.  
  31. cout << vowel << " words beginning with vowels "<< endl;
  32. cout << constant <<" words beginning with constants " << endl;
  33. cout << other <<" others" << endl;
  34. }

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

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6. void cprimerplus_exercise_6_8()
  7. {
  8. char ch;
  9. int sum = 0;
  10. ifstream inFile;
  11. inFile.open("1.txt");
  12. if( !inFile.is_open())
  13. {
  14. cout << "Could not open the file!\n";
  15. cout << "Program terminating.\n";
  16. exit(EXIT_FAILURE);
  17. }
  18.  
  19. inFile >> ch;
  20. while (inFile.good())
  21. {
  22. ++sum;
  23. inFile >> ch;
  24. }
  25. if (inFile.eof())
  26. {
  27. cout << "End of file reached.\n";
  28. }else if(inFile.fail())
  29. {
  30. cout << "Input terminated by data mismatch.\n";
  31. }else
  32. {
  33. cout << "Input terminated for unknown reason.\n";
  34. }
  35. cout << "there are " << sum << " chars in this file!\n";
  36. }

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

4
Sam Stone
2000
Freida Flass
10050
Tammy Tubbs
5000
Rich Raptor
5500

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. using namespace std;
  6. const int SIZE = 60;
  7.  
  8. void cprimerplus_exercise_6_9()
  9. {
  10. char filename[SIZE];
  11. ifstream inFile;
  12. cout << "Enter the name of the file: ";
  13. cin.getline(filename,SIZE);
  14. inFile.open(filename);
  15. if (!inFile.is_open())
  16. {
  17. cout << "Could not open the file" << filename << endl;
  18. cout << "Program terminating.\n";
  19. exit(EXIT_FAILURE);
  20. }
  21.  
  22. struct patron
  23. {
  24. string name;
  25. double money;
  26. };
  27.  
  28. int num, cnt = 0, snt = 0;
  29. inFile >> num;
  30. inFile.get();
  31.  
  32. patron *ps = new patron[num];
  33. for (int i = 0; i < num; i++)
  34. {
  35. getline(inFile, ps[i].name);
  36. inFile >> ps[i].money;
  37. inFile.get();
  38. }
  39. cout << "Grand patrons:\n";
  40. for (int i = 0; i < num; i++)
  41. {
  42. if (ps[i].money >= 10000)
  43. {
  44. cout << ps[i].name << '\n' << ps[i].money << endl;
  45. ++cnt;
  46. }
  47. }
  48.  
  49. if ( cnt == 0)
  50. {
  51. cout << "none";
  52. }
  53.  
  54. cout << "\nPatrons:" << endl;
  55. for (int i = 0; i < num; i++)
  56. {
  57. if (ps[i].money < 10000)
  58. {
  59. cout << ps[i].name << '\t' << ps[i].money << endl;
  60. ++snt;
  61. }
  62.  
  63. }
  64. if ( snt == 0)
  65. {
  66. cout << "none";
  67. }
  68.  
  69. delete []ps;
  70.  
  71. inFile.close();
  72. }

c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)的更多相关文章

  1. c++primerplus(第六版)编程题——第4章(复合类型)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  2. c++primerplus(第六版)编程题——第3章(数据类型)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. 工程命名和文件命名可以命名成易识 ...

  3. c++primerplus(第六版)编程题——第5章(循环和关系表达式)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  4. 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)

    第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...

  5. C程序设计(谭浩强)第五版课后题答案 第一章

    大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...

  6. c primer plus(五版)编程练习-第七章编程练习

    1.编写一个程序.该程序读取输入直到遇到#字符,然后报告读取的空格数目.读取的换行符数目以及读取的所有其他字符数目. #include<stdio.h> #include<ctype ...

  7. C++ Primer Plus读书笔记(六)分支语句和逻辑运算符

    1. 以上均包含在cctype中 1 #include<cctype> 2 //#include<ctype.h> 2.文件操作 (1)头文件 1 #include<fs ...

  8. C算法编程题(六)串的处理

    前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...

  9. 某软件大赛C#版考题整理——【编程题】

    三.编程题(4小题共40.0分)程序及结果写入对应文框内 1. 孪生素数查找程序. 所谓孪生素数指的是间隔为2 的相邻素数,就像孪生兄弟.最小的孪生素数是(3, 5),在100 以内的孪生素数还有 ( ...

随机推荐

  1. decodeURIComponent

    var s = '%%' try { s = decodeURIComponent(s) } catch(e) { console.log(e) } console.log(s)

  2. 如何配置jdk和tomcat 转

    一.配置JDK1.解压JDK至D:\JDK1.5目录下(楼主可以自由选取目录).2.设置环境变量(右键我得电脑->属性->高级->环境变量),在系统变量中添加一个叫JAVA_HOME ...

  3. 怎样打开64位 Ubuntu 的32位支持功能?

    怎样打开64位 Ubuntu 的32位支持功能? 现在有一个让你可以在64位系统中使用32位软件的方法,就在你读了这篇文章然后照着做了之后就可以了.如果你有一个13.10或更高版本的Ubuntu/De ...

  4. mac下的改装人生——关于机械键盘

    这几天好像弄了很多关于机械键盘的东西,我自己的这块键盘也已经慢慢熟悉了,感觉打字超级爽哈,然后看了很多网上关于机械键盘的帖子,也看了很多教程,在Amazon和Taobao看了很多键盘的价位,前几天还试 ...

  5. 基础排序算法之快速排序(Quick Sort)

    快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...

  6. lightoj 1036 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1036 #include <cstdio> #include <cst ...

  7. linux mono

    linux下.net环境; rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm &am ...

  8. Redis教程03——Redis 发布/订阅(Pub/Sub)

    Pub/Sub 订阅,取消订阅和发布实现了发布/订阅消息范式(引自wikipedia),发送者(发布者)不是计划发送消息给特定的接收者(订阅者).而是发布的消息分到不同的频道,不需要知道什么样的订阅者 ...

  9. stickyListHeaders框架的使用

    最近在Demo中使用到了stickyListHeaders框架实现悬停效果 比较好用 在xml中 <se.emilsjolander.stickylistheaders.StickyListHe ...

  10. 封装Unity3d的dll时的经验总结

    部分时候,我们需要自己封装一些小工具来简化我们的工作. 实验时,偶然发现Unity3d的console在双击进行debug信息的输出定位时,只能跟进到dll的上一层,因此我们可以将unity3d自带的 ...