5-21 求前缀表达式的值 (25分)

5-21 求前缀表达式的值 (25分)_第1张图片

#include "iostream"
#include "algorithm"
#include "iomanip"
using namespace std; 

float f()
{
   char A[10];
   cin>>A; 

   if(A[1]==NULL )
   {
   switch(A[0])
   {
   case'*': return f()*f();  
   case'/': 
	   {
        float fenzi,fenmu;
		fenmu=f();
		fenzi=f();
        if(fenzi==0)
		{
		 cout << "ERROR"<<endl;
		 exit(0);
		}
        else return fenmu/fenzi;

	   }
   case'-': return f()-f();
   case'+': return f()+f();
   default: return atof(A);
   }
   }
   else
   {
     if(A[0]=='+' || A[0]=='-')
	 {
        char flag=A[0];
		int i=0;
        while(A[i])
		{     
         A[i]=A[i+1];
         i++;
		}
        if(flag=='-')
		return 0-atof(A);
		else
		return atof(A);

	 }
     else return atof(A);
   }
};

int main( )
{    
   
	float Sum;
	Sum=f();
	cout<<fixed<<setprecision(1)<<Sum<<endl;

     
	return 0;
}


你可能感兴趣的:(5-21 求前缀表达式的值 (25分))