Inverse Polish expression (stack, recursive)

Source: Internet
Author: User

1696: Inverse Polish expression
Total time limit:
1000ms
Memory Limit:
65536kB
Describe
The
inverse Polish expression is an arithmetic expression that puts the operator in front, such as the inverse Polish notation for the normal expression 2 + 3 for + 2 3. The advantage of inverse Polish expressions is that there is no need for precedence between operators, and there is no need to change the order of operations with parentheses, for example (2 + 3) * 4 for inverse Polish notation for * + 2 3 4. The subject solves the value of the inverse Polish expression, where the operator includes +-*/four.
Input
input is a row where the operator and the operand are separated by a space and the operand is a floating-point number.
Output
output is a row, the value of an expression.
The value of the expression can be output directly with printf ("%f\n", V).
Sample input
* + 11.0 12.0 + 24.0 35.0
Sample output
1357.000000
Tips
You can use Atof (str) to convert a string to a floating-point number of type Double. The atof is defined in the MATH.H.
This problem can be solved using the method of recursive invocation of functions.
#include <cstdio>#include<iostream>#include<windows.h>Doublecalculation () {Chara[Ten]; scanf ("%s", a); Switch(a[0])    {         Case '+':        returnCalculation () +calculation ();  Case '-':        returnCalculation ()-calculation ();  Case '*':        returnCalculation () *calculation ();  Case '/':        returnCalculation ()/calculation (); default:        returnAtof (a); }}intMain () {Doubleans; Ans=calculation (); printf ("%f\n", ans); return 0;}
If the heart is sunny, no words sorrow

Inverse Polish expression (stack, recursive)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.