Nyoj 128 prefix-type calculation

Source: Internet
Author: User

Prefix-Based calculation time limit: +Ms | Memory Limit:65535KB Difficulty:3
Describe

First explain what is infix:

such as 3+4, the most common expression of our formula is infix.

And the infix type in the order of operation with parentheses is: ((3+4))

Then put the operator in front of the parentheses is + (2 * (+ (3 4) 5))

Remove the brackets: + 2 * + 3 4 5

The last expression is the prefix representation of the formula.

Give you a prefix expression, please calculate the value of the prefix.

Like what:

+ 2 * + 3 4 5 The value is 37

Input
There are multiple sets of test data, one row for each set of test data, between any two operators, and between any two operands, and there is a space between the operand and the operator. The two operands you enter may be decimals, the data guarantees that the number of inputs is positive, and both are less than 10 and the number of operands is less than 500.
A flag that ends with EOF as input.
Output
for each set of data, output the value of the prefix expression. The output retains two decimal places.
Sample input
+ 2 * + 3 4 5+ 5.1/3 7
Sample output
37.005.53


Tip: This program is constantly recursive ... Function name: Atof functions: Converts a string into a floating-point number usage: Double atof (const char *nptr); Program Example: #include <stdlib.h> #include <stdio.h>

int main (void) {float F; Char *str = "12345.67";

f = atof (str);    printf ("string =%s float =%f/n", str, f); return 0; }

Transferred from: http://blog.csdn.net/xuefu2008/article/details/4646127
Program: # include <stdio.h># include <stdlib.h>char c[30];int i = 0;float Fun () {if (i! = 0)scanf ("%s", c);i = 1;if (c[0] = = ' + ')return fun () + fun ();else if (c[0] = = '-')Return fun ()-Fun ();else if (c[0] = = ' * ')return Fun () * Fun ();else if (c[0] = = '/')return fun ()/fun ();Elsereturn Atof (c);} int main (void) {while (~SCANF ("%s", c)){i = 0;printf ("%.2f\n", Fun ());}return 0;}
Copy to Google TranslateTranslation Results

Nyoj 128 prefix-type calculation

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.