1. expression a + b * c- (d + e) / f suffix expression for?
Sequentially scanning manner from left to right scan parameters into parameter symbols into the symbol stack Stack
Number of sweeps | Scan to value | Parameter stack | Symbols Stack | Explanation |
---|---|---|---|---|
the first time | a | a | The first scan is a parameter directly into the stack | |
the second time | + | a | + | The second is to scan plus sign directly into the stack |
the third time | b | ba | + | To the third scanning parameters b sequentially into stack |
the fourth time | * | ba | *+ | Fourth is scanned into the * symbol stack, because I do not know the title encounter to the next parameter is what we first do not move |
the fifth time | c | + * Cba | The fifth is scanned into the c parameter stack, because multiplication heightened level than is required to enter the parameter stack, the stack and then take the first good news out of the stack plus | |
the sixth time | - | + * Cba | - | Scan to sixth - directly into the stack symbol |
Seventh time | ( | + * Cba | (- | Seventh scans to (directly into the stack symbol |
The eighth time | d | d+*cba | (- | Eighth scanned directly into the parameter stack is d |
Ninth | + | d+*cba | +(- | Ninth is scanned into the + sign stack |
Tenth | e | ed+*cba | +(- | Tenth scanned directly into the parameter stack is e |
The eleventh time | ) | +ed+*cba | - | Eleventh scan is) left and right parentheses paired brackets symbols into the stack parameters |
Twelfth | / | +ed+*cba | /- | Twelfth scan is / Divide into the operator stack |
Thirteenth | f | -/f+ed+*cba | Thirteenth is scanned / high-level parameters into the stack |
Turn backwards to pop
abc*+de+f/-
Out for half an hour on the train
The definition of a stack of objects
package main.com.cs.suffix;
public class MyCharStack {
//定义栈
private char[] array;
//定义栈的最大范围
private int maxSize;
//栈顶
private int top;
public MyCharStack(int size){
this.maxSize = size;
array = new char[size];
top = -1;
}
//压入数据
public void push(char value){
if(top < maxSize-1){
array[++top] = value;
}
// throw new RuntimeException("栈大小超出范围");
}
//弹出栈顶数据
public char pop(){
return array[top--];
}
//访问栈顶数据
public char peek(){
return array[top];
}
//查看指定位置的元素
public char peekN(int n){
return array[n];
}
//为了便于后面分解展示栈中的内容,我们增加了一个遍历栈的方法(实际上栈只能访问栈顶元素的)
public void displayStack(){
System.out.print("Stack(bottom-->top):");
for(int i = 0 ; i < top+1; i++){
System.out.print(peekN(i));
System.out.print(' ');
}
System.out.println("");
}
//判断栈是否为空
public boolean isEmpty(){
return (top == -1);
}
//判断栈是否满了
public boolean isFull(){
return (top == maxSize-1);
}
}
Conjugation transfer method suffix definition
package main.com.cs.suffix;
public class InfixToSuffix {
private MyCharStack s1;//定义运算符栈
private MyCharStack s2;//定义存储结果栈
private String input;
//默认构造方法,参数为输入的中缀表达式
public InfixToSuffix(String in){
input = in;
s1 = new MyCharStack(input.length());
s2 = new MyCharStack(input.length());
}
//中缀表达式转换为后缀表达式,将结果存储在栈中返回,逆序显示即后缀表达式
public MyCharStack doTrans(){
for(int j = 0 ; j < input.length() ; j++){
System.out.print("s1(符号)栈元素为:");
s1.displayStack();
System.out.print("s2(参数)栈元素为:");
s2.displayStack();
char ch = input.charAt(j);
System.out.println("当前解析的字符:"+ch);
switch (ch) {
case '+':
case '-':
gotOper(ch,1);
break;
case '*':
case '/':
gotOper(ch,2);
break;
case '(':
s1.push(ch);//如果当前字符是'(',则将其入栈
break;
case ')':
gotParen(ch);
break;
default:
//1、如果当前解析的字符是操作数,则直接压入s2
//2、
s2.push(ch);
break;
}//end switch
}//end for
while(!s1.isEmpty()){
s2.push(s1.pop());
}
return s2;
}
public void gotOper(char opThis,int prec1){
while(!s1.isEmpty()){
char opTop = s1.pop();
if(opTop == '('){//如果栈顶是'(',直接将操作符压入s1
s1.push(opTop);
break;
}else{
int prec2;
if(opTop == '+' || opTop == '-'){
prec2 = 1;
}else{
prec2 = 2;
}
if(prec2 < prec1){//如果当前运算符比s1栈顶运算符优先级高,则将运算符压入s1
s1.push(opTop);
break;
}else{//如果当前运算符与栈顶运算符相同或者小于优先级别,那么将S1栈顶的运算符弹出并压入到S2中
//并且要再次再次转到while循环中与 s1 中新的栈顶运算符相比较;
s2.push(opTop);
}
}
}//end while
//如果s1为空,则直接将当前解析的运算符压入s1
s1.push(opThis);
}
//当前字符是 ')' 时,如果栈顶是'(',则将这一对括号丢弃,否则依次弹出s1栈顶的字符,压入s2,直到遇到'('
public void gotParen(char ch){
while(!s1.isEmpty()){
char chx = s1.pop();
if(chx == '('){
break;
}else{
s2.push(chx);
}
}
}
}
Defined test methods
package test.com.cs;
import main.com.cs.suffix.InfixToSuffix;
import main.com.cs.suffix.MyCharStack;
import org.junit.Test;
import java.util.Scanner;
public class suffix {
public static void main(String[] args) {
String input;
System.out.println("Enter infix:");
Scanner scanner = new Scanner(System.in);
input = scanner.next();
InfixToSuffix in = new InfixToSuffix(input);
MyCharStack my = in.doTrans();
my.displayStack();
}
}
Test Results