繁体   English   中英

从C ++中的文件输入中区分单词和计数

[英]Distinct word and count from file input in C++

这是必要条件:每遇到一个新单词,程序都应从动态内存中分配一个节点实例以包含该单词及其计数,并将其插入链接列表中,以便始终对列表进行排序。 如果列表中已经存在遇到的单词,则该单词的计数应增加。

我确实到处搜索,正确的解决方案是使用std :: map,但我不想使用它,因为到目前为止我还没有学到。 使用List或Vector并创建一个结构或类来操纵每个节点可以吗?

这是我正确的代码

class Node {
string word;
int count;

public:
    Node() {
        word = "";
        count = 1;
    }
    Node(const Node &other) : word(other.word), count(other.count) {
        // copy constructor 
    }
    ~Node() {} // Destructor

    void printWord() const {
        cout << count << " " << word << endl;
    }
    void loadWord(ifstream &fin) { 
        fin >> word;
    }
    void setWord(const string &word) {
        this->word = word;
    }
    const string& getWord() const {
        return word;
    }
    void incrementCount() {
        count++;
    }
};

void load(list<Node> &nodes, const char *file);
void print(const list<Node> &nodes);
bool isExist(const list<Node> &nodes, const string &word, Node &node);
void error(const string &message, const char *file);
const Node& getNode(const list<Node> &nodes, const string &word);

int main(int argc, char *argv[]) {

    list<Node> nodes;

    if (argc != 2) {
        cout << "Error syntax : require an input file\n";
        return 0;
    }
    load(nodes, argv[1]);
    print(nodes);

    return 0;   
}

void print(const list<Node> &nodes) {

    list<Node>::const_iterator itr;

    for (itr = nodes.begin(); itr != nodes.end(); itr++) {
        itr->printWord();
    }
cout << '\n';
}

void load(list<Node> &nodes, const char *file) { 

    ifstream fin;
    Node node;
    string temp;

fin.open(file);

if (!fin) 
    error("Cannot open file ", file); // exit

while (!fin.eof()) {
    if (fin.good()) {
        fin >> temp;
        if (!isExist(nodes, temp, node)) {
            node.setWord(temp);
            nodes.push_back(node);
        } else {
            // increase word count here
        }

    } else if (!fin.eof()) 
        error("Unable to read data from ", file);
}
fin.close();
}

bool isExist(const list<Node> &nodes, const string &word, Node &node) {
list<Node>::const_iterator itr;
for (itr = nodes.begin(); itr != nodes.end(); itr++) {
    if(word.compare(itr->getWord()) == 0) {
        return true;
    }
}
return false;
}

const Node& getNode(const list<Node> &nodes, const string &word) {
    list<Node>::const_iterator itr;
    for (itr = nodes.begin(); itr != nodes.end(); itr++) {
        if(word.compare(itr->getWord()) == 0) {
            return *itr;
        }
    }
    return NULL; // This is fail what should I do to return a NULL value when not found
}

void error(const string &message, const char *file) {
cerr << message << file << '\n';
exit(0);
}

代码不起作用,我只是尝试通过应用Java知识来生成解决方案来解决该问题,但是在c ++中控制对象似乎有所不同。 有人可以检查我的代码并建议我一种更好的方法吗?

谢谢。

不,这不是练习向量和列表的最佳任务。 您确实必须查看std :: map文档,并编写3行高效,美观的代码。 您为什么不应用TreeMap Java知识?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM