本文共 3963 字,大约阅读时间需要 13 分钟。
1.将中缀表达式转化为逆波兰式的思路,如将1+2转化为12+: 定义一个stringbuffer来存放后缀式,并定义一栈stack - loop:读取读取输入的字符串,将当前读入的字符记为a:
- step1:如果a为操作数则直接append到stringbuffer中.
- step2:如果a为左括号“(”则将a进栈
- step3:如果a为右括号,则从栈顶开始找,并移动栈顶指针,如果当前栈顶元素非左括号则将栈顶元素append到stringbuffer中,直到找到第一个左括号,找到后将左括号出栈,并注意不要将左括号放到stringbuffer中.
- step4:如果a为运算符,则取栈顶元素和a进行比较如果栈顶元素的运算符优先级小于a则将a入栈,否则将栈顶弹出存到stringbuffer中,直到栈顶元素的运算符优先级小于a为址,注左括号和右括号的运算优先级比+-*/都低在这里
- d loop;
最后将stringbuffer进行toString并拼上stack中的元素就得到后缀表达式 2.计算逆波兰式的思路 -
- 定义一栈 stack:
- 输入一段后缀表达式如12+:
- loop:逐个读取字符串中的字符记为b:
- 1.如果a为操作数,则入栈
- 2.如果a为操作符,则弹出栈顶最前进两个数用运算符a进行计算,并将计算结果入栈
- end loop;
3,最后将stack的栈元素输入则为运算结果 贴上代码: 首先定义一个简单栈,所有代码中都没有进行异常判断,考虑的是实现过程中,各位看官如果觉得代码垃圾请轻拍: 栈: - / 自定义栈
- class MyStack<T> {
- private Entry<T> top = new Entry<T>(null, null);
- private int size;
-
- public MyStack() {
- top.next = null;
- top.element = null;
- }
-
-
- public MyStack<T> push(T t) {
- Entry<T> node = new Entry<T>(top.next, t);
- top.next = node;
- size++;
- return this;
- }
-
-
- public T pop() {
- if (size == 0) {
- return null;
- }
- Entry<T> t = top.next;
- Entry<T> foo = top.next.next;
- top.next = foo;
- foo = null;
- size--;
- return t.element;
- }
-
- public T top() {
- return top.next.element;
- }
-
- public int size() {
- return size;
- }
- }
-
-
- class Entry<T> {
- T element;
- Entry<T> next;
-
- Entry(Entry<T> next, T e) {
- this.element = e;
- this.next = next;
- }
- }
//算法实现类Rpn -
-
- public class Rpn {
-
-
-
- public int countRpn(String src) {
- MyStack<String> stack = new MyStack<String>();
- for (int i = 0; i < src.length(); i++) {
- String foo = src.charAt(i) + "";
- if (!isOperator(foo)) {
- stack.push(foo);
- } else {
-
- String a = stack.pop();
- String b = stack.pop();
- int temp = count(b, a, foo);
- stack.push(temp + "");
-
- }
- }
- return Integer.parseInt(stack.pop());
- }
-
-
- public boolean isOperator(String e) {
- if (null == e || "".equals(e)) {
- return false;
- }
- return "+".equals(e) || "*".equals(e) || "-".equals(e) || "/".equals(e);
- }
-
-
- public boolean isLeft(String s) {
- return "(".equals(s);
- }
-
- public boolean isRight(String s) {
- return ")".equals(s);
- }
-
-
- public int count(String a, String b, String e) {
- int temp1 = Integer.parseInt(a);
- int temp2 = Integer.parseInt(b);
- if ("+".equals(e)) {
- return temp1 + temp2;
- } else if ("-".equals(e)) {
- return temp1 - temp2;
- } else if ("*".equals(e)) {
- return temp1 * temp2;
- } else {
- return temp1 / temp2;
- }
- }
-
-
- public String reverse2Rpn(String src) {
- MyStack<String> stack = new MyStack<String>();
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < src.length(); i++) {
- String temp = src.charAt(i) + "";
- if (!isOperator(temp) && !isRight(temp) && !isLeft(temp)) {
-
- sb.append(temp);
- } else if (isOperator(temp)) {
-
- if (stack.size() == 0) {
- stack.push(temp);
- } else if ("+".equals(temp) || "-".equals(temp)) {
- if (isLeft(stack.top())) {
- stack.push(temp);
- } else {
- String top = stack.top();
- boolean next = true;
- while (("*".equals(top) || "/".equals(top)) && next) {
- top = stack.pop();
- sb.append(top);
- next = stack.size() == 0 ? false : true;
- }
- stack.push(temp);
- }
-
-
- } else {
- stack.push(temp);
-
- }
- } else if (isLeft(temp)) {
- stack.push(temp);
-
- } else if (isRight(temp)) {
- String top = stack.pop();
- boolean next = true;
- while (!isLeft(top) && next) {
- sb.append(top);
- top = stack.pop();
- next = stack.size() == 0 ? false : true;
- }
- }
- }
- while (stack.size() > 0) {
- sb.append(stack.pop());
- }
- return sb.toString();
- }
-
- public static void main(String[] args) {
- Rpn rpn = new Rpn();
- String src = rpn.reverse2Rpn("((1+2)*2)*4)+9*(1+2)");
- System.out.println("后缀表达式为:"+src);
- System.out.println("计算结果:"+rpn.countRpn(src));
- }
-
- }
转载地址:http://rvuni.baihongyu.com/