java实现四则运算,难点主要在理解加减乘除优先级以及使用递归
2016-01-07 16:02
791 查看
采用递归的方式实现基本的四则运算。
首先弄清楚四则运算的优先级,比如一个混杂加法和减法的式子,减法的优先级要高于加法,也就是你从左往右算,先算减法是正确的,先算加法会得到错误的答案。比如3-2+1,先算加法,得到的结果是0。
同理,除法优先级高于乘法,乘法优先级高于减法。
而在递归运算中,是递归底层计算后,一层层把结果往外翻,所以,应该解析顺序应该是:加、减、乘、除。与优先级刚好相反。理解了这里基本就可以完成四则运算的算法了。
下面是代码实现(增加了括号的处理):
import java.math.BigDecimal;
public class CalculateNum {
public BigDecimal cal(String str) {
if (str == null) {
return null;
}
String fuhao = "";
int index = 0;
str = str.replaceAll("--", "+"); // 等价替换;
str = str.replaceAll(" ", ""); // 去掉空格
fuhao = "(";
index = str.lastIndexOf(fuhao);
if (index >= 0) {
int rightIndex = str.indexOf(")", index);
String left = str.substring(0, index);
String right = "";
if (rightIndex + 1 < str.length()) {
right = str.substring(rightIndex + 1);
}
BigDecimal middle = cal(str.substring(index + 1, rightIndex));
return cal(left + middle + right);
}
fuhao = "+";
index = str.lastIndexOf(fuhao);
if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.add(right);
}
fuhao = "-";
index = str.lastIndexOf(fuhao);
if (index == 0) { // 负数处理
BigDecimal result = cal(str.substring(index + 1));
if (result.compareTo(new BigDecimal("0")) == -1) { // 小于0
return result.abs(); // 绝对值
} else {
return result.negate(); // 相反数
}
} else if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.subtract(right);
}
fuhao = "*";
index = str.lastIndexOf(fuhao);
if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.multiply(right);
}
fuhao = "/";
index = str.lastIndexOf(fuhao);
if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.divide(right);
}
return new BigDecimal(str);
}
}
首先弄清楚四则运算的优先级,比如一个混杂加法和减法的式子,减法的优先级要高于加法,也就是你从左往右算,先算减法是正确的,先算加法会得到错误的答案。比如3-2+1,先算加法,得到的结果是0。
同理,除法优先级高于乘法,乘法优先级高于减法。
而在递归运算中,是递归底层计算后,一层层把结果往外翻,所以,应该解析顺序应该是:加、减、乘、除。与优先级刚好相反。理解了这里基本就可以完成四则运算的算法了。
下面是代码实现(增加了括号的处理):
import java.math.BigDecimal;
public class CalculateNum {
public BigDecimal cal(String str) {
if (str == null) {
return null;
}
String fuhao = "";
int index = 0;
str = str.replaceAll("--", "+"); // 等价替换;
str = str.replaceAll(" ", ""); // 去掉空格
fuhao = "(";
index = str.lastIndexOf(fuhao);
if (index >= 0) {
int rightIndex = str.indexOf(")", index);
String left = str.substring(0, index);
String right = "";
if (rightIndex + 1 < str.length()) {
right = str.substring(rightIndex + 1);
}
BigDecimal middle = cal(str.substring(index + 1, rightIndex));
return cal(left + middle + right);
}
fuhao = "+";
index = str.lastIndexOf(fuhao);
if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.add(right);
}
fuhao = "-";
index = str.lastIndexOf(fuhao);
if (index == 0) { // 负数处理
BigDecimal result = cal(str.substring(index + 1));
if (result.compareTo(new BigDecimal("0")) == -1) { // 小于0
return result.abs(); // 绝对值
} else {
return result.negate(); // 相反数
}
} else if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.subtract(right);
}
fuhao = "*";
index = str.lastIndexOf(fuhao);
if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.multiply(right);
}
fuhao = "/";
index = str.lastIndexOf(fuhao);
if (index > 0) {
BigDecimal left = cal(str.substring(0, index));
BigDecimal right = cal(str.substring(index + 1));
return left.divide(right);
}
return new BigDecimal(str);
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树