国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java數據結構與算法之中綴表達式轉為后綴表達式的方法

2019-11-26 13:59:53
字體:
來源:轉載
供稿:網友

本文實例講述了java數據結構與算法之中綴表達式轉為后綴表達式的方法。分享給大家供大家參考,具體如下:

//stackpublic 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;  }}//InToPostpublic 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();      }    }  }}//Testpublic 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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米脂县| 新巴尔虎左旗| 康定县| 青冈县| 兰溪市| 新津县| 盖州市| 镇巴县| 瓮安县| 茌平县| 布尔津县| 泗水县| 广宗县| 英吉沙县| 泸西县| 泰州市| 高台县| 黎平县| 荃湾区| 全椒县| 临沂市| 民权县| 桦甸市| 霍山县| 新余市| 大姚县| 盐池县| 永城市| 万山特区| 乌鲁木齐县| 崇义县| 栖霞市| 平谷区| 团风县| 襄垣县| 康平县| 南木林县| 吉木乃县| 连平县| 兴海县| 舞阳县|