| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1467 人关注过本帖
标题:已知:表达式327.45*(265.78 - 32.54/2.3)+26.32 求:用栈实现该表达式的值 ...
只看楼主 加入收藏
zhaobing
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2004-10-3
收藏
 问题点数:0 回复次数:3 
已知:表达式327.45*(265.78 - 32.54/2.3)+26.32 求:用栈实现该表达式的值。用VC

让大家帮个忙。

搜索更多相关主题的帖子: 表达 
2004-10-11 12:58
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
我也忘了stack是预定义好的函数还是要自定义了,总之就是先把优先级最低的压stack,然后往后一直压stack,最后的运算最高,由于stack顶的先出stack,于是先算最顶端的运算,然后一直往下作运算……如此,你翻翻书,看stack盏函数的用法。
2004-10-13 01:15
zhaobing
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2004-10-3
收藏
得分:0 

我编的这个题程序 运行不出来。谁能帮我改改!

#include<iostream.h> #include<stdio.h> #include<stdlib.h> #include<malloc.h> #define OVERFLOW -2; #define ERROR -1; #define STACK_INIT_SIZE 100; #define STACKINCREMENT 10; typedef int Status; #define OK 1; typedef struct{ char * base; char * top; int stacksize; }SqStack1; typedef struct{ double * base; double * top; int stacksize; }SqStack2; Status InitStack1(SqStack1 &S){ S.base=(char *) malloc (STACK_INIT_SIZE*sizeof(char)); if(!S.base)exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; }//InitStack Status InitStack2(SqStack2 &S){ S.base=(double *)malloc(STACK_INIT_SIZE*sizeof(double)); if(!S.base)exit(OVERFLOW); S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; }//InitStack Status Push1 (SqStack1 &S,char e){ if(S.top-S.base>=S.stacksize){ S.base=(char *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(char)); if(!S.base)exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; }//Push Status Push2 (SqStack2 &S,double e){ if(S.top-S.base>=S.stacksize){ S.base=(double *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(double)); if(!S.base) exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } *S.top++=e; return OK; }//Push Status Pop1(SqStack1 &S,char &e){ if(S.top==S.base) return ERROR; e=*(--S.top); return OK; }//Pop Status Pop2(SqStack2 &S,double &e){ if(S.top==S.base) return ERROR; e=*(--S.top); return OK; }//Pop int bi_jiao(char a,char b) { if((a=='#')&&(b=='*')) return 1; if((a=='*')&&(b=='(')) return 1; if((a=='(')&&(b=='-')) return 1; if((a=='-')&&(b=='/')) return 1; if((a=='/')&&(b==')')) return 3; if((a=='-')&&(b==')')) return 3; if((a=='(')&&(b==')')) return 2; if((a=='*')&&(b=='+')) return 3; if((a=='#')&&(b=='+')) return 1; if((a=='+')&&(b=='#')) return 3; if((a=='#')&&(b=='#')) return 2; } double yun_suan(double z,char that,double w) { if(that=='+') return z+w; if(that=='-') return z-w; if(that=='*') return z*w; if(that=='/') return z/w; }

