【问题标题】:Reading words from an input file and counting unique words从输入文件中读取单词并计算唯一单词
【发布时间】:2016-08-06 22:40:16
【问题描述】:

大家好,我在执行这项任务时遇到了一些问题。我需要:

读取输入文件的每个单词。对于每个单词:

  1. 如果该词已存在于您的词库中,则只需将计数加 1。不要在您的清单中添加重复的单词。
  2. 如果您的单词清单中不存在该单词,请将其添加并将计数设置为 1。
  3. 读取所有输入文件后,关闭输入文件。
  4. 按 ASCII 字序对字库进行排序
  5. 显示单词库存,即列出单词及其计数。输出应按 ASCII 字序排序。

我允许的图书馆是iostreamiomanipstringcstringcerrnolimitssstreamfstreamcmath

到目前为止,我一直在数单词时遇到困难!我的代码计算字符,而不是单词。到目前为止我的代码如下:

#include "appl.h"
using namespace std;

/*
 Class definition file for Appl
*/
string getFileName(ios_base::open_mode parm);
struct wordBlock {
    string word;
    int count;
};

// Constructor
Appl::Appl()
{
    // id string is required for all CS 162 submissions.  *** DO NOT CHANGE ***
    _cs162_id_ = new string(__FILE__ + string(" compiled ")
                            + __DATE__ + string(" ") + __TIME__
                            + string(" using g++ ") + to_string(__GNUC__)
                            + string(".") + to_string(__GNUC_MINOR__) + string(".")
                            + to_string(__GNUC_PATCHLEVEL__));  
}

// Destructor
Appl::~Appl()
{
    delete _cs162_id_;
}

string inputFileName(ios_base::open_mode parm){
    fstream iFile;
    string inFileName = "";
    int count = 1;

    if(parm == ios::in){
        while(count != 0){
            cout << "Enter an input file name that exists: ";
            getline(cin,inFileName);
            iFile.open(inFileName.c_str() , ios::in);
            if(iFile.good() != true){
                cout << "?Invalid file name : file does not exist" <<                                   
                        count++;
                iFile.clear();
            }else{
                count = 0;
                iFile.close();
                return inFileName;
            }
        }
    }
}

// Main Routine
int Appl::main(int argc, char ** argv)
{
    fstream inFile;
    string inFileNames;

    inFileNames = inputFileName(ios::in);    
    inFile.open(inFileNames.c_str(), ios::in);

    wordBlock inventory[1000];

    if(inFile.is_open()){
        for(auto idx = 0; idx < 1000; idx ++){
            inventory[idx].word = idx;
            inventory[idx].count = 0;
        }

        while(inFile.peek() != EOF) {
            inventory[inFile.get()].count++;
        }
        for(auto idx = 0; idx < 1000; idx++){
            inventory[idx].word = idx;
            inventory[idx].count = 0;
        }

        while(inFile.peek() != EOF) {
            inventory[inFile.get()].count++;
        }
        for(auto idx = 0; idx < 1000; idx++){
            if(inventory[idx].count == 0) continue;
            cout << "Word " << inventory[idx].word << " occurs " << inventory[idx].count << " times" << endl;
        }
        inFile.clear();
        inFile.close();
    }
    return 0;
}

【问题讨论】:

  • 首先你需要阅读this istream::get reference。那么你也应该阅读"Why is iostream::eof inside a loop condition considered wrong?"。然后你需要重新思考你的设计,我建议你先把它写在某个地方,包括程序的要求、目标和目的,然后再考虑一下如何实现程序的目的。
  • 相关:我在您的描述中没有看到关于确定“唯一”的不区分大小写。您可能需要重新阅读作业并澄清这些小细节,因为它可能会使这变得更加复杂。例如:单词“all”与“All”不是“等价”作为 ASCII 编码,但在字典中几乎是等价的。
  • 对您可以使用或不可以使用的内容是否有任何限制? std::map 可以做任何事情,除了获取输入。
  • 是的,有限制。这些是我允许的库:#include #include #include #include #include #include #include #include #include
  • 感谢您迄今为止的所有帮助。 Joachim Pileborg 我明白你的意思!我认为我的 inFile.get() 不适合这种情况。

标签: c++


【解决方案1】:

你可能想使用 stl 中的 set 声明一组字符串类型

set <string> mySet;

bool myfunc(string word){
    pair<set<string>::iterator,bool>unique; 
/* use #include<utility>
    when a set insertion returns a pair of values, the second one is boolean type. and as set's element is always sorted and unique, it makes our life easy*/
unique = mySet.insert(word);

return unique.second;
}

【讨论】:

  • 感谢您的建议!不幸的是,我不能使用比提供的库更多的库。 :'(
  • 如果&lt;set&gt; 被允许分配,可能会考虑,尽管&lt;map&gt; 将是一个更强的选择。 两者都是不允许的。
【解决方案2】:

在伪代码中,您需要执行以下操作来获取计数:

  1. 创建一个计数器来跟踪库存中的字数,开头必须为 0(我们称之为 wordCount)。
  2. 从文件中读取一个单词。
  3. 遍历所有添加的单词并查看该单词是否在列表中
    • 从0到wordCount
  4. 将新单词与inventory[index] 处的单词进行比较
    • 如果它们匹配,则在 inventory[index] 处增加计数并停止循环
    • 如果不匹配,请转到下一个。
  5. 如果在wordCount 的末尾没有找到它,将inventory[wordCount] 设置为计数为0 的单词并增加wordCount
    • 由于您有基于 0 的索引,因此您的库存中当前计数的单词应该是空的,因此将其添加为新单词并增加计数。
  6. 重复直到读完所有单词。

要对单词进行排序,请阅读排序算法,冒泡排序之类的方法应该很容易实现:

  • operator&lt; 可以使用:if(str1 &lt; str2) {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2015-06-02
    • 1970-01-01
    • 2018-04-25
    • 2016-05-21
    • 2012-08-07
    • 2016-08-10
    相关资源
    最近更新 更多