eval_expr.cpp

来自「斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》」· C++ 代码 · 共 47 行

CPP
47
字号
// 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 + =
减小字号Ctrl + -
显示快捷键?