一、复习题

3.

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. char ch;
  7. int c1, c2;
  8. c1 = c2 = ;
  9.  
  10. while ((ch=cin.get())!='$')
  11. {
  12. cout << ch;
  13. c1++;
  14. if (ch = '$') //注意是=,不是==
  15. c2++;
  16. cout << ch;
  17. }
  18. cout << "c1=" << c1 << ",c2=" << c2 << endl;
  19.  
  20. system("pause");
  21. }

二、编程练习

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

  1. #include<iostream>
  2. #include<cctype>
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7. char ch;
  8. while ((ch = cin.get()) && ch != '@')
  9. {
  10. if (!isdigit(ch)) {
  11. if (islower(ch)) //如果是小写字符
  12. ch = toupper(ch);//转化为大写
  13. else if (isupper(ch))
  14. ch = tolower(ch);
  15. cout << ch;
  16. }
  17. }
  18.  
  19. system("pause");
  20. }

2. 编写一个程序,最多将10个donation值读到一个double数组中。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. const int SIZE = ;
  5. void main()
  6. {
  7. double donation[], sum = 0.0,average;
  8. int i,count=;
  9. for (i = ; i < SIZE; i++)
  10. {
  11. if (cin >> donation[i])
  12. sum += donation[i];
  13. else
  14. break;//退出最近的循环,即退出for循环
  15. }
  16. if (i == )
  17. return;
  18. else
  19. average = sum / i;
  20. for (int j = ; j < i; j++)
  21. {
  22. if (donation[j] > average)
  23. count++;
  24. }
  25. cout << "average=" << average << "," << count << " numbers bigger than average." << endl;
  26.  
  27. system("pause");
  28. }

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

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void showA() {
  5. cout << "Please enter one of the following choices:\n"
  6. << "a) carnivore\t" << "b) pianist\n"
  7. << "c) tree\t\t" << "d) game\n";
  8. }
  9.  
  10. void main()
  11. {
  12. showA();
  13. char ch;
  14. cin >> ch;
  15. int flag = ;
  16. while ()
  17. {
  18. switch (ch)
  19. {
  20. case 'a':cout << "A maple is a carnivore." << endl;
  21. flag = ;
  22. break;
  23. case 'b':cout << "A maple is a pianist." << endl;
  24. flag = ;
  25. break;
  26. case 'c':cout << "A maple is a tree." << endl;
  27. flag = ;
  28. break;
  29. case 'd':cout << "A maple is a game." << endl;
  30. flag = ;
  31. break;
  32. default:cout << "Please enter a,b,c,d :";
  33. cin >> ch;
  34. }
  35. if (flag)
  36. break;
  37. }
  38. system("pause");
  39. }

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. using namespace std;
  3.  
  4. const int strsize = ,num=;
  5. struct bop
  6. {
  7. char fullname[strsize]; //真实姓名
  8. char title[strsize]; //头衔
  9. char bopname[strsize]; //秘密姓名
  10. int preference; //偏好,当为0时为fullname,1为title,2为bopname
  11. };
  12.  
  13. void show() {
  14. cout<< "Benevolent Order of Programmers Report\n"
  15. << "a. display by name\t" << "b. display by title\n"
  16. << "c. display by bopname\t"<< "d. display by preference\n"
  17. << "q. quit\n";
  18. }
  19.  
  20. void main()
  21. {
  22. bop p[num] = {
  23. { "王一","程序员","first", },
  24. { "张二", "老师", "2nd", },
  25. { "李三", "公务员", "3rd", },
  26. { "赵四", "咨询师", "4th", },
  27. { "南宫五", "理发师", "5th", },
  28. { "龙六", "玩家", "6th", }
  29. };
  30. char ch;
  31. show();
  32. int flag = ;
  33. cout << "Enter your choice:";
  34. int l = sizeof(p)/sizeof(p[]);
  35. while (cin>>ch)
  36. {
  37. switch (ch)
  38. {
  39. case 'a':
  40. for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
  41. cout << p[i].fullname << endl;
  42. break;
  43. case 'b':
  44. for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
  45. cout << p[i].title << endl;
  46. break;
  47. case 'c':
  48. for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
  49. cout << p[i].bopname << endl;
  50. break;
  51. case 'd':
  52. for (int i = ; i < num&&strlen(p[i].fullname) != ; i++)
  53. {
  54. switch (p[i].preference)
  55. {
  56. case :cout << p[i].fullname << endl;
  57. break;
  58. case :cout << p[i].title << endl;
  59. break;
  60. case :cout << p[i].bopname << endl;
  61. break;
  62. }
  63. }
  64. break;
  65. case 'q':
  66. flag = ;
  67. cout << "Bye!\n";
  68. break;
  69. default:
  70. cout << "Enter error!";
  71. break;
  72. }
  73. if (flag)
  74. break;
  75. cout << "Next choice:";
  76. }
  77. system("pause");
  78. }

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

