/* 本程序由Turbo C2.0编译通过。英文文章请命名为english.txt并放在Turbo C所在目录下。运行结果以文件方式输出,输出文件result.txt也在Turbo C所在目录下。 word是不同的单词; count是该单词在文章中出现的次数; percent是文章中各单词出现的频率。 */ #include "stdio.h" main() { FILE *fp,*result; char ch='\0'; char word[1000][20]; /* 最多存1000个不同单词,每个单词在20个字符内。 */ int count_word[1000]={0}; /* 每个单词对应个数 */ int i=0,j=0,k=0,flag=2,total=0; float percent; /* 每个单词出现频率 */ clrscr(); if(((fp=fopen("english.txt","r"))&&(result=fopen("result.txt","w")))==NULL) { printf("Can't open file\n"); printf("Press any key to exit..."); getch(); exit(0); } printf("\nPlease wait..."); while(!feof(fp)) { ch=fgetc(fp); if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') { if(ch>='A'&&ch<='Z') ch+=32; flag=0; word[i][j]=ch; j++; } else flag++; if(flag==1) { total++; word[i][j]='\0'; count_word[i]++; for(k=0;k #include #include // for exit() #include #include using namespace std; template void writeList(const list& alist, const string& separator); // maintains a word and its frequency of occurrence class wordFreq { public: // initialize word and set freq to 1 wordFreq(const string& str): word(str), freq(1) {} // add 1 to the frequency void increment() { freq++; } // equality operator compares the word for the objects friend bool operator== (const wordFreq& lhs, const wordFreq& rhs) { return lhs.word == rhs.word; } // less than operator compares the word for the objects friend bool operator< (const wordFreq& lhs, const wordFreq& rhs) { return lhs.word < rhs.word; } // output an object in the format: word (freq) friend ostream& operator<< (ostream& ostr, const wordFreq& w) { ostr << w.word << " (" << w.freq << ')'; return ostr; } private: string word; // number of times word found int freq; }; template list::iterator seqSearch(list::iterator first, list::iterator last, const T& target); int main() { ifstream fin; // words read from file and inserted into wf list wf; // use for seqSearch() and displaying the list list::iterator iter; // prompt for the name of the file string fileName, word; cout << "Enter the name of the file containing the words: "; cin >> fileName; // error checking fin.open(fileName.c_str()); if (!fin) { cerr << "Cannot open " << fileName << endl; exit(1); } // read a word until end-of-file while (fin >> word) { // declare a wordFreq object with frequency 1 wordFreq obj(word); // search for word in the list wf iter = seqSearch (wf.begin(), wf.end(), obj); // did we locate the word? if (iter != wf.end()) // yes. increment the word frequency (*iter).increment(); else // word is new. insert obj into the list wf.push_back(obj); } // list member function sort() orders the list wf.sort(); // output each object on a separate line cout << endl; writeList(wf, "\n"); system("pause"); return 0; } template list::iterator seqSearch(list::iterator first, list::iterator last, const T& target) { // start at location first list::iterator iter = first; // compare list elements with item until either // we arrive at last or locate item while(iter != last && !(*iter == target)) iter++; // iter either points at item or is last return iter; } template void writeList(const list& alist, const string& separator = " ") { list::const_iterator iter; for (iter = alist.begin(); iter != alist.end(); iter++) cout << *iter << separator; cout << endl; }