由于公司需要筛选文章中的关键字,文章很多关键字也很多,一遍一遍的用Ctrl + f 很麻烦,拜托大神写了个简单的程序,自己根据需求改了改,贴在这里做个备份
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <fstream>
using namespace std;
int AcompareB(char *a, string b)
{
for(int i = 0; i < b.length(); i ++)
{
if(a[i] != b[i])
{
return 1;
}
}
return 0;
}
int main()
{
ifstream fin;
ofstream fout;
vector<string> KeyWord;
map<string, int> KeyWordNum;
int maxLength = 0;
char *hWords;
char *isBeUsed;
fout.open("D:\\output.txt", ios::out);
fin.open("D:\\input.txt", ios::in);
string temKeyWord;
while(fin >> temKeyWord)
{
KeyWord.push_back(temKeyWord);
KeyWordNum[temKeyWord] = 0;
if(temKeyWord.length() > maxLength)
{
maxLength = temKeyWord.length();
}
}
fin.close();
hWords = new char[KeyWord.size()];
isBeUsed = new char[maxLength];
for(int i = 0; i < KeyWord.size(); i ++)
{
hWords[i] = KeyWord[i][0];
}
fin.open("D:\\text.txt", ios::in);
while(fin.peek()!=EOF)
{
int spacenum = 0;
memset(isBeUsed, 0, maxLength);
char word = fin.get();
if(word == ' ' || word == '\n')
{
continue;
}
int temSign = 0;
for(int i = 0; i < KeyWord.size(); i ++)
{
if(word == hWords[i])
{
temSign = 1;
break;
}
}
if(temSign == 1)
{
isBeUsed[0] =word;
int cnt = 0;
for(int i = 1; i < maxLength; i ++)
{
if(fin.peek()== EOF)
{
break;
}
char th = fin.get();
if(th == ' '|| th == '\n')
{
spacenum += 1;
i --;
continue;
}
cnt ++;
isBeUsed[i] = th;
}
for(int i = 0; i < KeyWord.size(); i ++)
{
if(AcompareB(isBeUsed, KeyWord[i]) == 0)
{
KeyWordNum[KeyWord[i]]++;
}
}
fin.seekg(-cnt - spacenum ,ios::cur);
}
}
map<string,int>::iterator it;
for(it = KeyWordNum.begin(); it != KeyWordNum.end(); it ++)
{
fout << it->first << " " << it->second << endl;
}
fout.close();
fin.close();
return 0;
}
有需要的同学可以根据自己的需求改一改