C++Primer第五版习题答案(五)


C++Primer第五版课后习题答案目录

5.5

#include <iostream>

using namespace std;

int main()
{
	double score;
	string grade = "FEDCBA";
	char letterGrade;
	while (cin >> score)
	{
		if (score < 60)
			letterGrade = grade[0];
		else
		{
			letterGrade = grade[(score - 50) / 10];
		}
		cout << letterGrade << endl;
	}

	return 0;
}

5.6

#include <iostream>

using namespace std;

int main()
{
	double score;
	string grade = "FEDCBA";
	char letterGrade;
	while (cin >> score)
	{
		letterGrade = (score < 60) ? grade[0] : (grade[(score - 50) / 10]);
		cout << letterGrade << endl;
	}

	return 0;
}

5.9

#include <iostream>

using namespace std;

int main()
{
	char c;
	int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
	while (cin >> c)
	{
		if (c == 'a')
			++aCount;
		else if (c == 'e')
			++eCount;
		else if (c == 'i')
			++iCount;
		else if (c == 'o')
			++oCount;
		else if (c == 'u')
			++uCount;
	}
	cout << "aCount = " << aCount << endl;
	cout << "eCount = " << eCount << endl;
	cout << "iCount = " << iCount << endl;
	cout << "oCount = " << oCount << endl;
	cout << "uCount = " << uCount << endl;

	return 0;
}

5.10

#include <iostream>

using namespace std;

int main()
{
	char c;
	int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
	while (cin >> c)
	{
		if (c == 'a' || c == 'A')
			++aCount;
		else if (c == 'e' || c == 'E')
			++eCount;
		else if (c == 'i' || c == 'I')
			++iCount;
		else if (c == 'o' || c == 'O')
			++oCount;
		else if (c == 'u' || c == 'U')
			++uCount;
	}
	cout << "aCount = " << aCount << endl;
	cout << "eCount = " << eCount << endl;
	cout << "iCount = " << iCount << endl;
	cout << "oCount = " << oCount << endl;
	cout << "uCount = " << uCount << endl;

	return 0;
}

5.11

#include <iostream>

using namespace std;

int main()
{
	char c;
	int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
	int spaceCount = 0, tabCount = 0, newLineCount = 0;
	while (cin >> noskipws >> c)
	{
		if (c == 'a' || c == 'A')
			++aCount;
		else if (c == 'e' || c == 'E')
			++eCount;
		else if (c == 'i' || c == 'I')
			++iCount;
		else if (c == 'o' || c == 'O')
			++oCount;
		else if (c == 'u' || c == 'U')
			++uCount;
		else if (c == ' ')
			++spaceCount;
		else if (c == '\t')
			++tabCount;
		else if (c == '\n')
			++newLineCount;
	}
	cout << "aCount = " << aCount << endl;
	cout << "eCount = " << eCount << endl;
	cout << "iCount = " << iCount << endl;
	cout << "oCount = " << oCount << endl;
	cout << "uCount = " << uCount << endl;
	cout << "spaceCount = " << spaceCount << endl;
	cout << "tabCount = " << tabCount << endl;
	cout << "newLineCount = " << newLineCount << endl;

	return 0;
}

5.12

#include <iostream>

using namespace std;

