Last active
August 29, 2015 14:00
-
-
Save ssd863419/8244212afb7e3d89fe2a to your computer and use it in GitHub Desktop.
carculator 簡易計算機
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class Calc { | |
public String calc(String s) { | |
// TODO: 其實只能去掉 運算符號 前後的空白, 這邊先不管去掉所有的開始寫 | |
// TODO: 開頭為"-"的計算式, 會出錯 | |
s = s.replaceAll("\\s+", ""); // 去掉所有空白 | |
Matcher matcher_chen_chu = Pattern.compile("(\\d+\\.?\\d*|\\.\\d+)[\\*/](\\d+\\.?\\d*|\\.\\d+)").matcher(s); | |
Matcher matcher_jia_jian = Pattern.compile("(\\d+\\.?\\d*|\\.\\d+)[\\+\\-](\\d+\\.?\\d*|\\.\\d+)").matcher(s); | |
if (matcher_chen_chu.find()) { // 如果找到 * 或是 / 就處理這段, 而且只處理一次 | |
String str = matcher_chen_chu.group(); | |
float[] num = str2IntAry(str); | |
if (str.contains("*")) { | |
s = s.replaceFirst("(\\d+\\.?\\d*|\\.\\d+)\\*(\\d+\\.?\\d*|\\.\\d+)", String.valueOf(num[0]*num[1])); | |
} else { | |
s = s.replaceFirst("(\\d+\\.?\\d*|\\.\\d+)[/](\\d+\\.?\\d*|\\.\\d+)", String.valueOf(num[0]/num[1])); | |
} | |
} else if (matcher_jia_jian.find()) { // 如果找到 + 或是 - 就處理這段, 而且只處理一次 | |
String str = matcher_jia_jian.group(); | |
float[] num = str2IntAry(str); | |
if (str.contains("+")) { | |
s = s.replaceFirst("(\\d+\\.?\\d*|\\.\\d+)\\+(\\d+\\.?\\d*|\\.\\d+)", String.valueOf(num[0]+num[1])); | |
} else { | |
s = s.replaceFirst("(\\d+\\.?\\d*|\\.\\d+)\\-(\\d+\\.?\\d*|\\.\\d+)", String.valueOf(num[0]-num[1])); | |
} | |
} else { // 什麼運算符號都沒找到 就返回去 | |
return s; | |
} | |
return calc(s); // 如果有找到運算符號 就繼續呼叫自己 在往下試 | |
} | |
// 方便拆分 運[pppppppppppppp;'符號前後的數字 | |
public float[] str2IntAry(String s) { | |
float[] result = new float[]{0, 0}; | |
String[] ary = s.split("[\\+\\-\\*/]"); | |
result[0] = Float.parseFloat(ary[0]); | |
result[1] = Float.parseFloat(ary[1]); | |
return result; | |
} | |
public static void main(String[] args) { | |
Calc c = new Calc(); | |
String s = "2.2*3+.2+3.30-2+2*3+6/2+2+3"; | |
System.out.println(c.calc(s)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment