java数据结构与算法之中缀表达式转为后缀表达式的方法

网友投稿 200 2023-07-09


java数据结构与算法之中缀表达式转为后缀表达式的方法

本文实例讲述了java数据结构与算法之中缀表达式转为后缀表达式的方法。分享给大家供大家参考,具体如下:

//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

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{

http:// 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:算法实现不是很完善,有些复杂的表达式解析要出错,写出来做个纪念!

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

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{

http:// 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:算法实现不是很完善,有些复杂的表达式解析要出错,写出来做个纪念!

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:关于java中Map的九大问题分析
下一篇:Java 8中日期和时间的处理方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~