c++第三十一天

p159~p164:
switch语句
1、例程:统计文本中五个元音字母出现的次数。(利用输入输出重定向测试)

$ a <input.txt>output.txt
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    // unsigned int 可以缩写为 unsigned
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
    char ch;
    while (cin >> ch) {
        switch (ch) {
            case 'a':
                ++aCnt;
                break;
            case 'e':
                ++eCnt;
                break;
            case 'i':
                ++iCnt;
                break;
            case 'o':
                ++oCnt;
                break;
            case 'u':
                ++uCnt;
                break;
        }        
    }
    cout << "Number of vowel a: " << aCnt << '\n'
         << "Number of vowel e: " << eCnt << '\n'
         << "Number of vowel i: " << iCnt << '\n'
         << "Number of vowel o: " << oCnt << '\n'
         << "Number of vowel u: " << uCnt << endl;
    return 0;
}

input.txt

 In the year 1878 I took my degree of Doctor of Medicine of the
     University of London, and proceeded to Netley to go through the
     course prescribed for surgeons in the army. Having completed my
     studies there, I was duly attached to the Fifth Northumberland
     Fusiliers as Assistant Surgeon. The regiment was stationed in India
     at the time, and before I could join it, the second Afghan war had
     broken out. On landing at Bombay, I learned that my corps had
+
     advanced through the passes, and was already deep in the enemy's
     country. I followed, however, with many other officers who were in
     the same situation as myself, and succeeded in reaching Candahar in
     safety, where I found my regiment, and at once entered upon my new
     duties.

output.txt

Number of vowel a: 43
Number of vowel e: 77
Number of vowel i: 31
Number of vowel o: 43
Number of vowel u: 17

  

2、case label必须整型常量表达式

 

3、switch内部的控制流:如果没有break;将从该标签往后顺序执行所有的case分支。

 

4、default label之后必须至少跟上一个空语句。

 

5、switch内部尽量不要定义变量。

 

练习 5.9

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    // unsigned int 可以缩写为 unsigned
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
    char ch;
    while (cin >> ch) {
        if (ch == 'a') ++aCnt;
        if (ch == 'e') ++eCnt;
        if (ch == 'i') ++iCnt;
        if (ch == 'o') ++oCnt;
        if (ch == 'u') ++uCnt;
    }
    cout << "Number of vowel a: " << aCnt << '\n'
         << "Number of vowel e: " << eCnt << '\n'
         << "Number of vowel i: " << iCnt << '\n'
         << "Number of vowel o: " << oCnt << '\n'
         << "Number of vowel u: " << uCnt << endl;
    return 0;
}

 

练习 5.10

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    // unsigned int 可以缩写为 unsigned
    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
    char ch;
    while (cin >> ch) {
        if (ch == 'a' || ch == 'A') ++aCnt;
        if (ch == 'e' || ch == 'E') ++eCnt;
        if (ch == 'i' || ch == 'I') ++iCnt;
        if (ch == 'o' || ch == 'O') ++oCnt;
        if (ch == 'u' || ch == 'U') ++uCnt;
    }
    cout << "Number of vowel a: " << aCnt << '\n'
         << "Number of vowel e: " << eCnt << '\n'
         << "Number of vowel i: " << iCnt << '\n'
         << "Number of vowel o: " << oCnt << '\n'
         << "Number of vowel u: " << uCnt << endl;
    return 0;
}

 

练习 5.11

    while (cin >> ch) {
        if (ch == 'a') ++aCnt;
        if (ch == 'e') ++eCnt;
        if (ch == 'i') ++iCnt;
        if (ch == 'o') ++oCnt;
        if (ch == 'u') ++uCnt;
        if (ch == ' ') ++blankCnt;
        if (ch == '\t') ++tabCnt;
        if (ch == '\n') ++nCnt; 
    }

 

练习 5.12

没按题意做,另外题目中没有规定ffff怎么处理。

#include <iostream>
using namespace std;
int main()
{
    unsigned nff, nfl, nfi;
    nff = nfl = nfi = 0;
    char ch;
    bool lastf = false;
    while (cin >> ch) {
        if (ch == 'f' && lastf == false) {
            lastf = true;
            continue;
        }
        if (lastf) {
            if (ch == 'f') {
                ++nff;
            }
            if (ch == 'l') {
                ++nfl;
            }
            if (ch == 'i') {
                ++nfi;
            }
        }
    }
    cout << "nff=" << nff << '\n'
         << "nfl=" << nfl << '\n'
         << "nfi=" << nfi << '\n' << endl;
    return 0;
}

 

练习 5.13

a - 漏掉break

b - 在switch内定义变量有可能被跳过,所以是非法的

c - 非法的形式,编译器通过不了。正确的方式为case 'a': case 'b': case 'd':....

d - case label必须整型常量表达式。

 

posted @ 2017-03-13 08:50  xkfx  阅读(227)  评论(0编辑  收藏  举报