OCAC summer second game title L simple calculator solution to a problem

Simple calculator
[Title] Description
read only contains a +, -, *, / nonnegative integer calculation expression, the expression of the calculated value.
[Input format
input string contains a line, not more than 1000 characters, separated by a space between the integer and the operator. No illegal expression.
[] Output format
output value of the expression of the character string corresponding to two decimal place.
Sample input [1]
1 + 2
[output] Sample 1
3.00
[Enter] Sample 2
4 + 2 * 5--7 / 11
[output] Sample 2
13.36
[explain] Sample
1 2 = 3 +
4 + 2 * 5 - 7/11 ≈ 13.36
[title] analysis
we can use the stack to solve the road problems.
First of all, for us, This question is relatively simple, do not use parentheses.
In the case of encounter parentheses, we may use recursion to achieve the effect of "small brackets inside the parentheses first seek" the.
But because there is no, so we only need to deal with "Math" on it,
it can be found, "subtraction" is a class; "multiplication and division" is a class.
So we can do:
we open two stacks: stack2 divided by 2. stack1 and, for storing digital stack1, stack2 divided by 2. for storing the sign (+ or -),
First, we formula placed in stack1 first number,
then every time we deal with a down symbol and a number c a,
If c is the multiplication or division sign, then we only need to remove the top element b stack1, and then the result a * b or a / b in the stack can be placed.
If c is a plus or minus sign, then we must make a judgment stack1 number of elements in this case,
if stack1 is only one element, then we will put a stack1, the c stack2 put in, because for a coming said his next step is likely to be multiplication and division operations,
they still need to be at this time of a stay in the north of the top of the stack the next operation.
If stack1 there are two elements at a time when the sign is plus or minus sign, that is to say, I am new to add to the mix a portion of the beginning, and his ultimate goal is to stack1 in front of these two elements do addition and subtraction.
Then we can first stack1 two elements merge first, then we assume:
start stack1 came out of num1 (num1 is an expression of this element rearward as the stack is "last in, first out"),
came out from stack1 is num2 (num2 this is an expression of the front element),
and stack2 elements are removed, then c,
if c is +, then we will num2 + num1 back into stack1 in;
if c Yes - then we will num2-num1 back into stack1 in.
We have been to traverse the complete expressions in such a way.
At this point there may be an element stack1, or it may also be that there are two elements,
then in the presence of two elements, we can know, these two elements must be of addition and subtraction, multiplication and division number because we will not put stack2, only the minus sign placed in stack2. So we only need to look at the above process can be stack1 the elements into a a.
Then the end result is stack1 the only one remaining in this element.
Note: This question has a place to note is that he is 0 for the end of a separate line even if input is over.
Codes are as follows:

#include <. bits / STDC H ++> 
the using namespace STD; 

Stack <Double> STK1, STK2; 
// for storing digital STK1, STK2 for storing symbols, 
// since STK2 inside the symbol can only be + or -, it represented by + 1, represented by -1 - 

Double A; 
char C; 
String Line, S; 

int main () { 

    the while stk1.pop (); (stk1.empty ()!) 
    the while (stk2.empty ()!) stk2.pop (); 
    getline (CIN, Line); 
    the stringstream SS (Line); 
    SS >> A; 
    stk1.push (A); 
    the while (S SS >> >> A) { 
        C = S [0]; 
        Double B; 
        Switch (C) { 
            Case '*': 
                B = stk1.top (); 
                stk1.pop ();  
                stk1.push (A * B);
                BREAK;
            case '/':
                b = stk1.top();
                stk1.pop();
                stk1.push(b / a);
                break;
            default:
                if (stk1.size() == 2) { // 其实是==2就行
                    double num1 = stk1.top();
                    stk1.pop();
                    double num2 = stk1.top();
                    stk1.pop();
                    double flag = stk2.top();
                    stk2.pop();
                    stk1.push(num2 + flag * num1);
                }
                stk1.push(a);
                stk2.push( c == '+' ? 1 : -1 );
                break;
        }
    }
    if (stk1.size() == 2) {
        double num1 = stk1.top();
        stk1.pop();
        double num2 = stk1.top();
        stk1.pop();
        double flag = stk2.top();
        stk2.pop();
        stk1.push( num2 + flag * num1 );
    }
    double res = stk1.top();
    printf("%.2lf\n", res);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ocac/p/11131682.html