int main()
{
	char c;
	int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
	int spaceCount = 0, tabCount = 0, newLineCount = 0;
	int ffCount = 0, flCount = 0, fiCount = 0;
	char prec = '\0';
	while (cin >> noskipws >> c)
	{
		if (c == 'a' || c == 'A')
			++aCount;
		else if (c == 'e' || c == 'E')
			++eCount;
		else if (c == 'i')
			if (prec == 'f')
				++fiCount;
			else
				++iCount;
		else if (c == 'I')
			++iCount;
		else if (c == 'o' || c == 'O')
			++oCount;
		else if (c == 'u' || c == 'U')
			++uCount;
		else if (c == ' ')
			++spaceCount;
		else if (c == '\t')
			++tabCount;
		else if (c == '\n')
			++newLineCount;
		else if (c == 'f')
		{
			if (prec == 'f')
				++ffCount;
		}
		else if (c == 'l')
			if (prec == 'f')
				++flCount;
	
		prec = c;
	}
	cout << "aCount = " << aCount << endl;
	cout << "eCount = " << eCount << endl;
	cout << "iCount = " << iCount << endl;
	cout << "oCount = " << oCount << endl;
	cout << "uCount = " << uCount << endl;
	cout << "spaceCount = " << spaceCount << endl;
	cout << "tabCount = " << tabCount << endl;
	cout << "newLineCount = " << newLineCount << endl;
	cout << "ffCount = " << ffCount << endl;
	cout << "flCount = " << flCount << endl;
	cout << "fiCount = " << fiCount << endl;

	return 0;
}

5.14

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str, preStr;
	int count = 1;//记录单词连续出现的次数
	int maxCount = 1;
	string maxStr;
	while (cin >> str)
	{
		if (str == preStr)
			++count;
		else
			count = 1;
		
		if (count > maxCount)
		{
			maxCount = count;
			maxStr = str;
		}
		preStr = str;
	}
	if (maxCount == 1)
		cout << "任何单词都没有连续出现过" << endl;
	else
		cout << "单词" << maxStr << "连续出现了" << maxCount << "次" << endl;
}

5.17

#include <iostream>
#include <vector>

using namespace std;

bool isPrefix(vector<int>& v1, vector<int>& v2)
{
	if (v1.size() > v2.size())
		isPrefix(v2, v1);
	for (int i = 0; i < v1.size(); ++i)
	{
		if (v1[i] != v2[i])
			return false;
	}
	return true;
}

int main()
{
	vector<int> ivec1 = { 0, 1, 1, 2 };
	vector<int> ivec2 = { 0, 1, 1, 2, 3, 5, 8 };
	
	bool flag = isPrefix(ivec1, ivec2);

	cout << (flag ? "yes" : "no") << endl;


	return 0;
}

5.19

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s1, s2;

	do
	{
		cout << "请输入两个string对象:" << endl;
		cin >> s1 >> s2;
		if (s1.size() < s2.size())
			cout << s1 << endl;
		else
			cout << s2 << endl;
	} while (1);

	return 0;
}

5.20

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str, preStr;
	bool flag = false;
	while (cin >> str && !str.empty())
	{
		if (str == preStr)
		{
			flag = true;
			break;
		}
		else
			preStr = str;
	}
	if (flag)
		cout << "重复出现的单词为:" << preStr << endl;
	else
		cout << "没有任何单词是重复出现的" << endl;

	return 0;
}

5.21

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str, preStr;
	bool flag = false;
	while (cin >> str && !str.empty())
	{
		if (str == preStr && isupper(str[0]))
		{
			flag = true;
			break;
		}
		else
			preStr = str;
	}
	if (flag)
		cout << "重复出现的以大写字母开头的单词为:" << preStr << endl;
	else
		cout << "没有任何大写字母开头的单词是重复出现的" << endl;

	return 0;
}

5.23

#include <iostream>

using namespace std;

int main()
{
	
	int i, j;
	cin >> i >> j;
	cout << i / j << endl;

	return 0;
}

5.24

#include <iostream>

using namespace std;

int main()
{
	
	int i, j;
	cin >> i >> j;
	if (j == 0)
		throw runtime_error("除数不能为0");
	cout << i / j << endl;

	return 0;
}

5.25

#include <iostream>

using namespace std;

int main()
{
	int i, j;
	while (cout << "请输入两个整数:" << endl, cin >> i >> j)
	{
		try
		{
			
			if (j == 0)
				throw runtime_error("除数不能为0");
			cout << i / j << endl;
		}
		catch (runtime_error err)
		{
			cout << err.what() << "\nTry again ? Enter y or n" << endl;
			char c;
			cin >> c;
			if (c == 'n')
				break;
		}
	}
	

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值