void main() { SqStack1 OPTR; SqStack2 OPTD; int i(0),j(0); char x,y; double w,z,number; char a[8]; a[0]='#'; a[1]='*'; a[2]='('; a[3]='-'; a[4]='/'; a[5]=')'; a[6]='+'; a[7]='#'; double b[7]={327.45,0,256.78,32.54,2.3,0,26.32}; InitStack1(OPTR); InitStack2(OPTD); Push1(OPTR,a[i]);i++; Push2(OPTD,b[j]);j++; Push1(OPTR,a[i]);i++; for(int m(0);m<0;m++) { number=bi_jiao(*(OPTR.top-2),*(OPTR.top-1)); if(number==1) { if(b[j]!=0) Push2(OPTD,b[j]); j++; Push1(OPTR,a[i]);i++; } if(number==2) { Pop1(OPTR,x); Pop1(OPTR,x); if(OPTR.top==OPTR.base) break;//END if(b[j]!=0) Push2(OPTD,b[j]); j++; Push1(OPTR,a[i]);i++; } if(number==3) { Pop1(OPTR,x); Pop1(OPTR,y); Push1(OPTR,x); Pop2(OPTD,w); Pop2(OPTD,z); w=yun_suan(z,x,w); Push2(OPTD,w); } } cout<<*(OPTD.base)<<endl; }

2004-10-15 15:54
stnlcd
Rank: 1
等 级:新手上路
帖 子:177
专家分:1
注 册:2004-11-21
收藏
得分:0 

下面用c写的算法:输入表达式(如3+4*(5+6)),计算其结果.算法思想是先将表达式转换成后缀表达式.在用栈计算起值.

#include<string.h> #include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 typedef int Status; typedef int Boolean; #define STACK_INIT_SIZE 10 typedef char SElemType; typedef struct SqStack { SElemType *base; SElemType *top; int stacksize; }SqStack; Status InitStack(SqStack *S) { /* 构造一个空栈S */ (*S).base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

(*S).top=(*S).base; (*S).stacksize=STACK_INIT_SIZE; return OK; }

Status StackEmpty(SqStack S) { /* 若栈S为空栈,则返回TRUE,否则返回FALSE */ if(S.top==S.base) return TRUE; else return FALSE; } Status GetTop(SqStack S,SElemType *e) { /* 若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR */ if(S.top>S.base) { *e=*(S.top-1); return OK; } else return ERROR; }

Status Push(SqStack *S,SElemType e) { /* 插入元素e为新的栈顶元素 */ if((*S).top-(*S).base>=(*S).stacksize) /* 栈满,追加存储空间 */ { (*S).base=(SElemType *)realloc((*S).base,((*S).stacksize+2)*sizeof(SElemType));

(*S).top=(*S).base+(*S).stacksize; (*S).stacksize+=2; } *((*S).top)++=e; return OK; }

Status Pop(SqStack *S,SElemType *e) { /* 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR */ if((*S).top==(*S).base) return ERROR; *e=*--(*S).top; return OK; }

#define max_size 100

int popstack(char a,char b) { if((b=='+'||b=='-')&&(a=='*'||a=='/')) return 1; else return 0; }

int number(char a) { switch(a) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': return 1; default: return 0; } }

void backb(char* a) { int i,len,ip=0; char ch,cha[max_size];

SElemType e; SqStack s; gets(cha); InitStack(&s); Push(&s,'#'); len=strlen(cha); for(i=0;i<len;++i) { ch=cha[i]; if(number(ch)) a[ip++]=ch; else { if(ch==')') { Pop(&s,&e); while(e!='(') { a[ip++]=e; Pop(&s,&e); } }else { GetTop(s,&e); while(popstack(e,ch)&&!StackEmpty(s)) { Pop(&s,&e); a[ip++]=e; GetTop(s,&e); } Push(&s,ch); } }

} while(!StackEmpty(s)) { Pop(&s,&e); a[ip++]=e; } a[--ip]='\0'; }

int compute(char* a) { int i=0,n,n1,n2; int numstack[max_size]; int top=0; while(a[i]) { if(number(a[i])) numstack[top++]=(int)a[i]-48; else { n1=numstack[--top]; n2=numstack[--top]; switch(a[i]) { case '+': { n=n1+n2; break; } case '-': { n=n1-n2; break; } case '*': { n=n1*n2; break; } case '/': { n=n1/n2; break; } default: break; } numstack[top++]=n; } ++i; }

return numstack[--top]; }

int main(void) { char* a; puts("\n"); a=(char*)malloc(max_size*sizeof(char)); backb(a);

printf("\nResult:%d",compute(a)); return 0; }

######:由于是本人很久以前写的程序,所以懒得将其改为c++, 这个程序有个限制是:必须输入的是个位数!

如果读者有兴趣可以将程序改为不限制位数的算法:只需要加几个函数不麻烦!


要让一个男人破产,请给他一架相机,要让一个男人倾家荡产,请给他一架望远镜。
2004-11-21 20:53
快速回复:已知:表达式327.45*(265.78 - 32.54/2.3)+26.32 求:用栈实现该表达 ...
数据加载中...
 
   



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

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