谢谢大家啦
桌面计算器。超好的一个例子 好像在c++ programming language 的第5章,代码给你 /******************************** Cal_Parser.h the Parser of the desk calculator recursive descend technique *********************************/ #include<map> double prim(bool); //声明 double term(bool); double expr(bool);
double expr(bool get){ //handle + and - operation double left=term(get); for(;;){ switch(curr_tok){ case PLUS: left+=term(true); //get a token to add break; case MINUS: left-=term(true); break; default: return left; } } }
double term(bool get){ //handle * and / operation double left=prim(get); for(;;){ switch(curr_tok){ case MUL: left*=prim(true); break; case DIV: if(double d=prim(true)){ left/=d; break; } return error("devided by 0"); default: return left; } } }
double prim(bool get){ //handle primaries if(get)get_token(); //get a token to decide which type and value it is switch(curr_tok){ //type which is gotten in the get_token() case NUMBER: { double v=number_value;//value gotten in get_token() get_token(); //get next return v; } case NAME: { double& v=table[string_value]; if(get_token()==ASSIGN)v=expr(true); //if the input is like "a=3" return v; } case MINUS: return -prim(true); //"-3" case LP: { double e=expr(true); if(curr_tok!=RP)return error("where is )?"); get_token(); return e; } default: return error("where is primary?"); } } Token_Value curr_tok=PRINT; //hold the input type,see enum:Token_Value
double number_value; //hold the value the last NUMBER read
string string_value; //hold the value the last NAME read
int no_of_error; //hold the number of error enum Token_Value{ //input type. NAME, NUMBER, END='!', PLUS='+', MINUS='-', MUL='*', DIV='/', PRINT=';', ASSIGN='=', LP='(', RP=')' }; /********************************** Cal_SymbolTable.h the symbol table and all the global variables of desk calculator ***********************************/ #include <string> #include <map> using namespace std;
enum Token_Value{ //input type. NAME, NUMBER, END='!', PLUS='+', MINUS='-', MUL='*', DIV='/', PRINT=';', ASSIGN='=', LP='(', RP=')' };
//four global variable Token_Value curr_tok=PRINT; //hold the input type,see enum:Token_Value
double number_value; //hold the value the last NUMBER read
string string_value; //hold the value the last NAME read
int no_of_error; //hold the number of error
//map map<string,double>table; /********************** Cal_Input.h get the input from user ***********************/ #include<cctype> #include<iostream>
using namespace std;
Token_Value get_token(){ //get the value and type of value from input char ch=0; cin>>ch; switch(ch){ case 0: return curr_tok=END; case ';': //if input is operation case '*': case '/': case '+': case '-': case '(': case ')': case '=': case '!': return curr_tok=Token_Value(ch); case '0': case '1': case '2': case '3': case '4': //if number case '5': case '6': case '7': case '8': case '9': case '.': cin.putback(ch); cin>>number_value; //set value return curr_tok=NUMBER; //set type default: if(isalpha(ch)){ //'a'-'Z' cin.putback(ch); cin>>string_value; return curr_tok=NAME; } error("bad token"); return curr_tok=PRINT; } } #include "Cal_SymbolTable.h" #include "Cal_error.h" #include "Cal_Input.h" #include "Cal_Parser.h" #include <iostream> using namespace std;
int main(){ table["pi"]=3.1415926525897; table["e"]=2.71828182845904; while(cin){ get_token(); if(curr_tok==END)break; if(curr_tok==PRINT)continue; cout<<expr(false)<<'\n'; } return no_of_error; }