#include <iostream>
using namespace std;
#include <map>
#include <string>
#define divisionline() cout << "-----------------------------------------------------------------" <<endl
typedef struct tag_Count
{
unsigned int count;
float rate;
}WORDDATA;
void calcrate(map<string,WORDDATA> &wordcount)
{
long long words = 0;
for (map<string,WORDDATA>::iterator iter = wordcount.begin();
iter != wordcount.end(); ++iter)
{
words += iter->second.count;
}
for (map<string,WORDDATA>::iterator iter = wordcount.begin();
iter != wordcount.end(); ++iter)
{
iter->second.rate = iter->second.count/(float)words;
}
}
void printanalyse(map<string,WORDDATA> wordcount)
{
pair<string,WORDDATA> maxlength;
pair<string,WORDDATA> maxcount;
long long words = 0;
for (map<string,WORDDATA>::iterator iter = wordcount.begin();
iter != wordcount.end(); ++iter)
{
if (iter->first.length() > maxlength.first.length())
maxlength = *iter;
if (iter->second.count > maxcount.second.count)
maxcount = *iter;
words += iter->second.count;
}
cout << "Words total number is :" << endl << words << endl;
cout << "The longest word is :" <<endl;
cout << maxlength.first << " : " << maxlength.second.count << '\t' << maxlength.second.rate << endl;
cout << "The most count of word is :" << endl;
cout.width(maxlength.first.length());
cout << maxcount.first << " : " << maxcount.second.count << '\t' << maxcount.second.rate << endl;
}
void printresult(map<string,WORDDATA> wordcount)
{
unsigned int width = 0;
for (map<string,WORDDATA>::iterator iter = wordcount.begin();
iter != wordcount.end(); ++iter)
{
if (iter->first.length() > width)
width = iter->first.length();
}
cout.width(width);
cout << "word" << " " << "couts" << '\t' << "rate" << endl;
for (map<string,WORDDATA>::iterator iter = wordcount.begin();
iter != wordcount.end(); ++iter)
{
cout.width(width);
cout << iter->first << " : " << iter->second.count << '\t' << iter->second.rate << endl;
}
}
int getword(FILE **fp,int tch,string &temp)
{
int ch;
temp.clear();
temp += tch;
while (((ch = fgetc(*fp)) != EOF) && (isalpha(ch) || (ch == '-')))
temp += ch;
return ch;
}
int savecount(FILE **fp,int ch,map<string,WORDDATA> &wordcount)
{
string temp;
int res;
res = getword(fp,ch,temp);
map<string,WORDDATA>::iterator iter = wordcount.find(temp);
if (iter != wordcount.end())
{
++iter->second.count;
}
else
{
WORDDATA wd;
wd.count = 1;
wd.rate = 0;
wordcount.insert(make_pair(temp,wd));
}
return res;
}
int main(void)
{
map<string,WORDDATA> word_count;
string path;
FILE* fp;
int ch;
cout << "Please input the full file path:"<< endl;
divisionline();
cin >>path;
fp = fopen(path.c_str(),"r");
if (fp == NULL)
{
cout << "Open file faild!" << endl;
return 0;
}
while ((ch = fgetc(fp)) != EOF)
{
if (!isalpha(ch))
{
continue;
}
else
{
if (savecount(&fp,ch,word_count) == EOF)
break;
}
}
calcrate(word_count);
divisionline();
cout << "Show result:" << endl;
printresult(word_count);
divisionline();
cout << "Analyse words:" << endl;
printanalyse(word_count);
divisionline();
return 0;
}