#include<stdio.h>
#include<string.h>
#define SIZE 300
template <class type >
class Stack{
private :
long capicity ;
long top ;
type arr[SIZE] ;
public :
Stack();
~Stack();
void push( type ) ;
type pop() ;
type get_top() ;
long is_empty() ;
} ;
template<class type >
Stack<type>::Stack()
{
capicity = SIZE ;
top = -1;
}
template<class type >
Stack<type>::~Stack()
{
}
template<class type >
void Stack<type>::push( type input)
{
arr[ ++ top ] = input ;
}
template<class type >
type Stack<type>::pop()
{
if( !is_empty())
return arr[ top-- ] ;
}
template<class type >
type Stack<type> ::get_top()
{
if( !is_empty())
return arr[ top ] ;
}
template <class type >
long Stack<type>::is_empty ()
{
return top == -1 ;
}
char buffer[SIZE ] , ch , p ;
double num , a , b ;
long buffer_i ;
long get_char_num( char& ch , double & num , long & buffer_i )
{
ch = NULL ;
num = 0;
while( buffer[ buffer_i ] != '/0' && buffer[ buffer_i ] ==' ' )
buffer_i++ ;
if( buffer[ buffer_i ] >='0' && buffer[ buffer_i ] <= '9')
{
while( buffer[ buffer_i ] >='0' && buffer[ buffer_i ] <= '9')
num = num * 10 + buffer[ buffer_i ++ ] - '0' ;
//printf("%.2f /n" ,num );
return 0 ;// num
}
else
{
ch = buffer[ buffer_i++ ];
//printf("%c" , ch ) ;
return 1 ;
}
}
char precede( char & p, char &ch )
{
switch( p )
{
case '(': if( ch == ')')
return '=';
else
return '<';
case '*':
case '/': if( ch =='(')
return '<' ;
else
return '>';
case '+':
case '-':
if( ch == '(' || ch =='*' || ch== '/')
return '<';
else
return '>';
case ')':return '>' ;
// 没写#
case '#':
if( ch !='#') return '<';
}
return 0 ;
}
double operater( double &a, double &b , char & p )
{
switch( p )
{
case '+' :return a + b ;
case '-' :return b - a ;
case '*' :return a * b ;
case '/' :return b / a ;
}
return 0 ;
}
int main()
{
long tag = -1 ;
Stack<double> nu ;
Stack <char> op ;
while( gets( buffer) !=NULL && strcmp( buffer , "0") )
{
strcat( buffer , " # ");
op.push('#') ;
buffer_i = 0 ;
tag = get_char_num( ch, num ,buffer_i ) ;
while( ch !='#' || op.get_top() !='#' )
{
if( tag == 0) //num
{
nu.push( num ) ;
tag = get_char_num( ch , num , buffer_i );
}
else //char
{
char pp = op.get_top() ;
switch( precede( pp , ch ) )
{
case '<':
op.push(ch) ;
tag = get_char_num( ch , num , buffer_i );
break;
case '=':
op.pop();
tag = get_char_num( ch , num , buffer_i );
break;
case '>':
a = nu.pop();
b = nu.pop();
p = op.pop() ;
nu.push( operater( a , b , p ));
break ;
} //end switch
}//end if
}//end while
printf("%.2f/n" , nu.pop());
op.pop();
}
return 0 ;
}