C prime第三章作業(上)

2021-10-05 15:25:30 字數 2023 閱讀 2938

以下僅為個人理解所做,僅供參考!!!!

3.1:在.cpp檔案中,加入using namespace std; 

3.2:編寫一段程式從標準輸入中一次讀入一整行(使用getline()函式),修改該程式後sh

#include #include #include using namespace std;

int main()

/*cin >> s; //無法讀入空格後的字串

for(;i < s.size();i++)

cout << endl;*/

system("pause");

return 0;

}

3.3.請說明string類的輸入運算子和getline函式分別是如何處理空白字元的。

string 類的輸入運算子 不能讀取空白字元 ,比如hello world!空白字元後的字串無法讀取

getline()函式 可以讀取空白字元

3.4.編寫一段程式讀入兩個字串,比較其是否相等並輸出結果。如果不相等,輸出較大的字串。

#include #include #include using namespace std;

int main()

else if(max > s2)

else

system("pause");

return 0;

}

改寫上述程式,比較輸入的兩個字串,判斷是否等長,如果不等長輸出較大的字串

#include #include #include using namespace std;

int main()

else if(max > s2.size())

else

system("pause");

return 0;

}

3.5.編寫一段程式從中標準輸入讀入多個字串並將他們連線在一起,輸出連線成的最大字串。

#include #include #include using namespace std;

int main()

修改上述程式,用空格把輸入的多個字串分隔開來。

#include #include #include using namespace std;

int main()

3.6.編寫一段程式,使用範圍for語句將字串的所有字元用x代替。

#include #include #include using namespace std;

int main()

3.7.如果將迴圈控制變數的型別設為char將發生什麼?編寫程式驗證

字串的值不會改變

#include #include #include using namespace std;

int main()

3.8.略

3.9.

string s;

cout << s[0] << endl;

合法,s預設初始化為空字串,所以輸出指標的第乙個元素,輸出為空字

#include #include #include using namespace std;

int main()

3.10.編寫一段程式,讀入乙個包含標點的字串,將標點去除後輸出字串剩餘部分。

#include #include #include using namespace std;

int main()

3.11.

const string s = "keep out!";

for(auto &c : s){}

合法,c的型別為const char

可以編寫單例測試,把auto改為const char型別和其他型別去比較

第三章作業

1.完成課本每乙個程式設計題。要求先畫出流程演算法圖或n s圖,然後程式設計實現,有可能的話使用兩種以上方法 2.程式設計求 百錢百雞 問題。雞翁一值錢五,雞母 一值錢三,雞雛三值錢一。百錢買百雞,問雞翁 雞母 雞雛各幾何?3.程式設計輸入乙個整數,計算它是幾位數字,分別輸出每一位數字,並輸出各個數...

第三章作業

第三章作業 習題5,求e的近似值 求自然對數e的近似值 includeusing namespace std int n return a int main cout 習題6 求圓周率 求圓周率的近似值 include includeusing namespace std int main cout...

第三章作業

第三章 最簡單的c程式設計 順序程式設計 例3.1 有人用溫度計測量出用華氏法表示的溫度 如64 f 今要求把它轉換為以攝氏法表示的溫度 如17.8 c 例 3.2 計算存款利息。有1000元,想存一年。有3種方法可選 1 活期,年利率為r1 2 一年期定期,年利率為r2 3 存兩次半年定期,年利率...