# include
H # include
# include
# define INIT 100
Struct SNode
{
Char * Base;
Char * Top;
Int Maxsize;
};
Struct Node
{
Double * base;
Double * top;
Int maxsize;
};
Void CreateStack (SNode & S)
{
S.B ase=(char *) malloc (INIT * sizeof (char));
if (! S.B ase)
{
Printf (" error! \n");
}
The else
{
S.T op=S.B ase;
S.M axsize=INIT;
}
}
Void CreateTack (Node & S)
{
S.b ase=(double *) malloc (INIT * sizeof (double));
if (! S.b ase)
{
Printf (" error! \n");
}
The else
{
S.t op=S.b ase;
S.m axsize=INIT;
}
}
Void PushStack SNode & S, char (X)
{
* S.T op=X;
S.T op++;
}
Void PushTack (Node & S, float X)
{
* S.t op=X;
S.t op++;
}
Char PopStack (SNode & S)
{
S.T op -;
Return (* S.T op);
}
Float PopTack (Node & S)
{
S.t op -;
Return (* S.t op);
}
Char GetStackTop SNode (S)
{
Char e;
E=* (S.T op -);
Return e;
}
Float GetTackTop Node (S)
{
Char e;
E=* (S.t op -);
Return e;
}
Int Judge (char a)
{
If (a=='+' | | a=='-' | | a=='*' | | a=='/' | | a=='(' | | a==') '| | a==' # ')
return 1;
The else
return 0;
}
Double Operate (double a, char theta, double b)
{
Double end=0;
The switch (theta)
{
Case: '+'
End=a + b;
break;
In case the '-' :
End=a - b;
break;
Case: '*'
End=a * b;
break;
Case '/' :
End=a/b;
break;
}
Return the end;
}
Char Compare (char a, char b)
{
Char e;
The switch (a)
{
Case: '+'
If (b=='+' | | b=='-' | | b==') | | b=='#')
E='>';
The else
E='<';
break;
In case the '-' :
If (b=='+' | | b=='-' | | b==') | | b=='#')
E='>';
The else
E='<';
break;
Case: '*'
If (b=='(')
E='<';
The else
E='>';
break;
Case '/' :
If (b=='(')
E='<';
The else
E='>';
break;
Case '('
If (b==') ')
E='=';
The else
E='<';
break;
Case ') :
E='>';
break;
Case '#' :
If (b=='#')
E='<';
Else if (b==') ')
E='! ';
The else
E='=';
break;
}
}
Int main (int arg c, char * argv [])
{
Double num, x=0, y=0;
Int I=0, j=0;
Char TmpData [20].
Char theta, c;
SNode OPTR;
The Node OPND;
CreateStack (OPTR);
PushStack (OPTR, '#');
CreateTack (OPND);
C=argv [1] [j];
While (c!='#' | | GetStackTop (OPTR)!='#')
{
Printf (" 2 ");
If (Judge (c)==0)
{
TmpData [I]=c;
i++;
j++;
C=argv [1] [j];
If (Judge (c)==1)
{
TmpData [I]='\ 0';
Num=atof (TmpData);
PushTack (OPND, num);
I=0;
}
}
The else
The switch (Compare (GetStackTop (OPTR), c))
{
In case the '<' :
PushStack (OPTR, c);
j++;
C=argv [1] [j];
break;
Case: '='
PopStack (OPTR);
j++;
C=argv [1] [j];
break;
Case: '>'
Theta=PopStack (OPTR);
Y=PopTack (OPND);
X=PopTack (OPND);
PushTack (OPND to Operate (x, theta, y));
break;
}
}
Printf (" % g, "GetTackTop (OPND));
return 0;
}