完整逆波兰计算器(Java)
标签:als 图片 pac int ima double stat block import
完整逆波兰计算器(Java)
博客说明
文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!
功能
支持 + - * / ( )
多位数,支持小数,
兼容处理, 过滤任何空白字符,包括空格、制表符、换页符
基本思路
中缀表达式转后缀表达式
代码
package stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.Collections;
import java.util.regex.Pattern;
public class ReversePolishMultiCalc {
//匹配运算符
static final String SYMBOL = "\\+|-|\\*|/|\\(|\\)";
static final String LEFT = "(";
static final String RIGHT = ")";
static final String ADD = "+";
static final String MINUS = "-";
static final String TIMES = "*";
static final String DIVISION = "/";
//优先级
static final int LEVEL_01 = 1;
static final int LEVEL_02 = 2;
static final int LEVEL_HIGH = Integer.MAX_VALUE;
static Stack stack = new Stack();
static List data = Collections.synchronizedList(new ArrayList());
//去除所有空白符号
public static String replaceAllBlank(String s) {
return s.replaceAll("\\s+", "");
}
//判断是否为数字
public static boolean isNumber(String s) {
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
return pattern.matcher(s).matches();
}
//判断是否是运算符
public static boolean isSymbol(String s) {
return s.matches(SYMBOL);
}
//匹配运算优先级
public static int calcLevel(String s) {
if ("+".equals(s) || "-".equals(s)) {
return LEVEL_01;
} else if ("*".equals(s) || "/".equals(s)) {
return LEVEL_02;
}
return LEVEL_HIGH;
}
//匹配
public static List doMatch(String s) throws Exception {
if (s == null || "".equals(s.trim())) throw new RuntimeException("data is empty");
if (!isNumber(s.charAt(0) + "")) throw new RuntimeException("data illeagle,start not with a number");
s = replaceAllBlank(s);
String each;
int start = 0;
for (int i = 0; i calcLevel(stack.peek())) && calcLevel(each) = calcLevel(stack.peek())) {
if (LEVEL_HIGH == calcLevel(stack.peek())) {
stack.pop();
break;
}
data.add(stack.pop());
}
}
start = i;
} else if (i == s.length() - 1 || isSymbol(s.charAt(i + 1) + "")) {
each = start == 0 ? s.substring(start, i + 1) : s.substring(start + 1, i + 1);
if (isNumber(each)) {
data.add(each);
continue;
}
throw new RuntimeException("data not match number");
}
}
Collections.reverse(stack);
data.addAll(new ArrayList(stack));
System.out.println(data);
return data;
}
//计算结果
public static Double doCalc(List list){
Double d = 0d;
if(list == null || list.isEmpty()){
return null;
}
if(list.size() == 1){
System.out.println(list);
d = Double.valueOf(list.get(0));
return d;
}
ArrayList list1 = new ArrayList();
for (int i = 0; i
结果
感谢
尚硅谷
万能的网络
以及勤劳的自己
关注公众号: 归子莫,获取更多的资料,还有更长的学习计划
完整逆波兰计算器(Java)
标签:als 图片 pac int ima double stat block import
原文地址:https://www.cnblogs.com/guizimo/p/13192360.html
评论