java数据结构与算法之中缀表达式转为后缀表达式的方法
作者:modun 发布时间:2023-11-22 12:51:35
标签: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<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程序设计有所帮助。


猜你喜欢
- 中国科学院开源协会镜像站地址:IPV4/IPV6: http://mirrors.opencas.cn 端口:80IPV4/IPV6: ht
- 本文实例为大家分享了Java实现24点小游戏的具体代码,供大家参考,具体内容如下程序设计要求:24点游戏是经典的纸牌益智游戏。常见游戏规则:
- 本文实例讲述了Android编程创建桌面快捷方式的常用方法。分享给大家供大家参考,具体如下:Android在桌面上生成快捷方式有两种情况,一
- 结构体有时候我们仅需要一个小的数据结构,类提供的功能多于我们需要的功能;考虑到性能原因,最好使用结构体。结构体是值类型,存储在栈中或存储为内
- 我们肯定遇到过打开别人的项目时一直处于Building‘XXX'Gradle project info的情况。本文通过两种方法带领大
- 设计模式分类:创建型模式。结构型模式。行为模式。23种设计模式,如何记。面向对象的系统中有很多对象,创建型模式解决的问题就是如何创建对象,何
- 什么是Run Dashboard当springcloud的服务有多个时,管理多个服务的启动使用run会不好管理,这样我们就可以使用Run D
- 调用海康工业相机SDK采集图像并在Halcon窗口中显示最近做项目需要对海康相机进行二次开发,现将所学进行整理。开发环境 &nbs
- 什么是方法?System.out.println(),那么它是什么呢?系统类里的,对象out,输出方法printlnJava方法是语句的集合
- 思路:先从集合中找出来顶级的菜单,然后遍历顶级菜单,找出每个顶级菜单的所有子菜单,然后判断当前需要排列的集合是否为空,如果不为空的话,就在遍
- 前言相信大家都用过Spring Security和Shiro的框架,Spring Security必须配合Spring 全家桶使用和繁琐的配
- 在本人用editplus写java文件时碰到的问题。 import java.util.*;class collection{ &
- 本文实例为大家分享了Java实现员工管理系统的具体代码,供大家参考,具体内容如下本系统主要练习到的相关内容: 1、 流程控制语句 2、 类、
- 本文实例讲述了Android使用ListView实现下拉刷新及上拉显示更多的方法。分享给大家供大家参考,具体如下:今天得需求是做listvi
- 1 问题实现任意view经过自定义带4圆角或者2圆角的效果2 原理1) 实现view 4圆角我们只需要把左边的图嵌入到右边里面去,最终显示左
- 前段时间做了一个Android会议管理系统,项目需求涉及到EditText的图文混排,如图: 在上图的”会议详情”中,需要支持文
- 实现形式elevationMaterial Design提供了View的阴影效果设置。主要由两个属性决定:elevation和transla
- SpringBoot加载application.properties配置文件的坑事情的起因是这样的一次,本人在现场升级程序,升级完成后进行测
- 运行原理1、不同线程中所包含的栈帧是不允许存在相互引用的。2、如果当前方法调用了其他方法,方法返回之际,当前栈帧会传回此方法的执行结果给当前
- 主界面xml文件<RelativeLayout xmlns:android="http://schemas.android.