C++ primer(第五版)第5章习题答案

第五章 语句

5.1 

只含有一个单独的分号的语句就是空语句。如果在程序的某个地方,语法上需要一条语句但逻辑上不需要,此时应该使用空语句。

 

5.2

块也称为复合语句,是指花括号括起来的语句和声明的序列。如果在程序的某个地方,语法上需要一条语句但逻辑上需要多条语句,则应该使用块。

 

5.3

这种重写降低了代码的可读性。逗号运算符总是保证顺序并丢弃前面结果。但是,在这个例子中没有任何意义,但也是不容易理解的。

#include <iostream>
int main()
{
    int sum = 0, val = 1;
    while (val <= 10)
        sum += val, ++val;
    std::cout << "Sum of 1 to 10 inclusive is "
              << sum << std::endl;

    return 0;
}

 

5.4

(a) iter没有指向, std::string::iterator iter = s.begin();

(b)定义在控制结构当中的变量只在相应语句的内部可见,一旦语句结束,变量也就超出其作用范围了。

 

5.5

#include <iostream>
#include <vector>
#include <string>

using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::cin;

int main()
{
    vector<string> scores = {"F", "D", "C", "B", "A", "A++"};

    int grade{0};
    while (cin >> grade) {
        string lettergrade;
        if (grade < 60)
            lettergrade = scores[0];
        else {
            lettergrade = scores[(grade - 50) / 10];
            if (grade != 100) {
                if (grade % 10 > 7)
                    lettergrade += "+";
                else if (grade % 10 < 3)
                    lettergrade += "-";
            }
        }

        cout << lettergrade << endl;
    }

    return 0;
}

 

5.6

#include <iostream>
#include <vector>
#include <string>

using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::cin;

int main()
{
    vector<string> scores = {"F", "D", "C", "B", "A", "A++"};

    int grade{0};
    while (cin >> grade) {
        string lettergrade = grade < 60 ? scores[0] : scores[(grade - 50) / 10];
        lettergrade +=
            (grade == 100 || grade < 60)
                ? ""
                : (grade % 10 > 7) ? "+" : (grade % 10 < 3) ? "-" : "";
        cout << lettergrade << endl;
    }

    return 0;
}

 

5.7

(a)第二行加分号:

(b)二三行用加上花括号;

(c)在循环外定义变量 int val;

(d)判断条件一直为false,需改为 ival == 0

 

5.8

if语句嵌套时if分支多于else分支,如何匹配if,else的问题被称为悬挂else,C++中规定else与离他最近的尚未匹配的if匹配。

 

5.9

#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
    char ch;
    while (cin >> ch) {
        if (ch == 'a')
            ++aCnt;
        else if (ch == 'e')
            ++eCnt;
        else if (ch == 'i')
            ++iCnt;
        else if (ch == 'o')
            ++oCnt;
        else if (ch == 'u')
            ++uCnt;
    }
    cout << "Number of vowel a: \t" << aCnt << '\n' << "Number of vowel e: \t"
         << eCnt << '\n' << "Number of vowel i: \t" << iCnt << '\n'
         << "Number of vowel o: \t" << oCnt << '\n' << "Number of vowel u: \t"
         << uCnt << endl;

    return 0;
}

 

5.10


#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
    char ch;
    while (cin >> ch) switch (ch) {
        case 'a':
        case 'A':
            ++aCnt;
            break;
        case 'e':
        case 'E':
            ++eCnt;
            break;
        case 'i':
        case 'I':
            ++iCnt;
            break;
        case 'o':
        case 'O':
            ++oCnt;
            break;
        case 'u':
        case 'U':
            ++uCnt;
            break;
        }

    cout << "Number of vowel a(A): \t" << aCnt << '\n'
         << "Number of vowel e(E): \t" << eCnt << '\n'
         << "Number of vowel i(I): \t" << iCnt << '\n'
         << "Number of vowel o(O): \t" << oCnt << '\n'
         << "Number of vowel u(U): \t" << uCnt << endl;

    return 0;
}

 

5.11

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, spaceCnt = 0,
             tabCnt = 0, newLineCnt = 0;
    char ch;
    while (cin >> std::noskipws >> ch) switch (ch) {
        case 'a':
        case 'A':
            ++aCnt;
            break;
        case 'e':
        case 'E':
            ++eCnt;
            break;
        case 'i':
        case 'I':
            ++iCnt;
            break;
        case 'o':
        case 'O':
            ++oCnt;
            break;
        case 'u':
        case 'U':
            ++uCnt;
            break;
        case ' ':
            ++spaceCnt;
            break;
        case '\t':
            ++tabCnt;
            break;
        case '\n':
            ++newLineCnt;
            break;
        }

    cout << "Number of vowel a(A): \t" << aCnt << '\n'
         << "Number of vowel e(E): \t" << eCnt << '\n'
         << "Number of vowel i(I): \t" << iCnt << '\n'
         << "Number of vowel o(O): \t" << oCnt << '\n'
         << "Number of vowel u(U): \t" << uCnt << '\n' << "Number of space: \t"
         << spaceCnt << '\n' << "Number of tab char: \t" << tabCnt << '\n'
         << "Number of new line: \t" << newLineCnt << endl;

    return 0;
}

