#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <header.h>
typedef struct
{
char *base;
int stacksize;
int top;
}MySignStack;
typedef struct
{
int *base;
int stacksize;
int top;
}MyDataStack;
status InitSignStack(MySignStack &S);
status InitDataStack(MyDataStack &S);
status PushSign(MySignStack &S,char e);
status PushData(MyDataStack &S,int e);
status PopSign(MySignStack &S,char &e);
status PopData(MyDataStack &S,int &e);
char GetTopSign(MySignStack &S);
int GetTopData(MyDataStack &S);
char Compare(char a,char b);
status IsData(char c);
int Operation(int a,int b,char c);
void main()
{
MySignStack SignStack;
MyDataStack DataStack;
InitSignStack(SignStack);
InitDataStack(DataStack);
PushSign(SignStack,'#');
/*printf("请按照正确的顺序输入表达式,以‘#’结束:\n");
char c=getchar();*/
char c=getchar();
char temp;
int a,b;
int number=0;
int flag=0;
while(c!= '#' || GetTopSign(SignStack)!='#')
{
number=0;
flag=0;
while(IsData(c))
{
number =number*10+ (c-'0');
c=getchar();
flag=1;
}
if(flag==1)
PushData(DataStack,number);
switch(Compare(GetTopSign(SignStack),c))
{
case '<':
PushSign(SignStack,c);
c=getchar();
break;
case '=':
PopSign(SignStack,temp);
c=getchar();
break;
case '>':
PopSign(SignStack,temp);
PopData(DataStack,a);
PopData(DataStack,b);
PushData(DataStack,Operation(a,b,temp));
break;
default:
break;
}
}
printf("%d",GetTopData(DataStack));
}
status InitSignStack(MySignStack &S)
{
S.base=(char *)malloc(sizeof(char)*INITSIZE);
if(!S.base)
return ERROR;
S.top=0;
S.stacksize=INITSIZE;
return OK;
}
status InitDataStack(MyDataStack &S)
{
S.base=(int *)malloc(sizeof(int)*INITSIZE);
if(!S.base)
return ERROR;
S.top=0;
S.stacksize=INITSIZE;
return OK;
}
status PushSign(MySignStack &S,char e)
{
if(S.top==S.stacksize)
{
S.base=(char *)realloc(S.base,S.stacksize+INCREMENT);
S.stacksize+=INCREMENT;
}
S.base[S.top++]=e;
//printf("the sign is %c\n",S.base[S.top-1]);
return OK;
}
status PushData(MyDataStack &S,int e)
{
if(S.top==S.stacksize)
{
S.base=(int *)realloc(S.base,S.stacksize+INCREMENT);
S.stacksize+=INCREMENT;
}
S.base[S.top++]=e;
//printf("the data is %d\n",S.base[S.top-1]);
return OK;
}
status PopSign(MySignStack &S,char &e)
{
if(S.top==0)
return ERROR;
e=S.base[--S.top];
return OK;
}
status PopData(MyDataStack &S,int &e)
{
if(S.top==0)
return ERROR;
e=S.base[--S.top];
return OK;
}
char GetTopSign(MySignStack &S)
{
char e;
if(S.top==0)
return ERROR;
e=S.base[S.top-1];
return e;
}
int GetTopData(MyDataStack &S)
{
int e;
if(S.top==0)
return ERROR;
e=S.base[S.top-1];
return e;
}
char Compare(char a,char b)
{
if(a=='+')
{
if(b=='+' || b=='-' || b==')' || b=='#')
return '>';
else
return '<';
}
if(a=='-')
{
if(b=='*' || b=='/' || b=='(')
return '<';
else
return '>';
}
if(a=='*')
{
if(b=='(')
return '<';
else
return '>';
}
if(a=='/')
{
if(b=='(')
return '<';
else
return '>';
}
if(a=='(')
{
if(b==')')
return '=';
else
return '<';
}
if(a==')')
{
return '>';
}
if(a=='#')
{
if(b=='#')
return '=';
else
return '<';
}
}
status IsData(char c)
{
if(c>='0' && c<='9')
return 1;
else
return 0;
}
int Operation(int a,int b,char c)
{
if(c=='+')
return b+a;
if(c=='-')
return b-a;
if(c=='*')
return a*b;
if(c=='/')
return b/a;
}