0

I'm trying to make a simple android calculator app, I know it's not great and there's probably a hundred better ways to do it but this is what it looks like. What I need help with is the subtraction, multiplication and division in the "resultBtnClick" method. The addition part was easy I just used "result += ..." in a for-loop. So how do i make the numbers in the stack subtract/divide/multiply by each other?

public class MainActivity extends AppCompatActivity {

TextView tv1;
Boolean emptyResult = true;
private static char operator;
double result, res;

Stack values = new Stack();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv1 = (TextView) findViewById(R.id.tv1);
}
public void btnClick(View v)
{
    if (!emptyResult)
    {
        tv1.setText("");
        emptyResult = true;
    }
    switch(v.getId())
    {
        case R.id.btn1:
            tv1.setText(tv1.getText() + "1");
            break;
        case R.id.btn2:
            tv1.setText(tv1.getText() + "2");
            break;
        case R.id.btn3:
            tv1.setText(tv1.getText() + "3");
            break;
        case R.id.btn4:
            tv1.setText(tv1.getText() + "4");
            break;
        case R.id.btn5:
            tv1.setText(tv1.getText() + "5");
            break;
        case R.id.btn6:
            tv1.setText(tv1.getText() + "6");
            break;
        case R.id.btn7:
            tv1.setText(tv1.getText() + "7");
            break;
        case R.id.btn8:
            tv1.setText(tv1.getText() + "8");
            break;
        case R.id.btn9:
            tv1.setText(tv1.getText() + "9");
            break;
        case R.id.btn0:
            tv1.setText(tv1.getText() + "0");
            break;
        case R.id.btnDot:
            tv1.setText(tv1.getText() + ".");
            break;
        case R.id.btnClear:
            tv1.setText("");
            emptyResult = true;

            while (!values.isEmpty())
            {
                values.pop();
            }
            break;
    }
}
public void calculateBtnClick(View v)
{
    values.push(tv1.getText());

    switch(v.getId())
    {
        case R.id.btnAdd:
            operator = '+';
            break;
        case R.id.btnSub:
            operator = '-';
            break;
        case R.id.btnMult:
            operator = 'x';
            break;
        case R.id.btnDiv:
            operator = '/';
            break;
    }
    tv1.setText("");
}
public void resultBtnClick(View v)
{
    values.push(tv1.getText());

    switch(operator)
    {
        case '+':
            for (int i=0; i < values.size(); i++)
            {
                result += Double.valueOf(values.get(i).toString());
            }
            break;
        case '-':
            for (int i=0; i < values.size(); i++)
            {
                result -= Double.valueOf(values.get(i).toString());
            }
            break;
        case 'x':
            for (int i=0; i < values.size(); i++)
            {
                result *= Double.valueOf(values.get(i).toString());
            }
            break;
        case '/':
            for (int i=0; i < values.size(); i++)
            {
                result /= Double.valueOf(values.get(i).toString());
            }
            break;
    }
    while (!values.isEmpty())
    {
        values.pop();
    }
    res = ((long)(result * 100000 + 0.5))/100000.0;
    tv1.setText(Double.toString(res));
    result = 0;
    emptyResult = false;
}
}

Edit 1: I am also aware that I can only use one operator at a time which is the last clicked one, so if I were to click: 5+6-8x2+4 it would calculate it as: 5+6+8+2+4, this would probably require more work to fix it to work as a normal calculator so please help me with the main problem mentioned above, and if there is an easy, quick fix to this then you are welcome to add it to your answer but I would prioritize the answer to the main problem, thanks!

Edit 2: The multiplication and the division is not doing anything, I know the code is wrong, just wanted to clarify what the actual outcome is.

random1234
  • 777
  • 3
  • 17
  • 41
  • what exactly your error is? – Roljhon Mar 11 '17 at 16:21
  • Look in the "resultBtnClick" method, how do I for example subtract all the variables in the stack with each other? If I were to type 10-2-5, for the moment it does not do what I want it to. – random1234 Mar 11 '17 at 16:26
  • 1
    "Does not do what I want" does not describe at all what it really does vs. what you expected. If you start `result = 0`, then why are you essentially doing `0-10-2-5`? The 10 is positive! – OneCricketeer Mar 11 '17 at 16:32
  • @cricket_007 When i type in 10-2-5 I get -16.99999, so it is just adding the negative variables to "result" (Why it's -16.99999 instead of -17 i have no idea =S) – random1234 Mar 11 '17 at 16:42
  • Because you are using double types. http://stackoverflow.com/questions/588004/is-floating-point-math-broken – OneCricketeer Mar 11 '17 at 16:47
  • Sounds like homework... – NewBie1234 Mar 11 '17 at 17:50

2 Answers2

1

You need to get the first value off the stack, otherwise you subtract / multiply / divide against 0.

result = Double.valueOf(values.get(0)); // Get first, so 'result != 0'
for (int i = 1; i < values.size(); i++) {
    double val = Double.valueOf(values.get(i).toString());
    switch(operator)  {
        case '+':
            result += val;
            break;
        case '-':
            result -= val;
            break;

Consider research into the "Shunting-yard algorithm", though

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

It looks like your starting result at 0 instead of the first number. Addition works beacause 0+values.get(i) is the value of i. You need to initialize result first

  • The "resultBtnClick" method is run once I click the "=" sign on the calculator, so after the calculation is finished it clears the variable "result". What should i initialize it with? – random1234 Mar 11 '17 at 16:39
  • Your doing result -= stuff when theres nothing in result yet. before your loop you need to set result to the first number( bottom number on the stack ). Then loop through and do your subtractions. – pdoyle5000 Mar 11 '17 at 16:52
  • I would also heed Cricket's warning above about the double types not being reliably correct. You may want to use Math.Round at some point to throttle your decimals. – pdoyle5000 Mar 11 '17 at 16:53
  • That's what the "res = ((long)(result * 100000 + 0.5))/100000.0;" line is about. Is the "Math" class even available in Android Studio? It's not working when i type it in. – random1234 Mar 11 '17 at 17:08