package com.zk.zhengze;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Arithmetic {
private String ONE_NUM_REG = "((?<!//d)-)?//d+//.?+//d*";
public static void main(String[] args){
//System.out.println(Math.log(100));
//System.out.println(2^2);
String str = "74.2-0.5";
str = "-1/5+6*2+6";
str = "-1/(5+6)*2+6";
str = "1+[-5/2]-2";
str = "{2/(3+1)+1}+[-5/2]-2";
str = "{1+2*[5*(3-2)]/-5}123+31";
str = "{1+2*[5*(3-2)]/-asdf5}abc+31";
str = "{1+2*[5*(3-2)]/-5}+31*[1+2]";
//str = "asdf";
try {
System.out.println(str +" = "+new Arithmetic().arithmetic(str));
} catch (MyException e) {
System.out.println(e);
}
}
/**
* 复杂的运算(可以有小括号,中括号,大括号)
* */
public double arithmetic(String str) throws MyException{
if(isRight(str)){
str = str.replaceAll("//{", "(");
str = str.replaceAll("}", ")");
str = str.replaceAll("//[", "(");
str = str.replaceAll("]", ")");
String reg = "//(("+ONE_NUM_REG+"([//*/+-]"+ONE_NUM_REG+")*)//)";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
boolean isHave = m.find();
if(isHave){
do{
str = str.replaceFirst(reg,""+two(m.group(1)));
str = str.replaceFirst("--", "");//将返回值与前面的负号中和
m = p.matcher(str);
}
while(isHave = m.find());
}
}else{
throw new MyException("请输入正确的算式");
}
return two(str);
}
/**
* 简单的四则运算
* */
private double one (String str)throws MyException{
String reg = "("+ONE_NUM_REG+")([//*//+//-///])("+ONE_NUM_REG+")";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
if(m.find()){
char op = m.group(3).charAt(0);
double one = Double.parseDouble(m.group(1));
double two = Double.parseDouble(m.group(4));
switch(op){
case '+':
return one + two;
case '-':
return one - two;
case '*':
return one * two;
case '/':
return one / two;
}
}else{
try{
return Double.parseDouble(str);
}catch(Exception e){
return 0;
}
}
return 0;
}
/**
* 带小括号的运算
* */
private double two(String str)throws MyException{
String reg = "("+ONE_NUM_REG+")[//*///]("+ONE_NUM_REG+")";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(str);
while(m.find()){
str = str.replaceFirst(reg,""+one(m.group()));
m = p.matcher(str);
}
reg = "("+ONE_NUM_REG+")[+-]("+ONE_NUM_REG+")";
p = Pattern.compile(reg);
m = p.matcher(str);
while(m.find()){
str = str.replaceFirst(reg,""+one(m.group()));
str = str.replaceFirst("--", "-");
m = p.matcher(str);
}
return one(str);
}
/**
* 查看一个算式是否格式正确
* */
private boolean isRight(String str){
int[][] numOfSign = new int[3][2];
for(int i = 0 ;i<str.length();i++){
switch(str.charAt(i)){
case '{':
numOfSign[0][0]++;
break;
case '}':
numOfSign[0][1]++;
break;
case '(':
numOfSign[1][0]++;
break;
case ')':
numOfSign[1][1]++;
break;
case '[':
numOfSign[2][0]++;
break;
case ']':
numOfSign[2][1]++;
break;
}
}
boolean isRight = ((numOfSign[0][0] == numOfSign[0][1])&&
(numOfSign[1][0] == numOfSign[1][1])&&
(numOfSign[2][0] == numOfSign[2][1])); //查看符号是否配对
if(isRight){
String exceptionReg = "[^//d//*/+-//{//}//[//]//(//)]";
Pattern exception_pp = Pattern.compile(exceptionReg);
Matcher exception_mm = exception_pp.matcher(str);
isRight = isRight && !exception_mm.find(); //查看是否含有不是算式里的符号
if(isRight){
exceptionReg = "//d}//d"; //查看是否含有多余括号
exception_pp = Pattern.compile(exceptionReg);
exception_mm = exception_pp.matcher(str);
isRight = isRight && !exception_mm.find();
}
}
return isRight;
}
}
class MyException extends Exception{
private static final long serialVersionUID = 1L;
private String message;
MyException(String str){
//super();
message = str;
}
public String toString(){
return message;
}
}
大家一起优化一下这个程序,哈哈……
阅购网:http://www.yuegouok.com