BODMAS系统的加减法 [英] adding and subtracting for BODMAS system

查看:26
本文介绍了BODMAS系统的加减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在构建一个简单的公式计算器,但在加减法上遇到了麻烦.您应该知道,在计算方程时,您遵循优先级的算术规则,即括号、顺序:幂函数、除法、乘法、加法和减法.问题是加法和减法的优先级相同,因此您可以从左到右阅读.到目前为止,这是我的代码:

I have been building a simple formula calculator and have gotten stuck with addition and subtraction. As you should know, when calculating an equation, you follow the arithmetic rules of precedence, i.e. brackets, order: power functions, division, multiplication, addition and subtraction. The problem is that addition and subtraction are given equal priority, so therefore you can read it from left to right. Here is my code so far:

{
ArrayList<String> equation = java.util.Arrays.asList({"2","-","2","+","5"});

  while(equation.contains("+")){
          addMe(equation);
  }
  while(equation.contains("-")){
          minusMe(equation);            
  }
}

public static void addMe(ArrayList<String> numberList){
 for (int i = 0, n = numberList.size(); i < n; i++) { 
   String value = (String) numberList.get(i); 
   if(value.equals("+")){

    String wordBefore = (String) numberList.get(i-1);
    String wordAfter = (String) numberList.get(i+1);
    System.out.println("This is the word before " + wordBefore);
    System.out.println("This is the word after " + wordAfter);
    double doubleFromBefore = Double.parseDouble(wordBefore);
    double doubleFromAfter = Double.parseDouble(wordAfter);
    double answer = doubleFromBefore + doubleFromAfter;
    System.out.println("This is the answer: " + answer);
    String stringAnswer = String.valueOf(answer);
    String newNum2 = value.replace(value, stringAnswer);
    numberList.set(i,newNum2);
    numberList.remove(i-1);
    numberList.remove(i);
    break;
  }
 }   
}

minusMe 方法与 addMe 方法完全相同,只是在相关地方加了-".我遇到的问题是一次从左到右读取一个方程,然后执行加法或减法.理想情况下,我认为我需要将我的 2 while 循环与迭代器结合起来,以解决问题,但我的尝试没有奏效.知道这是否会解决我的问题吗?如果是这样,请提供修改后的循环.

The minusMe method is exactly the same as the addMe method except with "-" in relevant places. The problem I am having is getting the equation read from left to right one item at a time and either doing the add or subtract method. Ideally I think I need to combine my 2 while loops with an iterator, to solve the problem but my attempts haven't worked. Any idea as to if this will solve my problem? If so please provide amended loop.

问候

推荐答案

看看这个

java.uti.ArrayList<String> equation = java.util.Arrays.asList({"2","-","2","+","5"});
java.util.Iterator<String> equIterator = equation.iterator();
int result = 0;
int multiplier = 1;
while(equIterator.hasNext()){
     String operandOrOperator = equIterator.next();
     if(operandOrOperator.equals("+")){
          multiplier=1;
     }else if(operandOrOperator.equals("-")){
          multiplier=-1;
     }else if(operandOrOperator.equals("*")){
          result*=Integer.parseInt(equIterator.next()); // Assuming that next element will be there always after operator.
     }else{
          result+=(multiplier * Integer.parseInt(operandOrOperator));
     }
}
System.out.println("Final result : " + result);

这篇关于BODMAS系统的加减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