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.