| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 540 人关注过本帖
标题:[求助]帮忙写个程序
只看楼主 加入收藏
Vivian薇
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-10-5
收藏
 问题点数:0 回复次数:1 
[求助]帮忙写个程序
模拟一个电子计算器,完成两个整数的四则运算
谢谢大家啦
2005-10-05 18:54
type_error
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2005-9-28
收藏
得分:0 

桌面计算器。超好的一个例子 好像在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; }

2005-10-05 20:22
快速回复:[求助]帮忙写个程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034490 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved