编程和原则第6章令牌和计算器

时间:2016-08-17 09:29:43

标签: c++ class switch-statement c++14

这是代码设置:

class Token {
public:
char kind;        // what kind of token
double value;     // for numbers: a value 
Token(char ch)    // make a Token from a char
    :kind(ch), value(0) { }    
Token(char ch, double val)     // make a Token from a char and a double
    :kind(ch), value(val) { }
};

//------------------------------------------------------------------------------

class Token_stream {
public: 
Token_stream();   // make a Token_stream that reads from cin
Token get();      // get a Token (get() is defined elsewhere)
void putback(Token t);    // put a Token back
private:
bool full;        // is there a Token in the buffer?
Token buffer;     // here is where we keep a Token put back using putback()
};

//------------------------------------------------------------------------------

// The constructor just sets full to indicate that the buffer is empty:
Token_stream::Token_stream()
:full(false), buffer(0)    // no Token in buffer
{
}

//------------------------------------------------------------------------------

// The putback() member function puts its argument back into the Token_stream's buffer:
void Token_stream::putback(Token t)
{
if (full) error("putback() into a full buffer");
buffer = t;       // copy t to buffer
full = true;      // buffer is now full
}

//------------------------------------------------------------------------------

Token get()
{
if (full) {       // do we already have a Token ready?  //Syntax error "full" and "buffer" not declared
    // remove token from buffer
    full=false;
    return buffer;
} 

char ch;
cin >> ch;    // note that >> skips whitespace (space, newline, tab, etc.)

switch (ch) {
case ';':    // for "print"
case 'q':    // for "quit"
case '(': case ')': case '+': case '-': case '*': case '/': 
    return Token(ch);        // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '9':
    {    
        cin.putback(ch);         // put digit back into the input stream
        double val;
        cin >> val;              // read a floating-point number
        return Token('8',val);   // let '8' represent "a number"
    }
  default:
    error("Bad token");
  }
}

//------------------------------------------------------------------------------

Token_stream ts;        // provides get() and putback() 

//------------------------------------------------------------------------------

double expression();    // declaration so that primary() can call expression()

//------------------------------------------------------------------------------

// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '(':    // handle '(' expression ')'
    {    
        double d = expression();
        t = ts.get();
        if (t.kind != ')') error("')' expected)");
        return d;
    }
case '8':            // we use '8' to represent a number
    return t.value;  // return the number's value
default:
    error("primary expected");
 }
}

//------------------------------------------------------------------------------

// deal with *, /, and %
double term()
{
double left = primary();
Token t = ts.get();        // get the next token from token stream

while(true) {
    switch (t.kind) {
    case '*':
        left *= primary();
        t = ts.get();
    case '/':
        {    
            double d = primary();
            if (d == 0) error("divide by zero");
            left /= d; 
            t = ts.get();
            break;
        }
    default: 
        ts.putback(t);     // put t back into the token stream
        return left;
    }
  }
}

//------------------------------------------------------------------------------

// deal with + and -
double expression()
{
double left = term();      // read and evaluate a Term
Token t = ts.get();        // get the next token from token stream

while(true) {    
    switch(t.kind) {
    case '+':
        left += term();    // evaluate Term and add
        t = ts.get();
        break;
    case '-':
        left += term();    // evaluate Term and subtract
        t = ts.get();
        break;
    default: 
        ts.putback(t);     // put t back into the token stream
        return left;       // finally: no more + or -: return the answer
    }
 }
}

我很难理解expression()term()primary()Expression()表示,一旦它读取了令牌,如果令牌是一个数字,它将返回左侧,即term(),然后它返回左侧,即primary(),最后返回数字本身的值。但是,我不明白的是开关盒。 Expression()指出左=左+/- term()但在这种情况下左边已经存在为term()?代码不会一遍又一遍地重复乘法,除法,加法和减法吗?开关盒也似乎永远不会返回任何东西,而只是获得下一个令牌,最后在确认它找到一个操作员并继续进行计算之后断开,所以我很困惑这将如何呈现任何计算指示没有像cout或者返回的东西?

1 个答案:

答案 0 :(得分:1)

关于交换机:它们都在适当的情况下返回一个double,并继续在其他(非错误)情况下使用和处理。

关于无限循环:expression()调用term()term()调用primary(),primary()调用expression()只有当它读取'('和它们时所有调用get()来消耗输入,所以一切都是输入驱动的并且有效输入(以'q'或';'结尾),应该在消耗所有输入后以值结束,这是由表达式返回的( )。

关于left + = term():left是term()的结果,在此语句之后,left是左边的任何内容+ term()的结果的结果,所以左边仍然是号。

代码中的一个错误:Token get()应为Token Token_stream::get()