C++程序设计原理与实践

Chapter 6

2018-10-18  本文已影响0人  Jianbaozi
#include "../../std_lib_facilities.h"
double expression();
double term();
double primary();
class Token {
public:
    Token(char ch) :kind(ch),value(0){}
    Token(char ch, double val) :kind(ch), value(val) {}
    char kind;
    double value;
};
class Token_stream {
public:
    Token_stream();
    Token get();
    void putback(Token t);
private:
    Token buffer;
    bool full;
};
Token_stream ts;
Token_stream::Token_stream():buffer(0),full(false) {};
Token Token_stream::get() {
    if (full) {
        full = false;
        return buffer;
    }
    char ch;
    cin >> ch;
    switch (ch) {
    case ';':
    case 'q':
    case '(': case ')': case '+': case '-': case '*':case '/':
        return Token{ch};
    case '.':
    case '0':case '1':case '2':case '3':case '4':
    case '5':case '6':case '7':case '8':case '9':
    {
        cin.putback(ch);
        double val;
        cin >> val;
        return Token{ '8',val };
    }
    default:
        error("Bad Token");
    }
}
void Token_stream::putback(Token t) {
    if (full)error("putback to a full buffer.");
    buffer = t;
    full = true;
}
double expression() {
    double left = term();
    Token t = ts.get();
    while (true) {
        switch (t.kind)
        {
        case '+':
            left += term();
            t = ts.get();
            break;
        case '-':
            left -= term();
            t = ts.get();
            break;
        default:
            ts.putback(t);
            return left;
        }
    }
}
double term() {
    double left = primary();
    Token t = ts.get();
    while (true) {
        switch (t.kind)
        {
        case '*':
            left *= term();
            t = ts.get();
            break;
        case '/':
            left /= term();
            t = ts.get();
            break;
        default:
            ts.putback(t);
            return left;
        }
    }
}
    double primary() {
        Token t = ts.get();
        switch (t.kind)
        {
        case '(':
        {
            double d = expression();
            t = ts.get();
            if (t.kind != ')')error("')' expected.");
            return d;
        }
        case '8':
            return t.value;
        default:
            error("primary expected.");
        }
    }
#include "../../std_lib_facilities.h"
#include "Header.h"
int main() {
    try {
        double val = 0;
        while (cin) {
            Token t = ts.get();
            if (t.kind == 'q') break;
            if (t.kind == ';')
                cout << "=" << val << endl;
            else
                ts.putback(t);
            val = expression();
        }
        system("pause");
    }
    catch (exception &e) {
        cerr << "error: " << e.what() << endl;
        system("pause");
    }
    catch (...) {
        cerr << "Oops:unknown exception!" << endl;
        system("pause");
        return 2;
    }
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读