C++Primer12.3.1节练习

练习12.27:

class QueryResult;
class TextQuery
{
public:
	//构造函数
	TextQuery(ifstream& infile);
	//定义查询结果函数query
	QueryResult query(const string& s)const;

private:
	std::shared_ptr<std::vector<std::string>>file;
	std::_In_place_key_extract_map<std::string, shared_ptr<std::set<std::vector<std::string>::size_type>>>wm;
};

class QueryResult
{
public:
	//构造函数
	QueryResult(std::string s,std::shared_ptr<std::set<std::vector<std::string>::size_type>>p,
		std::shared_ptr<std::vector<std::string>>f) : sought(s),lines(p),file(f){ }

private:
	//查询的单词
	std::string sought;
	//出现的行号
	std::shared_ptr<std::set<std::vector<string>::size_type>>lines;
	//输入的文件
	std::shared_ptr<std::vector<std::string>>file;
};

练习12.28:

#include <iostream>
#include <string>
using namespace std;
#include <vector>
#include <fstream>
#include <memory>
#include <set>
#include <map>
#include <sstream>


//读取文件
void ReadFile(ifstream& infile, vector<string>& file, map<string, set<vector<string>::size_type>>& mp)
{
	string line;
	while (getline(infile, line))
	{
		file.push_back(line);
		//记录行号,从第0行开始
		int num = file.size() - 1;
		istringstream istr(line);
		string word;
		while (istr >> word)
		{
			mp[word].insert(num);
		}
	}
}

//查询打印
ostream& findWord(ostream& os, const string& sought,map<string,set<vector<string>::size_type>>& mp,vector<string>& file)
{
	//在map中查找单词
	auto it = mp.find(sought);
	if (it == mp.end())
	{
		os << sought << " occurs 0 time" << endl;
	}
	else
	{
		//打印行号及出现的行
		set<vector<string>::size_type> num = it->second;
		os << sought << " occurs " << num.size() << " times" << endl;
		//按升序打印出现的行
		for (const auto& c : num)
		{
			os << "(line " << c << " )" << file[c] << endl;
		}
	}
	return os;
}

//用户查询单词
void QueryWord(ifstream& infile, map<string, set<vector<string>::size_type>>& mp, vector<string>& file)
{
	ReadFile(infile, file, mp);
	while (true)
	{
		cout << "enter word to look for,or q to quit: ";
		string s;
		if (!(cin >> s) || s == "q")
		{
			break;
		}

		//查询结果并打印
		findWord(cout, s, mp, file)  << endl;
	}
}


int main()
{
	//保存文本
	vector<string>file;
	//保存映射
	map<string, set<vector<string>::size_type>>mp;

	ifstream infile("text.txt");
	QueryWord(infile, mp, file);
	system("pause");
	return 0;
}

结果:

 文件内容:

练习12.29:使用do while重写程序,同样可以很好的实现,但是之前的写法更好,简单易懂

//用户查询单词
void QueryWord(ifstream& infile, map<string, set<vector<string>::size_type>>& mp, vector<string>& file)
{
	ReadFile(infile, file, mp);
	do
	{
		cout << "enter word to look for,or q to quit: ";
		string s;
		if (!(cin >> s) || s == "q")
		{
			break;
		}

		//查询结果并打印
		findWord(cout, s, mp, file) << endl;
	} while (true);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
c primer 15.9作业是关于异常处理的内容。在这一中,主要介绍了C语言中的错误处理机制和异常处理方式。 异常处理是一种程序设计中的重要思想,它允许应对出现的各种错误或异常情况,从而增加程序的健壮性和可靠性。C语言中的异常处理主要通过使用错误码和错误处理函数来实现。 在进行异常处理时,通常需要先定义一些错误码,用于标识可能出现的异常情况。C语言提供了一些标准的错误码,如errno.h头文件中定义的错误码,还可以根据需要自定义错误码。 接下来,我们需要在程序中合适的位置进行错误检测并进行异常处理。可以使用if语句或者switch语句等条件语句来检测错误码,并根据不同的错误码执行相应的错误处理代码。 错误处理代码的内容可以根据具体情况而定,它可以是打印错误信息、修复错误、返回错误码等操作。在处理完异常后,可以继续执行后续的程序逻辑,或者返回到调用处继续处理。 除了使用错误码和错误处理函数进行异常处理外,C语言还提供了一种特殊的异常处理方式,即信号处理。信号处理是通过捕捉和处理操作系统发送的信号来实现的,通过注册信号处理函数,可以在程序遇到特定信号时执行相应的处理代码。 总之,C语言中的异常处理是一种重要的错误处理机制,可以提高程序的可靠性和健壮性。通过定义错误码、错误处理函数和信号处理,可以有效地捕捉和处理各种异常情况。在编写C程序时,合理地使用异常处理机制是至关重要的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白学C++.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值