Stack entry - simple calculator template - infix turn suffix

Topic Link

Postfix expression is also called Reverse Polish Notation, free of brackets on the back of the operator involved in computing two syntax component.

Postfix expression evaluation calculation

Sequential scanning from left to right postfix expression. The last digit of the stack is the answer.

(1) If the number is pushed onto the stack.

(2) If the operator to pop two numbers from the stack for operation, the operation result onto the stack.

Postfix infix expression turn

Scan from left to right infix expression.

(1) When the digital input is directly output to the subsequent expression of the sequence.

(2) When faced with an open bracket, its stack.

(3) When faced with closing parenthesis, first determine whether the stack is empty, if it is empty, the brackets do not match, an exception report and exit. If not empty, then pop the stack elements sequentially, hard against the first open parenthesis (brackets also will open the pop-up). The pop-up element is output to the postfix expression sequence. If you do not encounter open parenthesis, the report exception and exit.

(4) when the input is the operator (four operations + - * / one)

  • Cycle, when the (non-empty stack, the stack is not open bracket && && stack operator precedence is not lower than the priority of operator input) , the operation is repeated to pop the top element, into postfix expression sequence.
  • The operator input pressure onto the stack.

(5) infix all scanned, clear the stack, all pop into postfix sequence. If the pop-up element has open parenthesis, report anomalies and exit.

Templates - postfix expression turn infix expression

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;

int priority(char c)
{
    if (c == '+' || c == '-')return 0;
    else if (c == '*' || c == '/')return 1;
}

int main()
{
    char in[205];
    char post[205] = { 0 };
    stack<char> s;
    scanf("%s", in);
    int l = strlen(in);
    int size = 0;
    for (int i = 0; i < l; i++)
    {
        if (in[i] >= '0'&&in[i] <= '9')post[size++] = in[i];
        else if (in[i] == '(')s.push(in[i]);
        else if (in[i] == ')')
        {
            if (s.empty())
            {
                printf("Wrong!\n");
                return 0;
            }
            while (s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            if (s.top() != '(')printf("Wrong!/n");
            else s.pop();//弹出开括号
        }
        else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-')
        {
            while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            s.push(in[i]);
        }
    }
    if (!s.empty())
    {
        post[size++] = s.top();
        s.pop();
    }
    printf("%s\n", post);
    system("pause");
    return 0;
}

The idea is simple calculator solution to a problem

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;

int priority(char c)
{
    if (c == '+' || c == '-')return 0;
    else if (c == '*' || c == '/')return 1;
}

int main()
{
    char in[205];
    char post[205] = { 0 };
    stack<char> s;
    scanf("%s", in);
    int l = strlen(in);
    int size = 0;
    for (int i = 0; i < l; i++)
    {
        if (in[i] >= '0'&&in[i] <= '9')post[size++] = in[i];
        else if (in[i] == '(')s.push(in[i]);
        else if (in[i] == ')')
        {
            if (s.empty())
            {
                printf("Wrong!\n");
                return 0;
            }
            while (s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            if (s.top() != '(')printf("Wrong!/n");
            else s.pop();//弹出开括号
        }
        else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-')
        {
            while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            s.push(in[i]);
        }
    }
    if (!s.empty())
    {
        post[size++] = s.top();
        s.pop();
    }
    stack<int> ans;
    int len = strlen(post);
    for (int i = 0; i < len; i++)
    {
        if (post[i] >= '0'&&post[i] <= '9')ans.push(post[i] - '0');
        else
        {
            int b = ans.top(); ans.pop();
            int a = ans.top(); ans.pop();
            int c;
            if (post[i] == '+')c = a + b;
            else if (post[i] == '-')c = a - b;
            else if (post[i] == '*')c = a * b;
            else if (post[i] == '/')c = a / b;
            ans.push(c);
        }
    }
    printf("%d\n", ans.top());
    system("pause");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/yun-an/p/11357212.html