5000 tvarps:不收税

5001~15000 tvarps: 10%

15001~35000 tvarps: 15%

35000 tvarps以上: 20%

例如,收入为38000 tvarps时,所得税为5000x0.00 + 10000x0.10 + 20000x0.15+3000x0.20,即4600 tvarps。请编写一个程序,使用循环来要求用户输入收入,并报告所得税。当用户输入负数或非数字时,循环将结束。

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void main()
  5. {
  6. double money;
  7. cout << "请输入您的收入:";
  8. while (cin>>money)
  9. {
  10. if (money < )
  11. break;
  12. else if (money <= )
  13. cout << "不交税" << endl;
  14. else if (money <= )
  15. cout << (money - )*0.1 << endl;
  16. else if (money <= )
  17. cout <<*0.1+(money - )*0.15 << endl;
  18. else
  19. cout << * 0.1 +*0.15+ (money - )*0.2 << endl;
  20. cout << "请输入您的收入:";
  21. }
  22.  
  23. system("pause");
  24. }

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

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. struct donation
  6. {
  7. string name;
  8. double money;
  9. };
  10.  
  11. void main()
  12. {
  13. cout << "请输入捐献者数量:";
  14. int num;
  15. cin >> num;
  16. int s[] = { };
  17. donation *p = new donation[num];
  18.  
  19. for (int i = ; i < num; i++)
  20. {
  21. cout << "请输入第" << (i + ) << "个捐献者的姓名:";
  22. cin >> p[i].name;
  23. cout << "请输入第" << (i + ) << "个捐献者的捐款数额:";
  24. cin >> p[i].money;
  25. }
  26. int flag = ;//标示,判断是否打印“none”
  27. cout << "重要捐款人如下:" << endl;
  28. for (int i = ; i < num; i++)
  29. {
  30. if (p[i].money > )
  31. {
  32. cout << p[i].name << "捐款" << p[i].money << "元" << endl;
  33. flag = ;
  34. }
  35. }
  36. if (flag == )
  37. cout << "none" << endl;
  38. flag = ;
  39. cout << "其他捐款人如下:" << endl;
  40. for (int i = ; i < num; i++)
  41. {
  42. if (p[i].money <= )
  43. {
  44. cout << p[i].name << "捐款" << p[i].money << "元" << endl;
  45. flag = ;
  46. }
  47. }
  48. if (flag == )
  49. cout << "none" << endl;
  50. delete p[];//用完new一定要记得delete
  51. system("pause");
  52. }

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

  1. #include<iostream>
  2. #include<cctype>
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7. cout << "Enter words (q to quit):" << endl;
  8. char ch;
  9. cin.get(ch);
  10. int vowel = , consonant = , other = , flag = ;
  11. while (ch != 'q')
  12. {
  13. if (isalpha(ch)) {//如果参数是字母,返回true
  14. if (flag)
  15. {
  16. switch (ch) {
  17. case 'a':
  18. case 'A':
  19. vowel++;
  20. flag = ; break;
  21. case 'e':
  22. case 'E':
  23. vowel++;
  24. flag = ; break;
  25. case 'i':
  26. case 'I':
  27. vowel++;
  28. flag = ; break;
  29. case 'o':
  30. case 'O':
  31. vowel++;
  32. flag = ; break;
  33. case 'u':
  34. case 'U':
  35. vowel++;
  36. flag = ; break;
  37. default:
  38. consonant++;
  39. flag = ; break;
  40. }
  41. }
  42. }
  43. else if (isspace(ch) || ispunct(ch))//如果参数是空格、tab、换行等空白字符,或者是标点符号,返回true
  44. flag = ;
  45. else {
  46. if (flag)
  47. {
  48. other++;
  49. flag = ;
  50. }
  51. }
  52. cin.get(ch);
  53. }
  54. cout << vowel << " words beginning with vowels " << endl;
  55. cout << consonant << " words beginning with consonants " << endl;
  56. cout << other << " others" << endl;
  57. system("pause");
  58. }

