pat02-linear structure 3. Find the value of the prefix expression (25)

Source: Internet
Author: User

02-Linear Structure 3. To find the value of the prefix expression (25) time limit MS Memory limit 65536 KB code length limit 8000 B award Program Standard

The arithmetic expression has the form of prefix notation, infix notation and suffix notation. The prefix expression means that the two-tuple operator precedes two operands, such as the prefix expression for 2+3* (7-4) +8/4: + + 2 * 3-7 4/8 4. Please design the program to calculate the result value of the prefix expression.

Input Format Description:

Enter a prefix expression of no more than 30 characters in a row, containing only the + 、-、 *, \, and operands, separated by a space between the different objects (operands, operation symbols).

Output Format Description:

The output prefix expression evaluates to 1 digits after the decimal point, or error message "error".

Sample input and output:

Serial number Input Output
1
+ + 2 * 3-7 4/8 4
13.0
2
/-25 + *-2 3 4/8 4
12.5
3
/5 + *-2 3 4/8 2
ERROR
4
+10.23
10.2

Submit Code

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7#include <string>8#include <stack>9 using namespacestd;Ten intMain () { One     //freopen ("D:\\input.txt", "R", stdin); Astack<Double>fi; -stack<string>input; -     strings; the      -     //bool Can=true; -      while(cin>>s) { - Input.push (s); +     } -      while(!Input.empty ()) { +s=input.top (); A Input.pop (); at         if(s.length () = =1&&! (s[0]>='0'&&s[0]<='9')){ -             DoubleA=fi.top (); - Fi.pop (); -             Doubleb=fi.top (); - Fi.pop (); -             Switch(s[0]){ in                  Case'+':{ -                     //cout<< "+:" <<a+b<<endl; toFi.push (A +b); +                      Break; -                 } the                  Case'-':{ *                     //cout<< "-:" <<a-b<<endl; $Fi.push (A-b);Panax Notoginseng                      Break; -                 } the                  Case'*':{ +                     //cout<< "*:" <<a*b<<endl; AFi.push (A *b); the                      Break; +                 } -                  Case'/':{ $                     if(b==0){ $printf"error\n"); -                         //Can=false; -                         return 0; the                     } -                     //cout<< "/:" <<a/b<<endl;WuyiFi.push (A/b); the                      Break; -                 } Wu                 default:{ -printf"error\n"); About                         return 0; $                 } -             } -}Else if((s[0]>='0'&&s[0]<='9')|| (S.length () >1&&! (s[0]>='0'&&s[0]<='9') && (s[1]>='0'&&s[1]<='9'))){ -             DoubleCal=1; A             intI=0; +             if(s[i]=='+'|| s[i]=='-'){ the                 if(s[i]=='-'){ -cal=-1; $                 } thei++; the             } the             Doubleintpart=0, decpart=0; the              while(I<s.length () &&s[i]!='.'){//Note Conversion -intpart*=Ten; inintpart+=s[i++]-'0'; the             } thei++; About             intJ; the              for(J=s.length ()-1; j>=i;j--){ thedecpart+=s[j]-'0'; thedecpart*=0.1; +             } -Fi.push (cal* (intpart+decpart)); the}Else{Bayiprintf"error\n"); the             return 0; the         }     -     } -     //cout<<fi.top () <<endl; the      theprintf"%.1lf\n", Fi.top ()); the     return 0; the}

pat02-linear structure 3. Find the value of the prefix expression (25)

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.