【练习】有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,

有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。

这两个文件内容如下:

context.txt

“并不是每个人都需要$(qunar)自己的粮食,$(flight.1)每个人都需要做自己穿的$(flight.2),我们说着别人发明的$(hotel),使用别人发明的数学......我们一直在$(tuan)别人的成果。使用人类的已有经验和知识$(travel.1)来进行,是一件$(travel.2)的事情” 

word.conf

flight=也不是:衣服

qunar=种植

hotel=语言

tuan=使用

travel=发明创造:很了不起


// ConsoleApplication.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include	 <map>
using namespace std;

map<string, string> m_mapFind;

vector<string> Spit(string strContent, string strSymbol)
{
	vector<string> outVec;
	string strTemp = strContent;
	int pos =0;
	while (true)
	{
		pos = strTemp.find(strSymbol, pos);
		if (pos != -1)
		{
			outVec.push_back(strTemp.substr(0, pos));
			strTemp = strTemp.substr(pos + strSymbol.size());
		}
		else
		{
			outVec.push_back(strTemp);
			break;
		}
	}
	return outVec;
}

void GetWords()
{
	ifstream inWord;
	inWord.open("E:\\word.cnf", ios::in);

	int pos = -1;
	int posEnd = -1;
	string lineContent;

	string strKey;
	string strValue;
	while (getline(inWord, lineContent))
	{
		posEnd = lineContent.find('=');
		if (posEnd == -1)
			continue;
		strKey = lineContent.substr(0, posEnd);
		strValue = lineContent.substr(posEnd + 1);
		vector<string> valList = Spit(strValue, ":");
		if (valList.size() != 0)
		{
			if (valList.size() == 1)
			{
				m_mapFind.insert(make_pair(strKey, valList[0]));
				continue;
			}
			for (int i = 0; i < valList.size(); i++)
			{
				char str[10];
				sprintf(str, "%d", i + 1);
				string strTempKey = strKey;
				strTempKey = strTempKey + "." + str;
				m_mapFind.insert(make_pair(strTempKey, valList[i]));
			}
		}
		else
		{
			string strValue = lineContent.substr(posEnd + 1);
			m_mapFind.insert(make_pair(strKey, strValue));
		}

	}
	inWord.close();
}

int main()
{
	ifstream inWord,inContent;
	string  strWord,strContent;
	inContent.open("E:\\context.txt", ios::in);

	GetWords();
	
	char c;
	while ((c = inContent.get()) != EOF)
	{
		strContent.push_back(c);
	}

	string  strRet;
	for (int i = 0; i < strContent.size();i++)
	{
		if (strContent[i] == '$'  && strContent[i+1] == '(')
		{
			int posEnd = strContent.find(")", i + 1);
			string strKey = strContent.substr(i+2,posEnd-i-2);
			strRet += m_mapFind[strKey];
			i = posEnd;
			continue;
		}
		strRet += strContent[i];
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值