#include"stdio.h"
#include"stdlib.h"
#define MaxSize 10
#define OK 1
#define True 1
#define False 0
#define OverFlow 0
#define UnderFlow 0
typedef int ElemType;
typedef struct
{
ElemType Element[MaxSize];
int top;
}SeqStack;
void Initial(SeqStack*S_pointer)
{
S_pointer->top=-1;
}
int Empty(SeqStack*S_pointer)
{
if(S_pointer->top==-1)
return True;
else
return False;
}
int Full(SeqStack*S_pointer)
{
if(S_pointer->top==MaxSize-1)
return True;
else
return False;
}
int Push(SeqStack*S_pointer,ElemType x)
{
if(Full(S_pointer)==True)
return OverFlow;
else
{
S_pointer->top++;
S_pointer->Element[S_pointer->top]=x;
return OK;
}
}
int Pop(SeqStack*S_pointer)
{
if(Empty(S_pointer)==True)
return UnderFlow;
else
{
S_pointer->top--;
return OK;
}
}
int Top(SeqStack*S_pointer)
{
if(Empty(S_pointer)==True)
return UnderFlow;
else
{
return OK;
}
}
int Precede(int t1,int t2)
{
int f;
switch(t2)
{
case'+':
case'-':if(t1=='('||t1=='#')
f='<';
break;
case'*':
case'/':if(t1=='*'||t1=='/'||t1==')')
f='>';
else
f='<';
break;
case'(':if(t1==')')
{
printf("ERROR1\n");
exit(0);
}
else
f='<';
break;
case')':switch(t1)
{
case'(':f='=';
break;
case'#':printf("ERROR\n");
exit(0);
default:f='>';
}
}
return f;
}
int In(int c)
{
switch(c)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'#':return 1;
default:return 0;
}
}
int Operate(int a,int theta,int b)
{
int c;
switch(theta)
{
case'+':
c=a+b;
break;
case'-':
c=a-b;
break;
case'*':
c=a*b;
break;
case'/':
c=a/b;
}
return c;
}
int EvaluateExpression()
{
SeqStack OPTR,OPND;
int a,b,d,x,theta;
char c;
char z[6];
int i;
Initial(&OPTR);
Push(&OPTR,'#');
Initial(&OPND);
c=getchar();
x=Top(&OPTR);
while(c!='#'||x!='#')
{
if(In(c))
{
switch(Precede(x,c))
{
case'<':Push(&OPTR,c);
c=getchar();
break;
case'=':x=Pop(&OPTR);
c=getchar();
break;
case'>':theta=Pop(&OPTR);
b=Pop(&OPND);
a=Pop(&OPND);
Push(&OPND,Operate(a,theta,b));
}
}
else if(c>='0'&&c<='9')
{
i=0;
do
{
z[i]=c;
i++;
c=getchar();
}
while(c>='0'&&c<='9');
z[i]=0;
d=atoi(z);
Push(&OPND,d);
}
else
{
printf("ERROR3\n");
exit(0);
}
x=Top(&OPTR);
}
x=Top(&OPND);
return x;
}
int main()
{
printf("请输入算术表达式,负数要用(0-正数)表示,并以#结束\n");
printf("例如:7+(4-2)*3+12/2#\n");
printf("%d\n",EvaluateExpression());
return 0;
}
#include"stdlib.h"
#define MaxSize 10
#define OK 1
#define True 1
#define False 0
#define OverFlow 0
#define UnderFlow 0
typedef int ElemType;
typedef struct
{
ElemType Element[MaxSize];
int top;
}SeqStack;
void Initial(SeqStack*S_pointer)
{
S_pointer->top=-1;
}
int Empty(SeqStack*S_pointer)
{
if(S_pointer->top==-1)
return True;
else
return False;
}
int Full(SeqStack*S_pointer)
{
if(S_pointer->top==MaxSize-1)
return True;
else
return False;
}
int Push(SeqStack*S_pointer,ElemType x)
{
if(Full(S_pointer)==True)
return OverFlow;
else
{
S_pointer->top++;
S_pointer->Element[S_pointer->top]=x;
return OK;
}
}
int Pop(SeqStack*S_pointer)
{
if(Empty(S_pointer)==True)
return UnderFlow;
else
{
S_pointer->top--;
return OK;
}
}
int Top(SeqStack*S_pointer)
{
if(Empty(S_pointer)==True)
return UnderFlow;
else
{
return OK;
}
}
int Precede(int t1,int t2)
{
int f;
switch(t2)
{
case'+':
case'-':if(t1=='('||t1=='#')
f='<';
break;
case'*':
case'/':if(t1=='*'||t1=='/'||t1==')')
f='>';
else
f='<';
break;
case'(':if(t1==')')
{
printf("ERROR1\n");
exit(0);
}
else
f='<';
break;
case')':switch(t1)
{
case'(':f='=';
break;
case'#':printf("ERROR\n");
exit(0);
default:f='>';
}
}
return f;
}
int In(int c)
{
switch(c)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'#':return 1;
default:return 0;
}
}
int Operate(int a,int theta,int b)
{
int c;
switch(theta)
{
case'+':
c=a+b;
break;
case'-':
c=a-b;
break;
case'*':
c=a*b;
break;
case'/':
c=a/b;
}
return c;
}
int EvaluateExpression()
{
SeqStack OPTR,OPND;
int a,b,d,x,theta;
char c;
char z[6];
int i;
Initial(&OPTR);
Push(&OPTR,'#');
Initial(&OPND);
c=getchar();
x=Top(&OPTR);
while(c!='#'||x!='#')
{
if(In(c))
{
switch(Precede(x,c))
{
case'<':Push(&OPTR,c);
c=getchar();
break;
case'=':x=Pop(&OPTR);
c=getchar();
break;
case'>':theta=Pop(&OPTR);
b=Pop(&OPND);
a=Pop(&OPND);
Push(&OPND,Operate(a,theta,b));
}
}
else if(c>='0'&&c<='9')
{
i=0;
do
{
z[i]=c;
i++;
c=getchar();
}
while(c>='0'&&c<='9');
z[i]=0;
d=atoi(z);
Push(&OPND,d);
}
else
{
printf("ERROR3\n");
exit(0);
}
x=Top(&OPTR);
}
x=Top(&OPND);
return x;
}
int main()
{
printf("请输入算术表达式,负数要用(0-正数)表示,并以#结束\n");
printf("例如:7+(4-2)*3+12/2#\n");
printf("%d\n",EvaluateExpression());
return 0;
}