这是一串有问题的代码,找了半天实在不知道错在哪,先贴在这儿,看以后能发现错误不

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

  1. #include<iostream>
  2. #include<fstream>//文件IO
  3. using namespace std;
  4.  
  5. void main()
  6. {
  7. ifstream inFile;//声明ifstream对象
  8. inFile.open("1.txt");//关联文件
  9.  
  10. if (!inFile.is_open())//文件打开失败
  11. {
  12. cout << "Could not open the file " << endl;
  13. exit(EXIT_FAILURE);
  14. }
  15. char ch;
  16. int count = ;
  17.  
  18. inFile >> ch;
  19. while (inFile.good())//输入正确
  20. {
  21. ++count;
  22. cout << ch;
  23. inFile >> ch;
  24. }
  25. cout << endl;
  26.  
  27. if (inFile.eof())
  28. cout << "End of file reached." << endl;
  29. else if (inFile.fail())
  30. cout << "Input terminated by data misamatch." << endl;
  31. else
  32. cout << "Input terminated for unknown reason." << endl;
  33.  
  34. cout << "文件包含" << count<<"个字符" << endl;
  35. inFile.close();
  36.  
  37. system("pause");
  38. }

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

4

Sam Stone

2000

Freida Flass

100500

Tammy Tubbs

5000

Rich Raptor

55000

  1. #include<iostream>
  2. #include<fstream>//文件IO
  3. #include<string>
  4. using namespace std;
  5.  
  6. struct donation
  7. {
  8. string name;
  9. double money;
  10. };
  11. void main()
  12. {
  13. ifstream inFile;//声明ifstream对象
  14. inFile.open("1.txt");//关联文件
  15. if (!inFile.is_open())//文件打开失败
  16. {
  17. cout << "Could not open the file " << endl;
  18. exit(EXIT_FAILURE);
  19. }
  20.  
  21. int num, i = ;
  22. inFile >> num;
  23. inFile.get();//抵消换行
  24. donation *p = new donation[num];
  25. for (int i = ; i < num; i++)
  26. {
  27. getline(inFile, p[i].name);
  28. inFile >> p[i].money;
  29. inFile.get();//抵消换行
  30. }
  31.  
  32. int flag = ;//标示,判断是否打印“none”
  33. cout << "重要捐款人如下:" << endl;
  34. for (int i = ; i < num; i++)
  35. {
  36. if (p[i].money > )
  37. {
  38. cout << p[i].name << "捐款" << p[i].money << "元" << endl;
  39. flag = ;
  40. }
  41. }
  42. if (flag == )
  43. cout << "none" << endl;
  44. flag = ;
  45. cout << "其他捐款人如下:" << endl;
  46. for (int i = ; i < num; i++)
  47. {
  48. if (p[i].money <= )
  49. {
  50. cout << p[i].name << "捐款" << p[i].money << "元" << endl;
  51. flag = ;
  52. }
  53. }
  54. if (flag == )
  55. cout << "none" << endl;
  56.  
  57. if (inFile.eof())
  58. cout << "End of file reached." << endl;
  59. else if (inFile.fail())
  60. cout << "Input terminated by data misamatch." << endl;
  61. else
  62. cout << "Input terminated for unknown reason." << endl;
  63.  
  64. inFile.close();
  65. delete [] p;//用完new一定要记得delete
  66. system("pause");
  67. }

