《C++ Primer》第五版课后习题解答_第五章(2)(09-14)

系统环境: windows 10 1703

编译环境:Visual studio 2017


5.9

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

int main()
{
    unsigned int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0;
    char ch;
    while (cin >> ch)
    {
        if (ch == 'a' || ch == 'A')
        {
            ++acnt;
        }
        else if (ch == 'e' || ch == 'E')
        {
            ++ecnt;
        }
        else if (ch == 'i' || ch == 'I')
        {
            ++icnt;
        }
        else if (ch == 'o' || ch == 'O')
        {
            ++ocnt;
        }
        else if (ch == 'u' || ch == 'U')
        {
            ++ucnt;
        }
    }
    cout << "Number of Vowel a: " << acnt << endl;
    cout << "Number of Vowel e: " << ecnt << endl;
    cout << "Number of Vowel i: " << icnt << endl;
    cout << "Number of Vowel o: " << ocnt << endl;
    cout << "Number of Vowel u: " << ucnt << endl;
    return 0;
}

5.10

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

int main()
{
    unsigned int 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;
        default:
            ;
            break;
        }
    }
    cout << "Number of Vowel a: " << acnt << endl;
    cout << "Number of Vowel e: " << ecnt << endl;
    cout << "Number of Vowel i: " << icnt << endl;
    cout << "Number of Vowel o: " << ocnt << endl;
    cout << "Number of Vowel u: " << ucnt << endl;
    return 0;
}

5.11

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

int main()
{
    unsigned int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, spacecnt = 0, entercnt = 0, tabcnt = 0;
    char ch;
    while (cin >> std::noskipws >> ch) // std::noskipws 表示不忽略任何空白 只有添加了这条才能统计空格,制表符,换行符的数量
    {
        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 '\n':
            ++entercnt;
            break;
        case '\t':
            ++tabcnt;
            break;
        default:
            ;
            break;
        }
    }
    cout << "Number of Vowel a: " << acnt << endl;
    cout << "Number of Vowel e: " << ecnt << endl;
    cout << "Number of Vowel i: " << icnt << endl;
    cout << "Number of Vowel o: " << ocnt << endl;
    cout << "Number of Vowel u: " << ucnt << endl;
    cout << "Number of Vowel space: " << spacecnt << endl;
    cout << "Number of Vowel enter: " << entercnt << endl;
    cout << "Number of Vowel tab: " << tabcnt << endl;
    return 0;
}
注意:在输入语句中应添加 std::noskipws,表示不忽略任何空白 只有添加了这条才能统计空格,制表符,换行符的数量。

5.12

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

int main()
{
    unsigned int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, spacecnt = 0, entercnt = 0, tabcnt = 0, ffcnt = 0, flcnt = 0, ficnt = 0;
    char ch, temp;
    while (cin >> std::noskipws >> ch) // std::noskipws 表示不忽略任何空白 只有添加了这条才能统计空格,制表符,换行符的数量
    {
        switch (ch)
        {
        case 'a': case 'A':
            ++acnt;
            break;
        case 'e': case 'E':
            ++ecnt;
            break;
        case 'i':
            if (temp == 'f')
                ++ficnt;
        case 'I':
            ++icnt;
            break;
        case 'o': case 'O':
            ++ocnt;
            break;
        case 'u': case 'U':
            ++ucnt;
            break;
        case ' ':
            ++spacecnt;
            break;
        case '\n':
            ++entercnt;
            break;
        case '\t':
            ++tabcnt;
            break;
        case 'f':
            if (temp == 'f')
                ++ffcnt;
            break;
        case 'l':
            if (temp == 'f')
                ++flcnt;
            break;
        default:
            break;
        }
        temp = ch;
    }
    cout << "Number of Vowel a: " << acnt << endl;
    cout << "Number of Vowel e: " << ecnt << endl;
    cout << "Number of Vowel i: " << icnt << endl;
    cout << "Number of Vowel o: " << ocnt << endl;
    cout << "Number of Vowel u: " << ucnt << endl;
    cout << "Number of Vowel space: " << spacecnt << endl;
    cout << "Number of Vowel enter: " << entercnt << endl;
    cout << "Number of Vowel tab: " << tabcnt << endl;
    cout << "Number of Vowel ff: " << ffcnt << endl;
    cout << "Number of Vowel fl: " << flcnt << endl;
    cout << "Number of Vowel fi: " << ficnt << endl;
    return 0;
}

5.13

(a) 1. 没有使用 break,程序结果有可能和预期结果不符。

      2. 使用了后置递增运算符。根据书中所说,若非必须,最好改成前置递增运算符。

      程序可以修改为:

    unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
    char ch = next_text();
    switch (ch)
    {
    case 'a':
        ++aCnt;
        break;
    case 'e':
        ++eCnt;
        break;
    default:
        ++iouCnt;
        break;
    }
(b) 1. 非法操作,不允许跨过变量的初始化语句直接跳转到该变量作用域内的另一个位置。

      2. 最好在 default 中写 break 语句。

      可以修改为:

    unsigned index = some_value();
    switch (index)
    {
    case 1: // 将变量定义在了块内,并且在两个 case 语句中都重新定义了变量 ix
        {
        int ix = get_value();
        ivec[ix] = index;
        break;
        }
    default:
        {
        int ix = ivec.size() - 1;
        ivec[ix] = index;
        break;
        }
    }
(c) 1. case 标签必须是整型常量表达式,使用逗号表达式不能达到预期的结果。

      2. 变量拼写错误,递增递减的变量中 C 应为大写。

      3. 最好添加 default 标签。

      4. 使用了后置递增运算符。根据书中所说,若非必须,最好改成前置递增运算符。

      可以修改为:

    unsigned evenCnt = 0, oddCnt = 0;
    int digit = get_num() % 10;
    switch (digit)
    {
    case 1: case 3: case 5: case 7: case 9:
        ++oddCnt;
        break;
    case 2: case 4: case 6: case 8: case 0:
        ++evenCnt;
        break;
    default:
        break;
    }
(d) 1. case 标签后必须是常量表达式。

      2. 最好添加 default 标签。

      可以修改为:

    const unsigned ival = 512, jval = 1024, kval = 4096; // 把此处定义的变量变成常量变量
    unsigned bufsize;
    unsigned swt = get_bufCnt();
    switch (swt)
    {
    case ival:
        bufsize = ival * sizeof(int);
        break;
    case jval:
        bufsize = jval * sizeof(int);
        break;
    case kval:
        bufsize = kval * sizeof(int);
        break;
    default:
        break;
    }

5.14

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

int main()
{
    unsigned int max = 0, cnt = 1;
    string str, temp, word;
    while (cin >> str)
    {
        if (temp == str)
        {
            ++cnt;
        }
        else
        {
            if (max <= cnt)
            {
                max = cnt;
                word = temp;
            }
            cnt = 1;
        }
        temp = str;
    }
    if (max <= cnt)
    {
        max = cnt;
        word = str;
    }
    cout << "The word occurs most was: " << word << ". It occurs " << max << " times." << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值