Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code works. I've done a parsing calculator in the past, which seems to be totally different. I suggest using a debugger , run in eclipse with the bug button, and when you get to the point where things go wrong, add a breakpoint in your event handling method. and step through using the step over button , and step into any methods that might have the bug. May need to do it a few times.</p> <p>I needed to use the debugger to get the code below to work. Because it lets you change the operation e.g. pressing 3 + - 2 = will give 1 ; the +/- button is not working yet because the code is simplified to look at the labels of the button, specifically the first character.</p> <p>What I found with the debugger was : </p> <ol> <li>when the last result was a float, and the next input is an integer, it didn't branch to the float op float part of the doOperation, because of improper nesting of sigDiff in an outer separate if block.</li> <li>when the decimal point was entered, it was not registered, because the testing for setting the boolean currentNumberDecimal occurred before the first decimal point was processed, and should have occurred after the first decimal point is processed.</li> </ol> <p>Also found that an integer divided by an integer has to be retested for whether the result is an integer or a float. Can't remember if the debugger found that for me.</p> <pre><code>package com.example.calc1; import android.os.Bundle; import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { Number lastNumber; char lastOp; char beforeLastOp; char pendingOp; private boolean currentNumberDecimal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void buttonPressed(View view) { Button b = (Button) view; Log.d("Buttons ", "pressed " + b); EditText edit1 = (EditText) findViewById(R.id.editText1); char ch = b.getText().length() &gt; 0 ? b.getText().charAt(0) : 0; switch (ch) { case '=': doCalculation(edit1); edit1.setText(lastNumber.toString()); lastOp = 0; beforeLastOp = 0; lastNumber = null; break; case '+': case '-': case '*': case '/': lastOp = ch; break; case 'A': edit1.getText().clear(); lastNumber = null; lastOp = 0; break; } if ((ch == '.' || Character.isDigit(ch)) &amp;&amp; lastOp != 0) { doCalculation(edit1); beforeLastOp = lastOp; lastOp = 0; edit1.getText().clear(); currentNumberDecimal = ch == '.'; } if ((ch == '.' &amp;&amp; !currentNumberDecimal) || Character.isDigit(ch)) { edit1.getText().append(ch); } if (ch == '.' &amp;&amp; !currentNumberDecimal) currentNumberDecimal = true; } private void doCalculation(EditText edit1) { String s = edit1.getText().toString(); float f = Float.parseFloat(s); boolean sigDiff = true; try { float f2 = (float) Integer.parseInt(s); sigDiff = Math.abs(f - f2) &gt; Float.MIN_NORMAL; } catch (NumberFormatException nfe) { sigDiff = true; } if (lastNumber == null &amp;&amp; !sigDiff) { lastNumber = Integer.parseInt(s); } else if (lastNumber == null &amp;&amp; sigDiff) { lastNumber = f; } else if (!sigDiff &amp;&amp; lastNumber.getClass().equals(Integer.class)) { int i = Integer.parseInt(s); switch (beforeLastOp) { case '+': lastNumber = lastNumber.intValue() + i; break; case '-': lastNumber = lastNumber.intValue() - i; break; case '*': lastNumber = lastNumber.intValue() * i; break; case '/': Number n = lastNumber.intValue() / i; float f3 = lastNumber.floatValue() / i; if (Math.abs(f3 - n.floatValue()) &gt; Float.MIN_NORMAL) { lastNumber = f3; } else { lastNumber = n; } break; default: lastNumber = i; break; } } else { switch (beforeLastOp) { case '+': lastNumber = lastNumber.floatValue() + f; break; case '-': lastNumber = lastNumber.floatValue() - f; break; case '*': lastNumber = lastNumber.floatValue() * f; break; case '/': lastNumber = lastNumber.floatValue() / f; break; default: lastNumber = f; break; } } } </code></pre> <p>} </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload