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


     1、请看下面两个计算空格和换行符数目的代码片段:

    //Version 1

    1. while(cin.get(ch)) //quit on eof
    2. {
    3. if(ch == ‘ ’)
    4. spaces++;
    5. if(ch == ‘\n’)
    6. newlines++;
    7. }

    //Version 2

    1. while(cin.get(ch)) //quit on eof
    2. {
    3. if(ch == ‘ ’)
    4. spaces++;
    5. else if(ch == ‘\n’)
    6. newlines++;
    7. }

    第二种格式比第一种格式好在哪里呢?

    这两个版本将给出相同的答案,但if else版本的效率更高。例如,考虑当ch为空格时的情况。版本1对空格加1,然后看它是否为换行符。这将浪费时间,因为程序已经知道ch为空格,因此它不是换行符。在这种情况下,版本2将不会查看字符是否为换行符。

     2、在程序清单6.2中,用ch+1替换++ch将发生什么情况呢?

     程序清单6.2 ifelse.cpp

    1. //ifelse.cpp -- using the if else statement
    2. #include
    3. int main()
    4. {
    5. char ch;
    6. std::cout << "Type, and I shall repeat.\n";
    7. std::cin.get(ch);
    8. while (ch != '.')
    9. {
    10. if (ch == '\n')
    11. std::cout << ch; //done if newline
    12. else
    13. std::cout << ++ch; //done otherwise
    14. std::cin.get(ch);
    15. }
    16. //try ch + 1 instead of ++ch for interesting effect
    17. std::cout << "\nPlease excuse the slight confusion.\n";
    18. //std::cin.get();
    19. //std::cin.get();
    20. return 0;
    21. }

    ++ch和ch+1得到的数值相同。但++ch的类型为char,将作为字符打印,而ch+1是int类型(因为将char和int相加),将作为数字打印。

    3、请认真考虑下面的程序:

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. char ch;
    6. int ct1, ct2;
    7. ct1 = ct2 = 0;
    8. while ((ch = cin.get()) != '$')
    9. {
    10. cout << ch;
    11. ct1++;
    12. if (ch = '$')
    13. ct2++;
    14. cout << ch;
    15. }
    16. cout << "ct1 = " << ct1 << ", ct2 = " << ct2 << "\n";
    17. return 0;
    18. }

    假设输入如下(请在每行末尾按回车键):

            Hi!

            Send $10 or $20 now!

    则输出将是什么(还记得吗。输入被缓冲)?

    由于程序使用ch = '$',而不是ch == '$',因此输入和输出将如下:

            Hi!

            H$i$!$

            $Send $10 or $20 now!

            S$e$n$d$ $ct1 = 9, ct2 = 9

    在第二次打印前,每个字符都被转换为$字符。另外,表达式ch = '$'的值为$字符的编码,因此它是非0值,因而为true;所以每次ct2将被加1。

    4、创建表示下述条件的逻辑表达式:

            a. weight大于或等于115,但小于125

            b. ch为q或Q

            c. x为偶数,但不是26

            d. x为偶数,但不是26的倍数

            e. donation为1000-2000或guest为1

            f. ch是小写字母或大写字母(假设小写字母是依次编码的,大写字母也是依次编码的,但在大小写字母间编码不是连续的)。

    a. weight >= 115 && weight < 125

    b. ch == q || ch == Q

    c. x % 2 == 0 && x != 26

    d. x % 2 == 0 && !(x % 26 == 0)

    e. donation >= 1000 && donation <= 2000 || guest == 1

    f. (ch >= a && ch <= z) || (ch >= A && ch <= Z)

    5、在英语中,“I will not not speak(我不会不说)”的意思与“I will speak(我要说)”相同。在C++中,!!x是否与x相同呢?

    不一定。如果x为10,则!x为0,!!x为1。然而,如果x为bool变量,则!!x为x。

    6、创建一个条件表达式,其值为变量的绝对值。也是说,如果变量x为正,则表达式的值为x;但如果x为负,则表达式的值为-x——这是一个正值。

    (x < 0) ? -x : x

    (x >= 0) ? x : -x

    7、用switch改写下面的代码片段:

    1. if (ch == 'A')
    2. a_grade++;
    3. else if (ch == 'B')
    4. b_grade++;
    5. else if (ch == 'C')
    6. c_grade++;
    7. else if (ch == 'D')
    8. d_grade++;
    9. else
    10. f_grade++;

    答案:

    1. switch (ch)
    2. {
    3. case 'A':a_grade++;
    4. break;
    5. case 'B':b_grade++;
    6. break;
    7. case 'C':c_grade++;
    8. break;
    9. case 'D':d_grade++;
    10. break;
    11. default:f_grade++;
    12. break;
    13. }

    8、对于程序清单6.10,与使用数字相比,使用字符(如a和c)表示菜单选项和case标签有何优点呢?(提示:想想用户输入q和输入5的情况。)

    程序清单6.10 switch.cpp

    1. //switch.cpp -- using the switch statement
    2. #include
    3. using namespace std;
    4. void showmenu(); //function prototypes
    5. void report();
    6. void comfort();
    7. int main()
    8. {
    9. showmenu();
    10. int choice;
    11. cin >> choice;
    12. while (choice != 5)
    13. {
    14. switch (choice)
    15. {
    16. case 1:cout << "\a\n";
    17. break;
    18. case 2:report();
    19. break;
    20. case 3:cout << "The boss was in all day.\n";
    21. break;
    22. case 4: comfort();
    23. break;
    24. default:cout << "That's not a choice.\n";
    25. }
    26. showmenu();
    27. cin >> choice;
    28. }
    29. cout << "Bye!\n";
    30. return 0;
    31. }
    32. void showmenu()
    33. {
    34. cout << "Please enter 1, 2, 3, 4, or 5:\n"
    35. "1) alarm 2) report\n"
    36. "3) alibi 4) comfort\n"
    37. "5) quit\n";
    38. }
    39. void report()
    40. {
    41. cout << "It's been an excellent week for business.\n"
    42. "Sales are up 120%. Expenses are down 35%.\n";
    43. }
    44. void comfort()
    45. {
    46. cout << "Your employees think you are the finest CEO\n"
    47. "in the industry. The board of directors think\n"
    48. "you are the finest CEO in the industry.\n";
    49. }

    如果使用整数标签,且用户输入了非整数(如q),则程序将因为整数输入不能处理字符而挂起。但是,如果使用字符标签,而用户输入了整数(如5),则字符输入将5作为字符处理。然后,switch语句的default部分将提示输入另一个字符。

    9、请看下面的代码片段:

    1. int line = 0;
    2. char ch;
    3. while (cin.get(ch))
    4. {
    5. if (ch == 'Q')
    6. break;
    7. if (ch != '\n')
    8. continue;
    9. line++;
    10. }

    请重写该代码片段,不要使用break和continue语句。

    答案:

    1. int line = 0;
    2. char ch;
    3. while (cin.get(ch) && ch != 'Q')
    4. {
    5. if (ch == '\n')
    6. line++;
    7. }
  • 相关阅读:
    【HTML】HTML网页设计----动漫网站设计
    大数据开发写sql写烦了,要不要转?
    vue中,封装组件demo
    【Linux学习】Linux编辑器-vim使用
    计算机网络实验五 子网划分与路由器配置
    leetcode刷题:动态规划01(斐波那契数列)
    Notion 出现白屏的处理
    数据结构001:最大子数组和
    《python 数据可视化基础》第三章 散点图 scatter
    一篇概全,接口测试知识盲扫,真正的接口测试是如何做的...
  • 原文地址:https://blog.csdn.net/qq_40660998/article/details/126011909