map——单词的转换

程序实现目的:根据“转换规则文件(暗码)”对“待转换文件(明文)”进行转换。

使用数据结构:map的创建、搜索、以及遍历。


//map的创建、搜索、以及遍历
//单词转换
#include<iostream>//定义用于读写流的基本类型
#include<map>
#include<string>
#include<fstream>//定义用于读写命名文件的类型
#include<sstream>//定义用于读写内存string对象的类型

using namespace std;

//建立转换映射(map的创建)
map<string, string> buildMap(ifstream &map_files)
{
	map<string, string> trans_map;   //保存转换规则
	string key;   //要转换的单词(缩略词)
	string value;  //替换后的内容(缩略词相对应的完整内容)
	//读取第一个单词存入key,剩余的内容存入value。
	//在执行读取操作时,string对象会自动忽略开头的空白(空格、换行、制表符)并从第一个真正的字符开始读起,直到遇到下一处空白为止
	//getline函数的参数是一个输入流和一个string对象,函数从给定的输入流中读取内容,直到遇到换行符为止,然后把所读的内容存入那个string对象。
	while(map_files >> key && getline(map_files, value))
	{
		if (value.size() > 1)
			trans_map[key] = value.substr(1);//跳过缩略词和说完整内容之间空格————前导空格
		else
			throw runtime_error("no rule for" + key);
	}

	cout << "转换规则文件map:" << endl;
	for (auto s : trans_map)
		cout << s.first << " " << s.second << endl;
	cout << endl;

	return trans_map;
}

//生成转换文本(map的搜索)
const string & word_transform(const string &s, const map<string, string> &m)
{
	auto map_it = m.find(s);//首先确定给定的string是否在map中
	
	if (map_it != m.cend())
		return map_it->second; //使用替换短语
	else
		return s;  //否则返回原string
}

//单词转换程序(map的遍历)
void transform(ifstream &map_files,ifstream &input)
{
	auto trans_map = buildMap(map_files); //保存转化规则
	string text;
	cout << "转换后文本内容:" << endl;

	//转换后文本打印在控制台
	//用getline一行一行的读取输入文件是为了使输出中的换行位置能和输入文件中一样
	//使用istreamstring来处理当前行中的每个单词
	while (getline(input, text))   //读取输入文件的每一行
	{
		istringstream stream(text);    //读取每个单词
		string word;
		bool firstword = true;         //控制是否打印空格
		while (stream >> word)
		{
			if (firstword)
				firstword = false;
			else
				cout << " ";           //在单词间打印一个空格

			cout << word_transform(word, trans_map);//打印输出
		}
		cout << endl;  //完成一行的转换
	}


	转换后文件输出在输出文件
	//cout << "转换后文件输出在输出文件(..\\out_files.txt)" << endl;
	//ofstream out("..\\out_files.txt");
	//while (getline(input, text))   //读取输入文件的每一行
	//{
	//	istringstream stream(text);    //读取每个单词
	//	string word;
	//	bool firstword = true;         //控制是否打印空格
	//	while (stream >> word)
	//	{
	//	    if (firstword)
	//			firstword = false;
	//		else
	//			out << " ";           //在单词间打印一个空格

	//		out << word_transform(word, trans_map); //输出
	//	}
	//	out << endl;  //输出换行
	//}
	//out.close();
}

void main()
{
	ifstream map_files, input_files;
	map_files.open("..\\map_files.txt");//转换规则文件放在该源程序的上一级目录下
	input_files.open("..\\input_files.txt");//待转换文件放在该源程序的上一级目录下

	//将待转换文件文本内容打印在控制台
	cout << "待转换文件文本内容:" << endl;
	string str;
	while (getline(input_files, str))
	{
		cout << str << endl;
	}
	cout << endl;

	//一旦一个文件流已经打开,它就保持与对应文件的关联。实际上,对于一个已经打开的文件流调用open会失败,随后的试图使用文件流的操作都会失败
	input_files.close();//关闭input_files,以便我们将其用于其他文件
	input_files.open("..\\input_files.txt");

	transform(map_files, input_files);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值