IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 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;
}
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-03-11 22:34:38  更:2022-03-11 22:35:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 6:48:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码