• 中缀转换为后缀和前缀


    中缀转换为后缀:顺序建立,如果不是运算符,直接输出。如果是括号的话,先入左括号,然后等到右括号的时候,将这一段全部输出。然后再就是运算优先级的问题了,每一次插入,都需要保证要插入的运算符小于栈顶的运算符。

    中缀转化为前缀:通过两个栈实现,逆序建立,如果当前是字符的话,先放入第一个栈里面。入股如果是括号,将这两个括号里面的栈2的放入栈1里面。然后再就是运算符优先级的问题了,当当前的字符是加或者减的时候,栈2的顶大于等于当前的运算符的时候停止。然后将栈2里面得栈顶放入栈1中,当前的字符加入栈2.最后将栈2里面的字符全部放入栈1.

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 # define ll long long
     4 # define inf 0x3f3f3f3f
     5 const int maxn = 2e5+100;
     6 char Stack[maxn];
     7 char z1[maxn],z2[maxn];
     8 int top;
     9 string str,str1,str2,str3;
    10 int main()
    11 {
    12     cin>>str;
    13     int len=str.size();
    14     for(int i=0; str[i]!='#'; i++)
    15     {
    16         if(str[i]>='a'&&str[i]<='z')
    17             str1+=str[i];
    18         else if(str[i]=='(')
    19         {
    20             Stack[++top]=str[i];
    21         }
    22         else if(str[i]==')')
    23         {
    24             while(top&&Stack[top]!='(')
    25             {
    26                 str1+=Stack[top];
    27                 top--;
    28             }
    29             top--;
    30         }
    31         else if(str[i]=='+'||str[i]=='-')
    32         {
    33             while(top&&Stack[top]!='(')
    34             {
    35                 str1+=Stack[top];
    36                 top--;
    37             }
    38             Stack[++top]=str[i];
    39         }
    40         else if(str[i]=='*'||str[i]=='/')
    41         {
    42             while(top&&Stack[top]!='('&&(Stack[top]=='*'||Stack[top]=='/'))
    43             {
    44                 str1+=Stack[top];
    45                 top--;
    46             }
    47             Stack[++top]=str[i];
    48         }
    49     }
    50     while(top)
    51     {
    52         str1+=Stack[top];
    53         top--;
    54     }
    55     int top1=0,top2=0;
    56     for(int i=len-2; i>=0; i--)
    57     {
    58         if(str[i]>='a'&&str[i]<='z')
    59         {
    60             z1[++top1]=str[i];
    61         }
    62         else if((str[i]=='+'||str[i]=='-')&&(z2[top2]=='*'||z2[top2]=='/'))
    63         {
    64             z1[++top1]=z2[top2];
    65             z2[top2]=str[i];
    66         }
    67         else if(str[i]=='(')
    68         {
    69             while(z2[top2]!=')')
    70             {
    71                 z1[++top1]=z2[top2--];
    72             }
    73             top2--;
    74         }
    75         else
    76         {
    77             z2[++top2]=str[i];
    78         }
    79     }
    80     while(top2)
    81     {
    82         z1[++top1]=z2[top2--];
    83     }
    84     while(top1)
    85     {
    86         str2+=z1[top1];
    87         top1--;
    88     }
    89     for(int i=0; str[i]!='#'; i++)
    90     {
    91         if(str[i]=='('||str[i]==')')
    92             continue;
    93         str3+=str[i];
    94     }
    95     cout<<str2<<endl<<str3<<endl<<str1<<endl;
    96 }
  • 相关阅读:
    定时器准确定时八位时钟
    数码管八位显示时钟
    串口通讯
    定时器/计数器0(定时器)
    定时器/计数器0之计数器
    定时器/计数器0(计数器)
    外部中断0
    LCD带字符液晶显示I LOVE YOU
    Altium Designer Summer 09换成中文步骤
    BAT小米奇虎美团迅雷携程等等各大企业校招,笔试面试题。
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10601415.html
Copyright © 2020-2023  润新知