Home android simple calculator on Android

simple calculator on Android

Author

Date

Category

Hello.

I study the development under android, I write banal tasks (now – calculator).
And here’s actually a question – I shaped a string with an expression, how can I count it now? If I had a complex calculator, I would use the methods of reverse Polish record and other already known algorithms. But I have +, -, *, /. In the scripting languages, I would use something like eval (), and here it turned around …

Help, if you encountered, please:)


Answer 1, Authority 100%

main.xml

& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
& lt; linearlayout XMLNS: Android = "http://schemas.android.com/apk/res/android"
  Android: layout_width = "fill_parent"
  Android: layout_height = "fill_parent"
  Android: Orientation = "Vertical" & gt;
  & lt; editText
    Android: id = "@ + id / edittext1"
    Android: layout_width = "fill_parent"
    Android: layout_height = "wrap_content"
    Android: InputType = "Number" & gt;
    & lt; RequestFocus / & GT;
  & lt; / edittext & gt;
& lt; tableow
  Android: layout_width = "wrap_content"
  Android: layout_height = "wrap_content" & gt;
  & lt; button
    Android: id = "@ + id / button1"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Layout_Gravity = "Center"
    Android: text = "1" / & gt;
  & lt; button
    Android: id = "@ + id / button2"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Text = "2" / & gt;
  & lt; button
    Android: id = "@ + id / button3"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "3" / & gt;
  & lt; button
    Android: id = "@ + id / button4"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "+" / & gt;
  & lt; button
    Android: id = "@ + id / button5"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Text = "-" / & gt;
& lt; / tablerow & gt;
  & lt; tableow
  Android: layout_width = "wrap_content"
  Android: layout_height = "wrap_content" & gt;
  & lt; button
    Android: id = "@ + id / button6"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Layout_Gravity = "Center"
    Android: Text = "4" / ​​& gt;
  & lt; button
    Android: id = "@ + id / button7"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "5" / & gt;
  & lt; button
    Android: id = "@ + id / button8"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Text = "6" / & gt;
  & lt; button
    Android: id = "@ + id / button9"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "/" / & gt;
  & lt; button
    Android: id = "@ + id / button10"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Text = "*" / & gt;
& lt; / tablerow & gt;
  & lt; tableow
  Android: layout_width = "wrap_content"
  Android: layout_height = "wrap_content" & gt;
  & lt; button
    Android: id = "@ + id / button11"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Layout_Gravity = "Center"
    Android: Text = "7" / & gt;
  & lt; button
    Android: id = "@ + id / button12"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "8" / & gt;
  & lt; button
    Android: id = "@ + id / button13"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "9" / & gt;
  & lt; button
    Android: id = "@ + id / button20"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Text = "0" / & gt;
  & lt; button
    Android: id = "@ + id / button14"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "=" / & gt; 
& lt; / tablerow & gt;
 & lt; tableow
  Android: layout_width = "wrap_content"
  Android: layout_height = "wrap_content" & gt;
  & lt; button
    Android: id = "@ + id / beutton16"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: text = "Decimal" / & gt;
  & lt; button
    Android: id = "@ + id / button23"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Text = "Binary" / & gt;
  & lt; button
    Android: id = "@ + id / button22"
    Android: layout_width = "fill_parent"
    Android: layout_height = "fill_parent"
    Android: Text = "Clear" / & gt;
& lt; / tablerow & gt;
& lt; / linearlayout & gt;

activity.java

