服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java数据结构与算法之中缀表达式转为后缀表达式的方法

java数据结构与算法之中缀表达式转为后缀表达式的方法

2020-06-03 11:30modun JAVA教程

这篇文章主要介绍了java数据结构与算法之中缀表达式转为后缀表达式的方法,简单分析了java中缀表达式转为后缀表达式的相关实现方法与技巧,需要的朋友可以参考下

本文实例讲述了java数据结构与算法之中缀表达式转为后缀表达式的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//stack
public class StackX {
  private int top;
  private char[] stackArray;
  private int maxSize;
  //constructor
  public StackX(int maxSize){
    this.maxSize = maxSize;
    this.top = -1;
    stackArray = new char[this.maxSize];
  }
  //put item on top of stack
  public void push(char push){
    stackArray[++top] = push;
  }
  //take item from top of stack
  public char pop(){
    return stackArray[top--];
  }
  //peek the top item from stack
  public char peek(){
    return stackArray[top];
  }
  //peek the character at index n
  public char peekN(int index){
    return stackArray[index];
  }
  //true if stack is empty
  public boolean isEmpty(){
    return (top == -1);
  }
  //return stack size
  public int size(){
    return top+1;
  }
}
//InToPost
public class InToPost {
  private StackX myStack;
  private String input;
  private String outPut="";
  //constructor
  public InToPost(String input){
    this.input = input;
    myStack = new StackX(this.input.length());
  }
  //do translation to postFix
  public String doTrans(){
    for(int i=0; i<input.length(); i++){
      char ch = input.charAt(i);
      switch(ch){
      case '+':
      case '-':
        this.getOper(ch,1);
        break;
      case '*':
      case '/':
        this.getOper(ch,2);
        break;
      case '(':
        this.getOper(ch, 3);
        break;
      case ')':
        this.getOper(ch, 4);
        break;
      default:
        this.outPut = this.outPut + ch;
      }
    }
    while(!this.myStack.isEmpty()){
      this.outPut = this.outPut + this.myStack.pop();
    }
    return this.outPut;
  }
  //get operator from input
  public void getOper(char ch, int prect1){
    char temp;
    if(this.myStack.isEmpty()||prect1==3){
      this.myStack.push(ch);
    }
    else if(prect1==4){
      while(!this.myStack.isEmpty()){
        temp = this.myStack.pop();
        if(temp=='(')continue;
        this.outPut = this.outPut + temp;
      }
    }
    else if(prect1==1){
      temp = this.myStack.peek();
      if(temp=='(') this.myStack.push(ch);
      else{
        this.outPut = this.outPut + this.myStack.pop();
        this.myStack.push(ch);
      }
    }
    else{
      temp = this.myStack.peek();
      if(temp=='('||temp=='+'||temp=='-') this.myStack.push(ch);
      else{
        this.outPut = this.outPut + this.myStack.pop();
      }
    }
  }
}
//Test
public class TestInToPost {
  private static InToPost inToPost;
  private static String str;
  public static void main(String []args){
    str = "((A+B)*C)-D";
    inToPost = new InToPost(str);
    System.out.println(inToPost.doTrans());
  }
}

PS:算法实现不是很完善,有些复杂的表达式解析要出错,写出来做个纪念!

希望本文所述对大家java程序设计有所帮助。

延伸 · 阅读

精彩推荐