java數(shù)據(jù)結(jié)構(gòu)與算法之中綴表達(dá)式轉(zhuǎn)為后綴表達(dá)式的方法
本文實(shí)例講述了java數(shù)據(jù)結(jié)構(gòu)與算法之中綴表達(dá)式轉(zhuǎn)為后綴表達(dá)式的方法。分享給大家供大家參考,具體如下:
//stack
public 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;
}
}
//InToPost
public 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();
}
}
}
}
//Test
public 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:算法實(shí)現(xiàn)不是很完善,有些復(fù)雜的表達(dá)式解析要出錯(cuò),寫(xiě)出來(lái)做個(gè)紀(jì)念!
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
SpringCloud微服務(wù)集成Dubbo的詳細(xì)過(guò)程
Apache?Dubbo?是一款易用、高性能的?WEB?和?RPC?框架,同時(shí)為構(gòu)建企業(yè)級(jí)微服務(wù)提供服務(wù)發(fā)現(xiàn)、流量治理、可觀測(cè)、認(rèn)證鑒權(quán)等能力、工具與最佳實(shí)踐,這篇文章主要介紹了SpringCloud微服務(wù)集成Dubbo,需要的朋友可以參考下2024-03-03
Java如何解決發(fā)送Post請(qǐng)求報(bào)Stream?closed問(wèn)題
這篇文章主要介紹了Java如何解決發(fā)送Post請(qǐng)求報(bào)Stream?closed問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
永久解決 Intellij idea 報(bào)錯(cuò):Error :java 不支持發(fā)行版本5的問(wèn)題
這篇文章主要介紹了永久解決 Intellij idea 報(bào)錯(cuò):Error :java 不支持發(fā)行版本5的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Java中的HashMap和Hashtable區(qū)別解析
這篇文章主要介紹了Java中的HashMap和Hashtable區(qū)別解析,HashMap和Hashtable都實(shí)現(xiàn)了Map接口,但決定用哪一個(gè)之前先要弄清楚它們之間的區(qū)別,主要的區(qū)別有線程安全性、同步和速度,需要的朋友可以參考下2023-11-11
SpringBoot如何使用自定義注解實(shí)現(xiàn)接口限流
這篇文章主要介紹了SpringBoot如何使用自定義注解實(shí)現(xiàn)接口限流,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
使用EasyPOI實(shí)現(xiàn)多sheet動(dòng)態(tài)列導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了如何使用EasyPOI根據(jù)指定時(shí)間范圍創(chuàng)建動(dòng)態(tài)列,以及如何將數(shù)據(jù)組織成符合要求的格式并導(dǎo)出,感興趣的可以了解下2025-03-03
JAVA中while循環(huán)的使用與注意事項(xiàng)
這篇文章主要介紹了while循環(huán)在編程中的應(yīng)用,包括其基本結(jié)構(gòu)、語(yǔ)句示例、適用場(chǎng)景以及注意事項(xiàng),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01

