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

首頁 > 系統 > Android > 正文

Android開發中計算器的sin、cos及tan值計算問題分析

2019-10-22 18:23:09
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發中計算器的sin、cos及tan值計算問題。分享給大家供大家參考,具體如下:

接到一個需求 :要求計算器sin90=1,拿到知道很疑問 難道不等于一么?測試了四五個手機 ,有的滿足,有的sin90=0.8939…。查了api文檔后發現 jdk中Math.sin/cos/tan ()求值采用弧度值,目前覺大部分手機計算器 如果滿足sin(90)=1就不會滿足sin(pi/2)=1,因為其算法如果轉換弧度值(x/180*pi).當輸入弧度值算時會變為sin(弧度值/180*pi)使結果錯誤。實現計算器算法使可分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了!

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 易门县| 永城市| 石屏县| 华安县| 青冈县| 沙坪坝区| 玉龙| 吉安县| 红桥区| 宿松县| 华阴市| 六盘水市| 泾川县| 泊头市| 鹿邑县| 临江市| 隆子县| 平顶山市| 象山县| 斗六市| 松阳县| 唐河县| 富民县| 河东区| 明溪县| 泗水县| 夏津县| 贵州省| 南投市| 舒城县| 砀山县| 临安市| 鄂托克旗| 彰化县| 衡南县| 南皮县| 河北省| 连云港市| 烟台市| 烟台市| 凌源市|