Artikel ini menjelaskan metode mengubah ekspresi infix menjadi ekspresi akhiran dalam struktur dan algoritma data java. Bagikan untuk referensi Anda, sebagai berikut:
// kelas StackPublic Stackx {private int top; Char pribadi [] Stackarray; private int maxsize; // konstruktor public stackx (int maxSize) {this.maxsize = maxsize; this.top = -1; stackarray = char baru [this.maxsize]; } // Letakkan item di atas stack public void push (char push) {stackarray [++ top] = push; } // Ambil item dari atas stack public char pop () {return stackarray [top--]; } // mengintip item teratas dari stack public char peek () {return stackarray [top]; } // Mengintip karakter pada indeks n char public peekn (int index) {return stackarray [index]; } // true jika stack kosong public boolean isEmpty () {return (top == -1); } // pengembalian ukuran stack ukuran int publik () {return top+1; }} // kelas intopostpublic intopost {private stackx mystack; input string pribadi; private string output = ""; // Konstruktor Public Intopost (String Input) {this.input = input; mystack = stackx baru (this.input.length ()); } // lakukan terjemahan ke 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); merusak; case '*': case '/': this.getoper (ch, 2); merusak; case '(': this.getoper (ch, 3); break; case ')': this.getoper (ch, 4); merusak; default: this.output = this.output + ch; }} while (! this.mystack.isempty ()) {this.output = this.output + this.mystack.pop (); } return this.output; } // Dapatkan operator dari 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 == '(') lanjutkan; this.output = this.output + temp;}} lain if (prect1 == 1) {temp = this.mystack.peek (); if (temp == '(') this.mystack.push (ch); else {this.output = this.output + this.mystack.push (ch); else {this.output = this.output + this. } } 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;PS: Implementasi algoritma tidak terlalu sempurna, dan beberapa kesalahan penguraian ekspresi kompleks akan terjadi. Tuliskan sebagai peringatan!
Untuk informasi lebih lanjut tentang algoritma java, pembaca yang tertarik dengan situs ini dapat melihat topik: "struktur data java dan tutorial algoritma", "ringkasan tips node dom java", "ringkasan file operasi java dan direktori" dan "ringkasan tip operasi java cache" tips java "tips java" Tips "Java Cache Tips"
Saya harap artikel ini akan membantu pemrograman Java semua orang.