Source: Network
# Include < Stdio. h >
# Include < Stdlib. h >
# Include < Ctype. h >
# Define Maxop 100
# Define Number '0'
Int Getop ( Char []);
Void Push ( Double );
Double Pop ( Void );
Int Getch ( Void );
Void Ungetch ( Int );
Int Getop ( Char S [])
{
Int I, C;
While (S [ 0 ] = C = Getch ()) = ' ' | C = ' \ T ' );
S [ 1 ] = ' \ 0 ' ;
If ( ! Isdigit (c) && C ! = ' . ' )
Return C;
I = 0 ;
If (Isdigit (c ))
While (Isdigit (s [ ++ I] = C = Getch ()));
If (C = ' . ' )
While (Isdigit (s [ ++ I] = C = Getch ()));
S [I] = ' \ 0 ' ;
If (C ! = EOF)
Ungetch (C );
Return Number;
}
# Define Bufsize 100
Char Buf [bufsize];
Int Bufp = 0 ;
Int Getch ( Void )
{
Return (Bufp > 0 ) ? Buf [ -- Bufp]: getchar ();
}
Void Ungetch ( Int C)
{
If (Bufp > = Bufsize)
Printf ( " Ungetch: Too character characters \ n " );
Else
Buf [bufp ++ ] = C;
}
Main ()
{
Int Type;
Double OP2;
Char S [maxop];
While (Type = Getop (s )) ! = EOF ){
Switch (Type ){
Case Number:
Push (atof (s ));
Break ;
Case ' + ' :
Push (POP () + Pop ());
Break ;
Case ' * ' :
Push (POP () * Pop ());
Break ;
Case ' - ' :
OP2 = Pop ();
Push (POP () - OP2 );
Break ;
Case ' / ' :
OP2 = Pop ();
If (OP2 ! = 0.0 )
Push (POP () / OP2 );
Else
Printf ( " Error: Zero Divisor \ n " );
Break ;
/* Case '/N ':
Printf ("\ t %. 8g \ n", pop ());
Break; */
Case 0x0a :
Printf ( " \ T %. 8g \ n " , Pop ());
Break ;
Default :
Printf ( " Error: Unknown command % s \ n " , S );
Break ;
}
}
Return 0 ;
}
# Define # Maxval 100
Int SP = 0 ;
Double Val [maxval];
Void Push ( Double F)
{
If (SP < Maxval)
Val [SP ++ ] = F;
Else
Printf ( " Error: strack full, can't push % G \ n " , F );
}
Double Pop ( Void )
{
If (SP > 0 )
Return Val [ -- SP];
Else {
Printf ( " Error: Stack empty \ n " );
Return 0.0 ;
}
}