zoukankan      html  css  js  c++  java
  • 中缀表达式转后缀表达式

    中缀表达式

    后缀表达式(逆波兰表示法)

    中缀->后缀:借助辅助栈,遇到数字或字符直接打印,遇到符号与栈顶元素优先级比较,符号优先级高,则直接入栈。若当前符号优先级低,则依次出栈,直到栈顶元素比当前元素优先级低为止。

    遇到“( ”无条件入栈,遇到“ )”将栈内元素依次出栈,知道出到左括号为止。

    中缀->后缀:所有中缀表达式括起来,将符号拿到所在符号后面(对应的为栈顶元素大于等于当前元素时,弹出)

    后缀->前缀:遇到数字或字符直接入栈,遇到符号将栈顶元素后一个与栈顶构成表达式。

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string.h>
    #include<vector>
    #include <stack>
    using namespace std;
    int Priority(char a,char b)
    {
        if((a == '*' || a == '/') )
           return 1;
        else return 0;
    }
    int main()
    {
        stack<char> sta;
        char s[50];
        char ans[50];//装结果的数组
    
        while(scanf("%s",s) != EOF)
        {
            int i = 0;
            int j = 0;//控制答案数组的
            int k = 0;//是否输出空格
            for(i;s[i];i++)
            {
                if(s[i] == '(')//左括号直接压入
                    sta.push(s[i]);
                else if(s[i] == ')')//右括号
                {
                    if(k == 1)
                    {
                        ans[j++] = ' ';
                        k = 0;
                    }
                    while(sta.top() != '(')//弹出栈中左括号之前的符号
                    {
                        ans[j++] = sta.top();
                        ans[j++] = ' ';
                        sta.pop();
                    }
                    sta.pop();//弹出左括号
                }
                 else if((s[i] >= '1' && s[i] <= '9') || s[i] == '.')
                {
                    ans[j++] = s[i];
                    k = 1;
                }
                else//符号位+-*/
                {
                    if(k == 1)
                    {
                        ans[j++] = ' ';
                        k = 0;
                    }
                    if((s[i] == '-' || s[i] == '+') && i == 0)//第一个是正负号
                    {
                        if(s[i] == '-')
                            ans[j++] =  '-';
                    }
                    else if((s[i] == '-' || s[i] == '+') && i != 0 && s[i-1] == '(' )//有负数情况
                    {
                        if(s[i] == '-')
                           ans[j++] = s[i] ;
                        sta.pop();//删除左括号
                        while(s[++i] != ')')
                        {
                            ans[j++] = s[i];
                        }
                        ans[j++] = ' ';
                    }
                    else
                    {
                        //弹出所有优先级大于该运算符的栈顶元素,然后将该运算符入栈
                        while(!sta.empty() && Priority(sta.top(),s[i]) )
                        {
                            ans[j++] = sta.top();
                            ans[j++] = ' ';
                            sta.pop();
                        }
                        sta.push(s[i]);//优先级低,直接放入
                    }
                }
            }
            if(k == 1)
            {
                ans[j++] = ' ';
                k = 0;
            }
            while(!sta.empty())//弹出栈中剩余元素
            {
                ans[j++] = sta.top();
                ans[j++] = ' ';
                sta.pop();
            }
            ans[j-1]=0;
            puts(ans);
        }
    }
  • 相关阅读:
    关于权限控制
    关于<!DOCTYPE>
    Oracle恢复目录的管理使用简要
    绑定变量介绍
    重做日志时间戳说明
    UNDO表空间监控说明
    Oracle rac进阶管理专家指导系列文档
    延迟块清除介绍
    ORA12500内存耗尽一例
    undo自动调优介绍
  • 原文地址:https://www.cnblogs.com/Lune-Qiu/p/9096335.html
Copyright © 2011-2022 走看看