📄 eval_expr.cpp
字号:
// This program reads arithmetic expressions from standard// input, one per line, and evaluates them. The program// demonstrates stacks, scanning input using state transition// diagrams implemented by switch statements, and predictive// parsing of input according to a context-free grammar// Declarations needed across the application are placed// here, to ensure uniformity#include "eval_expr.h"int main(){ string tok_str; // Read the first token from the input int token = nexttoken( tok_str ); // Keep reading and evaluating expressions until the // user inputs a blank line while ( token != TOK_EOL ) { try { // Evaluate a single expression value_stack s; token = Expr( s, token, tok_str ); // Every expresion must be followed // by an end-of-line if ( token != TOK_EOL ) throw exception(); // The top value on the stack is the // result of the evaluation double y = s.top(); cout << y << endl; // The next token is the first of the // next expression, or an end-of-line token = nexttoken( tok_str ); } catch (...) { cout << "Syntax error" << endl; string garbage; getline( cin, garbage ); token = nexttoken( tok_str ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -