⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lexicon.cpp

📁 数据结构与程序设计教材源码 数据结构与程序设计教材源码
💻 CPP
字号:
 
void Lexicon::set_standard_tokens()
{
   int i = 0;
   String symbols = ( String )
      "; ( ) ~ abs sqr sqrt exp ln lg sin cos arctan round trunc ! % + - * / ^ x pi e";
   String word;
   while (get_word(symbols, i++, word) != fail) {
      Token t = word;
   }
   token_data[23].value = 3.14159;
   token_data[24].value = 2.71828;
}
 
int Lexicon::hash(const String &identifier) const
/* 
 
Post: Returns the location in table Lexicon::index_code that
      corresponds to the String identifier.
      If the hash table is full and does not contain a record
      for identifier, the exit function is called to terminate the
      program.
Uses: The class String, the function exit.
 
*/
{
   int location;
   const char *convert = identifier.c_str();
   char first = convert[0], second;     //  First two characters of identifier 
   if (strlen(convert) >= 2) second = convert[1];
   else second = first;

   location = first % hash_size;
   int probes = 0;
   while (index_code[location] >= 0 &&
          identifier != token_data[index_code[location]].name) {
      if (++probes >= hash_size) {
         cout << "Fatal Error: Hash Table overflow. Increase table size\n";
         exit(1);
      }
      location += second;
      location %= hash_size;
   }
   return location;
}
 
Lexicon::Lexicon()
/* 
 
Post: The Lexicon is initialized with
      the standard tokens.
Uses: set_standard_tokens
 
*/

{
   count = 0;
   for (int i = 0; i < hash_size; i++)
      index_code[i] = -1;     //  code for an empty hash slot
   set_standard_tokens();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -