0

我有两个问题

第一:当我尝试运行代码时,它给了我一个警告,上面写着“数组索引 4001 超出了数组的末尾(包含 4001 个元素)”

第二:我想从文件中读取单词,然后将它们传递给函数,这样我就可以将单词添加到哈希表并相应地索引它们,并打印文本文件中唯一单词的计数。size 函数就是这样做的。有人可以帮我吗

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

#define HASHSIZE 4001

using namespace std;

class entry {
 public:
  string word;

  int frequency;

  entry() { frequency = 0; }
};

class Hashtable {
 private:
  entry entryArr[HASHSIZE];

  int updateArr[HASHSIZE];

  int costArr[HASHSIZE];

  int sizeUnique = 0;

  int probeCount;

  int updateCount;

 public:
  int HashKey(string key)

  {
    int totalsum = 0;

    // this function is to assign every word a key value to be stored against.

    for (int i = 0; i < key.length(); i++) totalsum += int(key[i]);

    return (totalsum % HASHSIZE);
  }

  void update(string key) {
    int k = HashKey(key);

    if (entryArr[k].frequency == 0) {
      entryArr[k].frequency++;
      updateCount++;
      probeCount++;
      sizeUnique++;
    }
    // function to enter the unique words in the array
    else if (entryArr[k].word == key) {
      entryArr[k].frequency++;
      probeCount++;
    }

    while (entryArr[k].frequency != 0 && entryArr[k].word != key) {
      k++;
    }
    if (entryArr[k].word == key) {
      entryArr[k].frequency++;
    } else {
      entryArr[k].word = key;
    }
    sizeUnique++;
    updateCount++;
    probeCount++;
  }

  int probes() {
    costArr[HASHSIZE] = probeCount;
    return probeCount;
  }

  int size()  // function to count the total number of unique words occuring
  {
    int count = 0;
    updateArr[HASHSIZE] = updateCount;
    for (int i = 0; i < HASHSIZE; i++)
      if (updateArr[HASHSIZE] != 0) {
        count = costArr[i] / updateArr[i];
      }
    cout << count;
    return count;
  }
};

int main() {
  entry e;
  Hashtable h;

  ifstream thisfile("RomeoAndJuliet.txt");

  if (thisfile.is_open()) {
    while (!thisfile.eof) {
      h.update(e.word);
    }

    thisfile.close();
    cout << "The total number of unique words are: " << h.size();
  }

  return 0;
}
4

2 回答 2

4

具有 4001 个元素的数组具有有效索引 0,1,...,3999,4000,因为 C++ 从 0 开始索引。

于 2021-10-27T09:26:54.867 回答
2

当我尝试运行代码时,它给了我一个警告,上面写着“数组索引 4001 超出了数组的末尾(包含 4001 个元素)”

这是因为数组索引从 0 而不是 1 开始。因此大小为 4001 的数组可以安全地索引(访问)到 4000 而不是 4001。

我想从文件中读取单词,然后将它们传递给函数,这样我就可以将单词添加到哈希表并相应地索引它们并打印文本文件中唯一单词的计数

下面的程序显示了如何做到这一点。下面显示的程序计算给定单词在给定 input.txt 文件中出现的次数,然后在单词前面打印该计数。

#include <iostream>
#include <map>
#include <sstream>
#include<fstream>
int main() {
    std::string line, word;
   //this map maps the std::string to their respective count
    std::map<std::string, int> wordCount;
    
    std::ifstream inFile("input.txt");
    
    
    if(inFile)
    {
        while(getline(inFile, line, '\n'))        
        {
            
            std::istringstream ss(line);
            
            while(ss >> word)
            {
                //std::cout<<"word:"<<word<<std::endl;
            wordCount[word]++;
            }      
        }    
    }
    
    else 
    {
        std::cout<<"file cannot be opened"<<std::endl;
    }
    
    inFile.close();
    std::cout<<"Total unique words are: "<<wordCount.size()<<std::endl;
    for(std::pair<std::string, int> pairElement: wordCount)
    {
        std::cout << pairElement.first <<"-" << pairElement.second<<std::endl;
    }
    return 0;
}

这个程序的输出可以在这里看到。

请注意(如上例所示),无需为第二个问题中给出的目的创建单独的类。我们可以使用 4 到 6 行(不包括打开和关闭文件)的代码来做到这一点(如上所示)。

于 2021-10-27T09:42:58.700 回答