この記事では、Javaデータ構造とアルゴリズムのインフィックス式を接尾辞式に変換する方法について説明します。次のように、参照のために共有してください。
// stackpublic class stackx {private int top;プライベートチャー[] StackArray; Private int maxsize; // constructor public stackx(int maxsize){this.maxsize = maxsize; this.top = -1; stackArray = new char [this.maxsize]; } //スタックパブリックボイドプッシュ(charプッシュ)の上にアイテムを置く{stackarray [++ top] = push; } // stack public char pop(){return stackarray [top-]; } // Stack Public Char Peek()からトップアイテムを覗きます{return stackarray [top]; } // Index n public Char peekn(int index)でキャラクターを覗きます{return stackarray [index]; } // true stackが空の場合はpublic boolean isempty(){return(top == -1); } //スタックサイズを返すpublic int size(){return top+1; }} // intopostpublic class intopost {private stackx mystack;プライベート文字列入力;プライベート文字列output = ""; // constructor public intopost(string input){this.input = 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(); } this.output; } //入力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 == '(')継続; this.output = this.output + temp;}} else if(prect1 == 1){temp = this.mystack.peek(); if(temp == '(')this.mystack.push(ch); els {this.output = this.put + this.pusp. else {temp = this.mystack.peek(); if( '|| temp ==' + '|| temp ==' - ')this.mystack.push(ch); else {this.output + this.mystack.pop();}}}}}}} public static void main [] args){(a+b)*c)-d ";PS:アルゴリズムの実装はあまり完全ではなく、複雑な式解析エラーが発生します。記念として書き留めてください!
Javaアルゴリズムの詳細については、このサイトに興味のある読者は、「Javaデータ構造とアルゴリズムのチュートリアル」、「Java操作DOMノードのヒントの要約」、「Javaファイルの要約およびディレクトリ操作のヒント」、「Java Cache操作のヒントの要約」というトピックを見ることができます。
この記事がみんなのJavaプログラミングに役立つことを願っています。