Java实现:利用栈实现中缀到后缀的转换
2017-09-14 14:21
323 查看
import java.util.Scanner;
import java.util.Stack;
public class MediumStack {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* 设计以一个方法判断符号的优先级
*/
Scanner in = new Scanner(System.in);
char[] charArr = in.nextLine().toCharArray();
Stack<String> stack = new Stack<>();
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < charArr.length; i++) {
String readStr = String.valueOf(charArr[i]);
if (isOperator(readStr)) {
if (stack.isEmpty()) {
stack.push(readStr);
} else {
if (isMorePrevious(readStr, stack.peek()) || stack.peek().equals("(")) {
stack.push(readStr);
} else if (readStr.equals(")")) {
while (!stack.peek().equals("(")) {
sBuilder.append(stack.pop());
}
stack.pop();
} else {
sBuilder.append(stack.pop());
stack.push(readStr);
}
}
} else {
sBuilder.append(readStr);
}
}
while (!stack.isEmpty()) {
sBuilder.append(stack.pop());
}
System.out.println(sBuilder.toString());
in.close();
}
public static boolean isOperator(String operator) {
String operatorList = "*+-/()";
if (operatorList.contains(operator))
return true;
return false;
}
public static boolean isMorePrevious(String pre, String next) {
return rank(pre) > rank(next) ? true : false;
}
public static int rank(String symbol) {
switch (symbol) {
case "+":
return 1;
case "-":
return 1;
case "*":
return 2;
case "/":
return 2;
case "(":
return 3;
default:
return -1;
}
}
}
import java.util.Stack;
public class MediumStack {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* 设计以一个方法判断符号的优先级
*/
Scanner in = new Scanner(System.in);
char[] charArr = in.nextLine().toCharArray();
Stack<String> stack = new Stack<>();
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < charArr.length; i++) {
String readStr = String.valueOf(charArr[i]);
if (isOperator(readStr)) {
if (stack.isEmpty()) {
stack.push(readStr);
} else {
if (isMorePrevious(readStr, stack.peek()) || stack.peek().equals("(")) {
stack.push(readStr);
} else if (readStr.equals(")")) {
while (!stack.peek().equals("(")) {
sBuilder.append(stack.pop());
}
stack.pop();
} else {
sBuilder.append(stack.pop());
stack.push(readStr);
}
}
} else {
sBuilder.append(readStr);
}
}
while (!stack.isEmpty()) {
sBuilder.append(stack.pop());
}
System.out.println(sBuilder.toString());
in.close();
}
public static boolean isOperator(String operator) {
String operatorList = "*+-/()";
if (operatorList.contains(operator))
return true;
return false;
}
public static boolean isMorePrevious(String pre, String next) {
return rank(pre) > rank(next) ? true : false;
}
public static int rank(String symbol) {
switch (symbol) {
case "+":
return 1;
case "-":
return 1;
case "*":
return 2;
case "/":
return 2;
case "(":
return 3;
default:
return -1;
}
}
}
相关文章推荐
- 利用JAVA实现中缀表达式向后缀表达式的转换,并求出表达式的值
- 利用栈实现中缀转换为后缀(P67,数据结构与算法分析Java)
- 中缀到后缀表达式的转换:java-stack实现
- 堆栈的应用(2) 中缀算术表达式到后缀(逆波兰记法reverse polish notation)的转换及其计算 C++实现
- 利用Java反射实现普通对象到JSONObject的转换
- 算术表达式求值(中缀转后缀,后缀求值,java 栈实现)
- java实现 中缀转后缀
- 中缀表达式转后缀表达式java精确实现
- Java 利用 ICC 色彩空间 color space profile 实现 RGB 和 CMYK 颜色的相互转换
- 利用JAXB实现java实体类和xml互相转换
- 中缀表达式转后缀表达式java精确实现
- 中缀表达式转后缀表达式java精确实现
- 利用java反射实现Java Bean 类型转换
- JavaEE中利用JAXB来实现Java对象和XML文档进行相互转换
- 中缀表达式转后缀表达式的java实现
- 中缀表达式转换为前缀及后缀表达式并求值(java实现)
- 表达式求值,中缀后缀转换,表达式递归直接求值等相关算法的实现
- 【中缀转换成后缀或者前缀的思想、过程以及算法实现】
- java利用过滤器实现编码的转换,内容输出的替换
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。