Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hu.blogspot.visitcatalunya.calculadora;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- public double total = 0; // number shown in the result textView
- public boolean comma = false;
- public boolean editNum = true;
- public int sign = 1;
- public int decimal = 0;
- public double num1 = 0;
- public double num2 = 0;
- public int lastButton = 1;
- public int operation = 0;
- public int saveOperation = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- /*
- */
- public void Display(View v) {
- TextView result = (TextView) findViewById(R.id.result);
- result.setText(String.valueOf(total));
- }
- public void delete(View v) {
- total = 0;
- decimal = 0;
- comma = false;
- editNum = true;
- sign = 1;
- num1 = 0;
- num2 = 0;
- Display(v);
- lastButton = 1;
- saveOperation = 0;
- operation = 0;
- }
- public void dot(View v) {
- comma = true;
- lastButton = 18;
- }
- public void plusMinus(View v) {
- total = -total;
- sign *= -1;
- Display(v);
- lastButton = 19;
- }
- // ===========================================================================
- public void calculate(View v) {
- if (lastButton == 20) {
- operation = saveOperation;
- } else if (lastButton == 2) {
- if (operation == 1 || operation == 2) {
- num2 = total * num1 / 100;
- } else {
- num2 = total / 100;
- }
- } else {
- num2 = total;
- }
- if (operation == 1) {
- total = num1 + num2;
- num1 = total;
- } else if (operation == 2) {
- total = num1 - num2;
- num1 = total;
- } else if (operation == 3) {
- total = num1 * num2;
- num1 = total;
- } else if (operation == 4) {
- total = num1 / num2;
- num1 = total;
- }
- Display(v);
- editNum = false;
- lastButton = 20;
- comma = false;
- decimal = 0;
- saveOperation = operation;
- operation = 0;
- }
- public void sum(View v) {
- if (operation == 0) {
- num1 = total;
- total = 0;
- operation = 1;
- } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
- operation = 1;
- } else {
- num2 = total;
- calculate(v);
- operation = 1;
- }
- decimal = 0;
- comma = false;
- lastButton = 16;
- }
- public void res(View v) {
- if (operation == 0) { //
- num1 = total;
- total = 0;
- operation = 2;
- } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
- operation = 1;
- } else {
- num2 = total;
- calculate(v);
- operation = 2;
- }
- decimal = 0;
- comma = false;
- lastButton = 12;
- }
- public void per(View v) {
- if (operation == 0) { //
- num1 = total;
- total = 0;
- operation = 3;
- } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
- operation = 3;
- } else {
- num2 = total;
- calculate(v);
- operation = 3;
- }
- decimal = 0;
- comma = false;
- lastButton = 8;
- }
- public void div(View v) {
- if (operation == 0) {
- num1 = total;
- total = 0;
- operation = 4;
- } else if (lastButton == 4 || lastButton == 8 || lastButton == 12 || lastButton == 16 || lastButton == 20) { //changes operation with no more effects
- operation = 4;
- } else {
- num2 = total;
- calculate(v);
- operation = 4;
- }
- decimal = 0;
- lastButton = 4;
- }
- public void percent(View v) {
- if (lastButton != 2) {
- lastButton = 2;
- calculate(v);
- }
- }
- public void sqrtRoot(View v) {
- if (total < 0) {
- Toast.makeText(MainActivity.this, "Negative number, use +/- before", Toast.LENGTH_SHORT).show();
- } else {
- total = Math.sqrt(total);
- Display(v);
- }
- editNum = false;
- lastButton = 3;
- }
- //00000000000000000000000000000000000000000000000000000000000
- public void set0(View v) {
- if (lastButton == 20 || lastButton <= 3) {
- delete(v);
- }
- if (editNum) {
- if (!comma) {
- total = total * 10;
- } else {
- decimal++;
- }
- } else {
- total = 0;
- decimal = 0;
- editNum = true;
- sign = 1;
- }
- Display(v);
- lastButton = 17;
- }
- //11111111111111111111111111111111111111111111111111111111
- public void set1(View v) {
- if (lastButton == 20 || lastButton <= 3) {
- total = 0;
- }
- if (editNum) {
- if (!comma) {
- total = total * 10 + sign;
- } else {
- decimal++;
- total = total + sign / (Math.pow(10, decimal));
- }
- } else {
- total = 1;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 13;
- }
- //222222222222222222222222222222222222222222222222222222222
- public void set2(View v) {
- if (lastButton == 20 || lastButton <= 3) {
- total = 0;
- }
- if (editNum) {
- if (!comma) {
- total = total * 10 + 2 * sign;
- } else {
- decimal++;
- total = total + 2 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 2;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 14;
- }
- public void set3(View v) {
- if (lastButton == 20 || lastButton <= 3) {
- total = 0;
- }
- if (editNum) {
- if (!comma) {
- total = total * 10 + 3 * sign;
- } else {
- decimal++;
- total = total + 3 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 3;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 15;
- }
- public void set4(View v) {
- if (editNum) {
- if (!comma) {
- total = total * 10 + 4 * sign;
- } else {
- decimal++;
- total = total + 4 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 4;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 9;
- }
- public void set5(View v) {
- if (editNum) {
- if (!comma) {
- total = total * 10 + 5 * sign;
- } else {
- decimal++;
- total = total + 5 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 5;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 10;
- }
- public void set6(View v) {
- if (editNum) {
- if (!comma) {
- total = total * 10 + 6 * sign;
- } else {
- decimal++;
- total = total + 6 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 6;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 11;
- }
- public void set7(View v) {
- if (editNum) {
- if (!comma) {
- total = total * 10 + 7 * sign;
- } else {
- decimal++;
- total = total + 7 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 7;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 5;
- }
- public void set8(View v) {
- if (editNum) {
- if (!comma) {
- total = total * 10 + 8 * sign;
- } else {
- decimal++;
- total = total + 8 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 8;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 6;
- }
- public void set9(View v) {
- if (editNum) {
- if (!comma) {
- total = total * 10 + 9 * sign;
- } else {
- decimal++;
- total = total + 9 * sign / (Math.pow(10, decimal));
- }
- } else {
- total = 9;
- decimal = 0;
- sign = 1;
- editNum = true;
- }
- Display(v);
- lastButton = 7;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement