一道关于删除文本中重复内容的编程题,求教c/c++大神!!!

图片不太清楚,大神凑活着看吧~还有一道,会继续追加财富值,跪求大神啊~... 图片不太清楚,大神凑活着看吧~
还有一道,会继续追加财富值,跪求大神啊~
展开
 我来答
解竹0cg
2014-10-22 · TA获得超过246个赞
知道小有建树答主
回答量:172
采纳率:100%
帮助的人:89.2万
展开全部

void SplitStringByMark(const std::string& strString, char cMark, std::vector<std::string>& vecString)
{
std::string strStrIn = strString;
for (int nPos = 0; nPos < (int)strStrIn.size(); nPos++)
{
unsigned char ch = strStrIn[nPos];
if (ch > 0x80)  //汉字
{
nPos++;
continue;
}

if (cMark == strStrIn[nPos])
{
std::string strDiv = strStrIn.substr(0, nPos);
strStrIn = strStrIn.substr(nPos+1, strStrIn.size() - nPos - 1);
vecString.push_back(strDiv);
nPos = -1;
}
}
if (!strStrIn.empty())
{
vecString.push_back(strStrIn);
}
}

int _tmain(int argc, _TCHAR* argv[])
{

string strSource = "ab=123|bc=234|cd=345|dc=456";

vector<string> vecSplit;

SplitStringByMark(strSource, '|', vecSplit);

map<string, string> mapString;
vector<string>::iterator iterStr = vecSplit.begin();
for (; iterStr != vecSplit.end(); ++iterStr)
{
vector<string> vecStrInfo;
SplitStringByMark(*iterStr, '=', vecStrInfo);
if (2 != vecStrInfo.size())
{
cout<<"源字符串内容错误!";
break;
}

mapString.insert(pair<string, string>(vecStrInfo.at(0), vecStrInfo.at(1)));
}

string strIn = "";
cout<<"请输入要查找的字符串:"<<endl;
while(cin>>strIn)
{
if (mapString.count(strIn))
{
cout<<strIn<<"对应的值是:"<<mapString.at(strIn)<<endl;
}
else
{
cout<<"不存在"<<strIn<<"对应的值。"<<endl;
cout<<"要退出程序请输入:y,不退出请输入N"<<endl;
cin>>strIn;
if (strIn == "y")
{
break;
}
cout<<"请输入要查找的字符串:"<<endl;
}
}
system("PAUSE");
return 0;
}

 


第二个问题:我有一个十分垃圾的办法,只能用于你这种文本内容十分少的情况,多了就不行了。我也不了解真正的文本编译器是如何实现的,如果你找到了资料希望能给我分享一下。

#include <algorithm>
#define MAXBUFERSIZE 1024
int _tmain(int argc, _TCHAR* argv[])
{
ifstream inStream;
inStream.open("D:\\123.txt");

if (!inStream.is_open())
{
cout<<"未打开文件"<<endl;
exit(1);
}

vector<string> vecLineInfo;
char buffer[MAXBUFERSIZE];
while(inStream.getline(buffer, MAXBUFERSIZE))
{
vecLineInfo.push_back((string)buffer);
}
inStream.close();

sort(vecLineInfo.begin(), vecLineInfo.end());

vecLineInfo.erase(unique(vecLineInfo.begin(), vecLineInfo.end()), vecLineInfo.end());

ofstream outStream;
outStream.open("D:\\123.txt");
for (unsigned int i = 0; i < vecLineInfo.size(); ++i)
{
outStream<<vecLineInfo.at(i)<<endl;
}
outStream.close();
return 0;
}
明快还顽强的纯真
2014-10-22 · TA获得超过1447个赞
知道大有可为答主
回答量:2329
采纳率:66%
帮助的人:731万
展开全部

伪代码如下:

题1

vecBefStr;
vecAftStr;
strTemp;
read (char in string)
{
switch(char)
{ case '=':
{
vecBefStr.push(strTemp);
strTemp = "";
}
break;
case '|':
{
vecAftStr.push(strTemp);
strTemp = "";
}
break;
default:
strTemp += char;
break;
}
}

至于查找, 遍历2个数组匹配, 找到对应序号的字符串就OK了.

题2:

vecLine;
readline(line1);
if line1 not in vecLine
  vecLine.push(line1);

-.- 这题不要太简单啊... 判断是否在数组里, 直接遍历数组就是...

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2014-10-22
展开全部

#include "stdafx.h"

#include <stdlib.h>

#include <iostream>

#include <vector>


#include <string>

#include <fstream>

#include <cstdlib>


int main(int argc, char* argv[])

{

    ifstream ifile("E:\\temp.txt");

    vector<string> vec;

    string ss;

 

    while(ifile>>ss) vec.push_back(ss);

    ifile.close();


vector<string> vecWritedata;

    for(int i = 0; i < vec.size(); ++i)

{

if (0 == i) 

{

vecWritedata.push_back(vec[i]);

}

else

{

bool bexist = false;

for (int j = 0; j < vecWritedata.size(); ++j)

{

if (vec[i] == vecWritedata[j])

{

bexist = true;

break;

}

}


if (!bexist)

   vecWritedata.push_back(vec[i]);

}

}


ofstream ofile("E:\\temp.txt");

vector<string>::iterator it;

for(it = vecWritedata.begin(); it<vecWritedata.end(); it++)

ofile<<*it<<endl;

ofile.close();

system("PAUSE");

return 0;

}


已赞过 已踩过<
你对这个回答的评价是?
评论 收起
析君豪2Q
2014-10-22 · TA获得超过411个赞
知道小有建树答主
回答量:724
采纳率:50%
帮助的人:213万
展开全部
题1:先按|号分成数组,再按=号分;
题2:先把字符串都存入数组,再索引对比删除
更多追问追答
追问
大神,有没有具体点代买的实现,挺着急的。。会继续追加财富值的;
大神要具体代码呀,跪求啊,江湖救急啊!!!
追答
c++处理字符串真的很头疼,用其它语言行不行啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式