c++ - 我试图通过 C++ 中的相关实验室作业来理解类里面给出的伪代码

标签 c++ dictionary hashtable

所以我试图弄清楚教授在黑板上写的到底是什么,以及它如何回答我们要做的实验室作业。

这是实验作业:

Create a Hash Table and Hash map that holds all of the WORDS in the (given below) Declaration of Independence. Handle collisions using the chain method. (Note we will not be modifying this table nor doing deletions!) Programmatically answer the following questions:

  1. What is the size of your hash table?
  2. What is the longest collision (ie. Chain)
  3. What is the most frequently used word and how did you determine it?

Create a (second) Hash Table that holds all of the LETTERS in the Declaration of Independence.

  1. What is the size of your hash table

  2. What letter has the longest collision?

这是伪代码,我做了一些修改以修复一些错误:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>

using namespace std;

class Translate
{
    string word;

public:
    int trans(string word);
    w = word.charAT(0);  //gives a letter
    return #num;
};

class HashTable
{
    int size();
    int collision();
    int length();
    char fword();

public:
    Translate t;
    list<string> hashTable[29];
    bool insert(string word)
    {
         hashTable[t.trans(word)].push_back(word);
         return true;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    HashTable h;
    open file f("hash.txt");
    //h.insert(word)
    while (!f.eof())
    {
        h.insert(f.word());
    }

    cout << h.size;
    cout << h.collision.length;
    cout << h.fword;

    return 0;
}

我遇到的错误是:

Error 15 error C1903: unable to recover from previous error(s); stopping compilation Error 5 error C2014: preprocessor command must start as first nonwhite space
Error 4 error C2059: syntax error : 'return'
Error 13 error C2065: 'f' : undeclared identifier
Error 10 error C2065: 'file' : undeclared identifier Error 8 error C2065: 'open' : undeclared identifier Error 6 error C2143: syntax error : missing ';' before '}'
Error 1 error C2143: syntax error : missing ';' before '='
Error 11 error C2146: syntax error : missing ';' before identifier 'f'
Error 9 error C2146: syntax error : missing ';' before identifier 'file'
Error 14 error C2228: left of '.eof' must have class/struct/union
Error 3 error C2238: unexpected token(s) preceding ';'
Error 7 error C2238: unexpected token(s) preceding ';'
Error 12 error C3861: 'f': identifier not found
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error 19 IntelliSense: '#' not expected here Error 17 IntelliSense: class "std::basic_string, std::allocator>" has no member "charAT"
Error 21 IntelliSense: expected a ';'
Error 18 IntelliSense: expected a declaration
Error 22 IntelliSense: identifier "f" is undefined
Error 20 IntelliSense: identifier "open" is undefined
Error 16 IntelliSense: this declaration has no storage class or type specifier

我从未使用过 .c_str,而且我对 C++ 还是很陌生,所以我的知识有限。我知道有些地方需要标识符,但我认为有更好的方法来创建“打开文件”。我以前的知识是 C#、HTML 和一些 Python,其中 C++ 给我带来了一些学习和理解上的困难。任何帮助和/或见解将不胜感激!

最佳答案

代码乱七八糟,难以理解。但是,我会尽我最大的努力帮助我了解 C++ 和哈希方面的知识。

提议的代码修改

  1. 程序入口点:不要使用 int _tmain(int, _TCHAR*),而是使用 int main()。这应该可以保证您能够进行测试迁移到非 Windows 编译器。 来源:Unicode _tmain vs main

我想帮助解决剩下的问题,但是,发布的代码太难理解了。如果发布算法以供引用,将不胜感激。

关于c++ - 我试图通过 C++ 中的相关实验室作业来理解类里面给出的伪代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29336230/

相关文章:

c++ - 工作 C++03 代码上的 G++ (C++14) 链接器错误

c++ - 为什么 try...catch 需要抛出 EXACT 类型

c++ - 将任何库的 "isKeyPressed"函数转换为 "keyPressedOnce"?

c# - 如何创建 'Private' ResourceDictionary?

python - 如何对字典进行排序以从每个键的最高值到最低值打印?

c++ - 实现多进程日志记录的最佳方式(C++)

powershell - 在Powershell中将嵌套的哈希表导出到CSV

c++ - 类(C++)中通用查找函数的返回值冲突,返回什么?

c - 如何修复此调整大小功能?

python - 如何压缩numpy数组列表?