#include "stdio.h"
#include "conio.h"
#define stack_size 100
typedef struct /*栈的定义*/
{
char elem[stack_size];
int top;
}SqStack;
SqStack InitStack()/*栈的初始化*/
{ SqStack s;
s.top=-1; /*栈空*/
return s;
}
char GetTop(SqStack * s,char * e)/*取栈顶的元素*/
{ if(s->top==-1) return 0;
*e=s->elem[s->top];
}
void Push(SqStack * s,char e)
{ /*if(s->top==(stack_size-1)) printf("Stack Overflow!"); */
s->top++;
s->elem[s->top]=e;
}
char Pop(SqStack * s,char * e)
{ /* if(s->top==-1) printf("Stack Overflow!"); */
* e=s->elem[s->top--];
}
typedef struct /*栈的定义*/
{
float elem[stack_size];
int top;
}SqStack1;
SqStack1 InitStack1()/*栈的初始化*/
{ SqStack1 s;
s.top=-1; /*栈空*/
return s;
}
int GetTop1(SqStack1 * s,int * e)/*取栈顶的元素*/
{ if(s->top==-1) return 0;
*e=s->elem[s->top];
}
void Push1(SqStack1 * s,int e)
{ /*if(s->top==(stack_size-1)) printf("Stack Overflow!"); */
s->top++;
s->elem[s->top]=e;
}
int Pop1(SqStack1 * s,int * e)
{ /* if(s->top==-1) printf("Stack Overflow!"); */
* e=s->elem[s->top--];
}
int SelLeve(char c1)
{ if(c1=='+'|| c1=='-') return 1;
else
if (c1=='*'|| c1=='/' || c1=='%') return 2;
else
if(c1=='(') return -1;
}
main()
{
char expr[100]="6+(7-5)-8*6";
houzhui(expr);
getch();
}
int
num1,num2,l;
int
houzhui(char expr[])
{
SqStack Optr=InitStack();
char c,d,b[10];
int index=0,i=0,j=0,k,dlevel;
char hz[100];
while(expr[index]!='\0')
{ c=expr[index];
if(c>='0'&& c<='9')/*判断是不是数,如果是就把它取出放在运算数数组中*/
{
;
hz[i]=c;j=i;
i=index+1;k=i;
c=expr[i];
if(i<j){i=j+1;}
j=0;
while(c!='\0' && c>='0'&& c<='9' ||c=='.')
{hz[i]=c;i++;c=expr[i];j++;}
index=k+j;
hz[i]='@'; i++;
}
if(c=='+'|| c=='-')
{ int clevel=1;
d=GetTop(&Optr,&d);
dlevel=SelLeve(d);
if(d!='' && dlevel>=clevel)
{hz[i]=Pop(&Optr, &d);i++;hz[i]='@';i++;}
Push(&Optr,c);
index++;
}
if(c=='*'|| c=='/'|| c=='%')
{
int clevel=2;
d=GetTop(&Optr,&d);
dlevel=SelLeve(d);
if(d!='' &&dlevel >=clevel)
{hz[i]=Pop(&Optr, &d);i++;hz[i]='@';i++;}
Push(&Optr,c);
index++;
}
if(c=='(')
{ Push(&Optr,c);index++;}
if(c==')')
{ d=GetTop(&Optr,&d);
while(d!='('){hz[i]=Pop(&Optr,&d);i++;hz[i]='@';i++;d=GetTop(&Optr,& d);}
Pop(&Optr,&d); index++;
}
}
while(GetTop(&Optr,&d)!=''){hz[i]=Pop(&Optr,&d);i++;hz[i]='@';i++;}
l=i;
printf("hou zhui biao da shi :");
for(i=0;i<l;i++)printf("%c",hz[i]);
printf("\n------------------------------------------------------------");
printf("%d",l);
EvaluateExpr(hz);
}
int
EvaluateExpr(char hz[])
{ SqStack1 Opnd=InitStack1();
int i=0;
char c,d;
c=hz[i];
printf("\n%d",l);
while(i<l)
{
printf("\n%c",hz[i]);
if(c=='@'||c==''){i++;c=hz[i];}
else
{
if( c>='0'&& c<='9' ||c=='.')
{Push1(&Opnd,(int)(c));i++;c=hz[i];}
else{
switch(c)
{case '+' :num1=Pop1(&Opnd,&d);num2=Pop1(&Opnd,&d);num1=num2+num1; Push1(&Opnd,num1);break;
case '-' :num1=Pop1(&Opnd,&d);num2=Pop1(&Opnd,&d);num1=num2-num1; Push1(&Opnd,num1);break;
case '*' :num1=Pop1(&Opnd,&d);num2=Pop1(&Opnd,&d);num1=num2*num1; Push1(&Opnd,num1);break;
case '/' :num1=Pop1(&Opnd,&d);num2=Pop1(&Opnd,&d);num1=num2/num1; Push1(&Opnd,num1);break;
case '%' :num1=Pop1(&Opnd,&d);num2=Pop1(&Opnd,&d);num1=num2%num1; Push1(&Opnd,num1);break;
}
}
}
}
/* printf("\n%d", Pop1(&Opnd,&d));*/
}