C ++:在字符串中查找单词,计算找到的次数,然后打印单词的含义

时间:2020-02-23 17:57:02

标签: c++

我正在做作业,我尽力了。现在,我无法弄清缺少的东西或可以改变的地方。 我需要该程序才能读取文件。如果找到搜索词的开头,则会列出该词及其含义。如果他不止一次发现它,那么他只会写那个没有意义的词。 现在,如果程序找到更多的单词,它将写出第一个单词的含义,并写出找到的其他单词的单词。 我不知道我还能使用其他周期。如果您能帮助我,我将不胜感激。

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include<bits/stdc++.h>


using namespace std;

int main()
{

ifstream dictionary("dictionary.txt");
if(!dictionary.is_open()){
    cout<< "File failed to open" << endl;
    return 0;
    }

int option;

    cout << "1.<starting>" << endl;
    cout << "4.<stop>" << endl;
    cin >> option;

string find_word;
string word, meaning;
string line;
string found;
int count = 0;



    if (option == 1)
    {
        cout << "Find the meaning of the word beginning with the characters:";
        cin >> find_word;

        while (getline(dictionary,line))
            {
                stringstream ss(line);
                getline (ss, word, ';');
                getline (ss, meaning, ';');

            if (word.rfind(find_word, 0) != string::npos)
                {
                    count++;
                    if (count <=1)
                    {
                        found = word + meaning;
                        cout << found << endl;
                    }

                    if (count >= 2)
                    {
                        found = word ;
                        cout << found << endl;
                    }
                }
            }
    }


      if (option == 4)
    {
        return 0;
    }

dictionary.close();

    return 0;
}


编辑 dictionary.txt如下所示:

attention; attentionmeaning
attention; attentionmeaning2
computer; computermeaning
criminal; criminalmeaning
boat; boatmeaning
alien; alienmeaning
atter; meaning
.
.
etc.

例如,输入为:

Find the meaning of the word beginning with the characters: att

这就是我现在得到的(输出):

attention attentionmeaning
attention
atter

这是我期望的(期望输出):

attention 
attention
atter

如果程序仅找到一个搜索词,则应这样写:

Find the meaning of the word beginning with the characters: bo

输出:

boat boatmeaning

2 个答案:

答案 0 :(得分:1)

正如已经建议的那样,在读取文件时,您不知道是否有多个条目与您的搜索字词匹配。就是说,您需要一些中间结构来存储所有匹配的条目。

收集所有结果后,可以轻松检查数据是否包含多个结果,在这种情况下,仅打印不带含义的“单词”。如果只有一个结果,则可以打印“单词”及其含义。

该代码可能看起来像这样:

struct Entry {
    std::string name;
    std::string meaning;

    bool startsWith(const std::string& str) {
        return name.find(str) != std::string::npos;
    }
};

Entry createEntry(const std::string& line) {
    Entry entry;
    std::stringstream ss(line);
    std::getline(ss, entry.name, ';');
    std::getline(ss, entry.meaning, ';');
    return entry;
}

int main() {
    std::string query = "att";
    std::ifstream dictionary("dictionary.txt");
    std::vector<Entry> entries;
    std::string line;
    while (std::getline(dictionary, line)) {
        Entry entry = createEntry(line);
        if (entry.startsWith(query)) {
            entries.emplace_back(std::move(entry));
        }
    }

    for (const Entry& entry : entries) {
        std::cout << entry.name << (entries.size() > 1 ? "\n" : " " + entry.meaning + '\n');
    }
}

此代码肯定可以进行更多优化,但是为了简单起见,这足够了。


Demo

答案 1 :(得分:0)

问题是,在循环的第一时间,您不知道字符串中是否有一个或多个有效单词。我建议您在循环外部创建一个空列表,并将所有匹配的单词和含义对推入列表。然后,如果列表的大小为1,则可以输出单词和含义对,否则使用for循环遍历并仅打印单词。