欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-31 21:27:16
本文实例讲述了java数据结构与算法之中缀表达式转为后缀表达式的方法。分享给大家供大家参考,具体如下: //stack public class stackx...

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

//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算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

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