1. 程式人生 > >C++ primer第三章作業

C++ primer第三章作業

常量 空字符 prim iostream 作用 所有 使用範圍 get primer

3.1節

練習3.1:

使用恰當的using聲明重做1.4.1節(第11頁)和2.6.2節(第67頁)的練習

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

int main(void)
{
    int sum = 0;
    int i = 50;
    
    while (i <= 100) {
        sum += i;
        i++;
    }

    cout << sum << endl;
    return
0; } #endif #ifdef 2 #include <iostream> using std::cin; using std::cout; using std::endl; int main(void) { int n = 10; while (n >= 0) { cout << n << endl; n--; } return 0; } #endif #ifdef 3 #include <iostream> using std::cout; using std::cin; using std::endl; int
main(void) { int i, j; cout << "input two value" << endl; cin >> i >> j; int bigger, smaller; bigger = (i > j ? i : j); smaller = (i > j ? j : i); while ( smaller <= bigger ) { cout << smaller << endl; smaller++; } return
0; } #endif

3.2.2 節練習

練習3.2:

編寫一段程序從標準輸入中一次讀入一整行,然後修改該程序使其一次讀入一個詞。

#include <iostream>
#include <string>

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

int main(void)
{
    string str;

//    while (getline(cin, str))
//        cout << str << endl;

    while (cin >> str)
        cout << str << endl;

    return 0;

}

練習3.3:

請說明string類的輸入運算符和getline函數分別是如何處理空白字符的。

空白字符包含換行符,空格符,制表符等。
cin從第一個非空白字符開始,將標準輸入流的字符傳遞給指定字符串,並且以空字符為結束。
getline從標準輸入流讀取用戶輸入的內容,以換行符作為結束(換行符也被讀入流),並將流寫入str(拋棄換行符)。

練習3.4

編寫一段程序讀入兩個字符串,## 3.1節

練習3.1:

使用恰當的using聲明重做1.4.1節(第11頁)和2.6.2節(第67頁)的練習

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

int main(void)
{
    int sum = 0;
    int i = 50;
    
    while (i <= 100) {
        sum += i;
        i++;
    }

    cout << sum << endl;
    return 0;
}
#endif

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

int main(void)
{
    int n = 10;
    while (n >= 0) {
        cout << n << endl;
        n--;
    }
    return 0;
}
#endif

#ifdef 3
#include <iostream>

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

int main(void)
{
    int i, j;
    cout << "input two value" << endl;
    cin >> i >> j;

    int bigger, smaller;
    bigger = (i > j ? i : j);
    smaller = (i > j ? j : i);

    while ( smaller <= bigger ) {
        cout << smaller << endl;
        smaller++;
    } 

    return 0;
}


#endif

3.2.2 節練習

練習3.2:

編寫一段程序從標準輸入中一次讀入一整行,然後修改該程序使其一次讀入一個詞。

#include <iostream>
#include <string>

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

int main(void)
{
    string str;

//    while (getline(cin, str))
//        cout << str << endl;

    while (cin >> str)
        cout << str << endl;

    return 0;

}

練習3.3:

請說明string類的輸入運算符和getline函數分別是如何處理空白字符的。

空白字符包含換行符,空格符,制表符等。
cin從第一個非空白字符開始,將標準輸入流的字符傳遞給指定字符串,並且以空字符為結束。
getline從標準輸入流讀取用戶輸入的內容,以換行符作為結束(換行符也被讀入流),並將流寫入str(拋棄換行符)。

練習3.4

編寫一段程序讀入兩個字符串,比較其是否相等並輸出結果。如果不相等,輸出較大的那個字符串。改寫上述程序,比較輸入的兩個字符串是否等長,如果不等長,輸出長度較大的那個字符串。

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

int main(void)
{
    string str1, str2;
    cin >> str1 >> str2;
    
    string max;

    if (str1 != str2) {
        max = (str1 > str2) ? str1 : str2;
        cout << max << endl;
    } 

    if (str1.size() != str2.size()) {
        max = (str1.size() > str2.size()) ? str1 : str2;
        cout << max << endl;
    } 

    return 0;
}

練習3.5

編寫一段程序從標準輸入中讀入多個字符串並將它們連接在一起,輸出連接成的大字符串。然後修改上述程序,用空格把輸入的多個字符串分隔開來。

#include <iostream>
#include <string>

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

int main(void)
{
    string str1, str2, str3;

    cin >> str1 >> str2 >> str3;
    string cat = str1 + " " + str2 + " " + str3;

    cout << cat << endl;

    return 0;
}

3.2.3 節練習

練習3.6:

編寫一段程序,使用範圍for語句將字符串內的所有字符用X代替。

    string str = "hello world";
    cout << str << endl;

    for (auto &c : str) {
        c = ‘X‘;
    }

練習3.7:

就上一題完成的程序而言,如果將循環控制變量的類型設為char 將發生什麽?先估計一下結果,然後實際編程驗證。

將循環變量改為char類型,字符串的值不會改變。

技術分享圖片
技術分享圖片

練習3.8:

分別用while循環和傳統的for循環重寫第一題的程序,你覺得哪種形式更好呢?為什麽?

我覺得區別不大

    string str = "hello world";
    cout << str << endl;

    //1
    for (auto &c : str) {
        c = ‘X‘;
    }

    //2
    int i = 0;
    while (i < str.size()) {
         str[i] = ‘X‘;
    }
    cout << str << endl;

    //3
   // int i;
   // for (i = 0; i < str.size(); i++)
   //     str[i] = ‘X‘;
   // cout << str << endl;

練習3.9:

下面的程序有何作用?它合法嗎?如果不合法,為什麽?

string s;
cout << s[0] << endl;

s為空,s[0]的結果未定義,書上的答案。具體還有待探索。

練習3.10:

編寫一段程序,讀入一個包含標點符號的字符串,將標點符號去除後輸出字符串剩余的部分。

練習3.11:

下面的範圍for語句合法嗎?如果合法,c的類型是什麽?

const string s = "Keep out!";
for (auto &c : s) { /* ... */ }

不合法,s是一個常量字符串,不能對其進行修改。

3.3.1節練習

練習3.12:

下列vector對象的定義有什麽不正確的嗎?如果有,請指出來。對於正確的,描述起執行結果;對於不正確的,說明其錯誤的原因。

(a) vector

(b) vector

(c) vector

b選項不正確,類型不匹配。

練習3.13:

下列的vector對象各包含多少個元素?這些元素的值分別是多少?

(a) vector

(b) vector

(c) vector

(d) vector

(e) vector

(f) vector

3.3.2節練習

練習3.14:

編寫一段程序,用cin讀入一組整數並把它們存入一個vector對象

C++ primer第三章作業