递归 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; }