HDU1237: Simple Calculator

Source: Internet
Author: User
Problem Description reads a non-negative integer that contains only +,-, *,/, evaluates the expression, and computes the value of the formula.

The input test inputs contain several test cases, one row for each test case, and no more than 200 characters per line, separated by a space between integers and operators. There is no illegal expression. When only 0 o'clock input is completed in a row, the corresponding result is not output.

Output outputs 1 rows for each test case, that is, the value of the expression, exactly 2 digits after the decimal point.

Sample Input
1 + 2 4 + 2 * 5-7/11 0
Sample Output
3.00 13.36


Continue stack

#include <stdio.h> #include <string.h> #include <stack> using namespace std;
    int main () {int i;
    Double A, B;
    Char s[250],c;
        while (gets (s), strcmp (S, "0")!=0) {stack<char>s1;
        stack<double>s2;
                for (i=0; s[i]; i++) {if (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {a=0;
                    while (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {a=a*10+s[i]-' 0 ';
                i++;
                } i--;
            S2.push (a); } else if (s[i]== '-' | | |
                    s[i]== ' + ') {if (!s1.empty ()) {c=s1.top ();
                    S1.pop ();
                    A=s2.top ();
                    S2.pop ();
                    B=s2.top ();
                    S2.pop ();
                    if (c== ' + ') a+=b; else a=B-a;
                    S2.push (a);
                S1.push (S[i]);
            } else S1.push (S[i]);
                } else if (s[i]== '/' | | s[i] = = ' * ') {char ch = s[i];
                b=0;
                i+=2;
                    while (s[i]>= ' 0 ' &&s[i]<= ' 9 ') {b=b*10+s[i]-' 0 ';
                i++;
                } i--;
                A=s2.top ();
                S2.pop ();
                if (ch = = '/') a=a/b;
                else A = a*b;
            S2.push (a);
            }} while (!s1.empty ()) {c=s1.top ();
            S1.pop ();
            A=s2.top ();
            S2.pop ();
            B=s2.top ();
            S2.pop ();
            if (c== ' + ') a+=b;
            else a=b-a;
        S2.push (a); } printf ("%.2f\n", s2.toP ());
} return 0;

 }


 

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.