草滩小恪一直为如何学习英语而苦恼, 特别是单词的记忆。临近考试啦,草滩小恪想恶补一下英语单词, 但是草滩小恪又是very lazy 所以 草滩小恪就找到了草滩大学的历年英语考试卷, 想背一下 阅读 里面出现的高频词汇。草滩小恪认为这idea真TM太机智啦!!!。 但是, 很快草滩小恪就发现, 寻找短文里面的高频词汇真TN的不是人能干的事。那么问题来啦, 咋办呢? 机智的读者想必早已知道了咋办。 是的, 就是这么办的。
程序说明:
主要功能: 统计一篇英语文章里的高频词汇
附加功能:练习拼写这些高频词汇的一个小游戏。
参数: 建立文本文件file1并保存目的文章。 建立包含你不希望统计的高频词汇(如, is , am are等)的文本文件file_namol。
好啦, 开始游戏吧!
1 #include<iostream> 2 #include<map> 3 #include<vector> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 #include<iomanip> 8 #include<fstream> 9 #include<ctime> 10 using namespace std; 11 12 //对 map的value排序。 13 typedef pair<string, int> PAIR; 14 bool cmp(const PAIR& l, const PAIR& r) 15 { 16 return l.second > r.second; 17 } 18 19 const int MAXN = 1000 + 5; 20 char str[MAXN], ss[MAXN]; 21 string s; int len, tot=0; 22 double pinlv; 23 map<string, int> word; 24 25 //转化单词为string型 26 void change(int l, int r) 27 { 28 int j=0; 29 for(int i=l; i<=r; i++) 30 ss[j++] = str[i]; 31 ss[j]=‘\0‘; 32 s=ss; 33 } 34 35 int is(int i) 36 { 37 if(str[i]>=‘a‘&&str[i]<=‘z‘) 38 return 1; 39 else if(str[i]>=‘A‘&&str[i]<=‘Z‘) 40 { 41 str[i]=str[i]-‘A‘+‘a‘; //把大写的单词转化为小写。 42 return 1; 43 } 44 return 0; 45 } 46 47 //判断是否为单词 48 int judge(int i) 49 { 50 for(int j=i; j<len; j++) 51 { 52 if(!is(j)) return j-1; 53 } 54 return len; 55 } 56 57 //分离出单个的单词。 58 void getword(char *str, char*ss) 59 { 60 int left, right; 61 for(int i=0; i<len; i++) 62 { 63 if(is(i)) 64 { 65 tot++; 66 left = i; 67 right = judge(i); 68 change(left, right); 69 word[s]++; 70 i=right+1; 71 } 72 } 73 } 74 75 //计算单词出现的频率。 76 double pin(int n) 77 { 78 if(tot!=0) return (double) n*1.0/tot; 79 return 0; 80 } 81 82 //取绝对值函数。 83 int ABS(int i) 84 { 85 if(i<0) return -1*i; 86 return i; 87 } 88 89 // 画心型 进行刷屏。 90 void draw() 91 { 92 int N = 9; 93 int i = 0, j = 0; 94 for (i = -3*N/2; i <= N; i++) 95 { 96 for (j = -3*N/2; j <= 3*N/2; j++) 97 { 98 if ( (ABS(i) + ABS(j) < N) 99 || ((-N/2-i) * (-N/2-i) + ( N/2-j) * ( N/2-j) <= N*N/2) 100 || ((-N/2-i) * (-N/2-i) + (-N/2-j) * (-N/2-j) <= N*N/2) 101 ) 102 { 103 printf( "* " ); 104 } 105 else 106 { 107 printf( ". " ); 108 } 109 } 110 putchar( ‘\n‘ ); 111 } 112 } 113 114 //记忆时间控制 115 void Time_Control(int T) 116 { 117 clock_t start, finish; 118 start = clock(); 119 do 120 { 121 finish = clock(); 122 }while((int)(finish-start)/CLOCKS_PER_SEC<=T); 123 } 124 125 //对拼写成绩进行判定。 126 void solve(int wrong) 127 { 128 switch(wrong) 129 { 130 case 1: 131 cout<<"Sorry! you are wrong!\n"; 132 break; 133 case 2: 134 cout<<"So bad! you should do it carefully!\n"; 135 break; 136 case 3: 137 cout<<",,,what are you doging?, rubbish。\n"; 138 break; 139 case 4: 140 cout<<"I beg you to do it better.\n"; 141 break; 142 case 5: 143 cout<<"How rubbish you are!\n"; 144 break; 145 default : 146 cout<<"Oh, my God! I give in!\n"; 147 } 148 cout<<"\n\n\n\a\a\a"; 149 } 150 151 int main() 152 { 153 word.~map(); pinlv = 0.001;//设定高频 154 ifstream fcin("file1.txt"); //文章 155 ofstream fcout("file2.txt"); 156 //ifstream fcin2("file_namol.txt"); 157 int flag = 0;//控制文件结束 158 while(true) 159 { 160 fcin.getline(str, MAXN); 161 len = strlen(str); 162 163 if(len==0) 164 { 165 flag++; if(flag==3) break;//连续出现三个空行, 表示文件结束。 166 } 167 else flag = 0; 168 getword(str, ss); 169 } 170 171 //去除 连写 ,‘s , 和常见 简单单词。 172 ifstream fcin2("file_namol.txt"); 173 while(fcin2>>s) 174 word[s]=0; 175 176 //按频率排序。 177 vector<PAIR> words(word.begin(), word.end()); 178 sort(words.begin(), words.end(), cmp); 179 180 //把高频词汇写入文件 181 for(int i=0; i<words.size(); i++) 182 if(pin(words[i].second)>=pinlv) 183 { 184 fcout<<"单词:"<<setw(20)<<words[i].first<<"\t\t\t出现的频率:"<<pin(words[i].second)<<endl; 185 } 186 187 //记忆单词小游戏。 188 int wrong = 0; 189 for(int i=0; pin(words[i].second)>=pinlv; i++) 190 { 191 if(i%10==0) wrong = 0; 192 cout<<"请在五秒内记住该单词\n"; 193 cout<<words[i].first<<endl; 194 Time_Control(5); 195 draw(); 196 cout<<"请在 8 秒内拼写出刚才的那个单词\n\n"; 197 clock_t start, finish; 198 do{ 199 start = clock(); 200 cin>>s; 201 finish = clock(); 202 cin>>s; 203 if((int)(finish-start)/CLOCKS_PER_SEC>8) 204 cout<<"You finished it so slow, please do it faster!\n\n\n\n"; 205 }while((int)(finish-start)/CLOCKS_PER_SEC>8); 206 if(s==words[i].first) cout<<"You are so clever! wonderful!\n\n\n\n\n\n\n\n\n"; 207 else 208 { 209 wrong++; 210 solve(wrong); 211 } 212 } 213 return 0; 214 } 215
悄悄地告诉你草滩小恪的file_namol文件:
of the we and in have a would it as to life but what who is those day last i his us that should be days all sight are live each hours only or time our hearing death hero not values often more sometimes being an for make such same if take some do which at when most were with blind usually use always by until thought lost him old one out read realize regrets rule saved sense toward teach their there these they think thinking this tomorrow tasks under vigor vista was whose without wondering year sights years stroke suffered does chose nt s has been times living used change other clsss