C++ Primer 中文第 5 版练习答案 第 5 章 语句(14~25)

C++ Primer 中文版(第 5 版)练习解答合集

自己写的解答,如有错误之处,烦请在评论区指正!


#include <iostream>
using namespace std;
int main() {
	unsigned int maxCnt = 0, cnt = 0;
	string now, last, maxString;
	while (cin >> now) {
		if (now == last) {
			++cnt;
			if (cnt > maxCnt)
				maxCnt = cnt;
				maxString = now;
		} else {
			last = now;
			cnt = 0;
		} 
	}
	cout << "连续出现次数最多的单词是:" << maxString << '\n'
		 << "出现了 " << maxCnt << " 次。" << endl; 
	return 0;
} 
  1. a. ix0开始循环到sz - 1为止,循环次数为sz。错误:ix仅在for内可见。
    b. for缺少对ix的初始化语句(实在不想写也要放个;当空语句)
    c. 如果循环体内
  2. 如果只能选一种,我选while,感觉写起来更自由。
#include <iostream>
#include <vector>
using namespace std;
int main() {
	vector<int> a{0, 1, 1, 2};
	vector<int> b{0, 1, 1, 2, 3, 5, 8};
	int len = a.size() < b.size() ? a.size() : b.size();
	bool prefix = true;
	for (int i = 0; i != len; ++i) {
		if (a[i] != b[i]) {
			prefix = false;
			break;
		} 	
	} 
	if (prefix)
		cout << (a.size() < b.size() ? "a" : "b") << "是前缀" << endl;
	else
		cout << "没有前缀" << endl; 
	return 0;
} 
  1. a. 不断请求用户输入两个数字并输出他们的和,如果没有输入则退出循环。错误:do后面的语句需要花括号。
    b. 不断循环,直到get_response()的返回值为 0。错误:将变量声明放在了条件部分。
    c. 不断循环,直到get_response()的返回值为 0。错误:最好把ival的定义写在外面。
#include <iostream>
#include <string>
using namespace std;
int main() {
	string a, b;
	cout << "Please input two strings: " << endl;
	cin >> a >> b;
	do {
		cout << (a.size() < b.size() ? a : b) << endl;
		cout << "Please input two strings: " << endl;
	} while (cin >> a >> b);
	return 0;
} 
#include <iostream>
#include <string>
using namespace std;
int main() {
	bool flag = false;
	string last, now;
	while (cin >> now) {
		if (now == last) {
			flag = true;
			break;
		}
		last = now;
	} 
	if (flag)
		cout << "Repeat word: " << now << endl;
	else
		cout << "No repeat word." << endl; 
	return 0;
} 
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
	bool flag = false;
	string last, now;
	while (cin >> now) {
		if (now == last) {
			if (!isupper(now[0]))
				continue; 
			flag = true;
			break;
		}
		last = now;
	} 
	if (flag)
		cout << "Repeat word: " << now << endl;
	else
		cout << "No repeat word." << endl; 
	return 0;
} 
do {
	int sz = get_size();
} while (sz <= 0);
#include <iostream>
using namespace std;
int main() {
	int a, b;
	cin >> a >> b;
	cout << a / b << endl;
	return 0;
} 
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
	int a, b;
	cin >> a >> b;
	if (b == 0)
		throw runtime_error("b cannot be 0."); 
	else
		cout << a / b << endl;
	return 0;
} 
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
	int a, b;
	while (cin >> a >> b) { 
		try {
			if (b == 0)
				throw runtime_error("b cannot be 0.");
			cout << a / b << endl; 
		} catch (runtime_error err) {
			cout << err.what()
				 << "\nTry again? Enter y or n" << endl;
			char c;
			cin >> c;
			if (!cin || c == 'n')
				break;
		}
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值