[Explanations] P1449 postfix expression

[Explanations] P1449 postfix expression

Link Title: P1449 postfix
Title Description:
The so-called postfix expression refers to an expression: wherein the sequence is no longer referenced brackets, placed after the operational sign two operands, all calculated as the arithmetic symbol appears strictly new from left to right conduct (regardless of operator precedence).
Such as: 3 (5-2) corresponding to the postfix expression is +7: 3.5.2. - 7 + @. '@' Symbol for the end of the expression. '.' End symbol operand.
Input format: Input: postfix
output format: output: the value of the expression
Input Output Sample:
Input # 1

3.5.2.-*7.+@

Output # 1

16

Analysis:
The expression is a great use of the calculation. According to this question requires postfix expression, we can use the stack \ (O (N) \) values obtained in the postfix expression complexity.
Specific steps postfix expression is given below:
Image 1.png
The following is the postfix expression P1449 Interpretations:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll Stack[1200],x,top;char ch; 
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>ch&&ch!='@'){
        if(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48);
        else if(ch=='.'){Stack[++top]=x;x=0;}
        else if(ch=='+'){
            Stack[top-1]+=Stack[top];
            Stack[top]=0;top--;
        }
        else if(ch=='-'){
            Stack[top-1]-=Stack[top];
            Stack[top]=0;top--; 
        }
        else if(ch=='*'){
            Stack[top-1]*=Stack[top];
            Stack[top]=0;top--; 
        }
        else if(ch=='/'){
            Stack[top-1]/=Stack[top];
            Stack[top]=0;top--; 
        }
    }
    printf("%d\n",Stack[1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/cyanigence-oi/p/11723213.html