C++筆記:習題 3.7 (3.2.4)
/* C++ Primer 3.2.4 習題 3.7
* 題目: 編寫程序實現從標準輸入讀入兩個 string 對象,比較其是否相等,哪個較大,哪個較長。
*/
#include #include
using std::cin;
using std::cout;
using std::string;
using std::endl;
int main()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2);
if (s1 == s2)
cout << "S1, S2 is the same." << endl;
else if (s1 > s2)
cout << "S1 is biger than S2." << endl;
else
cout << "S2 is biger than S1." << endl;
if (s1.length() == s2.length())
cout << "S1, S2 is the same length." << endl;
else if (s1.length() > s2.length())
cout << "S1 is longer than S2." << endl;
else
cout << "S2 is longer than S1." << endl;
return 0;
}
© 轉載需附帶本文連結,依 CC BY-NC-SA 4.0 釋出。