哈希表解释器

Hash Table Interpreter

本文关键字:解释器 哈希表      更新时间:2023-10-16

我正在尝试制作一个哈希表解释器,遇到一些错误。

我收到这个错误,每次我使用hashString。

main.cpp:99:43: error: use of undeclared identifier 'hashString'
                        temp.next = table[hashString(temp.getName())];
                                          ^
main.cpp:100:31: error: use of undeclared identifier 'hashString'
                        table[hashString(temp.getName())] = temp;

我尝试声明hashstring(注释掉),但它只是给了我更多的错误,像这样:

main.cpp:105:34: error: no viable overloaded operator[] for type 'node [7]'

欢迎提出任何建议。谢谢!

我代码:

class node{
   public:
      node();
      node* next;
      string getName();
      int getNum();
      int getScope();
      void setName(string);
      void setNum(int);
      void setScope(int);
      int hashString(string s);
   private:
      string name;
      int num;
      int scope;
};
node::node(){
   next = 0;
   name = "";
   num = 0;
   scope = 0;
}
string node::getName(){
   return name;
}
int node::getNum(){
   return num;
}
int node::getScope(){
   return scope;
}
void node::setName(string s){
   name = s;
}
void node::setScope(int x){
   scope = x;
}
void node::setNum(int x){
   num = x;
}
int node::hashString(string s){
   int hash=0,size = s.size();
   for(int i= 0;i<size+1;i++){
      hash+= (int) s[i] * i;
   }
   return hash%7;
}
int main(){
   int curr_scope=0,num_line=0;
   node table[7];
   //node hashString(string s);
   ifstream myfile ("input.txt");
   if (myfile.is_open()){
      string s;
      while (getline (myfile,s)){
         num_line++;
         stringstream line(s);
         line >> s;
         if(s == "START"){
            curr_scope++;
            while(line >> s);
         }
         else if(s == "FINISH"){
            curr_scope--;
            while(line >> s);
         }
         else if(s == "COM"){
            while(line >> s);
         }
         else if(s == "VAR"){
            node temp;
            line >> s;
            cout << s << endl;
            temp.setName(s);
            line >> s;
            if(s == "="){
               line >> s;
               cout << s << endl;
               temp.setNum(atoi(s.c_str()));
               temp.setScope(curr_scope);
               if(table[hashString(temp.getName())] != 0){
                  temp.next = table[hashString(temp.getName())];
                  table[hashString(temp.getName())] = temp;
                  while(line >> s);
               }
               else if(table[hashString(temp.getName())] == 0){
                  table[hashString(temp.getName())] = temp;
                  while(line >> s);
               }
               else{
                  cout << "UNABLE TO ADD " << temp.getName() << "TO THE TABLE" << endl;
                  while(line >> s);
               }
            }
         }
         else if(s == "PRINT")
         {
            line >> s;
            node temp = table[hashString(s)];
            if(temp.getScope() == curr_scope){
               if(line >> s){
                  if(s == "++"){
                     cout << temp.getName() << " IS " << temp.getNum() + 1 << endl;
                     while(line >> s);
                  }
                  else if(s == "--"){
                     cout << temp.getName() << " IS " << temp.getNum() - 1 << endl;
                     while(line >> s);
                  }
                  else if(s == "+"){
                     line >> s;
                     cout << temp.getName() << " IS " << temp.getNum() + atoi(s.c_str()) << endl;
                     while(line >> s);
                  }
                  else if(s == "-"){
                     line >> s;
                     cout << temp.getName() << " IS " << temp.getNum() - atoi(s.c_str()) << endl;
                     while(line >> s);
                  }
                  else if(s == "/"){
                     line >> s;
                     cout << temp.getName() << " IS " << temp.getNum() / atoi(s.c_str()) << endl;
                     while(line >> s);
                  }
                  else if(s == "*"){
                     line >> s;
                     cout << temp.getName() << " IS " << temp.getNum() * atoi(s.c_str()) << endl;
                     while(line >> s);
                  }
                  else if(s == "%"){
                     line >> s;
                     cout << temp.getName() << " IS " << temp.getNum() % atoi(s.c_str()) << endl;
                     while(line >> s);
                  }
                  else if(s == "^"){
                     line >> s;
                     cout << temp.getName() << " IS " << pow(temp.getNum(),atoi(s.c_str())) << endl;
                     while(line >> s);
                  }
               }
            }
            else{
               cout << s << "IS UNDEFINED" << endl;
               cout << "ERROR HAS OCCURED ON LINE " << num_line << endl;
               while(line >> s);
            }
         }
         else{
            if(table[hashString(s)].getName == s){
               node temp = table[hashString(s)];
               line >> s;
               if(s == "="){
                  if(temp.getScope() == curr_scope){
                     line >> s;
                     table[hashString(temp)].setNum(atoi(s.c_str()));
                     while(line >> s);
                  }
               }
               else if(s == "++"){
                  table[hashString(temp)].setNum(table[hashString(temp)].getNum()+1);
                  while(line >> s);
               }
               else if(s == "--"){
                  table[hashString(temp)].setNum(table[hashString(temp)].getNum()-1);
                  while(line >> s);
               }
            }
            else
               cout << s << "IS UNDEFINED" << endl;
         }
      }
      myfile.close();
   }
   system("PAUSE");
   return 0;
}

为什么hashString是Node的一部分?

如果没有原因,那么您应该将其移动到一个自由函数,这将解决您的问题。

否则,您需要在node实例上调用它,如temp.hashString(s)

您也可以使它成为一个类静态函数,以便您可以这样调用它:node::hashString(s),如果您想使它与节点类相关联。