token.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 91 行

CPP
91
字号
 
Token_type Token::kind() const
{
   return symbol_table.token_data[code].kind;
}
 
int Token::priority() const
{
   return symbol_table.token_data[code].priority;
}
 
double Token::value() const
{
   return symbol_table.token_data[code].value;
}
 
String Token::name() const
{
   return symbol_table.token_data[code].name;
}
 
int Token::code_number() const
{
   return code;
}
 
void Token::set_parameters()
/* 
 
Post: All parameter values are printed for the user, and any changes
specified by the user are made to these values.
Uses: Classes List, Token_record, and String, and function
      read_num.
 
*/

{
   int n = parameters.size();
   int index_code;
   double x;

   for (int i = 0; i < n; i++) {
      parameters.retrieve(i, index_code);
      Token_record &r = symbol_table.token_data[index_code];

      cout << "Give a new value for parameter " << (r.name).c_str()
           << " with value " << r.value << endl;
      cout << "Enter a value or a new line to keep the old value: " << flush;
      if (read_num(x) == success) r.value = x;
   }
}
 
void Token::print_parameters()
{
   int n = parameters.size();
   int location;

   for (int i = 0; i < n; i++) {
      parameters.retrieve(i, location);
      Token_record &r = symbol_table.token_data[location];
      cout << (r.name).c_str() << " has value " << r.value << endl;
   }
}
 
void Token::set_x(double x_val)
{
   symbol_table.token_data[22].value = x_val;
}
 
Token::Token(const String &identifier)
/* 
 
Post: A Token corresponding to String identifier
      is constructed.  It shares its code with any other Token object
      with this identifier.
Uses: The class Lexicon.
 
*/

{
   int location = symbol_table.hash(identifier);
   if (symbol_table.index_code[location] == -1) {   //  Create a new record.
      code = symbol_table.count++;
      symbol_table.index_code[location] = code;
      symbol_table.token_data[code] = attributes(identifier);
      if (is_parameter(symbol_table.token_data[code]))
         parameters.insert(0, code);
   }
   else code = symbol_table.index_code[location];  //   Code of an old record
}

⌨️ 快捷键说明

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