#include <iostream>
#include <vector>
#include <cstring>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
vector<string> Letter;
float frequency1(string word)
{
float count=0.0;
for (int i=0;i<Letter.size();++i)
{
transform(word.begin(),word.end(),word.begin(),::tolower);
transform(Letter[i].begin(),Letter[i].end(),Letter[i].begin(),::tolower);
if (strcmp(word.c_str(),Letter[i].c_str())==0)
{
count+=1;
}
}
count=(count/Letter.size())*100;
if (count>=0.5)
{
return count;
}
else
{
return 0.0;
}
}
int main()
{
ifstream fin;
fin.open("frequent.txt");
if (fin.fail())
{
cout<<"Error opening file!"<<endl;
}
while(!fin.eof())
{
string buffer;
getline(fin,buffer,' ');
cout<<buffer<<endl;
Letter.push_back(buffer);
}
cout<<endl;
vector<string> frequent;
vector<float> frequency;
for (int i=0;i<Letter.size();++i)
{
string a=Letter[i];
int k=0;
for (int j=0;j<i;++j)
{
transform(a.begin(),a.end(),a.begin(),::tolower);
transform(Letter[j].begin(),Letter[j].end(),Letter[j].begin(),::tolower);
if (a==Letter[j])
{
break;
}
k++;
}
int size=Letter.size();
if (k!=size-1)
{
continue;
}
float counter=frequency1(a);
if(counter>0.0)
{
frequent.push_back(Letter[i]);
frequency.push_back(counter);
}
}
cout<<"Here are the repeated words"<<endl;
for (int i=0;i<frequency.size();++i)
{
cout<<" "<<frequent[i]<<", frequency: "<<frequency[i]<<endl;
}
system("PAUSE");
return 0;
}
我正在编写一个程序来确定文档(文本)中重复单词的频率。如果频率大于或等于 0.5,则该词作为重复词通过。但是当我运行它时,它没有显示任何重复的单词,尽管我什至手动计算并且知道文档中有重复的单词。我无法弄清楚问题所在。