10 high-frequency word filtering and 10-Frequency Word Filtering
// # Include <iostream. h> # include <string. h> # include <fstream. h> struct Word // define a structure array to store words {char w [20]; int num;} Word [10000]; void main () // main function {ifstream infile ("test1.txt", ios: in); char a, t [20]; int n; int wordTypeNum = 0, wordNum = 0; int I; int j; int ijt; for (I = 0; I <10000; I ++) // assign the initial value 0 Word [I] to the number of frequencies in the structure array num. num = 0; strcpy (Word [0]. w, "wow \ 0"); // It is convenient for future programming. Assign the first structure member w a word that is definitely not a word if (! Infile) // determine whether the File is opened successfully. Enable cerr <"File open error" <endl; else {while (infile. get (a) // read each character {if (a = ''| a = ',' | a = ', '| a = '! '| A = '? '| A =' "'| a =': '| a ='; ') // These are word separators, incomplete {t [wordNum] = '\ 0'; // word end for (I = 0; I <= wordTypeNum; I ++) // compare it with the previously stored words {if (strcmp (t, Word [I]. w) = 0) // find the same Word and add num to 1 {Word [I]. num ++; wordNum = 0 ;}} if (strcmp (t, Word [I]. w )! = 0) // the same Word is not found. New words are stored in the new structure {wordTypeNum ++; strcpy (Word [wordTypeNum]. w, t); Word [wordTypeNum]. num ++; wordNum = 0 ;}} else // character composition word {t [wordNum] = a; wordNum ++ ;}}/* cout <endl; for (I = 1; I <= wordTypeNum; I ++) cout <Word [I]. w <"" <Word [I]. num <endl; */for (I = 1; I <wordTypeNum; I ++) // sort the structure array in ascending order of num members {for (j = 1; j <wordTypeNum-I; j ++) {if (Word [j]. num <Word [j + 1]. num) {ijt = Word [j]. num; Word [j]. num = Word [j + 1]. num; Word [j + 1]. num = ijt ;}}cout <"the top 10 words with the highest frequency are as follows:" <endl; for (I = 1; I <= 10; I ++) // output the first 10 {cout <Word [I]. w <"" <Word [I]. num <endl;} infile. close ();}}