/**
* 使用棧存儲后綴表達式
* Create by Administrator
* 2018/6/13 0013
* 下午 2:25
**/
public class StackX {
private int maxSize;
private char[] stackArray;
private int top;
public StackX(int size) // 構造函數
{
maxSize = size;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) // 將項目放在堆棧的頂部
{
stackArray[++top] = j;
}
public char pop() // 從堆棧頂部取項
{
return stackArray[top--];
}
public char peek() // 從堆棧頂部查看
{
return stackArray[top];
}
public boolean isEmpty() // 如果棧為空,則為true
{
return (top == -1);
}
public boolean isFull() // 如果堆棧已滿 true
{
return (top == maxSize - 1);
}
public int size() // return size
{
return top + 1;
}
public char peekN(int n) // peek at index n
{
return stackArray[n];
}
public void displayStack(String s) {
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for (int j = 0; j < size(); j++) {
System.out.print(peekN(j));
System.out.print(' ');
}
System.out.println("");
}
}
/**
* 使用棧存儲計算過程結果
* Create by Administrator
* 2018/6/14 0014
* 上午 10:37
**/
public class StackR {
private int maxSize;
private int[] stackArray;
private int top;
public StackR(int size) // 構造函數
{
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public void push(int j) // 將項目放在堆棧的頂部
{
stackArray[++top] = j;
}
public int pop() // 從堆棧頂部取項
{
return stackArray[top--];
}
public int peek() // 從堆棧頂部查看
{
return stackArray[top];
}
public boolean isEmpty() // 如果棧為空,則為true
{
return (top == -1);
}
public boolean isFull() // 如果堆棧已滿 true
{
return (top == maxSize - 1);
}
public int size() // return size
{
return top + 1;
}
public int peekN(int n) // peek at index n
{
return stackArray[n];
}
public void displayStack(String s) {
System.out.print(s);
System.out.print("Stack (bottom-->top): ");
for (int j = 0; j < size(); j++) {
System.out.print(peekN(j));
System.out.print(' ');
}
System.out.println("");
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Create by Administrator
* 2018/6/13 0013
* 下午 2:38
**/
public class InTOPost {
private StackX stackX;
private StackR stackR;
private String input;
private String outPut = "";
public InTOPost(String in) {
this.input = in;
int stackSize = input.length();
stackX = new StackX(stackSize);
}
/**
* 中綴表達式轉后綴表達式
* @return
*/
public String doTrans() {
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);//拿到每個字符
stackX.displayStack("For " + ch + " ");
switch (ch) {
case '+':
case '-':
getOpera(ch, 1);
break;
case '*':
case '/':
getOpera(ch, 2);
break;
case ')':
getParen(ch);
break;
case '(':
stackX.push(ch);
break;
default:
outPut = outPut + ch; //是數字將其寫入輸出
break;
}
}
while (!stackX.isEmpty()) {
stackX.displayStack("While ");
outPut = outPut + stackX.pop();
}
stackX.displayStack("End ");
return outPut;
}
private void getOpera(char opThis, int prec1) {
while (!stackX.isEmpty()) {
char opTop = stackX.pop();
if (opTop == '(') {
stackX.push(opTop);
break;
} else {
int prec2;
if (opTop == '+' || opTop == '-') {
prec2 = 1;
} else {
prec2 = 2;
}
if (prec2 < prec1) {
stackX.push(opTop);
break;
} else {
outPut = outPut + opTop;
}
}
}
stackX.push(opThis);
}
private void getParen(char ch) {
while (!stackX.isEmpty()) {
char chx = stackX.pop();
if (chx == '(') {
break;
} else {
outPut = outPut + chx;
}
}
}
/**
* 計算后綴表達式的結果
* @param output
* @return
*/
public int doParse(String output) {
stackR = new StackR(20); //新建堆棧
char ch;
int num1, num2, interAns;
for (int i = 0; i < output.length(); i++) { //遍歷后綴表達式的字符串
ch = output.charAt(i); // 讀取到每一個字符
stackR.displayStack(ch + " ");
if (ch >= '0' && ch <= '9') { // 判斷是不是數字
stackR.push((int) (ch - '0')); // 放入到棧里
} else {
num2 = stackR.pop(); // 如果不是數字,就從棧里取出兩個數字
num1 = stackR.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;
}
stackR.push(interAns); // 把計算結果放入棧里
}
}
interAns = stackR.pop(); // 獲得最終結果
return interAns;
}
/**
* 獲取用戶輸入
* @return
* @throws IOException
*/
public static String getString() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
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;
}
InTOPost toPost = new InTOPost(input);
output = toPost.doTrans();
System.out.println("Postfix is " + output + "\n");
int result = toPost.doParse(output);
System.out.println("結果:" + result);
}
}
運行測試:
請輸入: (4+2*3)/2
For ( Stack (bottom-->top):
For 4 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 2 Stack (bottom-->top): /
While Stack (bottom-->top): /
End Stack (bottom-->top):
Postfix is 423*+2/
4 Stack (bottom-->top):
2 Stack (bottom-->top): 4
3 Stack (bottom-->top): 4 2
* Stack (bottom-->top): 4 2 3
+ Stack (bottom-->top): 4 6
2 Stack (bottom-->top): 10
/ Stack (bottom-->top): 10 2
結果:5
