使用 getline 和字符串流检查句子中的单词是否在文件中

Using getline and stringstream to check if words in a sentence are in a file

本文关键字:单词 文件 是否 句子 getline 字符串 检查 使用      更新时间:2023-10-16

我已经在做一项作业好几天了,我似乎无法弄清楚这一点。

我得到了 4 个包含单词的文件(形容词、名词、代词和动词),我必须检查用户输入的句子并确定句子的结构以及不同类型的单词是否按特定顺序出现。

我的方法是使用 getline 来获取句子,然后使用 istringstream 查看句子中的每个单词,并将每个单词与每个文件进行比较。

例如:我会得到第一个单词,遍历形容词文件并确定它是否是形容词。如果不是,那么我会查看同一个单词并遍历名词文件并确定它是否是名词,依此类推。

我不能为此分配使用数组、向量或字符串函数。这真的让我很失望。我想使用 istringstream 来查看句子中的每个单词,但我似乎无法做到这一点。

例如:在查看第一个单词并确定它是形容词、名词、代词还是动词之后,我想让他们使用 istringstream 查看句子中的第二个单词,依此类推。这是行不通的,我也不知道为什么。

在我完成这项工作后,我计划将每个单词放入布尔函数中,即检查第一个单词是否是形容词、名词、代词或动词,并根据它的位置返回 true。

我是C++的初学者,我快要疯了,试图做到这一点。有没有更好的方法?我想做的事情有什么问题?这是我到目前为止所拥有的:

string sentence;
string fileWord;
cout << "Input your sentence : " << endl;
getline(cin, sentence);
stringstream iss(sentence);
string firstWord;
string secondWord;

ifstream adjectiveFile;
adjectiveFile.open("/words/adjectives");
// Outputs error if there's an error opening the file
if(adjectiveFile.fail()){
cout << "Failed to open file" << endl;
exit(1);
}
// Opens noun file
ifstream nounFile;
nounFile.open("/words/nouns");
// Outputs error if there's an error opening the file
if(nounFile.fail()){
cout << "Failed to open noun file" << endl;
exit(1);
}
// Opens pronouns file
ifstream pronounFile;
pronounFile.open("/words/pronouns");
// Outputs error if there's an error opening the file
if(pronounFile.fail()){
cout << " Failed to open pronoun file" << endl;
exit(1);
}
// Opens verbs file
ifstream verbFile;
verbFile.open("/words/verbs");
// Outputs error if there's an error opening the file
if(verbFile.fail()){
cout << "Failed to open verb file" << endl;
exit(1);
}
// Gets first word from sentence and them determines what type of word   it is by looping through each file
iss >> firstWord;
while (adjectiveFile >> fileWord) {
if (firstWord == fileWord) {
cout << "The first word of the sentence is the adjective " << firstWord << endl;
}
}
while(nounFile >> fileWord){
if(firstWord == fileWord){
cout << "The first word of the sentence is the noun " << firstWord << endl;
}
}
while(pronounFile >> fileWord){
if(firstWord == fileWord){
cout << "The first word of the sentence is the pronoun " << firstWord << endl;
}
}
while(verbFile >> fileWord){
if(firstWord == fileWord){
cout << "The first word of the sentence is the verb " << firstWord << endl;
}
}

// Doesn't work. Can't access the second word of the sentence using istringstream. 
iss >> secondWord;
while(adjectiveFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the adjective " << secondWord << endl;
}
}
while(nounFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the noun " << secondWord << endl;
}
}
while(pronounFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the pronoun " << secondWord << endl;
}
}
while(verbFile >> fileWord){
if(secondWord == fileWord){
cout << "The second word of the sentence is the verb " << secondWord << endl;
}
}
return 0;
}

您正在尝试多次读取同一文件。在您第一次运行while(adjectiveFile >> fileWord){}后,指针将位于文件的末尾,因此下次您尝试调用同一循环时,adjectiveFile >> fileWord将失败,并且您永远不会真正检查是否secondWord == fileWord

你可以看看ifstream::seekg()ifstream::clear(),看看如何读两遍。话虽如此,正如上面的评论中提到的,复制和粘贴相同的代码 4x 肯定不是最好的方法。