maxword.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 46 行

CPP
46
字号
#include <iostream>#include <string>using namespace std;#include "worditer.h"#include "prompt.h"// illustrates nested loops using WordStreamIterator class// to find the word that occurs most often in a file// Owen Astrachan, 2/13/96, 4/10/99int main(){    int maxOccurs = 0;    int wordCount = 0;    string word,maxWord;     string filename = PromptString("enter file name: ");    WordStreamIterator outer,inner;        outer.Open(filename);         // open two iterators    inner.Open(filename);        for(outer.Init(); outer.HasMore(); outer.Next())    {   wordCount++;        word = outer.Current();       // current word for comparison        int count = 0;                // count # occurrences                for(inner.Init(); inner.HasMore(); inner.Next())        {   if (inner.Current() == word)   // found another occurrence            {   count++;            }        }        if (count > maxOccurs)             // maximal so far        {   maxOccurs = count;            maxWord = word;        }        if (wordCount % 100 == 0)          // update "progress bar"        {   cout << "..";            if (wordCount % 1000 == 0) cout << endl;        }    }    cout << endl << "word \"" << maxWord << "\" occurs "          << maxOccurs << " times" << endl;    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?