后缀表达式字符串计算 --只对个位数计算及 1* 2 -3+6/2
2012-03-29 11:27
295 查看
Java数据结构和算法中文第二版.pdf 代码
StackX.java package com.ch4.postfix; public class StackX { private int maxSize ; private int[] stackArray ; private int top ; public StackX(int size){ maxSize = size ; stackArray = new int[size] ; top = -1 ; } public void push(int elem){ stackArray[++top] = elem ; } public int pop(){ return stackArray[top--] ; } public int peek(){ return stackArray[top] ; } public boolean isEmpty(){ return (top == -1) ; } public int peekIndex(int index){ return stackArray[index] ; } public int size(){ return top +1 ; } public void displayStack(String info){ System.out.print(info) ; System.out.print("Stack (bottom -->top): ") ; for (int j = 0; j < size(); j++){ System.out.print(peekIndex(j)) ; System.out.print(' '); } System.out.println() ; } } Postfix.java package com.ch4.postfix; public class Postfix { private StackX theStack ; private String input ; public Postfix(String S){ input = S ; } public int doParse(){ theStack = new StackX(20) ; char ch ; int i ; int num1 , num2 ,interAns ; for (i = 0; i < input.length(); i++){ ch = input.charAt(i) ; theStack.displayStack("" + ch + "") ; if ((ch >= '0') && (ch <='9')){ theStack.push((int)(ch -'0')) ; }else{ num2 = theStack.pop() ; num1 = theStack.pop() ; switch(ch){ case '+' : interAns = num1 + num2 ; break ; case '-' : interAns = num1 - num2 ; break ; case '*' : interAns = num1 * num2 ; break ; case '/' : interAns = num1 / num2 ; break ; default: interAns = 0; break ; } theStack.push(interAns) ; } } interAns = theStack.pop() ; return interAns ; } } PostfixApp.java package com.ch4.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import com.ch4.infix.Infix; import com.ch4.postfix.Postfix; public class PostfixApp { /** * @param args */ public static void main(String[] args) throws IOException{ String input , output ; while(true ){ System.out.print("Enter infix: ") ; System.out.flush() ; input = getString() ; if (input.equals("")){ break ; } Infix theTrans = new Infix(input) ; output = theTrans.doTrans() ; Postfix postfix = new Postfix(output) ; System.out.println("src String : " + input) ; System.out.println("post src String : " + output) ; System.out.println("Postfix is :" + postfix.doParse()) ; ; } } public static String getString() throws IOException{ InputStreamReader isr = new InputStreamReader(System.in) ; BufferedReader br = new BufferedReader(isr) ; String s = br.readLine() ; return s ; } }
只对个位数计算,如 :1* 2 -3+6/2
运行结果:
Enter infix: 1 + 2*3 + (9-5) /2
For 1 Stack (bottom -->top):
For + Stack (bottom -->top):
For 2 Stack (bottom -->top): +
For * Stack (bottom -->top): +
For 3 Stack (bottom -->top): + *
For + Stack (bottom -->top): + *
For ( Stack (bottom -->top): +
For 9 Stack (bottom -->top): + (
For - Stack (bottom -->top): + (
For 5 Stack (bottom -->top): + ( -
For ) Stack (bottom -->top): + ( -
For / Stack (bottom -->top): +
For 2 Stack (bottom -->top): + /
While Stack (bottom -->top): + /
While Stack (bottom -->top): +
End Stack (bottom -->top):
src String : 1 + 2*3 + (9-5) /2
post src String : 123*+95-2/+
1Stack (bottom -->top):
2Stack (bottom -->top): 1
3Stack (bottom -->top): 1 2
*Stack (bottom -->top): 1 2 3
+Stack (bottom -->top): 1 6
9Stack (bottom -->top): 7
5Stack (bottom -->top): 7 9
-Stack (bottom -->top): 7 9 5
2Stack (bottom -->top): 7 4
/Stack (bottom -->top): 7 4 2
+Stack (bottom -->top): 7 2
Postfix is :9
相关文章推荐
- 算式类型字符串表达式读取并计算出结果(简单四则运算)--后缀表达式计算
- 中缀表达式转换成后缀表达式,进行简单表达式的计算
- 字符串四则运算表达式的计算(华为机试)
- 中缀表达式转后缀表达式,并计算后缀表达式值
- c#中实现类似js的Eval|.NET中执行Javascript(表达式是字符串的计算)
- 中缀表达式转后缀表达式及其计算
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 实现Linux下dc的功能,计算后缀表达式的值
- 字符串的前中后缀表达式,以及KMP算法。
- Basic Calculator:计算字符串表达式
- 后缀表达式计算算术表达式
- 中缀表达式与后缀表达式的转换及后缀表达式的计算
- 中缀表达式转后缀表达式并进行计算 原理
- asp.net字符串的数学表达式计算结果
- java实现中缀表达式转后缀表达式并且计算
- 1686 Lazy Math Instructor 计算表达式 后缀表达式 +-*/^()
- 【综合】中缀表达式转换成后缀表达式后,用后缀表达式计算出结果
- leetcode 227. basic calculator 计算字符串表达式的值 python
- Delphi 如何计算字符串表达式呢?
- JavaScript计算简单后缀表达式