Java数据结构——中序表达式转后续表达式实现

来源:互联网 发布:邱一平麟龙软件破解版 编辑:程序博客网 时间:2024/05/17 22:48

本来是想找java实现这个的方法,结果昨天晚上找了很久一直找不到java实现的源码(正常并且能看懂的)

嗯这只能怪我没有科学上网微笑

于是想自己写一个

于是有了悲剧の一天

从早上8点码到这个时候期间bug无数错误无数

我现在没有什么心情BBBB了代码就在下面看不懂私聊问我。

是用栈实现的

测试用例:

输入:1 + 2 * 3 + ( 4 * 5 + 6 ) * 7 = 

输出: 1 2 3 * + 4 5 * 6 + 7 * +

中序转为后序

package 第三章_表栈和队列;import java.util.Scanner;/** * 实现功能: 输入一个表达式(包含数字+-*除括号) a + b * c + ( d * e + f ) * g 计算出表达式的结果 *  * @author nangua */public class Stack_test {private static Scanner sc;// 运算符栈顶元素private static Operator topOperator = new Operator();public static void main(String[] args) {System.out.println("请输入表达式(以空格分隔,例如1 + 2 * ( 1 + 2 ) ):");sc = new Scanner(System.in);String input = sc.nextLine();// 转化为后缀表达式Scalculation1(input);}/** * 中缀表达式转化为后缀表达式 *  * @param input */private static void Scalculation1(String input) {// 得到输入的表达式并储存在数组中String[] expression = input.split(" ");@SuppressWarnings("rawtypes")MyStack<?> outputStack = new MyStack(); // 输出栈@SuppressWarnings("rawtypes")MyStack<?> operatorStack = new MyStack(); // 运算符栈// 储存(的位置int leftBracketsPosition = -1;// 运算符栈元素位置(从底往上依次为0,1,2...)int osCount = -1;// 储存栈顶以下的一个元素System.out.println("输入的表达式长度" + expression.length);// 中缀转换为后缀for (int i = 0; i < expression.length; i++) {// 区分是数字还是运算符// 如果是数字try {Integer t = Integer.valueOf(expression[i]);outputStack.push(t); // 如果是数字则推入输出栈// 如果是运算符} catch (Exception e) {// 储存运算符的OperatorOperator storageOperator = new Operator();// 如果是运算符则区分运算符优先级然后推入运算符栈switch (expression[i]) {case "*":storageOperator.setValue("*");break;case "/":storageOperator.setValue("/");break;case "+":storageOperator.setValue("+");break;case "-":storageOperator.setValue("-");break;case "(":storageOperator.setValue("(");// 记录(的位置break;case ")":storageOperator.setValue(")");break;case "=":storageOperator.setValue("=");}// 如果第二个元素不为空if (osCount != -1) {if (storageOperator.value.endsWith("=")) {while (osCount != -1) {outputStack.push(operatorStack.pop());osCount--;}} else if (storageOperator.value.endsWith("(")) {// 推入运算符栈operatorStack.push(storageOperator);osCount++;leftBracketsPosition = osCount;topOperator = (Operator) operatorStack.top();} else if (storageOperator.value.endsWith(")")) {System.out.println("qqqqqqqqqqqqq: " + osCount);while ((osCount != leftBracketsPosition) && (osCount >= 1)) {outputStack.push(operatorStack.pop());osCount--;}// 再把(号踢出去operatorStack.pop();osCount--;if (osCount != -1) {topOperator = (Operator) operatorStack.top();}} else if ((topOperator.priority >= storageOperator.priority)) {while (((topOperator.priority >= storageOperator.priority))) {if (topOperator.value.equals("(")) {// 推入运算符栈operatorStack.push(storageOperator);osCount++;topOperator = (Operator) operatorStack.top();break;} else {if (osCount == -1) { // 如果已经没有元素// 推入运算符栈operatorStack.push(storageOperator);osCount++;topOperator = (Operator) operatorStack.top();break;} else {// 将栈顶元素推入输出栈outputStack.push(topOperator);// 在operator中踢出已经推入输出栈的栈顶并设置新的栈顶operatorStack.pop();osCount--;if (osCount!=-1) {topOperator = (Operator) operatorStack.top();} else {// 推入运算符栈operatorStack.push(storageOperator);osCount++;topOperator = (Operator) operatorStack.top();break;}}}} } else if ((topOperator.priority <= storageOperator.priority)) {// 推入运算符栈operatorStack.push(storageOperator);osCount++;topOperator = (Operator) operatorStack.top();}} else {// 第一次推入运算符栈operatorStack.push(storageOperator);osCount++;topOperator = (Operator) operatorStack.top();}}}System.out.println("******************结果*********************");System.out.println("打印输出栈内所有元素:");outputStack.show();System.out.println();System.out.println("输出栈长度:" + outputStack.count);}/** * 数组实现的简单栈 *  * @author nangua */static class MyStack<T> {MyStack() {}Object[] array = new Object[1];;// 数组元素个数int count = 0;// 栈顶元素Object topOfStack = "-1";private void show() {Object o = new Object();for (int i = 0; i < array.length; i++) {o = array[i];if (o instanceof Operator) {Operator ceshi = (Operator) o;if ((!ceshi.value.equals("(")) && (!ceshi.value.equals(")"))) {System.out.print(" " + ceshi.value);}} else {System.out.print(" " + o);}}}/** * push进栈 *  * @param num */private void push(Object num) {// 如果数组为空if (count == 0) {array[count] = num;} else {// 如果数组不为空,扩充数组Object[] newarray = new Object[count + 1];// 复制数组for (int i = 0; i < array.length; i++) {newarray[i] = array[i];}array = newarray;array[count] = num;}count++;topOfStack = num;}/** * 找出栈顶的下一个元素 */private Object next() {// 如果总元素大于或等于2个if (count - 1 >= 0) {return array[count - 1];}return "无元素可next";}/** * 出栈 *  * @return */private Object pop() {// 如果数组为空if (count == 0) {try {throw new Exception("栈为空时不能pop");} catch (Exception e) {e.printStackTrace();}return "无元素可pop";} else {if (count == 1) {topOfStack = array[0];count = 0;} else {topOfStack = array[count - 1];// 缩小数组Object[] newarray = new Object[count - 1];for (int j = 0; j < newarray.length; j++) {newarray[j] = array[j];}array = newarray;count--;}return topOfStack;}}/** * 显示栈顶元素 */private Object top() {// 如果数组为空if (count == 0) {try {throw new Exception("栈为空时不能top");} catch (Exception e) {e.printStackTrace();}return "无元素可top";} else {if (count == 1) {topOfStack = array[0];} else {topOfStack = array[count - 1];}return topOfStack;}}/** * 测试方法:打印栈内数组 过滤掉了()左右括号 */}/** * 运算符类,储存优先级和运算符 *  * @author nangua */static class Operator {String value;// 优先级默认为-1int priority = -1;public Operator() {}public String getValue() {return value;}public void setValue(String value) {this.value = value;if (value.equals("+") || (value.equals("-"))) {this.priority = 1;} else if (value.equals("*") || (value.equals("/"))) {this.priority = 2;} else if (value.equals("(") || (value.equals(")"))) {this.priority = 3;} else if (value.equals("=")) {this.priority = 0;}}public int getPriority() {return priority;}public void setPriority(int priority) {this.priority = priority;}}}


3 0
原创粉丝点击