Dieser Artikel beschreibt die Methode zur Umwandlung von Infixausdrücken in Suffix -Ausdrücke in Java -Datenstrukturen und -algorithmen. Teilen Sie es für Ihre Referenz wie folgt weiter:
// StackPublic Class Stackx {private int top; private Char [] Stackarray; privat int maxsize; // Constructor public stackx (int maxSize) {this.maxSize = maxSize; this.top = -1; stackArray = new char [this.maxSize]; } // Element auf Stack public void push (char push) {StackArray [++ Top] = Push; } // Item von Top in Stack public char pop () {return stackArray [top--]; } // Peek das Top -Artikel von Stack public char peek () {return stackArray [top]; } // peek das Zeichen in Index n public char peekn (int index) {return stackArray [index]; } // true, wenn Stack leer ist, öffentlich boolean isEmpty () {return (top == -1); } // Stapelgröße zurückgeben public int size () {return top+1; }} // intopostpublic class intopost {private stackx mystack; private String -Eingabe; private String output = ""; // Konstruktor public Intopost (String -Eingabe) {this.input = input; mystack = new stackx (this.input.length ()); } // Übersetzung zu postfix public String dotrans () {für (int i = 0; i <input.length (); i ++) {char ch = input.charat (i); Switch (ch) {case '+': case '-': this.getOper (ch, 1); brechen; case '*': case '/': this.getoper (ch, 2); brechen; case '(': this.Getoper (ch, 3); break; case ')': this.getOper (ch, 4); brechen; Standard: this.output = this.output + ch; }} while (! this.mystack.isempty ()) {this.output = this.output + this.mystack.pop (); } return this.output; } // Operator von 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 == '(') Fortsetzung; this.output = this.output + temp;}} else if (propect1 == 1) {temp = this.mystack.peek (); if (temp == '(') this.mystack.push (ch); {this.output = this. }} else {temp = this.mystack.peek (); Intopost;PS: Die Implementierung von Algorithmus ist nicht sehr perfekt und es treten einige komplexe Ausdrucksanalysefehler auf. Schreiben Sie es als Gedenken aus!
Für weitere Informationen zu Java -Algorithmen können Leser, die an dieser Website interessiert sind, die Themen "Java -Datenstruktur und Algorithmus -Tutorial", "Zusammenfassung der Java -Operation DOM -Knoten -Tipps", "Zusammenfassung der Java -Datei- und Verzeichnisoperationstipps" und "Zusammenfassung der Java -Cache -Operation Tipps" anzeigen
Ich hoffe, dieser Artikel wird für Java -Programme aller hilfreich sein.