zoukankan      html  css  js  c++  java
  • 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 << '
    '
             << "Number of vowel e: " << eCnt << '
    '
             << "Number of vowel i: " << iCnt << '
    '
             << "Number of vowel o: " << oCnt << '
    '
             << "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 << '
    '
             << "Number of vowel e: " << eCnt << '
    '
             << "Number of vowel i: " << iCnt << '
    '
             << "Number of vowel o: " << oCnt << '
    '
             << "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 << '
    '
             << "Number of vowel e: " << eCnt << '
    '
             << "Number of vowel i: " << iCnt << '
    '
             << "Number of vowel o: " << oCnt << '
    '
             << "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 == '	') ++tabCnt;
            if (ch == '
    ') ++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 << '
    '
             << "nfl=" << nfl << '
    '
             << "nfi=" << nfi << '
    ' << endl;
        return 0;
    }

    练习 5.13

    a - 漏掉break

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

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

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

  • 相关阅读:
    IIS打开本地站点时,无法访问本地json文件的解决办法
    几种流行的前端框架(BootStrap、Layui、Element-UI、Mint UI、Angular、Vue.js、React)
    六大排序算法:插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序
    SpringBoot框架开发的优秀的项目「值得收藏学习」
    jmeter接口之json提取器应用
    【设计模式(23)】行为型模式之访问者模式
    【设计模式(22)】行为型模式之模板模式
    【设计模式(21)】行为型模式之策略模式
    HTML回忆笔记,给那些忘了但又没完全忘的人准备的
    vscode创建html文件使用"!+tab"不起作用的解决方法
  • 原文地址:https://www.cnblogs.com/xkxf/p/6541035.html
Copyright © 2011-2022 走看看