`
baby69yy2000
  • 浏览: 182594 次
  • 性别: Icon_minigender_1
  • 来自: 自己输入城市...
社区版块
存档分类
最新评论

Stack练习:: 中缀-后缀表达式

    博客分类:
  • Util
阅读更多
package Stack.Calculate;

import java.util.List;

public class calculation {
	private String expression = null;

	private InfixToPostfix inf = null;

	private PostfixEval pos = null;

	public calculation(String s) {
		expression = s;
		expression += "@";
	}

	public int calculat() {
		int result = 0;
		// 转化成后缀表达式
		inf = new InfixToPostfix(expression);
		List<String> list = inf.toPostfix();

		// 后缀表达式求值
		pos = new PostfixEval(list);
		result = pos.evaluate();
		return result;
	}

	public static void main(String[] args) {
		calculation c = new calculation("(300 + 200) / 100");
		System.out.println(c.calculat()); // 5
	}

}


中缀转化成后缀表达式
package Stack.Calculate;

import java.util.*;

public class InfixToPostfix {

	private String infixExp = null;

	private List<String> postfixExp = new ArrayList<String>();

	private Stack<Character> operStack = new Stack<Character>();

	private StringBuffer numBuf = new StringBuffer();

	private int priority(char op) {
		switch (op) {
		case '+':
		case '-':
			return 1;
		case '*':
		case '/':
			return 2;
		case '(':
		case '@':
			return 0;
		}
		return -1;
	}

	public InfixToPostfix(String s) {
		infixExp = s;
	}

	public List<String> toPostfix() {
		char ch, preChar;
		operStack.push('@');

		for (int i = 0; i < infixExp.length();) {
			ch = infixExp.charAt(i);
			switch (ch) {
			case '+':
			case '-':
			case '*':
			case '/':
				preChar = operStack.peek();
				while (priority(preChar) >= priority(ch)) {
					postfixExp.add("" + preChar);
					operStack.pop();
					preChar = operStack.peek();
				}
				operStack.push(ch);
				i++;
				break;
			case '(':
				operStack.push(ch);
				i++;
				break;
			case ')':
				char c = operStack.pop();
				while (c != '(') {
					postfixExp.add("" + c);
					c = operStack.pop();
				}
				i++;
				break;
			case '@':
				char c1;
				while (!operStack.isEmpty()) {
					c1 = operStack.pop();
					if (c1 != '@')
						postfixExp.add("" + c1);
				}
				i++;
				break;
			case ' ':
			case '\t':
				i++;
				break;
			default:
				if (Character.isDigit(ch)) {
					while (Character.isDigit(ch)) {
						numBuf.append(ch);
						ch = infixExp.charAt(++i);
					}
					postfixExp.add("" + numBuf);
					numBuf = new StringBuffer();
				} else {
					System.err.println("Illegal operator");
				}

			} // switch end~
		}

		return postfixExp;
	} // toPostfix end~

	/*
	 * public static void main(String[] args) { InfixToPostfix in = new
	 * InfixToPostfix("50 + 2 * 3@"); List<String> list = in.toPostfix();
	 * System.out.println(list); }
	 */

}


后缀表达式求值
package Stack.Calculate;

import java.util.*;

public class PostfixEval {
	private Stack<String> operandStack;

	private List<String> list;

	private boolean isOperator(String s) {
		return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
	}

	private int getOperand() {
		/*
		 * if (operandStack.isEmpty()) throw new
		 * ArithmeticException("PostfixEval: Too many operators");
		 */

		return Integer.parseInt(operandStack.pop());
	}

	private int compute(int left, int right, String item) {
		if (item.equals("+"))
			return left + right;
		else if (item.equals("-"))
			return left - right;
		else if (item.equals("*"))
			return left * right;
		else {
			if (right == 0)
				throw new ArithmeticException("PostfixEval: divide by 0");
			return left / right;
		}
	}

	public PostfixEval(List<String> list) {
		this.list = list;
		this.operandStack = new Stack<String>();
	}

	public int evaluate() {
		int left, right, expValue = 0;
		String item;
		for (int i = 0; i < list.size(); i++) {
			item = list.get(i);
			if (isOperator(item)) {
				right = getOperand();
				left = getOperand();
				expValue = compute(left, right, item);
				operandStack.push("" + new Integer(expValue));
			} else {
				operandStack.push(item);
			}
		}

		/*
		 * if (!operandStack.isEmpty()) throw new
		 * ArithmeticException("PostfixEval: Too many operands");
		 */
		return Integer.parseInt(operandStack.pop());
	}

}
分享到:
评论

相关推荐

    Stack练习 中缀-后缀表达式 -

    后缀表达式是相当有用处,转换成后缀表达式后求值会简单很多.那么该如何转换?这里告诉你 !

    自定义栈中缀表达式转换为后缀表达式并求值

    自定义栈,中缀表达式转换为后缀表达式并求值,三个抽象数据类型定义(1.class stack 2.class Middle_expressionToPost_expression 3.class Post_expression_value)

    数据结构(中缀表达式转后缀表达式)

    /*程序由本人编译,并且经过多次测试,正确无误!目前该转换算法只支持数字在0至9之间的+-*/四元运算转换.*/ /**************程序员信息 ***************东北大学*******************东大很厉害**************** ...

    栈和队列的应用实验 利用栈实现中缀表达式与前缀表达式的转换

    后缀表达式 (Postfix Notation),比如前所述的中缀表达式转换为后缀表达式为:"A B C * - D +"。 四、实例 中缀:a+b*c-(d+e) 后缀:((a(bc)* )+ (de)+ )- 把括号去掉:abc*+de+- 前缀:-( +(a *(bc)) +(de)) 把...

    中缀表达式转后缀表达式

    利用之前的栈的顺序存储和栈的链式存储,写了一个就近匹配和中缀表达式转后缀表达式,

    用堆栈实现中缀转后缀表达式计算课程设计

    用堆栈实现中缀表达式转后缀,并计算后缀表达式的结果。

    中缀表达式转换为后缀表达式及逆波兰式求值

    该程序实现了运算表达式转换为中缀表达式、中缀表达式转换为后缀表达式及后缀表达式求值。该程序已实现加减乘除括号运算符及求余、幂指数的求解

    前缀、中缀、后缀表达式

    利用STL中stack,解析前、后缀表达式,并将中缀表达式转换到相应的前、后缀表达式。

    简单计算器 中缀转后缀 求和

    使用stack将中缀表达式转化为后缀表达式然后计算求和

    java栈实现计算器中缀表达式

    java数字栈和符号栈模拟计算器(中缀表达式) “计算中缀表达式”可以称得上是一个特别经典的关于栈的算法...中缀表达式、后缀表达式等概念在这里就不赘述了,让我们直奔主题。 题目:输入一个中缀表达式,计算其结果。

    中缀转后缀

    已知中缀表达式,求后缀表达式,用到了stack。

    中缀表达式转化为后缀表达式

    数据结构课程中中缀转后缀的例子,li yong le stack jie gou

    stack-2&3_栈_数据结构_后缀表达式_Stack2_stack2_

    利用栈结构完成后缀表达式求值及中缀表达式求值。

    中缀表达式转后缀.cpp

    #include #include using namespace std; struct Stack{ char *Data;... Stack * s=(Stack *)malloc(sizeof(Stack)); s-&gt;Data=(char *) malloc(sizeof(char)*Size); s-&gt;top=-1; s-&gt;MaxSize=Size; }

    四则运算表达式的转换并计算结果

    数据结构的一些算法。由中缀表达式转换成后缀表达式然后计算结果,以后如果有需要还可以联系我

    计算器源码 java版

    *数据结构课程设计——计算器 ... 主要思想:1)将中缀表达式转化为后缀表达式 2):根据后缀表达式计算表达式结果 数据结构:堆栈(使用JAVA中util包中Stack类) 列表(使用JAVA中util包中ArrayList类) */

    java计算器源码+文档

    主要思想:1)将中缀表达式转化为后缀表达式 2):根据后缀表达式计算表达式结果 数据结构:堆栈(使用JAVA中util包中Stack类) 列表(使用JAVA中util包中ArrayList类) 界面良好,代码完整可直接运行,文档很好哦!

    2_1逆波兰.c

    //存放后缀表达式 double _stack[50]; //定义栈,用于计算逆波兰式子 int flag[50]; //用于区分+、-号的含义,0表示运算符,1表示正负号 //生成逆波兰式 void NiBolan() { memset(flag,0,sizeof(flag)); //flag...

    逆波兰表达式实现简单计算器功能

    输入一个中缀表达式,转成后缀表达式(逆波兰表达式),使用stack计算结果 要求支持小括号,和多位整数,我们暂时不考虑小数问题 逆波兰表达式书写 逆波兰表达式(Reverse Polish notation, RPN,或逆波兰记法),也...

    第五次实验报告2

    第五次实验报告实验题目通过一个表达式(中缀或后缀形式)构造一棵表达式树,并进行前序遍历、中序遍历输出实验过程通过后缀表达式,使用stack容器辅助实现后缀表达式

Global site tag (gtag.js) - Google Analytics