#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0
#define Elemtype int
#define elemtype char
char opt[7][7]=
{
{’>’,’>’,’<’,’<’,’<’,’>’,’>’},
{’>’,’>’,’<’,’<’,’<’,’>’,’>’},
{’>’,’>’,’>’,’>’,’<’,’>’,’>’},
{’>’,’>’,’>’,’>’,’<’,’>’,’>’},
{’<’,’<’,’<’,’<’,’<’,’=’,’ ‘},
{’>’,’>’,’>’,’>’,’ ‘,’>’,’>’},
{’<’,’<’,’<’,’<’,’<’,’ ‘,’=’},
};
char op[7]={’+’,’-’,’*’,’/’,’(’,’)’,’#’};
typedef struct stack1
{
Elemtype data;
struct stack1 *next;
}InitStack,*LinkStack1;
typedef struct stack2
{
elemtype optr;
struct stack2 *next;
}initStack,*LinkStack2;
int Push_dat(LinkStack1 top1,Elemtype x)
{
LinkStack1 temp;
temp=(LinkStack1)malloc(sizeof(InitStack));
if(!temp)
return FALSE;
temp->data=x;
temp->next=top1->next;
top1->next=temp;
return TRUE;
}
int Pop_dat(LinkStack1 top1,Elemtype *x)
{
LinkStack1 temp;
temp=top1->next;
if(temp==NULL)
return FALSE;
top1->next=temp->next;
*x=temp->data;
free(temp);
return TRUE;
}
int GetTop_dat(LinkStack1 top1)
{
int x;
LinkStack1 temp;
temp=top1->next;
if(temp==NULL)
return FALSE;
x=temp->data;
return x;
}
int Push_optr(LinkStack2 top2,elemtype ch)
{
LinkStack2 temp;
temp=(LinkStack2)malloc(sizeof(initStack));
if(!temp)
return FALSE;
temp->optr=ch;
temp->next=top2->next;
top2->next=temp;
return TRUE;
}
int Pop_optr(LinkStack2 top2,elemtype *ch)
{
LinkStack2 temp;
temp=top2->next;
if(temp==NULL)
return FALSE;
top2->next=temp->next;
*ch=temp->optr;
free(temp);
return TRUE;
}
char GetTop_optr(LinkStack2 top2)
{
LinkStack2 temp;
char c;
temp=top2->next;
if(temp==NULL)
return FALSE;
c=temp->optr;
return c;
}
char Precede(char cr,char ch)
{
int i,j,x1,x2;
for(i=0;i<7;i++)
{
if(crop[i])
{
x1=i;
break;
}
}
for(j=0;j<7;j++)
{
if(chop[j])
{
x2=j;
break;
}
}
return opt[x1][x2];
}
int IsOpr(char c)
{
if(c==’+’||c==’-’||c==’*’||c==’/’||c==’(’||c==’)’||c==’#’)
return 1;
else
return 0;
}
int Operate(int x,char opr,int y)
{
int result;
switch(opr)
{
case’+’:
result=x+y;
break;
case’-’:
result=x-y;
break;
case’’:
result=xy;
break;
case’/’:
result=x/y;
break;
}
return result;
}
int ch_dat(char ch)
{
int x,i;
i=(int)ch;
switch(i)
{
case 48:{x=0;break;}
case 49:{x=1;break;}
case 50:{x=2;break;}
case 51:{x=3;break;}
case 52:{x=4;break;}
case 53:{x=5;break;}
case 54:{x=6;break;}
case 55:{x=7;break;}
case 56:{x=8;break;}
case 57:{x=9;break;}
}
return x;
}
int EvaluateExpression()
{
char ch,cr,theta,ch1;
int result,a,b,tranvalue;
InitStack OPND;
initStack OPTR;
LinkStack1 top1;
LinkStack2 top2;
top1=&OPND;
top2=&OPTR;
top1->next=NULL;
top2->next=NULL;
Push_optr(&OPTR,’#’);
printf(“Please input an exprssion such as ‘3+7*6#’ :”);
ch=getchar();
while((ch!=’#’)||(GetTop_optr(&OPTR)!=’#’))
{
if(!IsOpr(ch))
{
tranvalue=ch_dat(ch);
Push_dat(&OPND,tranvalue);
ch=getchar();
}
else
{
cr=GetTop_optr(&OPTR);
switch(Precede(cr,ch))
{
case ‘<’:
Push_optr(&OPTR,ch);
ch=getchar();
break;
case ‘=’:
Pop_optr(&OPTR,&ch);
ch=getchar();
break;
case ‘>’:
Pop_optr(&OPTR,&theta);
Pop_dat(&OPND,&b);
Pop_dat(&OPND,&a);
result=Operate(a,theta,b);
Push_dat(&OPND,result);
break;
}
}
}
printf(“The answer is :%d\n”,GetTop_dat(&OPND));
}
void main()
{
char anw;
printf("\t\tThe Simlpe Expreesion Evaluation\n\n");
EvaluateExpression();
getchar();
while(1)
{ printf(“Do you want to continue?(Y/N) “);
scanf(”%c”,&anw);
printf("\n");
getchar();
if(anw==‘Y’)
{
EvaluateExpression();
getchar();
}
else if(anw==‘N’)
break;
}
}