利用数学公式计算数值

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Stack;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

public class FormulaComputUtil {

	public static String comput(String formula) {
		boolean isBigDecimal = formula.contains(".") || formula.contains("/");
		char[] charSeq=formula.toCharArray();
		Stack s1 = new Stack();
		Stack s2 = new Stack();
		StringBuilder sb = new StringBuilder();
		char prev='+';
		for(char c : charSeq) {
			switch (c) {
			case ' ':break;case '+':
				if('+'==prev||'-'==prev||'*'==prev||'/'==prev)break;
				case '-':
					if('+'==prev||'-'==prev||'*'==prev||'/'==prev) {
						sb.append(c);
						break;
					}
				case '*':case '/':case '(':case ')':
				if(sb.length()>0) {
					s2.push(sb.toString());
					sb=new StringBuilder();
				}
				handOpr(s1,s2,c);
				break;
			default:
				sb.append(c);
				break;
			}
			prev=c;
		}
		if(sb.length()>0)s2.push(sb.toString());sb=null;
		while(!s1.isEmpty())s2.push(String.valueOf(s1.pop()));
		return computNN(s2,isBigDecimal);
	}
	private static void handOpr(Stack s1,Stack s2,char opr) {
		char c = '\0';
		if(')'==opr){
			while('('!=(c=s1.pop()))if(')'!=c) {
				s2.push(String.valueOf(c));
			}
		}else if(s1.isEmpty()||'('==(c=s1.peek())||oprLv(opr)>oprLv(c)) {
			s1.push(opr);
		}else {
			s2.push(String.valueOf(s1.pop()));
			handOpr(s1,s2,opr);
		}
	}
	private static int oprLv(char opr) {
		switch (opr) {
		case '+':return 0;
		case '-':return 0;
		case '*':return 1;
		case '/':return 1;
		case '(':return 2;
		case ')':return 2;
		default:return -1;
		}
	}

	private static String computBigDecimal(String num1,String num2,String opr) {
		switch (opr) {
		case "+":
			return new BigDecimal(num1).add(new BigDecimal(num2)).toString();
		case "-":
			return new BigDecimal(num1).subtract(new BigDecimal(num2)).toString();
		case "*":
			return new BigDecimal(num1).multiply(new BigDecimal(num2)).toString();
		case "/":
			return new BigDecimal(num1).divide(new BigDecimal(num2)).toString();
		default:
			return "0";
		}
	}
	private static String computInteger(String num1,String num2,String opr) {
		switch (opr) {
		case "+":
			return String.valueOf(Integer.parseInt(num1)+Integer.parseInt(num2));
		case "-":
			return String.valueOf(Integer.parseInt(num1)-Integer.parseInt(num2));
		case "*":
			return String.valueOf(Integer.parseInt(num1)*Integer.parseInt(num2));
		case "/":
			return String.valueOf(Integer.parseInt(num1)/Integer.parseInt(num2));
		default:
			return "0";
		}
	}
	
	private static String computNN(Stack strs,boolean isBigDecimal) {
		Stack stacks = new Stack();
		String a,b;
		for(String str : strs) {
			switch (str) {
			case "":break;
			case "+":
			case "-":
			case "*":
			case "/":
				b=stacks.pop();
				a=stacks.pop();
				stacks.push(isBigDecimal?computBigDecimal(a, b, str):computInteger(a, b, str));
				break;
			default:
				stacks.push(str);
				break;
			}
		}
		return stacks.pop();
	}
}

 

你可能感兴趣的:(利用数学公式计算数值)