java里使用面向对象的思想编写一个计算器类(Calculator),可以实现两个数的加、减、乘、除运算

 我来答
fuhaiwei
2013-04-06 · TA获得超过3409个赞
知道大有可为答主
回答量:974
采纳率:75%
帮助的人:1388万
展开全部
package calculate;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Calculator {
public static void main(String[] args) {
System.out.println(calculate("x=1+2+3+4,y=1*2*3*4"));
System.out.println(new Calculator("2*2*3.1415926"));
System.out.println(new Calculator("(10+10)*10-10/10"));
System.out.println(new Calculator("2^10"));
System.out.println(new Calculator("10!"));
System.out.println(new Calculator("(2*(3+2))!"));
}
public static String calculate(String exp) {
return new Calculator(exp).calculate();
}
private static String regex_num = "(\\d+(\\.\\d+)?)";
private String exp;
private String result;
public Calculator(String exp) {
this.exp = exp;
this.result = exp.replaceAll("\\s+", "");
}
public String calculate() {
calculateSub();
calcuateFactorial();
calcuate("\\^", Handler.Power);
calcuate("\\*", Handler.Multiply);
calcuate("/", Handler.Divide);
calcuate("\\+", Handler.Add);
calcuate("\\-", Handler.Subtract);
return result;
}
private void calculateSub() {
Pattern pattern = Pattern.compile("\\(([^\\(\\)]+)\\)");
Matcher matcher = pattern.matcher(result);
while (matcher.reset(result).find()) {
updateResult(matcher, calculate(matcher.group(1)));
}
}
private void calcuateFactorial() {
Pattern pattern = Pattern.compile(regex_num + "!");
Matcher matcher = pattern.matcher(result);
while (matcher.reset(result).find()) {
BigDecimal decimal = new BigDecimal(result.substring(0, result.length() - 1));
if (decimal.toString().indexOf(".") > -1) {
throw new RuntimeException("when a!, a must is a integer");
}
updateResult(matcher, factorial(decimal.intValue()));
}
}
public static BigInteger factorial(int num) {
if (num < 1) {
throw new RuntimeException("num can't < 1");
}
if (num == 1) {
return BigInteger.ONE;
} else {
BigInteger bigInteger = BigInteger.valueOf(num);
return bigInteger.multiply(factorial(num - 1));
}
}
private void calcuate(String regex_sign, Handler handler) {
Pattern pattern = Pattern.compile(regex_num + regex_sign + regex_num);
Matcher matcher = pattern.matcher(result);
while (matcher.reset(result).find()) {
String[] split = matcher.group().split(regex_sign);
BigDecimal decimal = new BigDecimal(split[0]);
BigDecimal decimal2 = new BigDecimal(split[1]);
updateResult(matcher, handler.calculate(decimal, decimal2));
}
}
private void updateResult(Matcher matcher, Object value) {
result = result.substring(0, matcher.start()) + value + result.substring(matcher.end());
}
public String toString() {
return exp + "=" + calculate();
}
public interface Handler {
public BigDecimal calculate(BigDecimal decimal, BigDecimal decimal2);
public static final Handler Add = new Handler() {
public BigDecimal calculate(BigDecimal decimal, BigDecimal decimal2) {
return decimal.add(decimal2);
}
};
public static final Handler Subtract = new Handler() {
public BigDecimal calculate(BigDecimal decimal, BigDecimal decimal2) {
return decimal.subtract(decimal2);
}
};
public static final Handler Multiply = new Handler() {
public BigDecimal calculate(BigDecimal decimal, BigDecimal decimal2) {
return decimal.multiply(decimal2);
}
};
public static final Handler Divide = new Handler() {
public BigDecimal calculate(BigDecimal decimal, BigDecimal decimal2) {
if (decimal2.equals(BigDecimal.ZERO)) {
throw new RuntimeException("when a / b, b can't be zero");
}
return decimal.divide(decimal2);
}
};
public static final Handler Power = new Handler() {
public BigDecimal calculate(BigDecimal decimal, BigDecimal decimal2) {
if (decimal2.toString().indexOf(".") > -1) {
throw new RuntimeException("when a ^ b, b must is a integer");
}
return decimal.pow(decimal2.intValue());
}
};
}
}
刚才的用到了一些工具类,现在把它去掉
更多追问追答
追问
看不懂呀,我只是初学    JAVA,能不能按我的请求做下,谢谢了
追答

哦,你是初学啊,我还以为你要做一个计算字符串的计算器呢,我明白了,马上写一个简单的。


public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
return a / b;
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式