利用栈实现表达式求值的问题(c语言数据结构)
#include<stdio.h>#include<stdlib.h>
#include<ctype.h>
#define STACK_SIZE 20
#define STACKINCREMENT 1
typedef struct stack_num{
int *top_n;
int *base_n;
int stacksize1;
}sqstack_N;
typedef struct stack_op{
char *top_o;
char *base_o;
int stacksize2;
}sqstack_O;
void Initstack_optr(sqstack_O *s);
void Initstack_opnd(sqstack_N *s);
void push(int n,sqstack_N *s);
int pop(sqstack_N *s);
void enter_stack(char ch,sqstack_O *s);
char pop_stack(sqstack_O *s);
char get_top(sqstack_O *s);
char comp(char p,char q);
int operate(int a,char theta,int b);
int main(void)
{
sqstack_N s1;
sqstack_O s2;
int ch;
char theta;
int n,a,b;
Initstack_optr(&s2);
Initstack_opnd(&s1);
puts("enter the data");
ch=getchar();
while(ch!='#'||get_top(&s2)!='#')
{
if(isdigit(ch))
{
n=ch-48;
push(n,&s1);
ch=getchar();
}
else
switch(comp(get_top(&s2),ch))
{
case '<':{
enter_stack(ch,&s2);
ch=getchar();
break;
}
case '=':{
ch=getchar();
break;
}
case '>':{
theta=pop_stack(&s2);
a=pop(&s1);
b=pop(&s1);
push(operate(a,theta,b),&s1);
ch=getchar();
break;
}
}
}
printf("the result is %d\n", pop(&s1));
return 0;
}
void Initstack_optr(sqstack_O *s)
{
s->base_o=(char *)malloc(STACK_SIZE*sizeof (char));
if(!s->base_o)
exit(1);
s->stacksize2=STACK_SIZE;
s->base_o[0]='#';
s->top_o=s->base_o+1;
}
void Initstack_opnd(sqstack_N *s)
{
s->base_n=(int *)malloc(STACK_SIZE*sizeof (int));
if(!s->base_n)
exit(2);
s->top_n=s->base_n;
s->stacksize1=STACK_SIZE;
}
void push(int n,sqstack_N *s)
{
if(s->top_n-s->base_n>=s->stacksize1)
{
s->base_n=(int *)realloc(s->base_n,(s->stacksize1+STACKINCREMENT)*sizeof(int));
if(!s->base_n)
exit(3);
s->top_n=s->base_n+s->stacksize1;
s->stacksize1+=STACKINCREMENT;
}
*(s->top_n)++=n;
}
int pop(sqstack_N *s)
{
int n;
if(s->top_n==s->base_n)
return 0;
n=*--(s->top_n);
return n;
}
void enter_stack(char ch,sqstack_O *s)
{
if(s->top_o-s->base_o>=s->stacksize2)
{
s->base_o=(char *)realloc(s->base_o,(s->stacksize2+STACKINCREMENT)*sizeof(char));
if(!s->base_o)
exit(3);
s->top_o=s->base_o+s->stacksize2;
s->stacksize2+=STACKINCREMENT;
}
*(s->top_o)++=ch;
}
char pop_stack(sqstack_O *s)
{
char ch;
if(s->top_o==s->base_o)
return 0;
ch=*--(s->top_o);
return ch;
}
char get_top(sqstack_O *s)
{
char ch;
if(s->top_o==s->base_o)
return 0;
ch=*(s->top_o-1);
return ch;
}
char comp(char p,char q)
{
char m[7]="+-*/()#";
char n[7][7]=
{
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
int i=0,j=0;
while(m[i]!=p)
i++;
while(m[j]!=q)
j++;
return n[i][j];
}
int operate(int a,char theta,int b)
{
int res;
switch(theta)
{
case '+':{
res=a+b;
break;}
case '-':{
res=a-b;
break;
}
case '*':{
res=a*b;
break;
}
case '/':{
if(b!=0)
res=a/b;
else
printf("error in / with b=0");
break;
}
}
return res;
}
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define STACK_SIZE 20
#define STACKINCREMENT 1
typedef struct stack_num{
int *top_n;
int *base_n;
int stacksize1;
}sqstack_N;
typedef struct stack_op{
char *top_o;
char *base_o;
int stacksize2;
}sqstack_O;
void Initstack_optr(sqstack_O *s);
void Initstack_opnd(sqstack_N *s);
void push(int n,sqstack_N *s);
int pop(sqstack_N *s);
void enter_stack(char ch,sqstack_O *s);
char pop_stack(sqstack_O *s);
char get_top(sqstack_O *s);
char comp(char p,char q);
int operate(int a,char theta,int b);
int main(void)
{
sqstack_N s1;
sqstack_O s2;
int ch;
char theta;
int n,a,b;
Initstack_optr(&s2);
Initstack_opnd(&s1);
puts("enter the data");
ch=getchar();
while(ch!='#'||get_top(&s2)!='#')
{
if(isdigit(ch))
{
n=ch-48;
push(n,&s1);
ch=getchar();
}
else
switch(comp(get_top(&s2),ch))
{
case '<':{
enter_stack(ch,&s2);
ch=getchar();
break;
}
case '=':{
ch=getchar();
break;
}
case '>':{
theta=pop_stack(&s2);
a=pop(&s1);
b=pop(&s1);
push(operate(a,theta,b),&s1);
ch=getchar();
break;
}
}
}
printf("the result is %d\n", pop(&s1));
return 0;
}
void Initstack_optr(sqstack_O *s)
{
s->base_o=(char *)malloc(STACK_SIZE*sizeof (char));
if(!s->base_o)
exit(1);
s->stacksize2=STACK_SIZE;
s->base_o[0]='#';
s->top_o=s->base_o+1;
}
void Initstack_opnd(sqstack_N *s)
{
s->base_n=(int *)malloc(STACK_SIZE*sizeof (int));
if(!s->base_n)
exit(2);
s->top_n=s->base_n;
s->stacksize1=STACK_SIZE;
}
void push(int n,sqstack_N *s)
{
if(s->top_n-s->base_n>=s->stacksize1)
{
s->base_n=(int *)realloc(s->base_n,(s->stacksize1+STACKINCREMENT)*sizeof(int));
if(!s->base_n)
exit(3);
s->top_n=s->base_n+s->stacksize1;
s->stacksize1+=STACKINCREMENT;
}
*(s->top_n)++=n;
}
int pop(sqstack_N *s)
{
int n;
if(s->top_n==s->base_n)
return 0;
n=*--(s->top_n);
return n;
}
void enter_stack(char ch,sqstack_O *s)
{
if(s->top_o-s->base_o>=s->stacksize2)
{
s->base_o=(char *)realloc(s->base_o,(s->stacksize2+STACKINCREMENT)*sizeof(char));
if(!s->base_o)
exit(3);
s->top_o=s->base_o+s->stacksize2;
s->stacksize2+=STACKINCREMENT;
}
*(s->top_o)++=ch;
}
char pop_stack(sqstack_O *s)
{
char ch;
if(s->top_o==s->base_o)
return 0;
ch=*--(s->top_o);
return ch;
}
char get_top(sqstack_O *s)
{
char ch;
if(s->top_o==s->base_o)
return 0;
ch=*(s->top_o-1);
return ch;
}
char comp(char p,char q)
{
char m[7]="+-*/()#";
char n[7][7]=
{
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
int i=0,j=0;
while(m[i]!=p)
i++;
while(m[j]!=q)
j++;
return n[i][j];
}
int operate(int a,char theta,int b)
{
int res;
switch(theta)
{
case '+':{
res=a+b;
break;}
case '-':{
res=a-b;
break;
}
case '*':{
res=a*b;
break;
}
case '/':{
if(b!=0)
res=a/b;
else
printf("error in / with b=0");
break;
}
}
return res;
}