检查文件中在同一行中同时包含一个或 none 个字符串的所有行

Check all lines in the file that have both, one or none of the strings in the same line

我想检查所选字符串是否出现在同一行中。 可悲的是我没有得到正确的输出。 该文件包含此文本

/* one 
two
one two
two one
two 
one
something
else */



#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void display(ifstream& MyFile, string a, string b)
{
    string s="";
    int choice;
    cout << "Choose the mode you want to use " << endl;
    cout << "1. Find both words" << endl;
    cout << "2. Find one words" << endl;
    cout << "3. Find where there is none words" << endl;
    cout << "Choose the mode you want to use : ";
    cin >> choice;
    switch (choice) {
    case 1:
        while (getline(MyFile, s))
        {
    
            if (s.find_first_of(a, 0) && (s.find_first_of(b, 0)) )
            {
                cout << "Found both words" << endl;  
            }
        }
        break;
    case 2:
        while (getline(MyFile, s))
        {
            if ((s.find_first_of(a, 0)  && !s.find_first_of(b, 0)) || (!s.find_first_of(a, 0) && s.find_first_of(b, 0) ))
            {
                cout << "Found one word" << endl;
            }
        }
        break;
    case 3:
        while (getline(MyFile, s))
        {
            getline(MyFile, s);
            if (!s.find_first_of(a, 0) && !s.find_first_of(b, 0))
            {
                cout << "No words " << endl;
            }
        }
        break;
    default:
        cout << "Wrong Input" << endl;
    }
}

  
int main()
{
    ifstream Myfile("Mydata.dat");
    string a = "one", b = "two",fileData;
    display(Myfile, a, b);
    Myfile.close();
    return 0;
}

我得到了 5 次“找到两个词”,而它应该是 2 次。其他选项也会发生同样的事情。我的 想法是,在 if 语句中,我错误地比较了函数 s.find_firstof(str, pos),因为此函数 returns 是 str 中任何字符的 s 中第一个实例的索引,从位置 pos 开始搜索。我的第二个想法是我读错了文件的数据。

Found both words
Found both words
Found both words
Found both words
Found both words

两个明显的错误:

find_first_of() returns a size_t (位置)不是布尔值。要检查是否成功,您需要 find_first_of() != string::npos

假设 find_first_of() 做了 return 一个 bool:

if (s.find_first_of(a, 0) || (s.find_first_of(b, 0)) )
{
     cout << "Found both words" << endl;  
}

如果你找到其中一个字符串,那么你就找到了两个?错误的。您需要 &&(逻辑与)而不是 ||(逻辑或)

忽略评论中的单词与字符串问题,我将通过计算找到的字符串数量来简化您的代码:

int num_words_found = 0;
if (s.find_first_of(a, 0) != string::npos) { num_words_found++; }
if (s.find_first_of(b, 0) != string::npos) { num_words_found++; }
// none: num_words_found= 0
// one/either: num_words_found= 1
// both: num_words_found= 2

你的 if 语句是错误的。

我改变了验证一个字符串是否包含另一个字符串的方式,基本上s.find(a)将returna在s中的起始位置,如果它存在于其中,或者return -1 如果没有。 这样,像这样验证 s.find(a) != -1s.find(a) <s.length() 将以相同的方式工作,因为 -1 到 int 的转换是 size_t

的最大值

这应该可以解决您的问题:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void display(ifstream &MyFile, string a, string b)
{
    string s = "";
    int choice;
    cout << "Choose the mode you want to use " << endl;
    cout << "1. Find both words" << endl;
    cout << "2. Find one words" << endl;
    cout << "3. Find where there is none words" << endl;
    cout << "Choose the mode you want to use : ";
    cin >> choice;
    switch (choice)
    {
    case 1:
        while (getline(MyFile, s))
        {

            if (s.find(a) < s.length() && (s.find(b) < s.length()))
            {
                cout << "Found both words" << endl; // means that this line have BOTH words
            }
        }
        break;
    case 2:
        while (getline(MyFile, s))
        {
            if (s.find(a) < s.length() || (s.find(b) < s.length()))
            {
                cout << "Found one word" << endl; //means that this line have at least one of the two words
            }
            /*
            if (s.find(a) < s.length() && (s.find(b) < s.length())) 
            {
                cout << "Found one word" << endl; //means that this line have the two words at the same time
            }
            */
        }
        break;
    case 3:
        while (getline(MyFile, s))
        {
            getline(MyFile, s);
            if (!s.find_first_of(a, 0) && !s.find_first_of(b, 0))
            {
                cout << "No words " << endl; // means that this line do not have ANY of the two words
            }
        }
        break;
    default:
        cout << "Wrong Input" << endl;
    }
}

int main()
{
    ifstream Myfile("Mydata.dat");
    string a = "one", b = "two", fileData;
    display(Myfile, a, b);
    Myfile.close();
    return 0;
}

这段代码的输出是:

Choose the mode you want to use 
1. Find both words
2. Find one words
3. Find where there is none words
Choose the mode you want to use : 1
Found both words
Found both words

Choose the mode you want to use 
1. Find both words
2. Find one words
3. Find where there is none words
Choose the mode you want to use : 2
Found one word
Found one word
Found one word
Found one word
Found one word
Found one word