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程序设计有所帮助。
0
投稿
猜你喜欢
- 前言在上一篇中,我们初步了解了Sentinel的基本概念,以及其有关限流方面的基础理论,本篇将通过简单的与框架进行整合,看看Sentinel
- 本文实例讲述了Java数组高级算法与Arrays类常见操作。分享给大家供大家参考,具体如下:冒泡排序冒泡排序原理冒泡排序代码:package
- 生成1. 生成error 文件的路径:你可以通过参数设置-XX:ErrorFile=/path/hs_error%p.log, 默认是在Ja
- 详细描述maven中央仓库发布jar包的中间过程, 以及遇到的一些问题汇总, 尽量用文字描述清楚, 耐心看下去, 就一定会发布成功----S
- 前言本文主要给大家介绍了关于Spring4自定义@Value功能的相关内容,使用的Spring版本4.3.10.RELEASE,下面话不多说
- 1.首先,需要指定获取的文件夹,以及获取文件的文件名;文件夹:strLocalPath = System.Windows.Forms.App
- 一、概述;从字面上讲,就是停止这个世界,看到这个字眼,就觉得这是可怕的事情,那到底什么是stop-the-world?stop-the-wo
- 多线程编程多线程编程模式.NET 中,有三种异步编程模式,分别是基于任务的异步模式(TAP)、基于事件的异步模式(EAP)、异步编程模式(A
- 原因每次使用idea新建项目,就会在默认的c盘下的一个maven仓库中下载jar包,可是我自己指定maven仓库不是这个。如何让idea在新
- 将网络资源url转化为File文件将互联网上的http开头的url资源,保存到本地。 private File getNetUrlHttp(
- 此次简单的操作将数据从数据库导出生成excel报表以及将excel数据导入数据库首先建立数据库的连接池:package jdbc;impor
- 一、什么是 LRU 算法就是一种缓存淘汰策略。计算机的缓存容量有限,如果缓存满了就要删除一些内容,给新内容腾位置。但问题是,删除哪些内容呢?
- 前言Zuul 是Netflix 提供的一个开源组件,致力于在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。也有很多公司使用它来作为
- 序言小编在项目中有遇到使用 flutter 实现扫码枪接入的需求。为方便使用,小编把能力封装成 package 并发布。好记性不如烂笔头,下
- Spring Data Jpa复杂查询总结只是做一个总结所以就不多说废话了实体类@Entity@Table(name = "t_h
- 使用 replace 函数动态填充字符串String str="Hello {0},我是 {1},今年{2}岁"
- 我这里有一个需求需要修改Person类中的一个属性上的注解的值进行修改,例如:public class Person { private i
- 本文主要关注如何使用mybatis/mybatis plus连接SQL Server数据库,因此将省略其他项目配置、代码。框架选择应用框架:
- 一、概述在软件开发中,有时需要保存一个对象的状态,以便于允许用户取消相关操作或者从以往的状态中恢复过来。比如一个文档版本管理系统,可以根据需
- 简介MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)