[C++ Primer Plus] 第6章、分支语句和逻辑运算符(二)课后习题的更多相关文章

  1. C++ primer plus读书笔记——第6章 分支语句和逻辑运算符

    第6章 分支语句和逻辑运算符 1. 逻辑运算符的优先级比关系运算符的优先级低. 2. &&的优先级高于||. 3. cctype中的函数P179. 4. switch(integer- ...

  2. C++ Primer Plus 6th 读书笔记 - 第6章 分支语句和逻辑运算符

    1. cin读取错误时对换行符的处理 #include <iostream> using namespace std; int main() { double d; char c; cin ...

  3. [C++ Primer Plus] 第6章、分支语句和逻辑运算符(一)程序清单

    程序清单6.2 #include<iostream> using namespace std; void main() { char ch; cout << "Typ ...

  4. c++primerplus(第六版)编程题——第6章(分支语句和逻辑运算符)

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

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

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

  6. [C++ Primer Plus] 第8章、函数探幽(二)课后习题

    1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数.不过,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第 ...

  7. [C++ Primer Plus] 第4章、复合类型(二)课后习题

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

  8. [C++ Primer Plus] 第3章、处理数据(二)课后习题

    1 . 编写一个小程序,要求用户使用一个整数输出自己的身高(单位为厘米),然后将身高转换为米和厘米.该程序使用下划线字符来指示输入位置.另外,使用一个 const 符号常量来表示转换因子. #incl ...

  9. 重拾c++第三天(6):分支语句与逻辑运算符

    1.逻辑运算符 && || ! 2.关系运算符优先级高于逻辑运算符 3.cctype库中好用的判断 4. ?:符号用法: 状态1?结果1:结果2 5.switch用法: switch ...

随机推荐

  1. 把一张图片变成base64

    // image_file可为urlprivate function base64EncodeImage($image_file) { $image_info = getimagesize($imag ...

  2. CentOS从源码安装Erlang

    ### 首先下载资源 wget http://erlang.org/download/otp_src_18.3.tar.gz ### 解压 .tar.gz ### 安装依赖包 yum install ...

  3. vue中子传父,父传子的具体用法

    先说明下父组件Login,子组件signCon 子拿到父数据可以通过,在子组件里面设置props:['name']的方法拿到. 首先在父组件中定义数据了: data(){Englishname:'li ...

  4. 求最短路的三种方法:dijkstra,spfa,floyd

    dijkstra是一种单源最短路算法.在没有负权值的图上,vi..vj..vk是vi到vk最短路的话,一定要走vi到vj的最短路.所以每次取出到起点距离最小的点,从该点出发更新邻接的点的距离,如果更新 ...

  5. Gym 101873G - Water Testing - [皮克定理]

    题目链接:http://codeforces.com/gym/101873/problem/G 题意: 在点阵上,给出 $N$ 个点的坐标(全部都是在格点上),将它们按顺序连接可以构成一个多边形,求该 ...

  6. Python爬虫实例(四)网站模拟登陆

    一.获取一个有登录信息的Cookie模拟登陆 下面以人人网为例,首先使用自己的账号和密码在浏览器登录,然后通过抓包拿到cookie,再将cookie放到请求之中发送请求即可,具体代码如下: # -*- ...

  7. vue开发记录--通用时间格式函数

    parseTime(time, fm) { // 解析时间 time: 时间戳或者实践对象 fm: 格式 默认是{y}-{m}-{d} {h}:{i}:{s} if (arguments.length ...

  8. HTML链接式引入CSS和JS

    <!-调用CSS-> <link href="./XXXXX.css" rel="stylesheet" type="text/cs ...

  9. 深入理解Spring AOP之二代理对象生成

    深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...

  10. Ubuntu上Qt+Tcp网络编程之简单聊天对话框

    首先看一下实现结果: >>功能: (1)服务器和客户端之间进行聊天通信: (2)一个服务器可同时给多个客户端发送消息:(全部连接时)   也可以只给特定的客户端发送消息:(连接特定IP) ...