您的位置:首页 > 移动开发 > 微信开发

草滩小恪与英语单词--弱爆的小程序

2015-06-14 15:08 519 查看
草滩小恪一直为如何学习英语而苦恼, 特别是单词的记忆。临近考试啦,草滩小恪想恶补一下英语单词, 但是草滩小恪又是very lazy 所以 草滩小恪就找到了草滩大学的历年英语考试卷, 想背一下 阅读 里面出现的高频词汇。草滩小恪认为这idea真TM太机智啦!!!。 但是, 很快草滩小恪就发现, 寻找短文里面的高频词汇真TN的不是人能干的事。那么问题来啦, 咋办呢? 机智的读者想必早已知道了咋办。 是的, 就是这么办的。

程序说明:

主要功能: 统计一篇英语文章里的高频词汇

附加功能:练习拼写这些高频词汇的一个小游戏。

参数: 建立文本文件file1并保存目的文章。 建立包含你不希望统计的高频词汇(如, is , am are等)的文本文件file_namol。

好啦, 开始游戏吧!

#include<iostream>
#include<map>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<fstream>
#include<ctime>
using namespace std;

//对 map的value排序。
typedef pair<string, int> PAIR;
bool cmp(const PAIR& l, const PAIR& r)
{
return l.second > r.second;
}

const int MAXN = 1000 + 5;
char str[MAXN], ss[MAXN];
string s; int len, tot=0;
double pinlv;
map<string, int> word;

//转化单词为string型
void change(int l, int r)
{
int j=0;
for(int i=l; i<=r; i++)
ss[j++] = str[i];
ss[j]='\0';
s=ss;
}

int is(int  i)
{
if(str[i]>='a'&&str[i]<='z')
return 1;
else if(str[i]>='A'&&str[i]<='Z')
{
str[i]=str[i]-'A'+'a';    //把大写的单词转化为小写。
return 1;
}
return 0;
}

//判断是否为单词
int judge(int i)
{
for(int j=i; j<len; j++)
{
if(!is(j)) return j-1;
}
return len;
}

//分离出单个的单词。
void getword(char *str, char*ss)
{
int left, right;
for(int i=0; i<len; i++)
{
if(is(i))
{
tot++;
left = i;
right = judge(i);
change(left, right);
word[s]++;
i=right+1;
}
}
}

//计算单词出现的频率。
double pin(int n)
{
if(tot!=0) return (double) n*1.0/tot;
return 0;
}

//取绝对值函数。
int ABS(int i)
{
if(i<0) return -1*i;
return i;
}

//  画心型  进行刷屏。
void draw()
{
int N = 9;
int i = 0, j = 0;
for (i = -3*N/2; i <= N; i++)
{
for (j = -3*N/2; j <= 3*N/2; j++)
{
if ( (ABS(i) + ABS(j) < N)
|| ((-N/2-i) * (-N/2-i) + ( N/2-j) * ( N/2-j) <= N*N/2)
|| ((-N/2-i) * (-N/2-i) + (-N/2-j) * (-N/2-j) <= N*N/2)
)
{
printf( "* " );
}
else
{
printf( ". " );
}
}
putchar( '\n' );
}
}

//记忆时间控制
void Time_Control(int T)
{
clock_t start, finish;
start =  clock();
do
{
finish = clock();
}while((int)(finish-start)/CLOCKS_PER_SEC<=T);
}

//对拼写成绩进行判定。
void solve(int wrong)
{
switch(wrong)
{
case 1:
cout<<"Sorry! you are wrong!\n";
break;
case 2:
cout<<"So bad! you should do it carefully!\n";
break;
case 3:
cout<<",,,what are you doging?, rubbish。\n";
break;
case 4:
cout<<"I beg you to do it better.\n";
break;
case 5:
cout<<"How rubbish you are!\n";
break;
default :
cout<<"Oh, my God! I give in!\n";
}
cout<<"\n\n\n\a\a\a";
}

int main()
{
word.~map();   pinlv = 0.001;//设定高频
ifstream fcin("file1.txt");    //文章
ofstream fcout("file2.txt");
//ifstream fcin2("file_namol.txt");
int flag = 0;//控制文件结束
while(true)
{
fcin.getline(str, MAXN);
len = strlen(str);

if(len==0)
{
flag++; if(flag==3) break;//连续出现三个空行, 表示文件结束。
}
else flag = 0;
getword(str, ss);
}

//去除 连写 ,'s  , 和常见 简单单词。
ifstream fcin2("file_namol.txt");
while(fcin2>>s)
word[s]=0;

//按频率排序。
vector<PAIR> words(word.begin(), word.end());
sort(words.begin(), words.end(), cmp);

//把高频词汇写入文件
for(int i=0; i<words.size(); i++)
if(pin(words[i].second)>=pinlv)
{
fcout<<"单词:"<<setw(20)<<words[i].first<<"\t\t\t出现的频率:"<<pin(words[i].second)<<endl;
}

//记忆单词小游戏。
int wrong = 0;
for(int i=0; pin(words[i].second)>=pinlv; i++)
{
if(i%10==0) wrong = 0;
cout<<"请在五秒内记住该单词\n";
cout<<words[i].first<<endl;
Time_Control(5);
draw();
cout<<"请在 8 秒内拼写出刚才的那个单词\n\n";
clock_t start, finish;
do{
start =  clock();
cin>>s;
finish = clock();
cin>>s;
if((int)(finish-start)/CLOCKS_PER_SEC>8)
cout<<"You finished it so slow, please do it faster!\n\n\n\n";
}while((int)(finish-start)/CLOCKS_PER_SEC>8);
if(s==words[i].first) cout<<"You are so clever! wonderful!\n\n\n\n\n\n\n\n\n";
else
{
wrong++;
solve(wrong);
}
}
return 0;
}


View Code

悄悄地告诉你草滩小恪的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: