## 第5章 語句
### 5.3.2
case標簽必須是整型常量表達式。
### 5.4.1
- 練習5.14
```cpp
/* 文件名:ex5.14.cpp */
/* C++ Primer中文版第5版, 練習5.14 */
/* 題目要求:從標準輸入中讀取若干string對象,輸出連續重復出現次數最大的單詞的出現次數 */
/* 注意事項:在Windows平臺上C++文件結束符是Ctrl+Z,且該字符前面不允許有除回車符之外的其他字符 */
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main(void)
{
vector<string> vStr;
string str0, str1, strOut;
int num = 1, max = 0;
while (cin >> str0)
{
while (cin >> str1)
{
if (str0 == str1)
{
num++;
if (max < num)
{
max = num;
strOut = str0;
}
}
else
{
str0 = str1;
num = 1;
}
}
}
if (max <= 1)
cout << "No duplicated string." << endl;
else
cout << "The string \"" << strOut << "\" has appeared " << max << " times." << endl;
return 0;
}
```
### 5.4.2
- 練習5.17
```cpp
/* 文件名:ex5.17.cpp */
/* C++ Primer中文版第5版, 練習5.17 */
/* 題目要求:從標準輸入中讀取兩組整型序列,判斷其中一組序列是否是另一組的前綴 */
/* 注意事項:輸入C++文件結束符(Ctrl+Z)之后,應調用輸入流成員函數clear()重新激活該輸入流對象 */
#include <iostream>
#include <vector>
using namespace std;
bool isPrefix(vector <int> &iv0, vector <int> &iv1)
{
vector<int> &ivShort = (iv0.size() <= iv1.size()) ? iv0 : iv1;
vector<int> &ivLong = (iv0.size() <= iv1.size()) ? iv1 : iv0;
for (auto begS = ivShort.begin(), endS = ivShort.end(), begL = ivLong.begin(); begS != endS; ++begS, ++begL)
{
if (*begS != *begL)
return false;
}
return true;
}
int main(void)
{
int i = 0;
vector <int> iv0, iv1;
//input
cout << "Enter a sequence of integers separated by SPACE key: ";
for (; cin >> i;)
iv0.push_back(i);
cin.clear();//更改cin的狀態標示符
cin.sync();//清除緩存區的數據流
cout << "Enter another sequence of integers separated by SPACE key: ";
for (; cin >> i;)
iv1.push_back(i);
cin.clear();
cin.sync();
if (isPrefix(iv0, iv1))
cout << "One of the two sequences is the prefix of the other." << endl;
else
cout << "None of the two sequences is the prefix of the other." << endl;
cout << "Enter any key to end...";
cin.get();
return 0;
}
```
參考鏈接:http://www.cnblogs.com/hubavyn/p/3996413.html
### 5.6.3
- 練習5.24
```cpp
/* 文件名:ex5.24.cpp */
/* C++ Primer中文版第5版, 練習5.24 */
/* 題目要求:不作異常處理,運行時拋出異常的結果 */
#include <iostream>
using namespace std;
int main(void)
{
int a = 0, b = 0;
cout << "Enter two integers separated by SPACE: ";
(cin >> a >> b).sync();
if (0 == b)
throw runtime_error("The divisor mustn't be 0.");
cout << a << " / " << b << " = " << (a / b) << endl;
cout << "Enter any key to end..." << endl;
cin.get();
return 0;
}
```
下圖為報錯截圖,運行環境:VS2013,Debug Win32

- 練習5.25
```cpp
/* 文件名:ex5.25.cpp */
/* C++ Primer中文版第5版, 練習5.25 */
/* 題目要求:給練習5.24代碼添加異常處理 */
#include <iostream>
using namespace std;
int main(void)
{
int a = 0, b = 0;
do
{
cout << "Enter two integers separated by SPACE: ";
(cin >> a >> b).sync();
try
{
if (0 == b)
throw runtime_error("The divisor mustn't be 0.");
}
catch (runtime_error err)
{
cout << err.what() << "\nTry Again? Enter y or n. ";
char c;
cin >> c;
if (!cin || c == 'n')
break;
}
if (0 != b)
break;
} while (true);
cout << a << " / " << b << " = " << (a / b) << endl;
cout << "Enter any key to end...";
cin.get();
return 0;
}
```
實際運行效果(VS2013,Debug Win32):
