四则运算(小清新)

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>你真棒</title>
<style type="text/css">
body 
{
    background-image: url(start.jpg); 
    background-size:1200px 700px;
    text-align:center;
}
</style>
</head>
<body>
<div style="text-align: center; font-family: 微软雅黑; font-size: 50px; color: #C03; "><strong><em>四则运算</em></strong></div>
<div style="text-align: center; font-family: 微软雅黑; font-size: 15px; color: #C03; "><strong><em>Welcome To User The System</em></strong></div>
<br/>

<form  name="form1" method="post" action="arithmetic.jsp" >
<blockquote>
  <blockquote>
    <blockquote>
      <blockquote>
        <blockquote>
          <blockquote>
            <blockquote>
             <blockquote>
              <blockquote>
               <blockquote>
               <div style="color: #C03">
               <p>选择题目类型:
			<input name="elementNum" value="2" type="radio" />2元运算
			<input name="elementNum" value="3" type="radio" checked="checked"/>3元运算<br><br>
              <strong>题目数量:
				<select name="count">
					<option>10</option>
					<option>20</option>
					<option>30</option>
					<option>40</option>
					<option>50</option>
					<option>60</option>
				</select>
				
			<br /><br />
			限定时间:
				<select name="timelimit">
					<option>1</option>
					<option>2</option>
					<option>3</option>
					<option>4</option>
					<option>5</option>
					<option>6</option>
				</select>
				分钟
		<br />
	
                    <input name="" type="submit" value="开始答题" style="margin:0 auto;display:block;" />
                    
                 </strong>
              </p>
              </div>
              </blockquote>
              </blockquote>
              </blockquote>
            </blockquote>
          </blockquote>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
</blockquote>

</form>
</body>
</html>

 

public class Arithmetic {
	public static void main(String args[]){
		System.out.println(arithmetic("2.2+((3+4)*2-22)/2*3.2"));
	}
	public static double arithmetic(String exp){
		String result = parseExp(exp).replaceAll("[\[\]]", "");
		return Double.parseDouble(result);
	}
	/**
	 * 解析计算四则运算表达式,例:2+((3+4)*2-22)/2*3
	
	 */
	public static String parseExp(String expression){
		//String numberReg="^((?!0)\d+(\.\d+(?<!0))?)|(0\.\d+(?<!0))$";
		expression=expression.replaceAll("\s+", "").replaceAll("^\((.+)\)$", "$1");
		String checkExp="\d";
		String minExp="^((\d+(\.\d+)?)|(\[\-\d+(\.\d+)?\]))[\+\-\*\/]((\d+(\.\d+)?)|(\[\-\d+(\.\d+)?\]))$";
		//最小表达式计算
		if(expression.matches(minExp)){
			String result=calculate(expression);
			
			return Double.parseDouble(result)>=0?result:"["+result+"]";
		}
		//计算不带括号的四则运算
		String noParentheses="^[^\(\)]+$";
		String priorOperatorExp="(((\d+(\.\d+)?)|(\[\-\d+(\.\d+)?\]))[\*\/]((\d+(\.\d+)?)|(\[\-\d+(\.\d+)?\])))";
		String operatorExp="(((\d+(\.\d+)?)|(\[\-\d+(\.\d+)?\]))[\+\-]((\d+(\.\d+)?)|(\[\-\d+(\.\d+)?\])))";
		if(expression.matches(noParentheses)){
			Pattern patt=Pattern.compile(priorOperatorExp);
			Matcher mat=patt.matcher(expression);
			if(mat.find()){
				String tempMinExp=mat.group();
				expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp));
			}else{
				patt=Pattern.compile(operatorExp);
				mat=patt.matcher(expression);
				
				if(mat.find()){
					String tempMinExp=mat.group();
					expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp));
				}
			}
			return parseExp(expression);
		}
		//计算带括号的四则运算
		String minParentheses="\([^\(\)]+\)";
		Pattern patt=Pattern.compile(minParentheses);
		Matcher mat=patt.matcher(expression);
		if(mat.find()){
			String tempMinExp=mat.group();
			expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp));
		}
		return parseExp(expression);
	}
	/**
	 * 计算最小单位四则运算表达式(两个数字)
	 * @param exp
	 * @return
	 */
	public static String calculate(String exp){
		exp=exp.replaceAll("[\[\]]", "");
		String number[]=exp.replaceFirst("(\d)[\+\-\*\/]", "$1,").split(",");
		BigDecimal number1=new BigDecimal(number[0]);
		BigDecimal number2=new BigDecimal(number[1]);
		BigDecimal result=null;
		
		String operator=exp.replaceFirst("^.*\d([\+\-\*\/]).+$", "$1");
		if("+".equals(operator)){
			result=number1.add(number2);
		}else if("-".equals(operator)){
			result=number1.subtract(number2);
		}else if("*".equals(operator)){
			result=number1.multiply(number2);
		}else if("/".equals(operator)){
			result=number1.divide(number2);
		}
		
		return result!=null?result.toString():null;
	}
}

  

原文地址:https://www.cnblogs.com/xiaojq/p/7994217.html