主题:入栈和出栈的不解
初学数据结构,要求编一进行算术表达式求值的程序,并且将中序表达式转换为后序表达式输出。运行我试过,没有错误,但无法显示。实在是不知道是什么原因,请帮忙解答一下#include<stdio.h>
#include<malloc.h>
#define NUM 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INCREMENT 10
typedef int status;
typedef struct{
char *base1;
char *top1;
char *temp1;
int size1;
}sqstack1;
sqstack1 OPTR;
typedef struct{
int *base2;
int *top2;
int *temp2;
int size2;
}sqstack2;
sqstack2 OPND;
status init1(sqstack1 *s1){
s1->base1=(char *)malloc(NUM*sizeof(char));
if(!s1->base1)
exit (OVERFLOW);
s1->temp1=s1->base1;
s1->top1=s1->temp1;
s1->size1=NUM;
return OK;
}
status gettop1(sqstack1 s1,char *e1){
if(s1.top1==s1.base1)
return ERROR;
else{
*e1=*(s1.top1-1);
return OK;
}
}
status push1(sqstack1 *s1,char e1){
if(s1->top1-s1->base1>=s1->size1){
s1->base1=(char *)realloc(s1->base1,(s1->size1+INCREMENT)*sizeof(char));
if(!s1->base1)
exit (OVERFLOW);
s1->top1=s1->base1+s1->size1;
s1->size1+=INCREMENT;
}
*s1->top1=e1;
s1->top1++;
return OK;
}
status pop1(sqstack1 *s1,char *e1){
if(s1->top1==s1->base1)
return ERROR;
else{
e1=*(--s1->top1);
return OK;
}
}
status init2(sqstack2 *s2){
s2->base2=(int *)malloc(NUM*sizeof(int));
if(!s2->base2)
exit (OVERFLOW);
s2->temp2=s2->base2;s2->top2=s2->temp2;
s2->size2=NUM;
return OK;
}
status gettop2(sqstack2 s2,int *e2){
if(s2.top2==s2.base2)
return ERROR;
*e2=*(s2.top2-1);
return OK;
}
status push2(sqstack2 *s2,int e2){
if(s2->top2-s2->base2>=s2->size2){
s2->base2=(int *)realloc(s2->base2,(s2->size2+INCREMENT)*sizeof(int));
if(!s2->base2)
exit (OVERFLOW);
s2->top2=s2->base2+s2->size2;
s2->size2+=INCREMENT;
}
*s2->top2=e2;
s2->top2++;
return OK;
}
status pop2(sqstack2 *s2,int *e2){
if(s2->top2==s2->base2)
return ERROR;
else{
e2=*(--s2->top2);
return OK;
}
}
status operate(int m,char f,int n){
int total;
if(f=='+')
total=m+n;
else if(f=='-')
total=m-n;
else if(f=='*')
total=m*n;
else
total=m/n;
return total;
}
status precede(char c1,char c2){
int compare;
if(c1=='#'&&c2!='#'||c1=='('&&c2!=')'||c2=='('||c1=='+'&&c2=='*'||c1=='-'&&c2=='*'||c1=='+'&&c2=='/'||c1=='-'&&c2=='/')
compare=-1;
else if(c1=='#'&&c2=='#'||c1=='('&&c2==')')
compare=0;
else
compare=1;
return compare;
}
void main()
{
char c;char m[NUM];
char e1,e_1,theta;
int e2,e_2,i=0,j=0,a,b,e,jieguo;
init1(&OPTR);push1(&OPTR,'#');
init2(&OPND);
c=getchar();
while(c!='#'||gettop1(OPTR,&e1)!='#'){
if(c>='0'&&c<='9'){
e=c-48;
push2(&OPND,e);
m[i]=c;i++;
gettop2(OPND,&e2);
c=getchar();
}
else
switch(precede(gettop1(OPTR,&e1),c)){
case -1:push1(&OPTR,c);
c=getchar();
break;
case 0:pop1(&OPTR,&e_1);
c=getchar();
break;
case 1:pop1(&OPTR,&theta);
pop2(&OPND,&b);
pop2(&OPND,&a);
push2(&OPND,operate(a,theta,b));
m[i]=theta;i++;break;
}
}
m[i]='\0';
jieguo=gettop2(OPND,&e2);
printf("%d\n",jieguo);
printf("%s",m);
}
#include<malloc.h>
#define NUM 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INCREMENT 10
typedef int status;
typedef struct{
char *base1;
char *top1;
char *temp1;
int size1;
}sqstack1;
sqstack1 OPTR;
typedef struct{
int *base2;
int *top2;
int *temp2;
int size2;
}sqstack2;
sqstack2 OPND;
status init1(sqstack1 *s1){
s1->base1=(char *)malloc(NUM*sizeof(char));
if(!s1->base1)
exit (OVERFLOW);
s1->temp1=s1->base1;
s1->top1=s1->temp1;
s1->size1=NUM;
return OK;
}
status gettop1(sqstack1 s1,char *e1){
if(s1.top1==s1.base1)
return ERROR;
else{
*e1=*(s1.top1-1);
return OK;
}
}
status push1(sqstack1 *s1,char e1){
if(s1->top1-s1->base1>=s1->size1){
s1->base1=(char *)realloc(s1->base1,(s1->size1+INCREMENT)*sizeof(char));
if(!s1->base1)
exit (OVERFLOW);
s1->top1=s1->base1+s1->size1;
s1->size1+=INCREMENT;
}
*s1->top1=e1;
s1->top1++;
return OK;
}
status pop1(sqstack1 *s1,char *e1){
if(s1->top1==s1->base1)
return ERROR;
else{
e1=*(--s1->top1);
return OK;
}
}
status init2(sqstack2 *s2){
s2->base2=(int *)malloc(NUM*sizeof(int));
if(!s2->base2)
exit (OVERFLOW);
s2->temp2=s2->base2;s2->top2=s2->temp2;
s2->size2=NUM;
return OK;
}
status gettop2(sqstack2 s2,int *e2){
if(s2.top2==s2.base2)
return ERROR;
*e2=*(s2.top2-1);
return OK;
}
status push2(sqstack2 *s2,int e2){
if(s2->top2-s2->base2>=s2->size2){
s2->base2=(int *)realloc(s2->base2,(s2->size2+INCREMENT)*sizeof(int));
if(!s2->base2)
exit (OVERFLOW);
s2->top2=s2->base2+s2->size2;
s2->size2+=INCREMENT;
}
*s2->top2=e2;
s2->top2++;
return OK;
}
status pop2(sqstack2 *s2,int *e2){
if(s2->top2==s2->base2)
return ERROR;
else{
e2=*(--s2->top2);
return OK;
}
}
status operate(int m,char f,int n){
int total;
if(f=='+')
total=m+n;
else if(f=='-')
total=m-n;
else if(f=='*')
total=m*n;
else
total=m/n;
return total;
}
status precede(char c1,char c2){
int compare;
if(c1=='#'&&c2!='#'||c1=='('&&c2!=')'||c2=='('||c1=='+'&&c2=='*'||c1=='-'&&c2=='*'||c1=='+'&&c2=='/'||c1=='-'&&c2=='/')
compare=-1;
else if(c1=='#'&&c2=='#'||c1=='('&&c2==')')
compare=0;
else
compare=1;
return compare;
}
void main()
{
char c;char m[NUM];
char e1,e_1,theta;
int e2,e_2,i=0,j=0,a,b,e,jieguo;
init1(&OPTR);push1(&OPTR,'#');
init2(&OPND);
c=getchar();
while(c!='#'||gettop1(OPTR,&e1)!='#'){
if(c>='0'&&c<='9'){
e=c-48;
push2(&OPND,e);
m[i]=c;i++;
gettop2(OPND,&e2);
c=getchar();
}
else
switch(precede(gettop1(OPTR,&e1),c)){
case -1:push1(&OPTR,c);
c=getchar();
break;
case 0:pop1(&OPTR,&e_1);
c=getchar();
break;
case 1:pop1(&OPTR,&theta);
pop2(&OPND,&b);
pop2(&OPND,&a);
push2(&OPND,operate(a,theta,b));
m[i]=theta;i++;break;
}
}
m[i]='\0';
jieguo=gettop2(OPND,&e2);
printf("%d\n",jieguo);
printf("%s",m);
}