5.12

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, spaceCnt = 0,
             tabCnt = 0, newLineCnt = 0, ffCnt = 0, flCnt = 0, fiCnt = 0;
    char ch, prech = '\0';
    while (cin >> std::noskipws >> ch) {       //no skip whitespace
        switch (ch) {
        case 'a':
        case 'A':
            ++aCnt;
            break;
        case 'e':
        case 'E':
            ++eCnt;
            break;
        case 'i':
            if (prech == 'f') ++fiCnt;
        case 'I':
            ++iCnt;
            break;
        case 'o':
        case 'O':
            ++oCnt;
            break;
        case 'u':
        case 'U':
            ++uCnt;
            break;
        case ' ':
            ++spaceCnt;
            break;
        case '\t':
            ++tabCnt;
            break;
        case '\n':
            ++newLineCnt;
            break;
        case 'f':
            if (prech == 'f') ++ffCnt;
            break;
        case 'l':
            if (prech == 'f') ++flCnt;
            break;
        }
        prech = ch;
    }

    cout << "Number of vowel a(A): \t" << aCnt << '\n'
         << "Number of vowel e(E): \t" << eCnt << '\n'
         << "Number of vowel i(I): \t" << iCnt << '\n'
         << "Number of vowel o(O): \t" << oCnt << '\n'
         << "Number of vowel u(U): \t" << uCnt << '\n' << "Number of space: \t"
         << spaceCnt << '\n' << "Number of tab char: \t" << tabCnt << '\n'
         << "Number of new line: \t" << newLineCnt << '\n' << "Number of ff: \t"
         << ffCnt << '\n' << "Number of fl: \t" << flCnt << '\n'
         << "Number of fi: \t" << fiCnt << endl;

    return 0;
}

 

5.13

(a)缺少break语句

(b) ix不在作用域内,需在switch外添加int ix;

(c) 语法错误 case 1: case 3: case5: case7: case 9:

(d) case标签必须是整形常量表达式

 

5.14

#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
    string pre_word, word, max_repeat_word;
    int repeat_times = 0, max_repeat_times = 0;

    while (cin >> word) {
        if (word == pre_word) {
            ++repeat_times;
        } else {
            repeat_times = 1;
            pre_word = word;
        }

        if (max_repeat_times < repeat_times) {
            max_repeat_times = repeat_times;
            max_repeat_word = pre_word;
        }
    }

    if (max_repeat_times <= 1){
        cout << "no word was repeated" << endl;
    } else {
        cout << "the word '" << max_repeat_word << "' occurred " << max_repeat_times << " times" << endl;
    }
}

 

5.15

(a) for语句头中定义的对象只能在for循环体内可见, int ix;要定义在循环体外边。

(b)语法错误,省略init-statement后要添加;

(c)循环条件一直为true,去掉expression部分的++sz

 

5.16

// while idiomatic
int i;
while ( cin >> i )
    // ...

// same as for
for (int i = 0; cin >> i;)
    // ...

// for idiomatic
for (int i = 0; i != size; ++i)
    // ...

// same as while
int i = 0;
while (i != size)
{
    // ...
    ++i;
}

我更喜欢使用for在这些例子中,它更简洁。更重要的是变量i仅在循环体内可用,减少了在循环外命名混乱的可能性,具有更好的维护性的但同时也带来了一丢丢不好的阅读体验。

 

5.17

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int main()
{
    vector<int> vec1{0, 1, 1, 2};
    vector<int> vec2{0, 1, 1, 2, 3, 5, 8};

    auto size = vec1.size() < vec2.size() ? vec1.size() : vec2.size();
    for (decltype(vec1.size()) i = 0; i != size; ++i) {
        if (vec1[i] != vec2[i]) {
            cout << "false" << endl;
            return 0;
        }
    }
    cout << "true" << endl;
    return 0;
}

5.18

(a) do,while之间缺少花括号

(b)条件部分的变量必须定义在循环体之外,否则会出现变量的先使用后定义。

(c)在do语句块内定义的变量无法在作用域外被访问到。

 

5.19


#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
    string rsp;
    do {
        cout << "Input two strings: ";
        string str1, str2;
        cin >> str1 >> str2;
        cout << (str1 <= str2 ? str1 : str2) << " is less than the other. "
             << "\n\n"
             << "More? Enter yes or no: ";
        cin >> rsp;
    } while (!rsp.empty() && rsp[0] == 'y');
    return 0;
}

 

5.20

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main() {
    string read, tmp;
    while (cin >> read)
        if (read == tmp)
            break;
        else
            tmp = read;

    if (cin.eof())
        cout << "no word was repeated." << endl;
    else
        cout << read << " occurs twice in succession." << endl;
    return 0;
}

while 循环体是语句块为什么不加花括号啊啊!!是不是作者的问题,还是if else 连接紧密

 

5.22

for (sz = get_size(); sz <= 0; sz=get_size());

 

5.23

#include <iostream>

int main(void)
{
    int a, b;
    std::cin >> a >> b;
    std::cout << static_cast<double>(a) / b << std::endl;

    return 0;
}

 

5.24

#include <iostream>
#include <stdexcept>

int main(void)
{
    int a, b;
    std::cin >> a >> b;

    if (b == 0) throw std::runtime_error("divisor is 0");

    std::cout << static_cast<double>(a) / b << std::endl;

    return 0;
}

 

5.25

#include <iostream>
#include <stdexcept>
using std::cin;
using std::cout;
using std::endl;
using std::runtime_error;

int main(void)
{
    int a, b;
    cout << "Input two integers: ";
    while (cin >> a >> b) {
        try {
            if (b == 0) throw runtime_error("divisor is 0");
            cout << static_cast<double>(a) / b << endl;
            cout << "Input two integers: ";
        }
        catch (runtime_error err) {
            cout << err.what() ;
            cout << "\nTry Again? Enter y or n:" << endl;
            char c;
            cin >> c;
            if (!cin || c == 'n')
                break;
        }
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值