中缀变后缀

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

int  pri( char  optr)
{
    
switch (optr)
    
{
       
case '='return 0;
       
case '^'return 3;
       
case '*'return 2;
       
case '/'return 2;
       
case '%'return 2;
       
case '+'return 1;
       
case '-'return 1
       
defaultreturn 0;
    }
 
}


int  main()
{
   stack
<char>s;
   s.push(
'#');
   
char ch;
   
char top;
   cout
<<"请输入中缀表达式以'#'结束:";
   
while(cin.get(ch) && ch != '#')
   
{
        
/*
        **如果是数字或者字符直接输出
        
*/

        
if(isdigit(ch)||isalpha(ch))
            cout
<<ch;
        
/*
        **如果是空格就跳过
        
*/

        
else if( isspace(ch) )
            ;
        
        
else
        
{
            
/*
            **如果是'('直接压入栈
            
*/

            
if( ch == '(')
                s.push(ch);
            
/*
            **如果是')',那么将栈元素弹出,将符号写出直到遇到一个对应的
            **左括号,但是这个左括号只被弹出并不输出
            
*/

            
else if( ch == ')')
            
{
                top 
= s.top();
                s.pop();
                
while( top != '(' && !s.empty())
                
{
                    cout
<<top;
                    top 
= s.top();
                    s.pop();
                }

            }

            
/*
            **如果见到任何其他的符号("+","*"),那么从栈中弹出栈元素直到发现
            **优先级更低的元素为止。
            
*/

            
else
            
{
                top 
= s.top();
                s.pop();
                
while(pri(top)>=pri(ch)&&top != '(')
                
{
                   cout
<<top;
                   top
=s.top();
                   s.pop();
                }

                s.push(top);
                s.push(ch);
            }

        }

   }

   top 
= s.top();
   s.pop();
   
while(!s.empty() && top != '#')
   
{
       cout
<<top;
       top
=s.top();
       s.pop();
       
   }

   cout
<<endl;
   
return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个Python编写的中缀表达式转化为后缀表达式的程序: ```python def infix_to_postfix(expression): precedence = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3} stack = [] postfix = [] for char in expression: if char.isalpha() or char.isdigit(): postfix.append(char) elif char == '(': stack.append(char) elif char == ')': top = stack.pop() while top != '(': postfix.append(top) top = stack.pop() else: while len(stack) != 0 and stack[-1] != '(' and precedence[stack[-1]] >= precedence[char]: postfix.append(stack.pop()) stack.append(char) while len(stack) != 0: postfix.append(stack.pop()) return ''.join(postfix) print(infix_to_postfix('a+b*c-d/e')) # abc*+de/- ``` 程序首先定义了运算符的优先级,使用一个字典来存储。然后定义了一个栈来存储运算符。程序按照算法依次扫描中缀表达式中的每个字符,如果是字母或数字,直接追加到后缀表达式中;如果是左括号,则将其压入栈中;如果是右括号,则将栈顶元素出栈并追加到后缀表达式中,直到遇到左括号;如果是运算符,则将其与栈顶运算符比较,如果栈顶运算符优先级大于或等于当前运算符,则将栈顶运算符出栈并追加到后缀表达式中,重复此操作直到栈顶元素优先级小于当前运算符或栈为空,然后将当前运算符压入栈中。最后,将栈中剩余运算符全部弹出,并追加到后缀表达式中。最后将后缀表达式中的字符组合成字符串并返回。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值