• C++PrimerPlus 第六章 分支语句和逻辑运算符(编程练习含答案)


    C++PrimerPlus 第六章 分支语句和逻辑运算符(编程练习含答案)

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

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. char ch;
    7. while (cin.get(ch) && ch != '@') {
    8. if (!isdigit(ch))
    9. if (islower(ch))
    10. cout << char(toupper(ch));
    11. else if (isupper(ch))
    12. cout << char(tolower(ch));
    13. else
    14. cout << ch;
    15. }
    16. return 0;
    17. }

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

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. const int SIZE = 10;
    7. array<double, SIZE> arr;
    8. int num = 0, sum = 0;
    9. while (num < SIZE && cin >> arr[num]) {
    10. cout << "#" << num + 1 << ":";
    11. sum += arr[num];
    12. num++;
    13. }
    14. int average = sum / num;
    15. int aveMoreNum = 0;
    16. for (int i = 0; i < num; i++) {
    17. if (arr[i] > average)
    18. aveMoreNum++;
    19. }
    20. cout << "The average value is: " << average << endl;
    21. cout << "There are " << aveMoreNum << " double value large than average value." << endl;
    22. if(!cin)
    23. {
    24. cin.clear();
    25. cin.get();
    26. }
    27. return 0;
    28. }

    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

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. cout << "Please enter one of the following choices:\nc) carnivore p) pianist\nt) tree g) game" << endl;
    7. char ch;
    8. cout << "Please enter a c, p, t, or g: ";
    9. cin >> ch;
    10. while ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g')) {
    11. cout << "Please enter a c, p, t, or g: ";
    12. cin >> ch;
    13. }
    14. string str;
    15. switch (ch) {
    16. case 'c':str = "carnivore"; break;
    17. case 'p':str = "pianist"; break;
    18. case 't':str = "tree"; break;
    19. case 'g':str = "game"; break;
    20. }
    21. cout << "A maple is a " << str << "." << endl;
    22. return 0;
    23. }

    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

            d. quit

    注意, “display by preference”并不意味着显示成员的偏好,而是意味着根据成员的偏好来列出成员。例如,如果偏好号为1,则选择d将显示程序员的头衔。该程序的运行情况如下:

            Benevolent Order of Programmers Report

            a. display by name           b. display by title

            c. display by bopname     d. display by preference

            d. 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!

    1. #include
    2. using namespace std;
    3. const unsigned int strsize = 128;
    4. struct bop {
    5. char fullname[strsize];
    6. char title[strsize];
    7. char bopname[strsize];
    8. int preference;
    9. };
    10. void DisplayByName(const struct bop* arrBOP, unsigned int size) {
    11. for (int i = 0; i < size; i++) {
    12. cout << arrBOP[i].fullname << endl;
    13. }
    14. }
    15. void DisplayByTitle(const struct bop* arrBOP, unsigned int size) {
    16. for (int i = 0; i < size; i++) {
    17. cout << arrBOP[i].title << endl;
    18. }
    19. }
    20. void DisplayByBopname(const struct bop* arrBOP, unsigned int size) {
    21. for (int i = 0; i < size; i++) {
    22. cout << arrBOP[i].bopname << endl;
    23. }
    24. }
    25. void DisplayByPreference(const struct bop* arrBOP, unsigned int size) {
    26. for (int i = 0; i < size; i++) {
    27. if (arrBOP[i].preference == 0)
    28. cout << arrBOP[i].fullname << endl;
    29. else if (arrBOP[i].preference == 1)
    30. cout << arrBOP[i].title << endl;
    31. else
    32. cout << arrBOP[i].bopname << endl;
    33. }
    34. }
    35. int main()
    36. {
    37. bop* arrBOP = new bop[strsize];
    38. cout << "Benevolent Order of Programmers Report" << endl;
    39. cout << "a. display by name \t b. display by title" << endl;
    40. cout << "c. display by bopname \t d. display by preference" << endl;
    41. cout << "q. quit" << endl;
    42. cout << "Enter your choice: ";
    43. char choice = 0;
    44. while (cin >> choice) {
    45. if (choice == 'q') {
    46. break;
    47. }
    48. if (choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd') {
    49. cout << "Please enter a, b, c, d, q:";
    50. continue;
    51. }
    52. switch (choice) {
    53. case 'a':
    54. DisplayByName(arrBOP, strsize);
    55. break;
    56. case 'b':
    57. DisplayByTitle(arrBOP, strsize);
    58. break;
    59. case 'c':
    60. DisplayByBopname(arrBOP, strsize);
    61. break;
    62. case 'd':
    63. DisplayByPreference(arrBOP, strsize);
    64. break;
    65. }
    66. cout << "Next choice: ";
    67. }
    68. cout << "Bye!" << endl;
    69. return 0;
    70. }

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

            5000tvarps:不收税

            5001~15000tvarps:10%

            15001~35000tvarps:15%

            35001tvarps以上:20%

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

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. double money;
    6. cout << "Please enter your income: ";
    7. while (cin >> money && money >= 0) {
    8. double tex = 0;
    9. if (money > 0 && money <= 5000) {
    10. tex = 0;
    11. }
    12. else if (money > 5000 && money <= 15000) {
    13. tex = (money - 5000) * 0.1;
    14. }
    15. else if (money > 15000 && money <= 35000) {
    16. tex = 10000 * 0.1 + (money - 15000) * 0.15;
    17. }
    18. else {
    19. tex = 10000 * 0.1 + 20000 * 0.15 + (money - 35000) * 0.2;
    20. }
    21. cout << "The tex you should pay is: " << tex << endl;
    22. cout << "Please enter your income: ";
    23. }
    24. return 0;
    25. }

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

    1. #include
    2. #include
    3. using namespace std;
    4. struct Person {
    5. string name = "";
    6. double money = 0;
    7. };
    8. int main()
    9. {
    10. cout << "Enter the number of Person: ";
    11. int numPerson = 0;
    12. cin >> numPerson;
    13. Person* arrPerson = new Person[numPerson];
    14. for (int i = 0; i < numPerson; i++) {
    15. cout << "#" << i + 1 << " Person: " << endl;
    16. cout << "Name: ";
    17. cin >> arrPerson[i].name;
    18. cout << "Money: ";
    19. cin >> arrPerson[i].money;
    20. }
    21. cout << "Grand Patrons" << endl;
    22. int numGrandPatrons = 0;
    23. for (int i = 0; i < numPerson; i++) {
    24. if (arrPerson[i].money > 10000) {
    25. cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
    26. numGrandPatrons++;
    27. }
    28. }
    29. if (numGrandPatrons == 0)
    30. cout << "NONE" << endl;
    31. cout << "Patrons" << endl;
    32. int numPatrons = 0;
    33. for (int i = 0; i < numPerson; i++) {
    34. if (arrPerson[i].money <= 10000) {
    35. cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
    36. numPatrons++;
    37. }
    38. }
    39. if(numPatrons == 0)
    40. cout << "NONE" << endl;
    41. delete[] arrPerson;
    42. return 0;
    43. }

    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 with consonants

            2 others

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. cout << "Enter words (q to quit): " << endl;
    8. string str;
    9. int numVow = 0, numCons = 0, numOther = 0;
    10. while (cin >> str) {
    11. if (str == "q")
    12. break;
    13. if (isalpha(str[0]))
    14. {
    15. switch(str[0])
    16. {
    17. case 'a':case 'A':
    18. case 'o':case 'O':
    19. case 'e':case 'E':
    20. case 'i':case 'I':
    21. case 'u':case 'U':
    22. numVow++;
    23. break;
    24. default:
    25. numCons++;
    26. break;
    27. }
    28. }
    29. else {
    30. numOther++;
    31. }
    32. }
    33. cout << numVow << " words beginning with vowels" << endl;
    34. cout << numCons << " words beginning with consonants" << endl;
    35. cout << numOther << " others" << endl;
    36. return 0;
    37. }

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

    1. #include
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. ifstream ifs;
    7. ifs.open("words.txt");
    8. if (!ifs)
    9. {
    10. cout << "文件未打开!!!" << endl;
    11. exit(0);
    12. }
    13. char ch;
    14. int num = 0;
    15. while (ifs >> ch)
    16. num++;
    17. cout << "文件中的字符数:" << num << endl;
    18. return 0;
    19. }

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

            4

            Sam Stone

            2000

            Freida Flass

            100500

            Tammy Tubbs

            5000

            Rich Raptor

            55000

     

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. struct Person {
    6. string name = "";
    7. double money = 0;
    8. };
    9. int main()
    10. {
    11. int numPerson = 0;
    12. string filename;
    13. ifstream ifs;
    14. cout << "Enter the file name: ";
    15. getline(cin, filename);
    16. ifs.open(filename.c_str());
    17. ifs >> numPerson;
    18. ifs.get();
    19. Person* arrPerson = new Person[numPerson];
    20. for (int i = 0; i < numPerson; i++) {
    21. getline(ifs, arrPerson[i].name);
    22. ifs >> arrPerson[i].money;
    23. ifs.get();
    24. }
    25. cout << "Grand Patrons" << endl;
    26. int numGrandPatrons = 0;
    27. for (int i = 0; i < numPerson; i++) {
    28. if (arrPerson[i].money > 10000) {
    29. cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
    30. numGrandPatrons++;
    31. }
    32. }
    33. if (numGrandPatrons == 0)
    34. cout << "NONE" << endl;
    35. cout << "Patrons" << endl;
    36. int numPatrons = 0;
    37. for (int i = 0; i < numPerson; i++) {
    38. if (arrPerson[i].money <= 10000) {
    39. cout << arrPerson[i].name << " " << arrPerson[i].money << endl;
    40. numPatrons++;
    41. }
    42. }
    43. if (numPatrons == 0)
    44. cout << "NONE" << endl;
    45. delete[] arrPerson;
    46. return 0;
    47. }

  • 相关阅读:
    快手自研Spark向量化引擎正式发布,性能提升200%
    JAVA学习-全网最详细
    Python数据容器:set(集合)
    编写高效的代码,你应该了解Array、Memory、ReadOnlySequence . . .
    华为OD机试真题 Java 实现【简易内存池】【2023 B卷 200分 考生抽中题】
    时序预测 | MATLAB实现AR、ARMA、ARIMA时间序列预测模型答疑
    SLAM从入门到精通(矩阵的使用)
    QT mysql 数据库线程池 与数据库操作封装
    Web应用防火墙的性能优化技术
    【qstock】几行代码实现数据获取、可视化到量化选股实战
  • 原文地址:https://blog.csdn.net/qq_40660998/article/details/126026640