/*337.in.txt:
*to being or not to being, is a question.
*do you have a question.
*yes, I have a question. I like Apple, Apple a very declicious!
***/
/*337.out.txt:
* All words: The number of words:
Do 1
I 2
to 1
Yes 1
A 4
Apple 2
Being 2
Declicious 1
Have 2
is 1
Like 1
Not 1
or 1
Question 3
to 1
Very 1
You 1
***/
1 #include <iostream>2#include <cctype>3#include <fstream>4#include <cstring>5 using namespacestd;6 7 Const intMaxlist =1010+Ten;//maximum value of the Word table8 Const intMaxword = -+Ten;//Maximum number of word lengths9 Ten structWordList One { A CharWord[maxword];//Word - intFre//Word frequency - }; the - voidSort (WordList list[],intLength//sort the Word table - { - intI, J, K; + for(i =0; I < length-1; i++) - { +K =i; A for(j = k +1; J < length; J + +) at { - if(strcmp (List[k].word, List[j].word) >0) -K =J; - } - if(k! =i) { -WordList tmp =List[i]; inList[i] =List[k]; -LIST[K] =tmp; to } + } - } the * //because the word length is different, this function is used to standardize the output format $ voidShoworder (WordList W,CharExtraintLen, ofstream& out) Panax Notoginseng { - out<<W.word; the for(inti =1; I <= --len; i++) + { A out<<Extra; the } + out<< W.fre <<Endl; - } $ $ intMain () - { -Ifstreaminch("337.in.txt");//reading a Word file theOfstream out("337.out.txt");//write a word and its number of files - Wuyi if(!inch) { thecout <<"Failure!"<< Endl;return 1; - } Wu -WordList List[maxlist];//Word list About intN =0;//the number of words $ CharTmp[maxword];//read a word - - while(inch>>tmp) - { A inti; + for(i =0; i < N; i++)//find the word in the word list the { - if(strcmp (List[i].word, tmp) = =0) {//find Then, fre++; $list[i].fre++; Break; the } the } the if(i = = N && isalpha (tmp[0]))//Add the word if it is not found, and the word frequency is set to 1, the number of words table n++ the{//punctuation is not counted - strcpy (List[n].word, TMP); inList[n++].fre =1; the } the } About the Sort (list, N); //You can sort the Word table after you write to the file the out<<"all words are: \ t word number: \ n"; the for(inti =0; i < N; i++) + { -Showorder (List[i],' ', strlen (List[i].word), out); the }Bayi return 0; the}
Word Frequency Statistics _ input to file