Reverse Polish Notation (recursive) - Learning Algorithm

problem

Reverse Polish Notation the operator is a front arithmetic expression, for example, conventional expression 2 reverse Polish notation +3 +23. RPN
advantage that do not have expression of the relationship between the priority of operators , there is no need to change the order of operations in parentheses, for example an inverse (3 + 2) * 4 Polish notation
+234. reverse Polish notation resolve the value of the body, wherein the operator comprises ± / four
input
input line, wherein operation between operators and are separated by spaces operand, floating point operand is
output
the output line, the value of the expression
sample input
+ 11.0 12.0 + 24.0 35.0
sample output
1257.000000
tips (11.0 + 12.0)
(24.0 + 35.0)
recursive problem solving recursive form of
reverse Polish notation defined
1: a genus is a reverse Polish notation, this number is
2: reverse Polish Notation operator is reverse Polish notation, the value of i two reverse Polish notation the value of the operation

atof

the atof () is the standard C language library in a string functions,
function is to convert the floating-point number to a string, the header file is used to <stdlib.h>.
This name is an acronym for "ascii to floating point numbers" of.
The syntax is: double atof (const char * nptr ).

Code:

#include<iostream>
 #include<stdio.h>
 #include<cstdlib>
 using namespace std;
 double exp(){
 //读入一个逆波兰表达式
 char s[20];
 cin>>s;
 switch(s[0]){
  case'+':return exp()+exp();
  case '-':return exp()-exp();
  case'*':return exp()*exp();
  case'/':return exp()/exp();
  default:return atof(s);
  break;
 }
 }
 int main(){
  printf("%lf",exp());
  return 0;
 }
Published 79 original articles · won praise 133 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_45822638/article/details/105028641