为了账号安全,请及时绑定邮箱和手机立即绑定

实现计算器的连续运算

标签:
Android

简易计算器的实现过程请参照Android攻城狮的第一门课(入门篇)

另外也推荐去看看本人的另一篇手记:无论怎么点都不会崩的计算器


上面说的简易计算器就是只能实现单个运算符的计算,比如1+1, 3*2... 本篇手记主要论述如何实现有多个运算符时的连续运算。再说一句闲话,这篇手记的产生是由于一位慕友的提问,后来我花了点时间实现了这个功能,核心代码如下:

  /** btnEqual 按钮事件 */
  private String btnEqualEvent(String str) {
    if (str == null  "".equals(str)  str.endsWith(" "))
      return str;
    if (str.contains(" ")) {
      str = getResult(str);
    }
    return str;
  }

  private String getResult(String str) {
    if (!str.contains(" ")) return str;

    if (str.startsWith(" ")) {
      String rightE = str.substring(3);
      String startOperation = str.substring(1, 2);
      String leftNumStr;
      if (rightE.contains(" ")) {
        leftNumStr = rightE.substring(0, rightE.indexOf(" "));
      } else {
        leftNumStr = rightE;
      }
      if (leftNumStr.endsWith(".")) {
        if (leftNumStr.length() == 1) return str;
        if ("-".equals(startOperation))
          leftNumStr = "-"+leftNumStr+"0";
        else
          leftNumStr = leftNumStr+"0";
      } else if (leftNumStr.startsWith(".")) {
        if ("-".equals(startOperation))
          leftNumStr = "-0"+leftNumStr;
        else
          leftNumStr = "0"+leftNumStr;
      } else {
        if ("-".equals(startOperation))
          leftNumStr = "-"+leftNumStr;
      }
      if (rightE.contains(" ")) {
        return getResult((leftNumStr + rightE.substring(rightE.indexOf(" "))));
      } else {
        return leftNumStr;
      }
    }

    if (!str.contains("x") && !str.contains("÷")) { // 若表达式中无乘除,即单纯的加减运算
      int operationLeftSpaceIndex;
      operationLeftSpaceIndex = str.indexOf(" ");
      String operation = str.substring(operationLeftSpaceIndex+1, operationLeftSpaceIndex+2);
      String leftNumStr = str.substring(0, operationLeftSpaceIndex);
      String rightNumStr;
      String rightE = str.substring(operationLeftSpaceIndex+3);
      if (rightE.contains(" ")) {
        rightNumStr = rightE.substring(0, rightE.indexOf(" "));
        double leftNum = Double.parseDouble(leftNumStr);
        double rightNum = Double.parseDouble(rightNumStr);
        double result;
        if ("+".equals(operation)) {
          result = leftNum + rightNum;
        } else {
          result = leftNum - rightNum;
        }
        return getResult(result+rightE.substring(rightE.indexOf(" ")));
      } else { // 只有一个运算符的加减运算
        rightNumStr = rightE;
        double leftNum = Double.parseDouble(leftNumStr);
        double rightNum = Double.parseDouble(rightNumStr);
        double result;
        if ("+".equals(operation)) {
          result = leftNum + rightNum;
        } else {
          result = leftNum - rightNum;
        }
        if (str.contains(".")) return result+"";
        else return (int) result + "";
      }

    } else { // 混合运算
      int multiplyIndex = str.indexOf("x");
      int divideIndex = str.indexOf("÷");
      int firstOperationIndex = -1;
      if (multiplyIndex == -1) firstOperationIndex = divideIndex;
      if (divideIndex == -1) firstOperationIndex = multiplyIndex;
      if (firstOperationIndex == -1) firstOperationIndex = Math.min(multiplyIndex, divideIndex);
      String operation = str.substring(firstOperationIndex, firstOperationIndex+1);
      String leftE = str.substring(0, firstOperationIndex-1);
      String rightE = str.substring(firstOperationIndex+2);
      String leftNumStr, rightNumStr;
      String leftNumLeftE, rightNumRightE;
      if (leftE.contains(" ")) {
        leftNumStr = leftE.substring(leftE.lastIndexOf(" ")+1);
        leftNumLeftE = leftE.substring(0, leftE.lastIndexOf(" ")+1);
      } else {
        leftNumStr = leftE;
        leftNumLeftE = "";
      }
      if (rightE.contains(" ")) {
        rightNumStr = rightE.substring(0, rightE.indexOf(" "));
        rightNumRightE = rightE.substring(rightE.indexOf(" "));
      } else {
        rightNumStr = rightE;
        rightNumRightE = "";
      }
      double leftNum = Double.parseDouble(leftNumStr);
      double rightNum = Double.parseDouble(rightNumStr);
      double result;
      if ("x".equals(operation)) {
        result = leftNum * rightNum;
      } else {
        result = leftNum / rightNum;
      }
      return getResult(leftNumLeftE + result + rightNumRightE);
    }
  }

代码看着不短,但其实就是对字符串的处理,运用了递归,如果大家有更好的实现方案,欢迎分享。

点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消