c++ primer 第五版第五章


5.01 什么是空語句,什么時候會用到?

空語句只含有一個單獨的分號。當循環的全部工作在條件部分就可以完成時,通常就會用到空語句。

5.02 什么是塊,什么時候會用到塊?

用花括號括起來的語句和聲明的序列成為塊,一個塊就是一個作用域。如循環體內有多條執行語句,就需要用到塊,函數也需要用到塊。

5.03 使用逗號運算符重寫while循環,使他不再需要塊,改寫之后可讀性提高了嗎?


while (val <= 10) sum += val, ++val;

可讀性並沒有提高,用塊更易讀。

5.04 說明下列例子的含義,如果存在問題,試着修改。


 - (a) while (string::iterator iter != s.end()) { /* ... */ }

 - (b) while ( bool status = find(word) ) {/* ... */ }

    if ( !status ) { /* ... */ }

對於a,iter沒有指向,直接判斷其不等於s.end()是非法的。

對於b,while執行的條件就是status為真,因此if判斷永遠不成立。

5.05 寫一段自己的程序,使用if else語句實現把數字成績轉化為字母成績的要求。


void test505()
{
    const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
    float grade;
    string letterGrade;
    while (cin >> grade)
    {   
        if (grade < 60) 
        {   
            letterGrade = scores[0];
        }   
        else
        {   
            letterGrade = scores[(grade-50)/10];
        }   
        cout << letterGrade << endl;
    }   
}

5.06 改寫上題程序,使用條件運算符代替if-else語句。


void test506()
{
    const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
    float grade;
    string letterGrade;
    while (cin >> grade)
    {   
        (grade < 60) ? letterGrade = scores[0] : letterGrade = scores[(grade-50)/10];
        cout << letterGrade << endl;
    }
}

5.07 改正下面代碼段中的錯誤。


-a  if (ival1 != ival2)

        ival1 = ival2

     else  ival1 = ival2 = 0;        // ival1 = ival2一句丟失了句末分號



-b  if (ival < minval)

            minval  = ival;

            occurs = 1;                    // if語句應該加大括號,否則occurs=1;不會執行。



-c  if (int ival = get_value())

            cout << "ival = " << ival << endl;

     if (!ival)

            cout << "ival = 0\n";        // 第二個if中的ival未定義,ival的作用域僅限於第一個if循環中



-d    if (ival = 0)

            ival = get_value();            // if中的判斷應寫為 if (0 == ival)

5.08 什么是“懸垂else”?c++語言是如何處理else字句的?

> 當if語句嵌套時,可能會出現if語句的數量大於else語句的情況,這時,我們如何確定給定的else是和哪個if匹配的呢?這個問題就稱作懸垂else。

c++中規定,else與離它最近且尚未匹配的if語句匹配。

5.09 編寫一段程序,使用if語句統計從cin讀入的文本中有多少元音字母。


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

5.10 上述代碼有一個問題,如果是大寫字母則不會被統計在內。編寫程序,使大寫字母和小寫字母都能夠統計在內。


void test509()
{
    int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
    char ch; 

    while (cin >> ch) 
    {   
        if (('a' == ch) || ('A' == ch))
        {   
            ++ aCnt;
        }   
        else if (('e' == ch) || 'E' == ch) 
        {   
            ++ eCnt;
        }   
        else if (('i' == ch) || 'I' == ch) 
        {   
            ++ iCnt;
        }   
        else if (('o' == ch) || 'O' == ch) 
        {   
            ++ oCnt;
        }   
        else if (('u' == ch) || 'U' == ch) 
        {   
            ++ uCnt;
        }   
    }   

    cout << "Number of vowel a is " << aCnt << "\nNumber of vowel e is " << eCnt 
        << "\nNumber of vowel i is " << iCnt << "\nNumber of vowel o is " << oCnt 
        << "\nNumber of vowel u is " << uCnt << endl;
}

5.11 修改統計元音字母的程序,使其也能統計空格、制表符和換行符的數量。


while (cin >> noskipws >> ch) 
    {   
        if (('a' == ch) || ('A' == ch))
        {   
            ++ aCnt;
        }   
        else if (('e' == ch) || 'E' == ch) 
        {   
            ++ eCnt;
        }   
        else if (('i' == ch) || 'I' == ch) 
        {   
            ++ iCnt;
        }   
        else if (('o' == ch) || 'O' == ch) 
        {   
            ++ oCnt;
        }   
        else if (('u' == ch) || 'U' == ch) 
        {   
            ++ uCnt;
        }   
        else if (' ' == ch) 
        {   
            ++ spaceCnt;
        }   
        else if ('\t' == ch) 
        {   
            ++ tabCnt;
        }   
        else if ('\n' == ch) 
        {   
            ++ newlineCnt;
        }   
    }   

    cout << "Number of vowel a is " << aCnt << "\nNumber of vowel e is " << eCnt 
        << "\nNumber of vowel i is " << iCnt << "\nNumber of vowel o is " << oCnt 
        << "\nNumber of vowel u is " << uCnt << endl;
    cout << "Number of space is " << spaceCnt << "\nNumber of tab is " << tabCnt
        << "\n Number of line feed is " << newlineCnt << endl;

注意cin要使用noskipws,noskipws會告訴istream讀取字符時不要跳過空白符。

5.12 修改統計元音字母的程序,使其能統計以下含有兩個字符的字符序列的數量:ff,fl, fi




5.13 下面顯示的每個程序都有一個常見的編程錯誤,指出錯誤在哪里,然后修改它們。

5.14

5.15 說明下面循環的含義並改正其錯誤。


-- a

for(int ix = 0; ix != sz; ++ ix) {...}    

if (ix != sz)         // for循環結束的條件是ix == sz,因此if語句永遠不成立。

 // ...



-- b

int ix;

for (ix != sz; ++ix) {/* ... */}        // 省略了初始化語句必須要有“;”,表示一個空語句。



--c

for (int ix = 0; ix != sz; ++ix; ++ sz) {/* ... */}        // ++ix和++sz同時執行,則for會一直循環下去。

5.16

5.17 假設有兩個包含整數的vector對象,編寫一段程序,檢驗其中一個vector的對象是否為另一個的前綴。為了實現這一目標,對於兩個不等長的vector,只需挑出長度較短的那個,把它的所有元素和另一個vector對象比較即可。例如,兩個vector的元素分別是0、1、1、2和0、1、1、2、3、5、8,則程序的返回結果應該為真。

5.18

5.19

5.20


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM