import java.util.ArrayList; import java.util.Stack; public class PostfixExp { public static void main(String[] args) { PostfixExp pExp = new PostfixExp(); //String exp = "a + b*c + (d * e + f) * g"; //string exp = "1+1+3*2*3-4"; //string exp = "2+(1+3)*2*3-4"; //string exp = "2+1+3*2*(3-4)"; String exp = "(13+10/2)*21*(39-4)"; String case1 = "(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4))"; String case2 = "(6*7+9+8/2*12+12/3)+88/11"; String case3 = "(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4)))"; String case4 = "(6*7+9+8/2*12+12/3)+((88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4)))+(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4))))"; String case5 = "(6*7+9+8/2*12+12/3)+((88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4)))+(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4))))+(6*7+9+8/2*12+12/3)+((88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4)))+(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4))))"; String case6 = "(3*(4+9)+8/(2*12)+12/3)+(((7+(13+10/2))*(21*(39-4)+(13+10/2)*21*(39-4)))+(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4))))+(6*7+9+8/2*12+12/3)+((88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4)))+(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4))))"; ArrayList<String> expList = pExp.change2PostfixExp(case6); for(String item : expList) { System.out.print(item + " "); } System.out.println("\n"); System.out.println("My answer is \t\t" + pExp.calculate(expList)); int rightAnswer = (3*(4+9)+8/(2*12)+12/3)+(((7+(13+10/2))*(21*(39-4)+(13+10/2)*21*(39-4)))+(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4))))+(6*7+9+8/2*12+12/3)+((88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4)))+(6*7+9+8/2*12+12/3)+(88/11+(13+10/2)*(21*(39-4)+(13+10/2)*21*(39-4)))); System.out.println("The right answer is \t" + rightAnswer); System.out.println(); } private Stack<String> opStack; private Stack<String> expStack; private String[] ops = {"+", "-", "*", "/", "(", ")", "#"}; public PostfixExp() { opStack = new Stack<String>(); expStack = new Stack<String>(); } private boolean isOp(char c) { String cStr = Character.toString(c); for (String op : ops) { if(cStr.equals(op)) { return true; } } return false; } private boolean isOp(String str) { for(String op : ops) { if(str.equals(op)) { return true; } } return false; } private int getOpPriority(String op){ if("+".equals(op) || "-".equals(op)) { return 1; } else if("*".equals(op) || "/".equals(op)) { return 2; } else if("(".equals(op)) { return 3; } else if("#".equals(op)) { return 0; } return Integer.MAX_VALUE; } public String[] getItems(String str) { ArrayList<String> list = new ArrayList<String>(); // Remove all blank spaces. str = str.replaceAll("\\s", ""); int i = 0; StringBuilder sBuilder = new StringBuilder(); while(i < str.length()) { if(i == str.length() - 1) { sBuilder.append( Character.toString(str.charAt(i))); list.add(sBuilder.toString()); } if(!isOp(str.charAt(i))) { sBuilder.append( Character.toString(str.charAt(i))); } else { if(sBuilder.length() > 0) { list.add(sBuilder.toString()); sBuilder.delete(0, sBuilder.length()); } list.add(Character.toString(str.charAt(i))); } ++i; } list.add("#"); String[] result = new String[list.size()]; return list.toArray(result); } public ArrayList<String> change2PostfixExp(String normalExp) { ArrayList<String> expList = new ArrayList<String>(); String[] items = getItems(normalExp); opStack.clear(); expStack.clear(); opStack.push("#"); int i = 0; while(i < items.length) { //System.out.println(i + ":" + items[i]); if(!isOp(items[i])) { expList.add(items[i]); } else { if(items[i].equals(")")){ while(!opStack.isEmpty() && !opStack.peek().equals("(")) { expList.add(opStack.pop()); } if(!opStack.isEmpty() && opStack.peek().equals("(")) { opStack.pop(); } ++i; continue; } if(items[i].equals("#")) { while(!opStack.isEmpty() && !opStack.peek().equals("#")) { expList.add(opStack.pop()); } break; } if(!opStack.isEmpty()) { while(!opStack.isEmpty() && (getOpPriority(opStack.peek()) >= getOpPriority(items[i]) && !opStack.peek().equals("(")) ) { expList.add(opStack.pop()); } opStack.push(items[i]); } } ++i; } return expList; } public double calculate(ArrayList<String> postfixExp) { Stack<Double> calcStack = new Stack<Double>(); int i = 0; String ite = null; double result = Double.MAX_VALUE; while(i < postfixExp.size()) { ite = postfixExp.get(i); if(!isOp(postfixExp.get(i))) { calcStack.push(new Double( Double.parseDouble( postfixExp.get(i)) )); } else { double num2 = calcStack.peek(); calcStack.pop(); if(!calcStack.isEmpty()) { double num1 = calcStack.peek(); calcStack.pop(); if("*".equals(ite)) { result = num1 * num2; } else if("+".equals(ite)) { result = num1 + num2; } else if("-".equals(ite)) { result = num1 - num2; } else if("/".equals(ite)) { result = num1 / num2; } System.out.println(num1 + "\t" + ite + "\t" + num2 + " = " + result); calcStack.push(result); } } ++i; } return result; } }
相关推荐
使用后缀表达式来实现表达式的计算。使用堆栈,把中缀表达式转换为后缀,支持运算符优先级。
二叉树计算表达式 行之有效的代码,完全可以上交老师那种!
计算表达式的算法,用C++写的,运行过,可以
逻辑表达式算法编译原理,是否汉字,读字符,是否运算符,是否逻辑运算符,指针回溯,跳过空格,跳过注释,外部接口_表达式计算,表达式计算,逻辑判断,函数调用,函数_位或,函数_测试,计算表达式数组,单个计算,输出状态
中缀表达式转化为后缀表达式算法及后缀表达式计算算法.doc.doc
使用树结构编写的表达式算法,程序更快,更加符合算法要求
本工程基于VS2010 C++开发,包含算法和工程。可直接运行使用,或用于项目开发。包含测试用例。
对算术表达式运算进行解析运算,在程序中只要输入类似:65+5*(78+32)-80/4+17 的表达式就可以得到运算结果;
率失真函数是在给定信源和特定失真度量的情况下,在保真度准则下求平均互信息的极小值。本程序给出了两种特殊情况下的率失真函数的计算
实验题目: 基于栈的算术表达式求值算法 实验环境: 学习完了数据结构第三章内容栈和队列 ...程序运行时,输入合法的算术表达式(中间值及最终结果要在0~9之间,可以包括加减乘除和括号),便可输出相应的计算结果。
读取输入字符存入数组中,逐个扫描数组元素遇操作数进栈,遇运算符计算并将结果进栈继续上述过程,直至数组读取完
2. 编写一个程序,对输入的一个算术表达式,检查其括号配对情况并计算表达式值 例如。。(2+2323*(2+4232))
获取键盘输入一个中缀表达式,将它转换成后缀表达式,并输出结果
内容索引:VC/C++源码,字符处理,表达式 VC++表达式解析(计算)工具源代码,由江汉石油学院计算机系的学生所编写。表达式中只能有圆括号、函数名、运算符、常数与变量。一、变量名的命名规则:1、可以是字符、或字符...
一种算法就是算符优先算法,它通过使用两个栈来实现,一个用于暂存操作数,另一个用于暂存操作符。直接计算出表达式结果。第二种算法是先将中缀...实现中缀表达式便后缀表达式并输出然后根据后缀表达式计算表达式!
算法一: 扫一遍表达式求值的基本思路是:用两个栈(一个字符栈、一个数字栈)边扫描边计算,最后在数字栈中...扫两遍表达式求值的基本思路是:先将中缀表达式转化为后缀表达式,再通过计算后缀表达式求表达式的值
简单有理数运算。 输入一个有理数算法,运用波兰表达算法将中缀表达式(一般表达式)转化为波兰表达,在运用递归函数计算波兰表达式,最后得出有理数运算的结果
四川大学计算机学院-数据结构与算法分析高分实验报告-利用后缀表达式计算中缀表达式的值.rar 都是自己非常认真完成的,每一个要点都实现到位,还额外实现了创新内容。 最后得到的分数也很好
构造正则表达式的简化DFA算法论文 介绍了构造等价于给定正则表达式的简化确定有限自动机(DFA) 的算 法. 方法是首先构造与正则表达式等价的非确定有限自动机(NFA) , 这里省略了构 造带E动作的有限自动机的操作, 然后...
标准基因表达式编程算法(GEP)在挖掘知识时采用恒定的变异和交叉率,没有考虑进化中个体适应度的变化,依然存在难以摆脱局部最优和收敛速度问题。为了解决这一问题,提出了将X条件云模型应用到基因表达式编程的算法...