中缀表达式:A+B*C
后缀表达式(计算机处理方式):ABC*+
1.读取*,计算*左边两位数字BC进行计算并得到结果res
2.读取+,计算A与res并计算得到结果
第一步:将中缀表达式转为后缀表达式(通过栈实现)
第二步:计算后缀表达式的结果
3+4-5
过程 |
输出字符串 | 栈 |
3 |
3 |
|
+ |
3 |
+ |
4 |
34 |
+ |
- |
34 |
+- |
5 |
345-+ |
读到-,读取45,计算得到-1;
读到+,读取3,-1,计算得到2。
A+B*(C-D)
过程 |
输出字符串 |
栈 |
A |
A |
|
+ |
A |
+ |
B |
AB |
|
* |
AB |
+* |
( |
AB |
+*( |
C | ABC |
+*( |
- |
ABC |
+*(- |
D |
ABCD |
+*(- |
) |
ABCD-(直到'(') |
+*( |
ABCD- |
+* |
|
ABCD-* |
+ |
|
ABCD-*+ |
1.从左往右读到第一个操作符-,计算C-D
2.读取第二个操作符*,计算B*(C-D)
3.读取第三个操作符+,计算A+B*(C-D)
public class StackX { // 存放数学符号及括号
private char[] stackArray;
private int maxSize;
private int top; // 数组存到第几位
public StackX(int s) {
maxSize = s;
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 char peekN(int n) { // 查看指定位置的字符,没有检查是否为空
return stackArray[n];
}
public boolean isEmpty() { // 空栈判断
return top == -1;
}
public int size() { // 栈中元素数量
return top+1;
}
public void displayStack(String s) { // 查看栈中数据
System.out.print(s);
System.out.print("Stack (bootm->top):");
for (int j = 0; j < size(); j++) {// 栈是后进先出的,因此采用打印指定元素的方式顺序打印
System.out.print(peekN(j) + " ");
}
System.out.println();
}
}
public class InToPost {// 利用栈实现中缀转为后缀
private StackX stack;
private String input; //中缀表达式
private String output=""; //后缀表达式,输出字符串
public InToPost(String in) {
input = in;
int stackSize = input.length();
stack = new StackX(stackSize);
}
public String doTrans() {
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);// 只能解析单个数字
stack.displayStack("For" + ch + " ");
switch(ch) {
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
stack.push(ch);
break;
case ')':
gotParen(ch); // 读取括号中的内容
default:
output = output + ch;
break;
}
}
while(!stack.isEmpty()) { // 读取剩余元素
stack.displayStack("while ");
output = output+stack.pop();
}
stack.displayStack("end... ");
return output;
}
public void gotOper(char opThis, int prec1) {
while(!stack.isEmpty()) {
char opTop = stack.pop();
if(opTop == '(') {
stack.push(opTop);
break;
} else {
int prec2;
if (opTop == '+' || opTop == '-') prec2 = 1;
else prec2 = 2;
if (prec2 < prec1) {
stack.push(opTop); // 放回符号
break;
} else { // 符号级别高,放入输出字符串 中
output = output+opTop;
}
}
}
stack.push(opThis); //当前符号放入栈里
}
public void gotParen(char ch) { //读取括号中内容
while(!stack.isEmpty()) {
char chx = stack.pop();
if(chx == '(') {
break;
} else {
output = output+chx; //弹出字符
}
}
}
}
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class InfixApp { 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 theTrans = new InToPost(input); output = theTrans.doTrans(); System.out.println("Postfix is " + output + "\n"); } } public static String getString() throws IOException { // 从控制台读取输入的字符串 InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; }}
运行结果:
Enter infix:3+8+9
For3 Stack (bootm->top):
For+ Stack (bootm->top):
For8 Stack (bootm->top):+
For+ Stack (bootm->top):+
For9 Stack (bootm->top):+
while Stack (bootm->top):+
end... Stack (bootm->top):
Postfix is38+9+
Enter infix:5+3*2
For5 Stack (bootm->top):
For+ Stack (bootm->top):
For3 Stack (bootm->top):+
For* Stack (bootm->top):+
For2 Stack (bootm->top):+ *
while Stack (bootm->top):+ *
while Stack (bootm->top):+
end... Stack (bootm->top):
Postfix is532*+
Enter infix:A*(B+C)-D/(E+F)
ForA Stack (bootm->top):
For* Stack (bootm->top):
For( Stack (bootm->top):*
ForB Stack (bootm->top):* (
For+ Stack (bootm->top):* (
ForC Stack (bootm->top):* ( +
For) Stack (bootm->top):* ( +
For- Stack (bootm->top):*
ForD Stack (bootm->top):-
For/ Stack (bootm->top):-
For( Stack (bootm->top):- /
ForE Stack (bootm->top):- / (
For+ Stack (bootm->top):- / (
ForF Stack (bootm->top):- / ( +
For) Stack (bootm->top):- / ( +
while Stack (bootm->top):- /
while Stack (bootm->top):-
end... Stack (bootm->top):
Postfix is ABC+)*DEF+)/-