让大家帮个忙。
我编的这个题程序 运行不出来。谁能帮我改改!
#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; }
下面用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++, 这个程序有个限制是:必须输入的是个位数!
如果读者有兴趣可以将程序改为不限制位数的算法:只需要加几个函数不麻烦!