C++或C 栈实现表达式求值运算

[复制链接]
查看11 | 回复5 | 2009-12-8 14:25:22 | 显示全部楼层 |阅读模式
发邮件给我,我闲着无聊看看,不幸看懂了就告诉你啊
回复

使用道具 举报

千问 | 2009-12-8 14:25:22 | 显示全部楼层
NXPD 当年在学校,搞了小一个月@
回复

使用道具 举报

千问 | 2009-12-8 14:25:22 | 显示全部楼层
你那个模块到底怎么划分呢?是输入-处理-输出,还是怎么的? 我有前些天做了一个,划分的不是很好,不过还是有一些小模块的。参考一下:http://hi.baidu.com/cdysxy1987/blog/item/feb62597a037bb027af4806e.html
回复

使用道具 举报

千问 | 2009-12-8 14:25:22 | 显示全部楼层
纯C啊?顺序栈可以吗?
回复

使用道具 举报

千问 | 2009-12-8 14:25:22 | 显示全部楼层
//数据结构//表达式求值//带注释#include #include using namespace std;const int MAXSIZE=100;template struct SqStack{ ElemType base[MAXSIZE]; int top;};template void initStack(SqStack& s){ s.top=0;}template void clearStack(SqStack& s){ s.top=0;}template bool isEmpty(SqStack& s){ return s.top==0;}template bool isFull(SqStack& s){ return s.top==MAXSIZE;}template ElemType peek(SqStack& s){ if(s.top==0){cerrvoid push(SqStack& s,const ElemType& e){ if(s.top==MAXSIZE){cerrElemType pop(SqStack& s){ if(s.top==0){cerr r; initStack(r);//初始化栈 push(r,'@');//给栈底放入'@'字符,它具有最低优先级 int i,j; i=0; j=0; char ch=s1; while(ch!='@'){if(ch==' ') ch=s1[++i];else if(ch=='('){ push(r,ch); ch=s1[++i];}else if(ch==')'){ while(peek(r)!='(')
s2[j++]=pop(r); pop(r); ch=s1[++i];}else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){ char w=peek(r); while(precede(w)>=precede(ch)){
s2[j++]=w;
pop(r);
w=peek(r); } push(r,ch); ch=s1[++i];}else{ while(isdigit(ch)||ch=='.'){
s2[j++]=ch;
ch=s1[++i]; } s2[j++]=' ';} } ch=pop(r); while(ch!='@'){if(ch=='('){ cerr s; initStack(s); istringstream ins(str); //把str定义为string流对象ins,P82 char ch; //用于输入字符 double x; //用于输入浮点数 ins>>ch; while(ch!='@') {switch(ch){case '+': x=pop(s)+pop(s); break;case '-': x=pop(s); //pop(s)弹出减数 x=pop(s)-x;//pop(s)弹出被减数 break;case '*': x=pop(s)*pop(s); break;case '/': x=pop(s); if(x!=0.0)
x=pop(s)/x; else {cerr>x; //从字符串输入流中读入一个浮点数}push(s,x); //把读入的数或进行相应运算的结果压入到s栈中ins>>ch; } if(!isEmpty(s)){x=pop(s);if(isEmpty(s)) return x;else{ cerr>ss; change(ss,s2); cout<<ss<<endl;
if(!checkString(ss)) cout<<"表达式错误!"; double r=compute(s2); cout<<r<<endl; }}这个要给钱才行
回复

使用道具 举报

千问 | 2009-12-8 14:25:22 | 显示全部楼层
邮箱[email protected],代码发给我看看
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行