在基于while(cin>>string)读取文本的前提下,我写了一个main函数来统计必要单词的频率并排序,该文件(b.cpp)可以运行。
但是我把它改写成一个类文件的时候并在Testwordcountclas.cpp文件对其测试时,输出结果中并没有去除指定的单词,同时也没有去除标点符号。
请问该如何修改
输入文本:
After a decade-long exploration, GYL has formulated a preliminary yet successful model for cultivating international specialised talents. As a pioneer in the rethinking and reshaping of education, the University proposed Syntegrative Education (SE) in 2016 to respond to the challenges brought about by AI and robotics in the innovation-driven era. Based on an existing specialised talent cultivating model, SE integrates specialised, industrial and management programmes to deliver international industry experts, with the ability to lead future industry development. Those leaders are highly versatile with the ability to drive the industries of the future. They will be well-rounded yet specialised, know their industries thoroughly, and be able to mobilise resources and lead people.
文件b.cpp
# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
int pos;
string word;//缓存字符串
string sep(" ,.?!:\"\t\n();");
vector<string> word_set;//确认不是stopword词汇后的单词集放进去
vector<int> word_num;//单词集对应的次数
vector<string> stop_word;//应删去的词
int size = 0;//向量的size
bool flag1,flag2;
stop_word.push_back("to");
stop_word.push_back("in");
stop_word.push_back("will");
stop_word.push_back("of");
stop_word.push_back("be");
stop_word.push_back("and");
stop_word.push_back("a");
stop_word.push_back("the");
cout<<"enter the text"<<endl;
while(cin>>word)
{
// 除去每一个word多余的符号
pos = word.find_first_of(sep);
if ( pos == word.size()-1) //符号出现在尾部
{
string tmp(word.substr(0, pos));
word = tmp;
}
else if (pos == 0)//符号出现在头部
{
string::size_type pos2 = word.find_first_not_of(sep, 1);
pos = word.find_first_of(sep, pos2+1);
string tmp(word.substr(pos2, pos-1));
word = tmp;
}
// 除去stop word
flag1=false;
for (int i = 0; i < stop_word.size(); i++)
{
if (word==stop_word[i])
{
flag1=true;
}
}
if (flag1)
{
continue;
}
//判断单词集是否存在单词或者与word相同的单词
size = word_set.size();
flag2 = true;
for (int i=0; i!=size; ++i)
if (word_set[i] == word) {
++word_num[i];
flag2 = false;
break;
}
//存入单词集并在次数向量中对应+1
if (flag2)
{
word_set.push_back(word);
word_num.push_back(1);
}
}
//给单词次数进行降序处理,同时改变单词顺序
for (int i = 0; i < word_set.size()-1; i++)
{
int currentmax = word_num[i];
int currentmaxindex = i;
string temps_storage;
for (int j=i+1; j < word_set.size(); j++)
{
if (word_num[j]>currentmax)
{
currentmax = word_num[j];
temps_storage = word_set[j];
currentmaxindex = j;
}
}
if (currentmaxindex!=i)
{
word_num[currentmaxindex] = word_num[i];
word_num[i]=currentmax;
word_set[currentmaxindex] = word_set[i];
word_set[i] = temps_storage;
}
}
// 输出单词以及对应的频率
for (int i = 0; i < word_num.size(); i++)
{
cout<<word_set[i]<<" "<<word_num[i]<<endl;
}
return 0;
}
声明的头文件 wordcount.h
#ifndef WORDCOUNT_H
#define WORDCOUNT_H
# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
#include <iomanip>
using namespace std;
// 在while(cin>>string)读取文本的情况下使用该类的成员函数
class Wordcount
{
public:
Wordcount();
void setword(string str);
bool getflag();
void removesep();
void checkword();
void saveword();
void descendingsort();
void showwordset();
private:
int pos2;
int pos;//标点符号的位置
int size;//向量的size
bool flag1, flag2;
string word;//缓存
string sep;//标点符号
vector<int> word_num;//单词的频率统计
vector<string> stop_word;//应删去的词
vector<string> word_set;//确认不是stopword词汇后的单词放进去该向量
};
#endif
定义文件 wordcount.cpp
# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
#include <iomanip>
#include"wordcount.h"
using namespace std;
Wordcount::Wordcount()
{
pos=0;
size=0;
string word;//缓存
bool flag1=false;
bool flag2=true;
string sep(" ,.?!:\"\t\n();");//分隔符号
vector<int> word_num;//单词的频率统计
vector<string> stop_word;//应删去的词
vector<string> word_set;//确认不是stopword词汇后的单词放进去该向量
stop_word.push_back("to");
stop_word.push_back("in");
stop_word.push_back("will");
stop_word.push_back("of");
stop_word.push_back("be");
stop_word.push_back("and");
stop_word.push_back("a");
stop_word.push_back("the");
}
void Wordcount::setword(string str)
{
word=str;
}
bool Wordcount::getflag()
{
return flag1;
}
// 请注意:以下函数用于处理带有标点符号的单词
// 除去每一个word多余的符号
void Wordcount::removesep()
{
pos = word.find_first_of(sep);//标点符号在word中的位置
if ( pos == word.size()-1) //符号出现在尾部
{
string tmp(word.substr(0, pos));
word = tmp;
}
else if (pos == 0)//符号出现在头部
{
pos2 = word.find_first_not_of(sep, 1);
pos = word.find_first_of(sep, pos2+1);
string tmp(word.substr(pos2, pos-1));
word = tmp;
}
}
void Wordcount::checkword()//检查该word是不是stopword中的单词
{
flag1=false;
for (int i = 0; i < stop_word.size(); i++)
{
if (word==stop_word[i])//如果是flag1变成true
{
flag1=true;
}
}
}
void Wordcount::saveword()
{
//判断单词集是否存在单词或者与word相同的单词
size = word_set.size();
flag2 = true;
for (int i=0; i!=size; ++i)
if (word_set[i] == word) {
++word_num[i];
flag2 = false;
break;
}
//存入单词集并在次数向量中对应+1
if (flag2)
{
word_set.push_back(word);
word_num.push_back(1);
}
}
void Wordcount::descendingsort()
{
//给单词次数进行降序处理,同时改变单词顺序
for (int i = 0; i < word_set.size()-1; i++)
{
int currentmax = word_num[i];
int currentmaxindex = i;
string temps_storage;
for (int j=i+1; j < word_set.size(); j++)
{
if (word_num[j]>currentmax)
{
currentmax = word_num[j];
temps_storage = word_set[j];
currentmaxindex = j;
}
}
if (currentmaxindex!=i)
{
word_num[currentmaxindex] = word_num[i];
word_num[i]=currentmax;
word_set[currentmaxindex] = word_set[i];
word_set[i] = temps_storage;
}
}
}
void Wordcount::showwordset()
{
// 输出单词以及对应的频率
for (int i = 0; i < word_num.size(); i++)
{
cout<<word_set[i]<<" "<<word_num[i]<<endl;
}
}
调用类进行测试 TestWordcountClass.cpp
#include "wordcount.h"
# include<iostream>
# include<stdio.h>
#include<string>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
string words;
Wordcount wordcount;
cout << "please enter the text" << endl;
while (cin >> words)//windows按ctrl+Z退出循环,mac按control+D
{
wordcount.setword(words);
wordcount.removesep();
wordcount.checkword();
//如果单词属于stopword,此时flag1变成true,那么continue语句跳出本次循环,即不会把该单词放入word_set
if (wordcount.getflag())
{
continue;
}
wordcount.saveword();
wordcount.descendingsort();
}
wordcount.showwordset();//循环结束后输出统计的单词
system("pause");
return 0;
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。