素年锦时琉璃醉 2015-07-03 03:08 采纳率: 0%
浏览 1311
已结题

用VETOR进行词频统计及排序,统计做出来了,排序怎么搞,代码如下。

#include #include #include 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 m_vector; string FileDestPath; string FileSrcPath; FILE *fr; FILE *fw;};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::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::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;}

  • 写回答

1条回答 默认 最新

  • oyljerry 2015-07-03 14:22
    关注

    排序可以用std::sort
    然后定义自己的比较函数

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?