Android開發(fā)中計算器的sin、cos及tan值計算問題分析
本文實(shí)例講述了Android開發(fā)中計算器的sin、cos及tan值計算問題。分享給大家供大家參考,具體如下:
接到一個需求 :要求計算器sin90=1,拿到知道很疑問 難道不等于一么?測試了四五個手機(jī) ,有的滿足,有的sin90=0.8939…。查了api文檔后發(fā)現(xiàn) jdk中Math.sin/cos/tan ()求值采用弧度值,目前覺大部分手機(jī)計算器 如果滿足sin(90)=1就不會滿足sin(pi/2)=1,因?yàn)槠渌惴ㄈ绻D(zhuǎn)換弧度值(x/180*pi).當(dāng)輸入弧度值算時會變?yōu)閟in(弧度值/180*pi)使結(jié)果錯誤。實(shí)現(xiàn)計算器算法使可分sin中是否含pi來進(jìn)行不同的處理
我的解決辦法如下:
修改代碼途徑
\packages\apps\Calculator\src\com\android\calculator\CalculatorExpressionEvaluator.java
部分源代碼:
輸入的算式經(jīng)過這個方法傳入,然后轉(zhuǎn)過另一個類求出計算值,該類在位置org.javia.arity.Symbols;(被封裝打不開,只能修改代入值)
public void evaluate(String expr, EvaluateCallback callback) {
expr = mTokenizer.getNormalizedExpression(expr);
// remove any trailing operators
while (expr.length() > 0 && "+-/*".indexOf(expr.charAt(expr.length() - 1)) != -1) {
expr = expr.substring(0, expr.length() - 1);
}
/*try {
if (expr.length() == 0 || Double.valueOf(expr) != null) {
callback.onEvaluate(expr, null, Calculator.INVALID_RES_ID);
return;
}
} catch (NumberFormatException e) {
// expr is not a simple number
}*/
if (expr.length() == 0) {
callback.onEvaluate(expr, null, Calculator.INVALID_RES_ID);
return;
}
try {
/*************代值的代碼在這里**********/
double result = mSymbols.eval(expr);
if (Double.isNaN(result)) {
callback.onEvaluate(expr, null, R.string.error_nan);
} else {
/* The arity library uses floating point arithmetic when evaluating the expression
leading to precision errors in the result. The method doubleToString hides these
errors; rounding the result by dropping N digits of precision.*/
final String resultString = mTokenizer.getLocalizedExpression(
Util.doubleToString(result, MAX_DIGITS, ROUNDING_DIGITS));
callback.onEvaluate(expr, resultString, Calculator.INVALID_RES_ID);
}
} catch (SyntaxException e) {
callback.onEvaluate(expr, null, R.string.error_syntax);
}
}
我的解決思路是:
斷某該字符串是否含有”sin( ” ,” cos( ” ,”tan(”字符,并且不含“sin(pi”,“cos(pi”,“tan(pi”, 如果有,在每個該字符后面添加字符串”pi/180*”
所以我在代入前加了一個正則表達(dá)式過濾
public void evaluate(String expr, EvaluateCallback callback) {
expr = mTokenizer.getNormalizedExpression(expr);
// remove any trailing operators
while (expr.length() > 0 && "+-/*".indexOf(expr.charAt(expr.length() - 1)) != -1) {
expr = expr.substring(0, expr.length() - 1);
}
/*try {
if (expr.length() == 0 || Double.valueOf(expr) != null) {
callback.onEvaluate(expr, null, Calculator.INVALID_RES_ID);
return;
}
} catch (NumberFormatException e) {
// expr is not a simple number
}*/
if (expr.length() == 0) {
callback.onEvaluate(expr, null, Calculator.INVALID_RES_ID);
return;
}
try {
/************** 添加的過濾代碼 ***********/
expr=expr.replaceAll("(?<=(sin|cos|tan)[(])(?!pi)","pi/180*");
double result = mSymbols.eval(expr);
if (Double.isNaN(result)) {
callback.onEvaluate(expr, null, R.string.error_nan);
} else {
/* The arity library uses floating point arithmetic when evaluating the expression
leading to precision errors in the result. The method doubleToString hides these
errors; rounding the result by dropping N digits of precision.*/
final String resultString = mTokenizer.getLocalizedExpression(
Util.doubleToString(result, MAX_DIGITS, ROUNDING_DIGITS));
callback.onEvaluate(expr, resultString, Calculator.INVALID_RES_ID);
}
} catch (SyntaxException e) {
callback.onEvaluate(expr, null, R.string.error_syntax);
}
}
然后就能滿足sin90=1了!
PS:這里再為大家推薦幾款計算工具供大家進(jìn)一步參考借鑒:
在線一元函數(shù)(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學(xué)計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue
在線計算器_標(biāo)準(zhǔn)計算器:
http://tools.jb51.net/jisuanqi/jsq
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- android實(shí)現(xiàn)簡易計算器
- Android開發(fā)實(shí)現(xiàn)的標(biāo)準(zhǔn)體重計算器功能示例
- android計算器簡單實(shí)現(xiàn)代碼
- 簡單實(shí)現(xiàn)Android計算器功能
- Android計算器編寫代碼
- 從零開始學(xué)android實(shí)現(xiàn)計算器功能示例分享(計算器源碼)
- Android計算器簡單邏輯實(shí)現(xiàn)實(shí)例分享
- android計算器代碼示例分享
- android計時器,時間計算器的實(shí)現(xiàn)方法
- Android Studio實(shí)現(xiàn)簡易計算器
相關(guān)文章
Android開發(fā)筆記之:在ImageView上繪制圓環(huán)的實(shí)現(xiàn)方法
本篇文章是對Android中在ImageView上繪制圓環(huán)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android百度地圖應(yīng)用開發(fā)基礎(chǔ)知識
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用開發(fā)基礎(chǔ)知識,為開發(fā)百度地圖應(yīng)用做準(zhǔn)備,感興趣的小伙伴們可以參考一下2016-06-06
Android中新引進(jìn)的Google Authenticator驗(yàn)證系統(tǒng)工作原理淺析
這篇文章主要介紹了Android中新引進(jìn)的Google Authenticator驗(yàn)證系統(tǒng)工作原理淺析,需要的朋友可以參考下2014-10-10
Android開發(fā)中的數(shù)據(jù)庫事務(wù)用法分析
這篇文章主要介紹了Android開發(fā)中的數(shù)據(jù)庫事務(wù)用法,分析了Android數(shù)據(jù)庫事務(wù)的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-06-06
基于Android實(shí)現(xiàn)百度地圖定位過程詳解
這篇文章主要介紹了基于Android實(shí)現(xiàn)百度地圖定位過程詳解,需要的朋友可以參考下2015-11-11
基于android studio的layout的xml文件的創(chuàng)建方式
這篇文章主要介紹了基于android studio的layout的xml文件的創(chuàng)建方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Flutter實(shí)現(xiàn)編寫富文本Text的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過Flutter實(shí)現(xiàn)編寫富文本Text,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以參考一下2022-11-11
Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之imageView圖片按比例縮放的實(shí)現(xiàn)方法,較為詳細(xì)的分析了Android中ImageView控件的scaleType屬性控制圖片縮放的具體用法,需要的朋友可以參考下2016-01-01
Android Service總結(jié)及詳細(xì)介紹
本文主要介紹Android Service的知識,這里整理了詳細(xì)資料及簡單實(shí)現(xiàn)示例代碼,有需要的小伙伴可以參考下2016-09-09
Android 實(shí)現(xiàn)IOS 滾輪選擇控件的實(shí)例(源碼下載)
這篇文章主要介紹了 Android 實(shí)現(xiàn)IOS 滾輪選擇控件的實(shí)例(源碼下載)的相關(guān)資料,需要的朋友可以參考下2017-03-03

