考虑一个手机应用功能,统计出用户的(英语)词汇量
1. 录入词库
2. 统计,连续弹出单词让用户识别,统计出认识的词汇总量,给用户作参考
一个简单的例子,根据txt文本中的英文内容提前出单词
//5-22 get words from a text
void getWords()
{
map <string, int> dic;
map <string, int> :: iterator pIter;
fstream infile;
char buf[1024*8];
//read a txt file and get the word in each line and store the words into a map
infile.open("D:\\2013\\05\\book\\test.txt");
while(!infile.eof()) //ÅжÏÎļşÊÇ·ñ¶Á½áÊø
{
infile.getline(buf, 1024*8);
char* p = buf;
char aWord[64] = {0};
int pos = 0;
while(*p != '\0')
{
if (*p != ' ' && *p != '(' && *p != '"' && *p != ',' &&
*p != '.' && *p != ':' && *p != '?' && *p != ')' && *p != '[' && *p != ']'
&& *p != '!')
{
aWord[pos++] = *p;
}
else
{
aWord[pos] = '\0';
if (pos > 0)
{
//ignore the case
if (aWord[0] >= 'A' && aWord[0] <= 'Z')
{
aWord[0] += ('a' - 'A');
}
string astr = aWord;
dic[astr] = 1;
}
pos = 0;
}
p++;
}
if (pos > 0)
{
aWord[pos] = '\0';
if (aWord[0] >= 'A' && aWord[0] <= 'Z')
{
aWord[0] += ('a' - 'A');
}
string astr = aWord;
dic[astr] = 1;
}
}
infile.close();
//write the words into a text, each word takes one line
fstream ofile;
ofile.open("D:\\2013\\05\\book\\result.txt", ios::app );
for ( pIter = dic.begin( ) ; pIter != dic.end( ) ; pIter++ )
{
ofile << pIter -> first << endl;
}
ofile.close();
}
5.22 Ag 4.44