递归 PROBLEM3 逆波兰式 ---ACM

easy quiz

 

#include <cstdlib>
#include <iostream>
#include <math.h>
/**
*    recursion   PROBLEM 3
*    逆波兰式   calculate reverse polish form
*/
using namespace std;
/**
*   do not use stack;   use recursion is more easy
*/
double exp()    
{
       char a[10];
       scanf("%s",a);   //scan one item (to the next block)
       switch(a[0])
       {
              case '+' : return exp()+exp();
              case '-' : return exp()-exp();
              case '*' : return exp()*exp();
              case '/' : return exp()/exp();
              default :return atof(a);   //string to float
       }

}
int main(int argc, char *argv[])
{
    double answer;
    answer=exp();
    printf("%f",answer);  //output 
    system("PAUSE");
    return EXIT_SUCCESS;
}

 

 

posted @ 2010-05-15 20:05  love && peace  阅读(383)  评论(0编辑  收藏  举报