#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 20
//---------------------------------------------------------------------
typedef struct istack{
int *base;
int *top;
int size;
}istack;
typedef struct cstack{
char *base;
char *top;
int size ;
}cstack;
//---------------------------------------------------------------------------
int operation(istack *i_stack,cstack *c_stack);
int operate(int num1,char a,int num2);
int i_push(istack *i_stack,int e);
char precede(char e,char ch);
int i_pop(istack *i_stack,int *e);
int i_gettop(istack *i_stack);
char c_push(cstack *c_stack,char e);
int c_pop(cstack *c_stack,char *e);
char c_gettop(cstack *c_stack);
//----------------------------------------------------------------------------
int main()
{
int result;
istack *i_stack=(istack *)malloc(sizeof(istack));
i_stack->base=(int *)malloc(sizeof(int)*STACKSIZE);
i_stack->top=i_stack->base;i_stack->size=STACKSIZE;
cstack *c_stack=(cstack *)malloc(sizeof(cstack));
c_stack->base=(char *)malloc(sizeof(char)*STACKSIZE);
c_stack->top=c_stack->base;c_stack->size=STACKSIZE;
result=operation(i_stack,c_stack);
printf("result=%d/n",result);
return 1;
}
//----------------------------------------------------------------------
int operation(istack *i_stack,cstack *c_stack)
{
char ch,chara;
char a;
int num1,num2;
c_push(c_stack,'#');
ch=getchar();
while(ch!='#'||c_gettop(c_stack)!='#')
{
if(ch==' '||ch=='/n')
continue;
else if(ch!='('&&ch!=')'&&ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'&&ch!='#')
{
int n;
n=atoi(&ch);
i_push(i_stack,n);
getchar();
ch=getchar();
}
else
{ chara=precede(c_gettop(c_stack),ch);
switch(chara)
{
case'<':c_push(c_stack,ch);
getchar();
ch=getchar();
break;
case'=':c_pop(c_stack,&a);
getchar();
ch=getchar();
break;
case'>':c_pop(c_stack,&a);
i_pop(i_stack,&num2);
i_pop(i_stack,&num1);
i_push(i_stack,operate(num1,a,num2));
break;
}
}
}
return i_gettop(i_stack);
}
//------------------------------------------------------------
char precede(char e,char ch)
{
char n;
if(e=='+'||e=='-')
{
if(ch=='*'||ch=='/'||ch=='(')return n='<';
else return n='>';
}
else if(e=='*'||e=='/')
{
if(ch=='(') return n='<';
else return n='>';
}
else if(e=='(')
{
if(ch==')') return n='=';
else return n='<';
}
else if(e==')')
{
if(ch=='(') return n='w';
else return n='>';
}
else
{
if(ch==')') return n='w';
else return n='<';
}
}
//---------------------------------------------------------------
int operate(int num1,char a,int num2)
{
switch(a)
{
case'+':return num1+num2;
break;
case'-':return num1-num2;
break;
case'*':return num1*num2;
break;
case'/':return num1/num2;
break;
default:break;
}
}
//-------------------------------------------------------------
int i_push(istack *i_stack,int e)
{
*i_stack->top=e;
i_stack->top++;
return 1;
}
//--------------------------------------------------------------
int i_pop(istack *i_stack,int *e)
{
if(i_stack->top==i_stack->base)
{
printf("栈为空,不能删除栈顶元素");
return 0;
}
else{
i_stack->top--;
*e=*i_stack->top;
return 1;
}
}
//------------------------------------------------------------------
int i_gettop(istack *i_stack)
{
int e;
if(i_stack->top==i_stack->base)
{
printf("栈为空,不能获取栈顶元素/n");
return -1;
}
e=*(i_stack->top-1);
return e;
}
//-------------------------------------------------------------------
char c_push(cstack *c_stack,char e)
{
*c_stack->top=e;
c_stack->top++;
return *c_stack->top;
}
//----------------------------------------------------------------------
int c_pop(cstack *c_stack,char *e)
{
if(c_stack->top==c_stack->base)
{
printf("栈为空,不能获取栈顶元素");
return 0;
}
else{
c_stack->top--;
*e=*c_stack->top;
return 1;
}
}
//-------------------------------------------------------------------
char c_gettop(cstack *c_stack)
{
char e;
if(c_stack->top==c_stack->base)
{
printf("栈为空,不能获取栈顶元素");
return 0;
}
e=*(c_stack->top-1);
return e;
}
//==================================================================