java数据结构与算法之中缀表达式转为后缀表达式的方法
作者:modun 时间:2023-11-22 12:51:35
本文实例讲述了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<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:算法实现不是很完善,有些复杂的表达式解析要出错,写出来做个纪念!
希望本文所述对大家java程序设计有所帮助。
标签:java,数据结构,算法
0
投稿
猜你喜欢
JFileChooser实现对选定文件夹内图片自动播放和暂停播放实例代码
2021-10-02 15:41:18
Mybatis关联查询结果集对象嵌套的具体使用
2021-07-12 22:09:18
Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件
2023-07-06 01:24:29
java高并发之理解进程和线程
2023-09-16 20:03:17
Java打印九九乘法表代码详情
2022-03-14 17:42:14
Java中MessageFormat的使用详解
2022-03-14 02:01:02
Java安全-ClassLoader
2023-08-18 02:12:21
Java中将base64编码字符串转换为图片的代码
2023-07-14 12:23:51
java HttpClient传输json格式的参数实例讲解
2023-08-08 13:21:26
Java基础题新手练习(二)
2022-03-10 00:11:57
C# 汉字与拼音互转的实现示例
2022-03-06 06:50:20
Android 创建与解析XML(四)——详解Pull方式
2023-06-03 07:10:23
Java中this关键字的用法详解
2023-10-04 05:05:53
C#使用HtmlAgilityPack抓取糗事百科内容实例
2022-01-16 10:45:16
深入浅析Java 抽象类和接口
2022-12-17 19:19:27
WinForm中实现picturebox自适应图片大小的方法
2022-09-07 21:14:01
C语言简明讲解变量的属性
2021-06-23 17:45:28
实例讲解JAVA设计模式之备忘录模式
2023-08-29 16:31:19
vc提示unexpected end of file found的原因分析
2022-01-19 12:58:00
轻松学习C#的结构和类
2023-12-10 13:46:19