Android開發(fā)中計算器的sin、cos及tan值計算問題分析
本文實例講述了Android開發(fā)中計算器的sin、cos及tan值計算問題。分享給大家供大家參考,具體如下:
接到一個需求 :要求計算器sin90=1,拿到知道很疑問 難道不等于一么?測試了四五個手機 ,有的滿足,有的sin90=0.8939…。查了api文檔后發(fā)現(xiàn) jdk中Math.sin/cos/tan ()求值采用弧度值,目前覺大部分手機計算器 如果滿足sin(90)=1就不會滿足sin(pi/2)=1,因為其算法如果轉換弧度值(x/180*pi).當輸入弧度值算時會變?yōu)閟in(弧度值/180*pi)使結果錯誤。實現(xiàn)計算器算法使可分sin中是否含pi來進行不同的處理
我的解決辦法如下:
修改代碼途徑
\packages\apps\Calculator\src\com\android\calculator\CalculatorExpressionEvaluator.java
部分源代碼:
輸入的算式經過這個方法傳入,然后轉過另一個類求出計算值,該類在位置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*”
所以我在代入前加了一個正則表達式過濾
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:這里再為大家推薦幾款計算工具供大家進一步參考借鑒:
在線一元函數(shù)(方程)求解計算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi
科學計算器在線使用_高級計算器在線計算:
http://tools.jb51.net/jisuanqi/jsqkexue
在線計算器_標準計算器:
http://tools.jb51.net/jisuanqi/jsq
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android開發(fā)筆記之:在ImageView上繪制圓環(huán)的實現(xiàn)方法
本篇文章是對Android中在ImageView上繪制圓環(huán)的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android中新引進的Google Authenticator驗證系統(tǒng)工作原理淺析
這篇文章主要介紹了Android中新引進的Google Authenticator驗證系統(tǒng)工作原理淺析,需要的朋友可以參考下2014-10-10
Android開發(fā)中的數(shù)據(jù)庫事務用法分析
這篇文章主要介紹了Android開發(fā)中的數(shù)據(jù)庫事務用法,分析了Android數(shù)據(jù)庫事務的功能、使用方法與相關注意事項,需要的朋友可以參考下2016-06-06
基于android studio的layout的xml文件的創(chuàng)建方式
這篇文章主要介紹了基于android studio的layout的xml文件的創(chuàng)建方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android開發(fā)之imageView圖片按比例縮放的實現(xiàn)方法
這篇文章主要介紹了Android開發(fā)之imageView圖片按比例縮放的實現(xiàn)方法,較為詳細的分析了Android中ImageView控件的scaleType屬性控制圖片縮放的具體用法,需要的朋友可以參考下2016-01-01
Android 實現(xiàn)IOS 滾輪選擇控件的實例(源碼下載)
這篇文章主要介紹了 Android 實現(xiàn)IOS 滾輪選擇控件的實例(源碼下載)的相關資料,需要的朋友可以參考下2017-03-03

