Given an expression string array, return the Polish notation of this expression. (remove the parentheses)
Aka, convert infix notation to prefix notation.
Example
For the expression [(5 − 6) * 7] (which represented by["(", "5", "−", "6", ")", "*", "7"]
), the corresponding polish notation is [* - 5 6 7] (which the return value should be["*", "−", "5", "6", "7"]
).
Clarification
Definition of Polish Notation:
public List<String> convertToPN(String[] expression) { Stack<String> operand = new Stack<>(); Stack<String> operator = new Stack<>(); for(int i=expression.length-1; i>=0; i--) { String s = expression[i]; if("+-*/".contains(s)) { // note: must be >, not >= while(!operator.isEmpty() && getPriority(operator.peek()) > getPriority(s)) { operand.push(operator.pop()); } operator.push(s); } else if(")".equals(s)) { operator.push(s); } else if("(".equals(s)) { while(!operator.peek().equals(")")) { operand.push(operator.pop()); } operator.pop(); } else { operand.push(s); } } while(!operator.isEmpty()) { operand.push(operator.pop()); } List<String> pn = new ArrayList<>(operand); Collections.reverse(pn); return pn; } private int getPriority(String s) { char c = s.charAt(0); if(c=='+' || c=='-') return 1; if(c=='*' || c=='/') return 2; return 0; }
相关推荐
netflix-infix-0.3.0.jar
notation-infix-postfix-prefix:在无记名的情况下进行转换的程序,可以将输入后的信息,输入后的信息,输入后的信息和输入后的信息进行转换
问题描述 中缀表达式就是我们通常所书写的数学表达式,后缀表达式也称为逆波兰表达式,在编译程序对我们书写的程序中的表达式进行语法检查时,往往就可以通过逆波兰表达式进行。我们所要设计并实现的程序就是将...
要运行演示: ./gradlew buildjava -jar build/libs/lucene-analyzing-infix-suggester-bug.jar异常显示为: Exception in thread "Thread-1" java.lang.RuntimeException: org.apache.lucene.store....
计算器这是我根据VidMob Engineering家庭练习指南开发的计算器。如何使用要求应该安装Python 3.x运行程序将所有文件存储在Calculator.zip中的相同位置运行Calculator.py 工作成功,您可以看到以下内容 请输入数学...
编译原理课程项目作业,java语言。语法规则实现中缀转后缀,且包含批量自动生成中缀表达式。运算符包含+、-、*、/和(),数字含两位小数以及负数的运算。作业成绩94的大作。
infix to postfix using linked list
中缀表达式转换为后缀表达式,并且求取表达式值,而且能够输出表达式
中缀前缀后缀转换器 简单的C#-Win32-应用程序,可将Infix Prefix后缀表示法转换为 用于: Windows Phone 8.1-> Windows 8-> Android(Xamarin):
InfixToPostfix.java 前缀转中缀 java实现
如果输入表达式错误,则返回“WRONG EXPRESSION”。 返回输入的其他明智的中缀。 测试 输入:-- +a-bc/-de+-fgh 输出:-- (a+bc) ((de)/(f-g+h)) 输入:-- *+a-bc/-de+-fghi 输出:-- 错误的表达 输入 :-- *+a-bc...
p5-sub-infix:Perl 5发行子Infix; 请参阅主页以进行下载,问题跟踪器以及更多信息
实现中缀表达式到后缀表达式的转换,为InfixToPostfix类实现convertExpr(String expression),其中参数expression为中缀表达式,其返回值为后缀表达式,最后将其输出到控制台即可。 中缀表达式符合人们平时的表达习惯
k-infix:用于任意表达式的简单但通用的infix解析。 完全可定制。 附带实现的基本数学表达式
infix转POSTFIX算法 infix转POSTFIX算法
the first of them converts a funciton from infix to postfix. the second is derived from the firts, it evaluates the postfix exprecion. the 3rd & 4th draw the function in their canvas. the last one is...
Data Structure_project_ infix to prefix_ infix to postfix
快速评估中缀表达希望您喜欢Swift的这个版本的“解析数学表达式”! 如果您想了解算法,请[LINK COMING SOON]。
#表示法树形图的前缀 该程序显示输入任何等式符号的树形图 用Java Swing API和AWT API制成 特征 自动在gui上绘制图形以显示方程式 Postifx表示法算法的中缀表示法 后缀表示法算法的前缀表示法 ...