C++ Primer(第五版) 5.4.1--5.6.3节练习

本文详细解答了C++ Primer第五版中5.4.1到5.6.3节的部分练习题目,包括对于for循环、do-while循环、while循环的使用以及异常处理的理解和应用。通过例子分析了循环控制语句的正确用法,强调了变量作用域和异常处理的重要性。

5.14    

#include <iostream>
#include <vector>

using namespace std;

//统计单词及次数的数据结构
struct wcnt {
	string word;
	int cnt;
};

int main()
{
	string word;
	vector<string> svec;
	
	while (cin >> word)
		svec.push_back(word);
	
	string pre_word = "";
	int cnt = 0;
	
	//ret用于保存出现最多的单词及次数
	struct wcnt ret;
	ret.word = pre_word;
	ret.cnt = 0;
	
	//遍历保存单词的vector
	auto beg = svec.begin();
	while (beg != svec.end()) {
		if (*beg == pre_word)	//如果和上个单词相等,增加次数
			++cnt;
		else {
			//单词出现次数比上次多,则刷新结果
			if (cnt > ret.cnt) {
				ret.word = pre_word;
				ret.cnt = cnt;
			}
			cnt = 0;
		}
		pre_word = *beg;
		++beg;
	}
	if (ret.cnt != 0)
		cout << ret.word << " occurs " << ret.cnt + 1 << " times" << endl;
	else 
		cout << "no repeat word" << endl;
	
	return 0;
}

5.15    (a) ix在for语句中定义,至if语句时已经离开ix的作用域;
           (b) 应改为for(ix = 0; ix!=sz; ++ix) { /* ...*/ }
           (c) ix和sz同时变化,循环无法终止。

5.16   for循环更方便控制。

5.17

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> ivec1, ivec2;
	int i;
	while (cin >> i) 
		ivec1.push_back(i);
	while (cin >> i)
		ivec2.push_back(i);
	
	auto beg1 = ivec1.begin(), beg2 = ivec2.begin();
	for (; beg1 != ivec1.end() && beg2 != ivec2.end(); ++beg1, ++beg2) {
		if (*beg1 != *beg2)
			break;
	}
	
	if (beg1 == ivec1.end() || beg2 == ivec2.end())
		cout << true << endl;
	else
		cout << false << endl;
	
	return 0;
		
}

5.18    (a) do语句块应该用花括号括起来;
           (b) while中的condition使用的变量必须定义在循环体之外;
           (c) ival必须定义在循环体之外。

5.19    

#include <iostream>

using namespace std;

int main()
{
	string s1, s2;
	do {
		cin >> s1 >> s2;
		if (s1.size() < s2.size())
			cout << s1 << endl;
		else
			cout << s2 << endl;
	} while (cin);
	
	return 0;
}

5.20

#include <iostream>

using namespace std;

int main()
{
	string word, pre_word;
	bool rep = false;
	while (cin >> word) {
		if (word == pre_word) {
			rep = true;
			break;
		}
		pre_word = word;
	}
	
	if (rep)
		cout << word << endl;
	else 
		cout << "no repeated word" << endl;
	
	return 0;
}

5.21    改写while循环如下:

while (cin >> word) {
    if (!isupper(word[0]))
  			continue;
	if (word == pre_word) {
		rep = true;
		break;
	}
	pre_word = word;
}

5.22    

int sz;
do {
    sz = get_size();
} while (sz <= 0);

5.23    先不使用异常处理:

#include <iostream>

using namespace std;

int main()
{
	int a, b;
	while (cin >> a >> b) {
		if (b == 0) {
			cout << "divider can not be 0" << endl;
			continue;
		}
		cout << a/b << endl;
	}
	
	return 0;
}

5.24    改为抛出异常:


	while (cin >> a >> b) {
		if (b == 0) {
			throw runtime_error("divider can not be 0");
			continue;
		}
		cout << a/b << endl;
	}

          没有try语句块且发生了异常,系统调用terminate函数并终止当前程序:

5.25    使用try和catch

#include <iostream>
#include <stdexcept>

using namespace std;

int main()
{
	int a, b;
	while (cin >> a >> b) {
		try {
			if (b == 0) 
				throw runtime_error("divider can not be 0");
			cout << a/b << endl;		
		} catch (runtime_error err) {
			cout << err.what() << endl;
			cout << "continue(y or n)?" << endl;
			char ch;
			cin >> ch;
			if (ch != 'y' && ch != 'Y')
				break;
		}
		
	}
	
	return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值