#if !defined(AFX_WORDPROCESS_H__DC6CE2F6_54C5_47DE_A1FF_F5C87B8C309F__INCLUDED_)
#define AFX_WORDPROCESS_H__DC6CE2F6_54C5_47DE_A1FF_F5C87B8C309F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct WORD_FREQ
{
string strword;
int freq;
}WF;
class CWordProcess
{
public:
void OutPut();
void PushVector(string word);
void Split(char *pstr,char *pword);
void ProcessFile();
CWordProcess(string filesrc,string filedest);
virtual ~CWordProcess();
private:
vector<WF> m_vector;
string FileDestPath;
string FileSrcPath;
FILE *fr;
FILE *fw;
};
#endif // !defined(AFX_WORDPROCESS_H__DC6CE2F6_54C5_47DE_A1FF_F5C87B8C309F__INCLUDED_)
#include "stdafx.h"
#include <stdio.h>
#include "WordProcess.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWordProcess::CWordProcess(string filesrc,string filedest)
{
FileSrcPath=filesrc;
FileDestPath=filedest;
}
CWordProcess::~CWordProcess()
{
fclose(fw);
fclose(fr);
}
void CWordProcess::ProcessFile()
{
fr=fopen(FileSrcPath.c_str(),"r");
fw=fopen(FileDestPath.c_str(),"w");
char szBuf[200]={'\0'};
char szWord[200]={'\0'};
while(!feof(fr))
{
fscanf(fr,"%s",szBuf);
Split(szBuf,szWord);
printf("%s",szWord);
PushVector(szWord);
}
OutPut();
}
void CWordProcess::Split(char *pstr, char *pword)
{
char *p=pstr;
char *p1=strchr(p,'/');
if(p1!=NULL)
{
*p1='\0';
strcpy(pword,p);
}
}
void CWordProcess::PushVector(string word)
{
WF wf;
vector<WF>::iterator iter;
for(iter=m_vector.begin();iter!=m_vector.end();iter++)
{
wf=(WF)*iter;
if(wf.strword==word)
{
wf.freq++;
*iter=wf;
return;
}
}
wf.strword=word;
wf.freq=1;
m_vector.push_back(wf);
}
void CWordProcess::OutPut()
{
WF wf;
vector<WF>::iterator iter;
for(iter=m_vector.begin();iter!=m_vector.end();iter++)
{
wf=*iter;
fprintf(fw,"%s ",wf.strword.c_str());
fprintf(fw,"%d",wf.freq);
fprintf(fw,"\n");
}
}
#include "stdafx.h"
#include "WordProcess.h"
int main(int argc, char* argv[])
{
string strfilesrc="d:\\199801.txt";
string strfiledest="d:\\result.txt";
CWordProcess f(strfilesrc,strfiledest);
f.ProcessFile();
printf("Hello World!\n");
return 0;
}
這是被讀取檔案http://pan.baidu.com/s/1sj1Gl7R
http://pan.baidu.com/s/1sj1Gl7R
uj5u.com熱心網友回復:
求助咯
uj5u.com熱心網友回復:
記得vector的排序專業#include <algorithm>
sort(v_ResList.begin();v_ResList.end());
uj5u.com熱心網友回復:
謝謝了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98830.html
標籤:基礎類
上一篇:資料結構求助
下一篇:新手 ,求大神指導
