Infix : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2).
Example : (A+B) * (C-D)

Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )

Given an Infix expression, convert it into a Prefix expression using two stacks.

Examples:

Input : A * B + C / D
Output : + * A B/ C D Input : (A - B/C) * (A/K-L)
Output : *-A/BC-/AKL
分析:
  1. Traverse the infix expression and check if given character is an operator or an operand.
  2. If it is an operand, then push it into operand stack.
  3. If it is an operator, then check if priority of current operator is greater than or less than or equal to the operator at top of the stack. If priority is greater, then push operator into operator stack. Otherwise pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until priority of current operator is less than or equal to operator at top of the operator stack.
  4. If current character is ‘(‘, then push it into operator stack.
  5. If current character is ‘)’, then check if top of operator stack is opening bracket or not. If not pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until top of operator stack is an opening bracket.
  6. The final prefix expression is present at top of operand stack.
 class Solution {
boolean isOperator(char C) {
return C == '-' || C == '+' || C == '*' || C == '/' || C == '^';
} int getPriority(char C) {
if (C == '-' || C == '+') {
return ;
} else if (C == '*' || C == '/') {
return ;
} else if (C == '^') {
return ;
} else {
return ;
}
}
String infixToPrefix(String infix) {
Stack<Character> operators = new Stack<>();
Stack<String> operands = new Stack<String>(); for (int i = ; i < infix.length(); i++) {
if (infix.charAt(i) == '(') {
operators.push(infix.charAt(i));
}
else if (infix.charAt(i) == ')') {
while (!operators.empty() && operators.peek() != '(') {
String op1 = operands.pop();
String op2 = operands.pop(); char op = operators.pop();
String tmp = op + op2 + op1;
operands.push(tmp);
}
} else if (!isOperator(infix.charAt(i))) {
operands.push(infix.charAt(i) + "");
}
else {
while (!operators.empty() && getPriority(infix.charAt(i)) <= getPriority(operators.peek())) {
String op1 = operands.pop();
String op2 = operands.pop(); char op = operators.pop();
operands.push(op + op2 + op1);
}
operators.push(infix.charAt(i));
}
} while (!operators.empty()) {
String op1 = operands.pop();
String op2 = operands.pop(); char op = operators.pop();
operands.push(op + op2 + op1);
}
return operands.peek();
}
}

Infix to Prefix conversion using two stacks的更多相关文章

  1. Postfix to Prefix Conversion & Prefix to Postfix Conversion

    Postfix to Prefix Conversion Postfix: An expression is called the postfix expression if the operator ...

  2. Infix to postfix conversion 中缀表达式转换为后缀表达式

    Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...

  3. Swift声明参考

    一条声明可以在你的程序里引入新的名字和构造.举例来说,你可以使用声明来引入函数和方法,变量和常量,或者来定义 新的命名好的枚举,结构,类和协议类型.你也可以使用一条声明来延长一个已经存在的命名好的类型 ...

  4. Swift5 语言参考(六) 声明

    一个声明引入了一个新的名称或构建到你的程序.例如,您使用声明来引入函数和方法,引入变量和常量,以及定义枚举,结构,类和协议类型.您还可以使用声明来扩展现有命名类型的行为,并将符号导入到其他地方声明的程 ...

  5. a note of R software write Function

    Functionals “To become significantly more reliable, code must become more transparent. In particular ...

  6. SpringBoot @ConfigurationProperties详解

    文章目录 简介 添加依赖关系 一个简单的例子 属性嵌套 @ConfigurationProperties和@Bean 属性验证 属性转换 自定义Converter SpringBoot @Config ...

  7. React 17 要来了,非常特别的一版

    写在前面 React 最近发布了v17.0.0-rc.0,距上一个大版本v16.0(发布于 2017/9/27)已经过去近 3 年了 与新特性云集的 React 16及先前的大版本相比,React 1 ...

  8. Prefix to Infix Conversion

    Infix : An expression is called the Infix expression if the operator appears in between the operands ...

  9. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

随机推荐

  1. 04 JQuery的使用

    01 对网站首页优化--定时弹出广告 <!-- 作者:offline 时间:2018-09-09 描述:在使用JQ前要导入jquery-1.11.0.min.js包 注意区分js和jq的对象 - ...

  2. PHP mysqli_query() 函数

    PHP mysqli_query() 函数 定义和用法 mysqli_query() 函数执行某个针对数据库的查询. mysqli_query(connection,query,resultmode) ...

  3. 收藏一个RMQ模板

    int a[1100]; int dp[maxn][20]; void rmq_init(){ for(int i=0;i<n;i++) dp[i][0]=a[i]; for(int j=1;( ...

  4. Codeforces Round #580 (Div. 2)

    这次比上次多A了一道,但做得太慢,rating还是降了. Problem A Choose Two Numbers 题意:给出两个集合A,B,从A,B中分别选出元素a,b使得a+b既不属于集合A,又不 ...

  5. codeforces#1163C2. Power Transmission (Hard Edition)(计算几何)

    题目链接: https://codeforces.com/contest/1163/problem/C2 题意: 给出$n$个点,任意两点连接一条直线,求相交直线的对数 数据范围: $1 \le n ...

  6. CodeForces 519D A and B and Interesting Substrings ——(奥义字符串)

    题意:给出26个字母每个字母的价值,问字符串中有多少个满足以下条件的子串: 1.子串的第一个和最后一个相同 2.子串除了头和尾的其他字符的价值加起来和尾0 这题普通方法应该是O(n^2),但是在1e5 ...

  7. Linux文件的加压缩解压缩tar命令

    linux下使用tar命令   解压 语法:tar [主选项+辅选项] 文件或者目录 使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用.主选项:c 创建新的档案 ...

  8. CDN之简介

    1. 什么是 CDN? 来自 <什么是 CDN?> CDN(内容交付网络)是一种高度分布式服务器平台,为交付 Web 应用程序.流媒体等内容专门优化.服务器网络分布于众多物理和网络位置,对 ...

  9. mybatis sql不等于

    转载地址:    https://blog.csdn.net/weixin_40797576/article/details/78796028 select * from test where id& ...

  10. mockjs从入门到精通视频教程

    Mock.js 是一款模拟数据生成器,旨在帮助前端攻城师独立于后端进行开发,帮助编写单元测试.提供了以下模拟功能: (1)根据数据模板生成模拟数据 (2)模拟 Ajax 请求,生成并返回模拟数据 (3 ...