开发者

Calculator solving three digits doesn't work properly

开发者 https://www.devze.com 2023-04-12 10:22 出处:网络
So my calculator works perfectly with any calculations involving two digits including the C button however if i attempt any three digits calculations this doesnt really work for example 1 + 2 + 3 = 5

So my calculator works perfectly with any calculations involving two digits including the C button however if i attempt any three digits calculations this doesnt really work for example 1 + 2 + 3 = 5 could someone tell me why is that and anyway i could fix it?

public class Calculator {  

  private long currentInput;          //current input
  private long previousInput;         // previous input
  private long result;            // result of calculation
  private String lastOperator = "";  // keeps track of the last operator entered


  /* New digit entered as integer value i - moves currentInput 1 decimal place to the left and adds i in "one's column" */
  public void inDigit(long i) {
    currentInput = (currentInput * 10) + i;
  }


  /* Operator entered  + - or *   */
  public void inOperator(String op) {
    previousInput = currentInput;      // save the new input as previous to get ready for next input
    currentInput = 0;
    lastOperator = op;                 // remember which operator was entered
  } 


   /* Equals operation sets result to previousInput + - 开发者_高级运维or * currentInput (depending on lastOperator) */
  public void inEquals() {
    if (lastOperator.equals("+")) {
      result = previousInput + currentInput;
    } else if (lastOperator.equals("-")) { 
      result = previousInput - currentInput;
    } else if (lastOperator.equals("*"))  {
      result = previousInput * currentInput;
    } 
    lastOperator = "";       // reset last operator to "nothing"
  }


  /* Clear operation */
  public void inClear() {
    currentInput = 0;
    previousInput = 0;
    result = 0;
    lastOperator = "";
  } 

  /* returns the current result */
  public String getResult() {  
    return Long.toString(result);  //converts int to String
  }

  /* returns the previous input value */
  public String getPreviousInput() {
    return Long.toString(previousInput);
  }
  /* returns the current input value */
  public String getCurrentInput() {
    return Long.toString(currentInput);
  }


Well, you're only storing the last two operands, so for your example of 1 + 2 + 3 = 5, the 1 is lost when you enter the second + sign, and 2 + 3 = 5.


You only store the last two "inputs" entered (in previousInput and currentInput) so when you go to three or more operands without first hitting =, all but the most recent two disappear.


your problem is when you add a new operator you just shift your values with out doing any calculations, if a previous operator is stored and both values are set when adding a new operator you need to do calculations for the existing values and operator before changing the stored operator


Because in your InEquals() method, you only use previousInput and currentInput so your 1 + 2 + 3 = 5 because for the 2nd '+' result = 2 + 3

0

精彩评论

暂无评论...
验证码 换一张
取 消