小型桌面计算器源程序#includeiostream#includestring#includemap#includecctypeusingnamespacestd;mapstring,doubletable;namespaceLexer{enumToken_value{NAME,NUMBER,END,PLUS='+',MINUS='-',MUL='*',DIV='/',PRINT=';',ASSIGN='=',LP='(',RP=')'};Token_valuecurr_tok=PRINT;doublenumber_value;stringstring_value;Token_valueget_token();}namespaceError{structZero_divide{};structSyntax_error{constchar*p;Syntax_error(constchar*q){p=q;}};}namespaceParser{doubleexpr(boolget);doubleterm(boolget);doubleprim(boolget);usingnamespaceLexer;usingnamespaceError;}namespaceDriver{intno_of_errors;std::istream*input;voidskip();}Lexer::Token_valueLexer::get_token(){charch;cinch;switch(ch){case'#':returncurr_tok=END;case';':case'*':case'/':case'+':case'-':case'(':case')':case'=':returncurr_tok=Token_value(ch);case'0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':case'.':cin.putback(ch);cinnumber_value;returncurr_tok=NUMBER;default:if(isalpha(ch)){cin.putback(ch);cinstring_value;returncurr_tok=NAME;}throwError::Syntax_error(badtoken);returncurr_tok=PRINT;}}doubleParser::expr(boolget){usingnamespaceLexer;doubleleft=term(get);for(;;)switch(curr_tok){casePLUS:left+=term(true);break;caseMINUS:left-=term(true);break;default:returnleft;}}doubleParser::term(boolget){usingnamespaceLexer;doubleleft=prim(get);for(;;)switch(curr_tok){caseMUL:left*=prim(true);break;caseDIV:if(doubled=prim(true)){left/=d;break;}throwError::Zero_divide();default:returnleft;}}doubleParser::prim(boolget){usingnamespaceLexer;if(get)get_token();switch(curr_tok){caseNUMBER:{doublev=number_value;get_token();returnv;}caseNAME:{double&v=table[string_value];if(get_token()==ASSIGN)v=expr(true);returnv;}caseMINUS:return-prim(true);caseLP:{doublee=expr(true);if(curr_tok!=RP)throwError::Syntax_error()expected);get_token();returne;}caseEND:return1;default:throwError::Syntax_error(primaryexpected);}}voidDriver::skip(){no_of_errors++;while(*input){charch;input-get(ch);switch(ch){case'\n':case';':return;}}}intmain(intargc,char*argv[]){cout...欢迎使用桌面计算器...endl;cout注意:1.如果输入了一个以字母开头的标识符;cout请在标识符的后面加上一个空格,否则程序将会出错endl;cout2.若输入了一个表达式,切记在后面加上;号endl;cout3.若想退出程序,请输出#endl;Driver::input=&cin;table[pi]=3.1415926535897932385;table[e]=2.7182818284590452354;while(*Driver::input){try{Lexer::get_token();if(Lexer::curr_tok==Lexer::END)break;if(Lexer::curr_tok==Lexer::PRINT)continue;coutParser::expr(false)endl;}catch(Error::Zero_divide){cerrattempttodividebyzeroendl;if(Lexer::curr_tok!=Lexer::PRINT)Driver::skip();}catch(Error::Syntax_errore){cerrsyntaxerror:e.pendl;if(Lexer::curr_tok!=Lexer::PRINT)Driver::skip();}}if(Driver::input!=&std::cin)deleteDriver::input;cout..谢谢使用..endl;returnDriver::no_of_errors;}