国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > Java > 正文

java實(shí)現(xiàn)字符串四則運(yùn)算公式解析工具類的方法

2024-07-14 08:41:39
字體:
供稿:網(wǎng)友

項(xiàng)目中用到用戶定義運(yùn)算公式進(jìn)行就算的需求,這樣需要進(jìn)行字符串四則運(yùn)算解析,下面提供字符串公式四則運(yùn)算解析與計(jì)算工具類,需要的同學(xué)可參考。

工具類如下:FormulaCalculator.java:

package org.nercita.bcp.record.util; import java.util.ArrayList;import java.util.LinkedList; /** * @author zhangwenchao * @since 2016-08-26 * 公式計(jì)算的工具類 */public class FormulaCalculator {		private static boolean isRightFormat = true; 	public static double getResult(String formula){ 		double returnValue = 0; 		try{ 			returnValue = doAnalysis(formula); 		}catch(NumberFormatException nfe){ 			System.out.println("公式格式有誤,請(qǐng)檢查:" + formula); 		}catch(Exception e){ 			e.printStackTrace(); 		} 		if(!isRightFormat){ 			System.out.println("公式格式有誤,請(qǐng)檢查:" + formula); 		} 		return returnValue;		} 	private static double doAnalysis(String formula){		double returnValue = 0; 		LinkedList<Integer> stack = new LinkedList<Integer>(); 		int curPos = 0; 		String beforePart = ""; 		String afterPart = ""; 		String calculator = ""; 		isRightFormat = true; 		while(isRightFormat&&(formula.indexOf('(') >= 0||formula.indexOf(')') >= 0)){						curPos = 0; 			for(char s : formula.toCharArray()){ 				if(s == '('){  					stack.add(curPos); 				}else if(s == ')'){  					if(stack.size() > 0){  						beforePart = formula.substring(0, stack.getLast());  						afterPart = formula.substring(curPos + 1);  						calculator = formula.substring(stack.getLast() + 1, curPos);  						formula = beforePart + doCalculation(calculator) + afterPart;  						stack.clear();  						break;  					}else{  						System.out.println("有未關(guān)閉的右括號(hào)!");  						isRightFormat = false;  					} 				} 				curPos++; 			} 			if(stack.size() > 0){ 				System.out.println("有未關(guān)閉的左括號(hào)!"); 				break; 			} 		} 		if(isRightFormat){ 			returnValue = doCalculation(formula); 		} 		return returnValue; 	} 		private static double doCalculation(String formula) { 		ArrayList<Double> values = new ArrayList<Double>(); 		ArrayList<String> operators = new ArrayList<String>(); 		int curPos = 0; 		int prePos = 0;		int minus = 0;				for (char s : formula.toCharArray()) { 			 if ((s == '+' || s == '-' || s == '*' || s == '/') && minus !=0 && minus !=2) { 								 			 				 values.add(Double.parseDouble(formula.substring(prePos, curPos).trim())); 								 operators.add("" + s); 								 prePos = curPos + 1;				 				 minus = minus +1;			 }else{				 				 minus =1;				 			 }			 curPos++; 				} 		values.add(Double.parseDouble(formula.substring(prePos).trim())); 		char op; 		for (curPos = 0; curPos <= operators.size() - 1; curPos++) {										op = operators.get(curPos).charAt(0); 			switch (op) { 			case '*': 				values.add(curPos, values.get(curPos) * values.get(curPos + 1)); 				values.remove(curPos + 1); 				values.remove(curPos + 1); 				operators.remove(curPos); 				curPos = -1;				break; 			case '/': 				values.add(curPos, values.get(curPos) / values.get(curPos + 1)); 				values.remove(curPos + 1); 				values.remove(curPos + 1); 				operators.remove(curPos); 				curPos = -1;				break; 			} 		} 		for (curPos = 0; curPos <= operators.size() - 1; curPos++) { 			op = operators.get(curPos).charAt(0); 			switch (op) { 			case '+': 				values.add(curPos, values.get(curPos) + values.get(curPos + 1)); 				values.remove(curPos + 1); 				values.remove(curPos + 1); 				operators.remove(curPos); 				curPos = -1;				break; 			case '-': 				values.add(curPos, values.get(curPos) - values.get(curPos + 1)); 				values.remove(curPos + 1); 				values.remove(curPos + 1); 				operators.remove(curPos); 				curPos = -1;				break; 			} 		} 		return values.get(0).doubleValue();	} 	public static void main(String[] args) {	 		System.out.println(FormulaCalculator.getResult("3-(4*5)+5"));			System.out.println(FormulaCalculator.getResult("7/2-(-4)"));					System.out.println(FormulaCalculator.getResult("1287763200000-1276272000000")/(3600*24*1000));	}	}

支持四則運(yùn)算,同時(shí)支持負(fù)數(shù)解析。

另附,小數(shù)數(shù)據(jù)保留位數(shù)工具類,SetNumberPrecision.java

package org.nercita.bcp.record.util; import java.text.DecimalFormat; /**  * @author zhangwenchao * 小數(shù)點(diǎn) 精度的工具類 */ public class SetNumberPrecision {			public static String setNumberPrecision(double x,int Number){		 		String p="#########0";		 		if(Number==0){			 			p="#########0";		 		}else{		  			p="#########0.";		  			for(int i=0;i<Number;i++){//for的巧妙運(yùn)用		   				p=p+"0"; 		  			}		 		}		  		DecimalFormat f = new DecimalFormat(p); 		  		String s = f.format(x).toString(); 		  		return s;		 	}	}

以上這篇java實(shí)現(xiàn)字符串四則運(yùn)算公式解析工具類的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂托克前旗| 沭阳县| 饶平县| 小金县| 大邑县| 嘉鱼县| 日照市| 西昌市| 光山县| 乐东| 汤原县| 龙里县| 威宁| 手游| 云和县| 安远县| 金溪县| 仙桃市| 垣曲县| 普兰店市| 泗阳县| 思南县| 繁昌县| 手机| 乐山市| 嵩明县| 太原市| 贵州省| 大宁县| 曲周县| 昌邑市| 雷山县| 曲沃县| 梨树县| 凤凰县| 邵东县| 巨鹿县| 行唐县| 岳普湖县| 阳江市| 拉孜县|