Java 四则运算表达式的分割

独自一人的昵称谁用了 2011-11-01 09:27:23
在写一个计算四则运算表达式的程序,想用堆栈去实现,因此要将表达式里面的数字和运算符分开来保存
例如2.5*4+(2*5)
想将2.5,4,2,5这个数存入一数组,而*,+(,)存入另一数组,Java中的split方法好像不能实现这个功能,求高手给思路!!!
...全文
810 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Antineutrino 2011-11-01
  • 打赏
  • 举报
回复
补充说明一下:
我的意思是1**3 8是错误的表达式,程序应该报错,如果拆分成数组,这种元素之间的顺序关系就减弱了,程序很可能会错误地把这个错误的表达式当成正确的1*3*8来计算
Antineutrino 2011-11-01
  • 打赏
  • 举报
回复
楼主可以去看看我的这篇博客,里面有完整的代码,你可以参考一下
http://blog.csdn.net/antineutrino/article/details/6763722
Antineutrino 2011-11-01
  • 打赏
  • 举报
回复
其实不需要拆分数组,一个一个字符解析比较好
因为拆分数组的话,可能会有问题,比如
1**3 8
拆分成数组来做,程序可能会错误地解析成
1*3*8
JieTouLangRen 2011-11-01
  • 打赏
  • 举报
回复
数字一个栈
运算符一个栈

Devil26 2011-11-01
  • 打赏
  • 举报
回复
String str = "2.5*4+(2*5)";
Pattern p;
Matcher m;
p = Pattern.compile("\\+|\\-|\\*|\\/|\\(|\\)");
m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
p = Pattern.compile("\\d+(\\.\\d+)*");
m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
24K純帥 2011-11-01
  • 打赏
  • 举报
回复
这种用堆栈做的吧~
龙四 2011-11-01
  • 打赏
  • 举报
回复
逆波兰式不是这么搞的吧
风尘中国 2011-11-01
  • 打赏
  • 举报
回复
楼主,你既然是知道用堆栈去实现,那么可以将这个字符串拆分为字符数组,然后挨个判断是什么字符,然后进行入栈出栈等操作,你现在这样想没有什么必要,而且你分开存储了也丧失了字符的位置信息
qybao 2011-11-01
  • 打赏
  • 举报
回复
中缀表达式转后缀表达式
分割字符看你想怎么解析,也就是说 2.5合法,2. 5算不算合法的问题,如果中间的存在空格可以合法,那么去掉所有的空格后,在把字符串变成字符数组,然后遍历该字符数组即可,如果中间存在空格算不合法,那么做法一样,只是在遍历的过程中要自己判断空格符
给段小例子

import java.util.*;

public class Test {
static final String symbol = "+-*/()"; //运算符
static final String[] priority = {"+-", "*/", "()"}; //运算符优先级

static Comparator<String> comp = new Comparator<String>() { //运算符比较器
public int compare(String s1, String s2) {
int n1=0, n2=0;
for (int i=0; i<priority.length; i++) {
if (priority[i].indexOf(s1) >= 0) {n1 = i;}
if (priority[i].indexOf(s2) >= 0) {n2 = i;}
}
return (n1 - n2);
}
};

public static void main(String[] args) throws Throwable {
String exp = "2.5*4+(2*5)";
List<String> list = analyze(exp); //中缀转后缀
System.out.println(list);
double result = cacl(list); //计算结果
System.out.printf("%.2f\n", result);
}

public static List<String> analyze(String exp) throws Exception { //分析
if (exp == null) {
throw new Exception ("illegal parameter.");
}
exp = exp.replaceAll("\\s*", ""); //去掉所有的空格(为了方便中间存在空格算合法)


List<String> list = new ArrayList<String>();
Stack<String> sym = new Stack<String>();

StringBuilder buf = new StringBuilder();
for (char c : exp.toCharArray()) {
if (symbol.indexOf(c) >= 0) { //如果是运算符
if (buf.length() > 0) { //如果有操作数
String v = buf.toString();
if (! v.matches("\\d+([.]\\d+)?")) {
throw new Exception ("illegal varaible("+v+").");
}
list.add(v);
buf.delete(0, buf.length());
}

if (c == '(') {
sym.push(String.valueOf(c));
} else if (c == ')') {
String last = "";
while (sym.size() > 0) {
last = sym.pop();
if (last.equals("(")) {
break;
} else {
list.add(last);
}
}
if (!"(".equals(last)) {
throw new Exception ("illigal express.");
}
} else if (sym.size() > 0) {
String s = String.valueOf(c);
String last = sym.peek();
if (last.equals("(") || comp.compare(s, last) > 0) {
sym.push(s);
} else {
last = sym.pop();
list.add(last);
sym.push(s);
}
} else {
sym.push(String.valueOf(c));
}
} else { //不是运算符则当作操作数(因为已经去除所有空格,这里不再需要判断空格)
buf.append(c);
}
}

if (buf.length() > 0) {
list.add(buf.toString());
}

while (sym.size() > 0) {
String last = sym.pop();
if ("()".indexOf(last) >= 0) {
throw new Exception ("illigal express.");
}
list.add(last);
}

return list;
}

public static double cacl(List<String> list) throws Exception { //计算
Stack<Double> val = new Stack<Double>();
double result = 0;
while (list.size() > 0) {
String s = list.remove(0);
if (symbol.indexOf(s) >= 0) {
double d1 = val.pop();
double d2 = val.pop();
if ("+".equals(s)) {
result = d2 + d1;
} else if ("-".equals(s)) {
result = d2 - d1;
} else if ("*".equals(s)) {
result = d2 * d1;
} else if ("/".equals(s)) {
result = d2 / d1;
} else {
throw new Exception ("illigal symbol("+s+").");
}
val.push(result);
} else {
if (!s.matches("\\d+([.]\\d+)?")) {
throw new Exception ("illigal variable("+s+").");
}
val.push(Double.valueOf(s));
}
}

return result;
}
}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