Stack and Queue 2 of the Data Structure Experiment of the Stack: Converting General Arithmetic Expressions to Suffixes

Data Structure Experiment Stack and Queue II: Convert general arithmetic expressions to suffix time
Limit: 1000 ms Memory Limit: 65536 KiB
Submit Statistic Discuss
Problem Description
For an arithmetic expression based on binary operators, convert it to the corresponding suffix , and output it.
Input
Inputs an arithmetic expression, terminated by the '#' character.
Output
outputs the suffix expression obtained by converting the expression.
Sample Input
a*b+(cd/e)*f#
Sample Output
ab*cde/-f*+
Hint

Source

#include<stdio.h>
#include<stdlib.h>
int top=0,i;
int main()
{
    char a[1001],stack[1001];
    scanf("%s",a);
    for(i=0;a[i]!='#';i++)
    {
        if(a[i]>='a'&&a[i]<='z')
        {
            printf("%c",a[i]);
        }
        else if(a[i]=='(')
        {
            stack[++top]=a[i];
        }
        else if(a[i]==')')
        {
            while(top!=0&&stack[top]!='(')
            {
                printf("%c",stack[top]);
                top--;
            }
            top--;//使左括号出栈,但是不输出
        }
        else if(a[i]=='+'||a[i]=='-')
        {
            while(top!=0&&stack[top]!='(')
            {
                printf("%c",stack[top]);
                top--;
            }
            stack[++top]=a[i];
        }
        else if(a[i]=='*'||a[i]=='/')
        {
            while(top!=0&&stack[top]!='('&&(stack[top]=='*'||stack[top]=='/'))
            {
                printf("%c",stack[top]);
                top--;
            }
            stack[++top]=a[i];
        }
    }
    while(top!=0)
    {
        printf("%c",stack[top]);
        top--;
    }
    return  0;
}

THINK:
1. When a letter is encountered, it is output directly.
2. When a left bracket is encountered, it is directly pushed onto
the stack . Pop the stack (TOP–)
4. When encountering addition and subtraction, push the output before the left parenthesis, and then the current operator onto the stack (because the output of the stack with a higher priority than its own, and then push it into the stack, The left parenthesis has the lowest priority, so these operators are all pushed to the stack when they encounter the left parenthesis. The priority of multiplication and division is greater than that of addition and subtraction. Therefore, when addition and subtraction encounters multiplication and division in the stack, the multiplication and division must be output. If addition and subtraction encounters in the stack The addition and subtraction is also the output of addition and subtraction in the stack) So if the current operator is addition and subtraction, it is equivalent to outputting all the output before the left parenthesis, and then pushes it into the stack
5. When multiplication and division are encountered, the same as the left of 4 Parentheses, just output the multiplication and division in the stack when encountering multiplication and division, and then push it into the stack
6. The general idea is to see when the output is good, and write the output constraints.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324690782&siteId=291194637