package calc.calc;
Import java.util.arraylist;
Import Android.app.ActiveIVITY;
Import android.os.Bundle;
Import android.text.method.digitsKeylistener;
Import Android.View.View;
Import android.widget.EditText;
Import android.widget.textView;
Public Class Calcactivity EXTENDS ACTIVITY {
  / ** textball variable * /
  EditText CalcdialogDisplay;
  / ** variable buttons * /
  TextView Binary;
  TextView AllClear;
  TextView Seven;
  TextView EIGHT;
  TextView Nine;
  TextView Division;
  TextView Four;
  TextView Five;
  TextView Six;
  TextView Multiply;
  TextView One;
  TextView Two;
  TextView Three;
  TextView subtract;
  TextView Decimal;
  TextView Zero;
  TextView Equals;
  TextView Addition;
  / ** The result that is entered into module for processing * /
  ArrayList & lt; Float & gt; Result = New ArrayList & LT; Float & gt; ();
  / ** first entered number * /
  Float Number1;
  / ** Second entered number * /
  Float Number2;
  int currentoperation = 0;
  INT NEXTOPERATION;
  /**Addition*/
  Final Static int Add = 1;
  /**Subtraction*/
  Final Static Int SUBTRACT = 2;
  /**Multiplication*/
  Final Static Int Multiply = 3;
  /**Division*/
  Final Static Int Division = 4;
  /**Equals*/
  Final Static In Equals = 5;
  / ** Finding an integer value from binary number * /
  Final Static Int Decimal = 6;
  / ** Finding a binary number from the whole * /
  Final Static Int Binary = 7;
  Final Static int Clear = 1;
  Final Static Int Dont_Clear = 0;
  int ClearCalcDisplay = 0;
  Public Void OnCreate (Bundle SavedInstanceState) {
    Super.ONCREATE (SavedInstanceState);
    setContentView (R.Layout.main);
    calcdialogdisplay = (editText) FindViewByid (R.ID.EditText1);
    binary = (textView) FindViewByid (R.ID.Button23);
    Allclear = (TextView) FindViewByid (R.ID.Button22);
    Seven = (textView) FindViewByID (R.ID.Button11);
    EIGHT = (TextView) FindViewByid (R.ID.Button12);
    Nine = (TextView) FindViewByid (R.ID.Button13);
    Division = (TextView) FindViewByid (R.ID.Button9);
    Four = (TextView) FindViewByid (R.ID.Button6);
    Five = (TextView) FindViewByid (R.ID.Button7);
    Six = (TextView) FindViewByid (R.ID.Button8);
    Multiply = (TextView) FindViewByID (R.ID.Button10);
    One = (TextView) FindViewByID (R.ID.Button1);
    Two = (TextView) FindViewByid (R.ID.Button2);
    Three = (TextView) FindViewByid (R.ID.Button3);
    subtract = (TextView) FindViewByid (R.ID.Button5);
    Decimal = (TextView) FindViewByid (R.ID.Button16);
    zero = (textView) FindViewByid (R.ID.Button20);
    Equals = (TextView) FindViewByid (R.ID.Button14);
    // addition = (TextView) FindViewByid (R.ID.Addition);
    calcdialogdisplay.setkeylistener (DigitsKeylistener.getInstance (True, True));
    registerlisteners ();
  }
  / * Handling on the screen * /
  Public void registerlisteners () {
    Seven.SetonClicklistener (new view.onclicklistener () {
      @Override
      Public Void OnClick (View V) {
        If (ClearCalcDisplay == Clear) {
          calcdialogdisplay.settext ("");
        }
        ClearCalcDisplay = dont_Clear;
        calcdialogdisplay.append ("7");
      }
    });
    Eight.SetonClicklistener (new view.onclicklistener () { 
@Override
      Public Void OnClick (View V) {
        If (ClearCalcDisplay == Clear) {
          calcdialogdisplay.settext ("");
        }
        ClearCalcDisplay = dont_Clear;
        calcdialogdisplay.append ("8");
      }
    });
    Nine.SetonClicklistener (new view.onclicklistener () {
      @Override
      Public Void OnClick (View V) {
        If (ClearCalcDisplay == Clear) {
          calcdialogdisplay.settext ("");
        }
        ClearCalcDisplay = dont_Clear;
        calcdialogdisplay.append (9 ");
      }
    });
    Division.SetonClicklistener (new view.onclicklistener () {
      @Override
      Public Void OnClick (View V) {
        CalClogic (Division);
      }
    });
    Allclear.SetonClicklistener (new view.onclicklistener () {
      @Override
      Public Void OnClick (View V) {
        calcdialogdisplay.settext ("");
        NUMBER1 = 0;
        NUMBER2 = 0;
        Result.Removeall (Result);
        currentoperation = 0;
        NEXTOPERATION = 0;
      }
    });
  Four.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      If (ClearCalcDisplay == Clear) {
        calcdialogdisplay.settext ("");
      }
      ClearCalcDisplay = dont_Clear;
      calcdialogdisplay.append ("4");
    }
  });
  Five.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      If (ClearCalcDisplay == Clear) {
        calcdialogdisplay.settext ("");
      }
      ClearCalcDisplay = dont_Clear;
      calcdialogdisplay.append ("5");
    }
  });
  Six.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      If (ClearCalcDisplay == Clear) {
        calcdialogdisplay.settext ("");
      }
      ClearCalcDisplay = dont_Clear;
      calcdialogdisplay.append ("6");
    }
  });
  Zero.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      If (ClearCalcDisplay == Clear) {
        calcdialogdisplay.settext ("");
      }
      ClearCalcDisplay = dont_Clear;
      calcdialogdisplay.append ("0");
    }
  });
  Decimal.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      CalClogic (Decimal);
    }
  });
  Multiply.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      CalClogic (Multiply);
    }
  });
  One.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      If (ClearCalcDisplay == Clear) {
        calcdialogdisplay.settext ("");
      }
      ClearCalcDisplay = dont_Clear;
      calcdialogdisplay.append ("1");
    }
  });
  Two.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      If (ClearCalcDisplay == Clear) {
        calcdialogdisplay.settext ("");
      }
      ClearCalcDisplay = dont_Clear;
      calcdialogdisplay.append ("2");
    }
  });
  Three.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      If (ClearCalcDisplay == Clear) {
        calcdialogdisplay.settext ("");
      }
      ClearCalcDisplay = dont_Clear;
      calcdialogdisplay.append ("3");
    }
  });
  subtract.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) {
      CalClogic (subtract);
    }
  });
  Equals.SetonClicklistener (new view.onclicklistener () {
    @Override
    Public Void OnClick (View V) { 
calcLogic (EQUALS);
    }
  });
  binary.setOnClickListener (new View.OnClickListener () {
    @Override
    public void onClick (View v) {
      calcLogic (BINARY);
    }
  });
}
  / * Function for converting from decimal to binary * /
  private float decToBin (float bin) {
    int i, b;
    long result = 0;
     for (i = 0; bin & gt; 0; i ++) {
      b = (int) (bin% 2);
      bin = (bin-b) / 2;
      result + = b * Math.pow (10, i);
    }
     return result;
  }
  / * Conversion function from two-faced to decimal value * /
  private float binToDec (float dec) {
    int result = 0;
    int mult = 1;
    while (dec & gt; 0) {
      result + = mult * ((int) dec% 10);
      dec / = 10;
      mult * = 2;
    }
    return result;
  }
  / * Function for calculating entered values ​​* /
  private void calcLogic (int operator) {
  result.add (Float.parseFloat (calcDialogDisplay.getText (). toString ()));
  if (operator! = EQUALS) {
    nextOperation = operator;
  } else if (operator == EQUALS) {
    nextOperation = 0;
  }
  switch (currentOperation) {
  /*Addition*/
    case ADD:
      number1 = result.get (0);
      number2 = result.get (1);
      result.removeAll (result);
      result.add (number1 + number2);
      calcDialogDisplay.setText (String.format ("%. 0f", result.get (0)));
      break;
   /*Subtraction*/
    case SUBTRACT:
      number1 = result.get (0);
      number2 = result.get (1);
      result.removeAll (result);
      result.add (number1 - number2);
      calcDialogDisplay.setText (String.format ("%. 0f", result.get (0)));
      break;
   /*Multiplication*/
    case MULTIPLY:
      number1 = result.get (0);
      number2 = result.get (1);
      result.removeAll (result);
      result.add (number1 * number2);
      calcDialogDisplay.setText (String.format ("%. 0f", result.get (0)));
      break;
    /*Division*/
    case DIVISION:
      number1 = result.get (0);
      number2 = result.get (1);
      result.removeAll (result);
      result.add (number1 / number2);
      calcDialogDisplay.setText (String.format ("%. 0f", result.get (0)));
      break;
    / * Get binary number * /
    case DECIMAL:
       number2 = result.get (1);
       result.removeAll (result);
       result.add (decToBin (number2));
       calcDialogDisplay.setText (String.format ("%. 0f", result.get (0)));
      break;
    / * Get decimal number * /
    case BINARY:
       number2 = result.get (1);
       result.removeAll (result);
       result.add (binToDec (number2));
       calcDialogDisplay.setText (String.format ("%. 0f", result.get (0)));
      break;
  }
  clearCalcDisplay = CLEAR;
  currentOperation = nextOperation;
  if (operator == EQUALS) {
    number1 = 0;
    number2 = 0;
    result.removeAll (result);
  }
}
}

Once upon a time failed for an interview


Answer 2

Parse the string and add the numbers, if the number is followed by space (s), or “+”, “-” if spaces and “*”, “/” then perform the action first and look further (recursion), if then the number then fold, if not repeat.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions