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

Pseudo-code given in class that I'm trying to comprehend with the associated lab assignment in c++

本文关键字:作业 伪代码 实验室 c++      更新时间:2023-10-16

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

这是实验室任务:

创建一个哈希表和哈希映射,其中包含(下面给出的)独立声明中的所有单词。使用链方法处理碰撞。(请注意,我们不会修改此表,也不会删除!)以程序方式回答以下问题:

  1. 你的哈希表有多大
  2. 最长的碰撞是什么(如链条)
  3. 最常用的词是什么?你是如何确定的

创建一个(第二个)哈希表,其中包含《独立宣言》中的所有字母。

  1. 你的哈希表的大小是多少

  2. 哪个字母碰撞时间最长?

这是我为修复一些错误所做的一些修改后的伪代码:

#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;
}

我的错误是:

错误15错误C1903:无法从以前的错误中恢复;停止编译错误5错误C2014:预处理器命令必须作为第一个非空白开始
错误4错误C2059:语法错误:"return"
错误13错误C2065:"f":未声明的标识符
错误10错误C2065:"文件":未声明的标识符错误8错误C2065:"打开":未声明的标识符错误6错误C2143:语法错误:缺少";"在"}"之前
错误1错误C2143:语法错误:缺少";"在'='
之前错误11错误C2146:语法错误:缺少";"在标识符"f"之前
错误9错误C2146:语法错误:缺少";"在标识符"file"之前
错误14错误C2228:'.ef'的左侧必须具有类/结构/联合
错误3错误C2238:";"前面有意外的令牌
错误7错误C2238:";"前面有意外的令牌
错误12错误C3861:"f":找不到标识符
错误2错误C4430:缺少类型说明符-假定为int。注意:C++不支持默认int
错误19 IntelliSense:此处不应为"#"错误17 IntelliSense:类"std::basic_string,std::allocater>"没有成员"charAT"
错误21 IntelliSense:应为";"
错误18 IntelliSense:应为声明
错误22 IntelliSense:标识符"f"未定义
错误20 IntelliSense:标识符"open"未定义
错误16 IntelliSense:此声明没有存储类或类型说明符

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

代码太混乱,无法理解。然而,我正在尽我所能帮助我掌握C++和hash方面的一些知识。

拟议的代码修改

  1. 程序入口点:使用int main()而不是int _tmain(int, _TCHAR*)。如果您迁移到非windows编译器,这将保证您能够测试结果。来源:Unicode _tmainmain

我想帮助完成剩下的部分,但是,发布的代码太难理解了。如果算法发布以供参考,那将是一件好事。

您应该更改以下几点:

  1. 假设trans()应该是一个函数定义,而不是声明,并且它后面的行应该是主体:
    1. 除非您特别希望复制传递的字符串,否则应该使用const string&而不是string
    2. 它应该有支架
    3. CCD_ 8是CCD_
    4. std::string定义了operator[],因此它可以像数组一样进行索引
    5. 我不确定#num是什么(我认为它来自Python,但我不熟悉),所以我不确定您打算如何计算返回值
      [因此,我假设您希望返回w,但要返回int而不是char。如果是这种情况,只返回return word[0];会更简单。]
  2. HashTable的成员有一些问题。
    1. 成员函数size()collision()length()fword()是私有的。这似乎不是故意的
    2. 成员变量thashTablepublic,而您可能希望它们是私有的。同样,这似乎不是故意的
    3. 函数实际上并没有在任何地方定义,除非您没有显示它们的定义。当您调用它们时,这将导致链接错误
  3. 虽然这不需要更改,但如果硬编码为始终返回true,则HashTable::insert()没有理由实际返回值。此外,如上文1.1中所述,参数可能应该是const string&
  4. _tmain()_TCHAR是Microsoft的扩展,可在Visual Studio和一些(但不是全部)旨在与之兼容的编译器(如C++Builder)上使用。如果您希望您的代码独立于平台,那么您可能需要main()。[请注意,这不需要更改。如果您只使用Visual Studio进行编译,则可以保持原样。如果您希望平台独立,则可以轻松地自己定义_tmain_TCHAR。]
  5. 打开文件:
    1. openfile都不是C++中的关键字,也不是类型(尽管FILE是C类型,但它似乎不是您想要的)。您似乎想要std::ifstream
    2. 您不应该在while循环中使用!f.eof()作为条件,因为直到读取失败后才会设置eofbit
    3. CCD_ 40没有成员函数CCD_。然而,如果给定一个可以接受的参数,提取运算符operator>>()将一次读取一个单词
  6. HashTable::size()HashTable::collision()HashTable::length()HashTable::fword()是函数。要调用它们,请使用operator()。如果您只是直接使用函数的名称,则不会调用它,而是引用它(这可以用于创建函数指针或函数引用)
  7. int没有成员函数length()。因此,您不能调用h.collision().length()。在C++中,如果你像那样链接函数调用,那么链中的每个函数都被视为是前一个类型的成员函数,而不是最左边的类型;这意味着,对于第一个函数之后的每个函数,都使用前一个函数的返回类型。(在这种情况下,h.collision()返回int,因此.length()尝试调用成员函数int::length()int不是类类型,因此没有任何成员函数。)

因此,考虑到这些,您的代码可以修改如下:

// Assuming your stdafx.h contains "#include <string>" and "#include <tchar.h>".
// If it doesn't, either put them there, or #include them here.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <list>
// #4: Defining _tmain and _TCHAR
#ifndef _tmain
    #define _tmain main
    typedef char _TCHAR;
#endif

using namespace std;
class Translate
{
    string word;
public:
    // #1: Fixing trans().
    int trans(const string& word)
    {
        char w = word[0];  // First letter of word.
        return w;     // Will be promoted to int.
    }
};
class HashTable
{
// #2: Making member functions public, and member variables private.
    Translate t;
    list<string> hashTable[29];
public:
    int size();
    int collision();
    int length();
    char fword();
    // #3: Making word a const reference.  Changing return type to void.
    void insert(const string& word)
    {
         hashTable[t.trans(word)].push_back(word);
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    HashTable h;
    // #5.1: Opening the file.
    ifstream f("hash.txt");
    //h.insert(word)
    // #5.2 & 5.3: Reading a word.
    std::string word;
    while (f >> word)
    {
        h.insert(word);
    }
    // #6: Calling functions.
    cout << h.size();
    cout << h.collision(); // #7: Assuming you wanted to output both h.collision() and
    cout << h.length();    //     h.length(), I put them on separate lines.
                           //     If you actually DID want h.collision().length(), then
                           //     h.collision() should return a type (or reference to a type)
                           //     with member function length(), or be an instance
                           //     (or reference to an instance) of a class with member function
                           //     length() (instead of being a function).
    cout << h.fword();
    return 0;
}

除了insert()之外,您仍然需要为HashTable的成员函数提供主体,并进行任何其他您想要的修改。如果成员word实际上不需要存储字符串,您可能还想从Translate中删除它。