《C++ primer》第五版习题答案整理——第五章 语句待排版

    xiaoxiao2022-07-04  143

    P155

    练习5.1

    空语句是只含一个分号的语句,表示当前什么也不做。

    在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该使用空语句。

     

    练习5.2

    块就是用花括号括起来的(可能为空的)语句和声明的序列,一个块就是一个作用域。

    在程序的某些地方,语法上需要一条语句,但是逻辑上需要多条语句,则应该使用复合语句。

     

    练习5.3

    会降低代码的可读性

    while(a > 1)

         sum += a ,a--;

     

    P156

    练习5.4

    其他代码也需要访问控制变量时,变量需要定义在语句的外部

    (a):iter变量未初始化,且需要定义在语句的外部

    (b):if部分无意义,在while循环中已经完成了判断

     

    P159

    练习5.5

    #include "stdafx.h"

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

           int i;

           cin >> i;

           if (i > 90)

           {

                  cout << "成绩为:A" << endl;

           }

           else if(i > 60)

           {

                  cout << "成绩为:B" << endl;

           }

           else

           {

                  cout << "成绩为:C" << endl;

           }

           system("pause");

    }

     

    练习5.6

    #include "stdafx.h"

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

           int i;

           cin >> i;

           char s = 'A';

           s = i < 60 ? 'C' : (i < 90 ? 'B' : 'A');

           cout << "成绩为" << s << endl;

     

     

           system("pause");

    }

     

    练习5.7

    ival1 = ival2 ; //缺少一个分号使用了两条语句,需要使用花括号括起来下面的if需要换成else if=替换为==

     

    练习5.8

    悬垂else:C++规定,else与其最近的尚未匹配的if相匹配

     

    P164

    练习5.9

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

    void main()

    {     

           char cval;

           int sum_a = 0,sum_e = 0,sum_i = 0,sum_o = 0,sum_u = 0;

           while (cin >> cval)

           {

                  if (cval == 'a')

                  {

                         sum_a++;

                  }

                  else if (cval == 'e')

                  {

                         sum_e++;

                  }

                  else if (cval == 'i')

                  {

                         sum_i++;

                  }

                  else if (cval == 'o')

                  {

                         sum_o++;

                  }

                  else if (cval == 'u')

                  {

                         sum_u++;

                  }

           }

           cout<<"元音字母a的个数为:"<<sum_a<<endl;

           cout<<"元音字母e的个数为:"<<sum_e<<endl;

           cout<<"元音字母i的个数为:"<<sum_i<<endl;

           cout<<"元音字母o的个数为:"<<sum_o<<endl;

           cout<<"元音字母u的个数为:"<<sum_u<<endl;

    }

     

    练习5.10

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

    void main()

    {     

           char cval;

           int char_a = 0,char_e = 0,char_i = 0,char_o = 0,char_u = 0;

           while (cin >> cval)

           {

                  switch (cval)

                  {

                         case 'a':

                case 'A':

                                ++char_a;

                                break;

                         case 'e':

                         case 'E':

                                ++char_e;

                                break;

                         case 'i':

                         case 'I':

                                ++char_i;

                                break;

                         case 'o':

                         case 'O':

                                ++char_o;

                                break;

                         case 'u':

                         case 'U':

                                ++char_u;

                                break;

                  }

           }

           cout<<"元音字母a的个数为:"<<char_a<<endl;

           cout<<"元音字母e的个数为:"<<char_e<<endl;

           cout<<"元音字母i的个数为:"<<char_i<<endl;

           cout<<"元音字母o的个数为:"<<char_o<<endl;

           cout<<"元音字母u的个数为:"<<char_u<<endl;

    }

     

    练习5.11

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

    void main()

    {

        char cval;

        int char_a = 0, char_e = 0, char_i = 0, char_o = 0, char_u = 0, sum_space = 0, sum_table = 0, sum_newline = 0;

        while (cin >> std::noskipws >> cval) // 此处的std::noskipws表示的是不忽略任何地方的空白(包括制表符和空格等)

        {

             switch (cval)

             {

             case 'a':

             case 'A':

                 ++char_a;

                 break;

             case 'e':

             case 'E':

                 ++char_e;

                 break;

             case 'i':

             case 'I':

                 ++char_i;

                 break;

             case 'o':

             case 'O':

                 ++char_o;

                 break;

             case 'u':

             case 'U':

                 ++char_u;

                 break;

             case ' ':

                 ++sum_space;

                 break;

             case '\t':

                 ++sum_table;

                 break;

             case '\n':

                 ++sum_newline;

                 break;

             }

        }

        cout << "元音字母a的个数为:" << char_a << endl;

        cout << "元音字母e的个数为:" << char_e << endl;

        cout << "元音字母i的个数为:" << char_i << endl;

        cout << "元音字母o的个数为:" << char_o << endl;

        cout << "元音字母u的个数为:" << char_u << endl;

        cout << "空格的个数为:" << sum_space << endl;

        cout << "制表符的个数为:" << sum_table << endl;

        cout << "换行符的个数为:" << sum_newline << endl;

    }

     

    练习5.12

    #include "stdafx.h"

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        string sIn = "";

        int nNnmff = 0, nNumfl = 0, nNumfi = 0;

        string sff = "ff";

        string sfl = "fl";

        string sfi = "fi";

        while (getline(cin, sIn))

        {

             if (sIn == sff)

             {

                 ++nNnmff;

             }

             else if (sIn == sfl)

             {

                 ++nNumfl;

             }

             else if (sIn == sfi)

             {

                 ++nNumfi;

             }

             cout << nNnmff << " " << nNumfi << " " << nNumfl << endl;

        }

     

     

        system("pause");

    }

     

    练习5.13

    每个case和default后面都没有break ix超出了作用域每个case只能对应一个值 case后面的对应值应为一个常量,可以加const修饰符,将ival、jval、kval变成常量

     

    P166

    练习5.14

    #include<iostream>

    #include<string>

    #include<vector>

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

    void main()

    {

        string My_string, before_string, max_repeatstring;

        int repeat_number = 0, flag = 0;

        while (cin >> My_string)

        {

             if (My_string == before_string)

             {

                 ++repeat_number;

             }

             else

             {

                 repeat_number = 1;

                 before_string = My_string;

             }

     

            if (flag < repeat_number)

             {

                 flag = repeat_number;

                 max_repeatstring = before_string;

             }//设置flag,max_repeatstring用来保存当前比较完字符串后的最大重复次数和对应字符串

        }

        if (flag == 1)

        {

             cout << "没有重复的字符串出现" << endl;

        }

        else

        {

             cout << "单词" << max_repeatstring << "出现了" << flag << "次" << endl;

        }

    }

     

    P168

    练习5.15

    ix应该在循环外定义缺少一个;号++sz,永远也结束不了

     

    练习5.16

    更倾向于用for,因为可以准确取到某个位置的值

     

    练习5.17

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        bool bIs = false;

        vector <int> vec1 = { 0,1,1,2 };

        vector <int> vec2 = { 0,1,1,2,3,5,8 };

        int size1 = vec1.size();

        int size2 = vec2.size();

        int realSize = size1 > size2 ? size2 : size1;

        for (int i = 0; i < realSize; i++)

        {

             if (vec1.at(i) == vec2.at(i))

             {

                 continue;

             }

             bIs = true;

        }

        if (bIs)

        {

             cout << "不存在前缀关系" << endl;

        }

        else

        {

             cout << "是前缀关系" << endl;

        }

     

        system("pause");

    }

     

    P170

    练习5.18

    do后面那么多条语句,需要使用花括号因为do循环体中肯定还需要使用到ival这个变量,所以应该将其定义在循环体外部ival超出了作用域

     

    练习5.19

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        cout << "请输入两个字符串:";

        string str1, str2;

        do

        {

             int size1 = str1.size();

             int size2 = str2.size();

             if (size1 < size2)

             {

                 cout << str1 << endl;

             }

             else

             {

                 cout << str2 << endl;

             }

     

        } while (cin >> str1 >> str2);

     

        system("pause");

    }

     

    P171

    练习5.20

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        string str;

        vector<string> strVec;

        int nPos = -1;

        while (cin >> str)

        {

             strVec.push_back(str);

             int nSize = strVec.size();

             for (int i = 0; i < nSize - 1; i++)

             {

                 if (str == strVec.at(i))

                 {

                      nPos = i;

                 }

             }

             if (nPos > 0)

             {

                 break;

             }

        }

        if (nPos > 0)

        {

             cout << "重复的是:" << str << endl;

        }

     

        system("pause");

    }

     

    练习5.21

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        string str;

        vector<string> strVec;

        int nPos = -1;

        while (cin >> str)

        {

             strVec.push_back(str);

             int nSize = strVec.size();

             for (int i = 0; i < nSize - 1; i++)

             {

                 string ss = strVec.at(i);

                 if (str == ss && ss[0] >= 'A' && ss[0] <= 'Z')

                 {

                      nPos = i;

                 }

             }

             if (nPos > 0)

             {

                 break;

             }

        }

        if (nPos > 0)

        {

             cout << "重复的是:" << str << endl;

        }

     

        system("pause");

    }

     

    P172

    练习5.22

     int sz;     do     {         sz = get_size();     }     while(sz<=0)

     

    P177

    练习5.23

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        int num1, num2;

        cin >> num1 >> num2;

        cout << num1 / num2 << endl;

     

        system("pause");

    }

     

    练习5.24

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        int num1, num2;

        cin >> num1 >> num2;

        if (num2 == 0)

        {

             throw runtime_error("Data Error!");

        }

        cout << num1 / num2 << endl;

     

        system("pause");

    }

     

    练习5.25

    #include <iostream>

    #include <string>

    #include <vector>

    using namespace std;

     

    int main()

    {

        int num1, num2;

        try

        {

             cin >> num1 >> num2;

             if (num2 == 0)

             {

                 throw runtime_error("Data Error!");

             }

            cout << num1 / num2 << endl;

        }

        catch (runtime_error err)

        {

             cout << "输入错误!";

        }

     

        system("pause");

    }

     

    最新回复(0)