이 기사에서는 Java 데이터 구조 및 알고리즘의 접미어 표현식으로 디스 픽스 표현식을 변환하는 방법에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
// stackpublic class stackx {private int top; 개인 char [] stackarray; 개인 int maxsize; // 생성자 public stackx (int maxsize) {this.maxsize = maxsize; this.top = -1; stackarray = new char [this.maxsize]; } // 스택 위에 항목을 공개 무효 푸시 (char push) {stackArray [++ top] = 푸시; } // 스택 상단에서 항목을 가져와 public char pop () {return stackarray [top-]; } // stack public char peek ()에서 상단 항목을 살펴보십시오 {return stackarray [top]; } // index n public char peekn (int index) {return stackarray [index]; } // Stack이 빈 공개 boolean isempty () {return (top == -1); } // 리턴 스택 크기 공개 int size () {return top+1; }} // intopostpublic class intopost {private stackx mystack; 개인 문자열 입력; 개인 문자열 출력 = ""; // 생성자 public intopost (문자열 입력) {this.input = 입력; mystack = new stackx (this.input.length ()); } // 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); 부서지다; CASE '*': CASE '/': this.getOper (ch, 2); 부서지다; case '(': this.getoper (ch, 3); break; case ')': this.getoper (ch, 4); 부서지다; 기본값 : this.output = this.output + ch; }} while (! this.mystack.isempty ()) {this.output = this.output + this.mystack.pop (); } reture this.output; } // 입력 공개 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 == '(') 계속; this.output = this.output + temp;}} else if (prect1 == 1) {temp = this.mystack.peek (); if (temp == '(') this.mystack.push (ch); 다른 {this.output = this.output + this.mystack.pop (); else {temp = this.mystack.peek (); public static void main (String [] args) {str = "(+b) -d";추신 : 알고리즘 구현은 그다지 완벽하지 않으며 일부 복잡한 표현식 구문 분석 오류가 발생합니다. 기념으로 쓰십시오!
Java 알고리즘에 대한 자세한 내용은이 사이트에 관심이있는 독자들이 주제를 볼 수 있습니다. "Java 데이터 구조 및 알고리즘 자습서", "Java Operation Dom Node Tips 요약", "Java 파일 및 디렉토리 작동 팁 요약"및 "Java Cache Operation Tips의 요약"을 볼 수 있습니다.
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.