## 第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.24程序运行结果截图](https://box.kancloud.cn/162a2bef6c5891f9c7d425c1842d6964_559x355.png =559x355)
- 练习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):
![练习5.25实际运行截图](https://box.kancloud.cn/cf9e062d25d3fb3b3a294520225ea4e2_442x175.png =442x175)