Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
19 kB
2
Indexable
package com.example.calculator;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public TextView input, output;
    Button btnAC, btnDel, btnPersent, btnDivision,btn7, btn8,btn9,btn5,btn6,btn4,btn3,btn2,btn1,btn0,btnPoint,multiply, subtract, add, equal;
    Button btnx2, btnDeg, btnSin, btnCos, btnTan, btnpow, btnlg, btnLn, btnMoNgoac, btnDongNgoac, btnSqrt, btnGiaiThua, btn1ChiaX, btnPi, btnRand,btnE;
    Boolean flag = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = findViewById(R.id.input);
        output = findViewById(R.id.output);

        if(savedInstanceState != null) {
            String dataInput = savedInstanceState.getString("input");
            String dataOutput = savedInstanceState.getString("output");
            input.setText(dataInput);
            output.setText(dataOutput);
        }


        if(flag) {
            btnSin = findViewById(R.id.btnSin);
            btnCos = findViewById(R.id.btnCos);
            btnTan = findViewById(R.id.btnTan);
            btnlg = findViewById(R.id.btnlg);
            btnLn = findViewById(R.id.btnLn);
            btnGiaiThua = findViewById(R.id.btnGiaiThua);
            btnx2 = findViewById(R.id.btnx2);
            btnSqrt = findViewById(R.id.btnSqrt);
            btn1ChiaX = findViewById(R.id.btn1ChiaX);
        }


        // initializing all our variables.
        btnAC = findViewById(R.id.btnAC);
        btnDel = findViewById(R.id.btnDel);
        btnMoNgoac = findViewById(R.id.btnMoNgoac);
        btnDongNgoac = findViewById(R.id.btnDongNgoac);
        btn0 = findViewById(R.id.btn0);
        btn9 = findViewById(R.id.btn9);
        btn8 = findViewById(R.id.btn8);
        btn7 = findViewById(R.id.btn7);
        btn6 = findViewById(R.id.btn6);
        btn5 = findViewById(R.id.btn5);
        btn4 = findViewById(R.id.btn4);
        btn3 = findViewById(R.id.btn3);
        btn2 = findViewById(R.id.btn2);
        btn1 = findViewById(R.id.btn1);
        btnPi = findViewById(R.id.btnPi);
        multiply = findViewById(R.id.multiply);
        subtract = findViewById(R.id.subtract);
        add = findViewById(R.id.add);
        equal = findViewById(R.id.equal);
        btnPoint = findViewById(R.id.btnPoint);
        btnDivision = findViewById(R.id.btnDivision);
// adding on click listener to our all buttons.

        ArrayList<Button> listNumber = new ArrayList<>();
        listNumber.add(btn0);
        listNumber.add(btn1);
        listNumber.add(btn2);
        listNumber.add(btn3);
        listNumber.add(btn4);
        listNumber.add(btn5);
        listNumber.add(btn6);
        listNumber.add(btn7);
        listNumber.add(btn8);
        listNumber.add(btn9);
        for(Button btn : listNumber) {
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    input.setText(input.getText().toString() + btn.getText().toString());
                }
            });
        }

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input.setText(input.getText().toString() + "+");
            }
        });
        btnDivision.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input.setText(input.getText().toString() + "/");
            }
        });
        subtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = input.getText().toString();
                if (str.charAt(str.length() - 1) != '-') {
                    input.setText(input.getText().toString() + "-");
                }
            }
        });
        multiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str = input.getText().toString();
                if (str.charAt(str.length() - 1) != '*') {
                    input.setText(input.getText().toString() + "*");
                }
            }
        });
        btnMoNgoac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input.setText(input.getText().toString() + "(");
            }
        });
        btnDongNgoac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input.setText(input.getText().toString() + ")");
            }
        });
        if(flag) {
            btnPi.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    input.setText(input.getText().toString() + Math.PI);
                    output.setText(btnPi.getText().toString());
                }
            });
            btnSin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    input.setText(input.getText().toString() + "sin");
                }
            });
            btnCos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    input.setText(input.getText().toString() + "cos");
                }
            });
            btnTan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    input.setText(input.getText().toString() + "tan");
                }
            });
            btn1ChiaX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    input.setText(input.getText().toString() + "^" + "(-1)");
                }
            });
            btnLn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    input.setText(input.getText().toString() + "ln");
                }
            });

            btnSqrt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (input.getText().toString().isEmpty()) {
                        Toast.makeText(getApplicationContext(),
                                "Please enter a valid number..", Toast.LENGTH_SHORT).show();
                    } else {
                        String str = input.getText().toString();

                        double r = Math.sqrt(Double.parseDouble(str));
                        input.setText(String.valueOf(r));
                    }
                }
            });
            btnx2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (input.getText().toString().isEmpty()) {
                        Toast.makeText(getApplicationContext(),
                                "Please enter a valid number..", Toast.LENGTH_SHORT).show();
                    } else {
                        Double d = Double.parseDouble(input.getText().toString());
                        Double square = d * d;
                        input.setText(square.toString());
                        output.setText("$d²");
                    }
                }
            });
            btnGiaiThua.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (input.getText().toString().isEmpty()) {
                        // if the entered number is empty we are displaying an error message.
                        Toast.makeText(getApplicationContext(),
                                "Please enter a valid number..", Toast.LENGTH_SHORT).show();
                    } else {
                        int value= Integer.parseInt(input.getText().toString());
                        int  fact = factorial(value);
                        output.setText(String.valueOf(fact));
                        input.setText(value+"!");
                    }
                }
            });
        }

        equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String curdata = input.getText().toString();
                String res = String.valueOf(Calculator.evaluateExpression(curdata));

                if(curdata.length()>80) output.setTextSize(28);
                else {
                    if(curdata.length()>25) output.setTextSize(42);
                    else output.setTextSize(55);}
//                Integer checkD = checkDau(curdata);
//                for(Integer i=1;i<=checkD;i++) curdata = curdata + ")";
//                input.setText(curdata);
                if(res.endsWith(".0")) res = res.replace(".0","");
                input.setText(res);
                output.setText("=" + res);
            }
        });
        btnAC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                output.setTextSize(60);
                input.setTextSize(40);
                input.setText("");
                output.setText("");
            }
        });
        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String curdata = input.getText().toString();
                if(!curdata.equals("0")) curdata=curdata.substring(0,curdata.length()-1);
                input.setText(curdata);
                output.setText("");
            }
        });

    }
    int factorial(int n){
        // this method is use to find factorial
        return  (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }

    // Check dấu đóng mo ngoặc
    Integer checkDau(String data)
    {
        Integer checkp=0;
        for(Integer i=0;i<= data.length()-1;i++)
        {
            if(data.charAt(i)=='(') checkp++;
            if(data.charAt(i)==')') checkp--;
        }
        return checkp;
    }
//    public static final double evaluate(final String str) {
//        //Intrinsics.checkNotNullParameter(str, "str");
//        return ((new Object() {
//            private int pos = -1;
//            private int ch;
//
//            public final int getPos() {
//                return this.pos;
//            }
//
//            public final void setPos(int var1) {
//                this.pos = var1;
//            }
//
//            public final int getCh() {
//                return this.ch;
//            }
//
//            public final void setCh(int var1) {
//                this.ch = var1;
//            }
//
//            public final void nextChar() {
//                ++this.pos;
//                this.ch = this.pos < str.length() ? str.charAt(this.pos) : -1;
//            }
//
//            public final boolean eat(int charToEat) {
//                while(this.ch == 32) {
//                    this.nextChar();
//                }
//
//                if (this.ch == charToEat) {
//                    this.nextChar();
//                    return true;
//                } else {
//                    return false;
//                }
//            }
//
//            public final double parse() {
//                this.nextChar();
//                double x = this.parseExpression();
//                if (this.pos < str.length()) {
//                    try {
//                        throw (Throwable)(new RuntimeException("Unexpected: " + (char)this.ch));
//                    } catch (Throwable throwable) {
//                        throwable.printStackTrace();
//                    }
//                }
//                return x;
//            }
//
//            public final double parseExpression() {
//                double x = this.parseTerm();
//
//                while(true) {
//                    while(!this.eat(43)) {
//                        if (!this.eat(45)) {
//                            return x;
//                        }
//
//                        x -= this.parseTerm();
//                    }
//
//                    x += this.parseTerm();
//                }
//            }
//
//            public final double parseTerm() {
//                double x = this.parseFactor();
//
//                while(true) {
//                    while(!this.eat(42)) {
//                        if (!this.eat(47)) {
//                            return x;
//                        }
//
//                        x /= this.parseFactor();
//                    }
//
//                    x *= this.parseFactor();
//                }
//            }
//
//            public final double parseFactor() {
//                if (this.eat(43)) {
//                    return this.parseFactor();
//                } else if (this.eat(45)) {
//                    return -this.parseFactor();
//                } else {
//                    double x = 0.0D;
//                    int startPos = this.pos;
//                    if (this.eat(40)) {
//                        x = this.parseExpression();
//                        this.eat(41);
//                    } else {
//                        String var10000;
//                        String func;
//                        if ((this.ch < 48 || this.ch > 57) && this.ch != 46) {
//                            if (this.ch < 97 || this.ch > 122) {
//                                try {
//                                    throw (Throwable)(new RuntimeException("Unexpected: " + (char)this.ch));
//                                } catch (Throwable throwable) {
//                                    throwable.printStackTrace();
//                                }
//                            }
//
//                            while(this.ch >= 97 && this.ch <= 122) {
//                                this.nextChar();
//                            }
//
//                            String var9 = str;
//                            int var10 = this.pos;
//                            boolean var7 = false;
//                            if (var9 == null) {
//                                throw new NullPointerException("null cannot be cast to non-null type java.lang.String");
//                            }
//
//                            var10000 = var9.substring(startPos, var10);
//                            //Intrinsics.checkNotNullExpressionValue(var10000, "(this as java.lang.Strin…ing(startIndex, endIndex)");
//                            func = var10000;
//                            x = this.parseFactor();
//                            double var11;
//                            if (str.equals("sqrt")) {
//                                var11 = Math.sqrt(x);
//                            } else if (str.equals( "sin")) {
//                                var11 = Math.sin(Math.toRadians(x));
//                            } else if (str.equals("cos")) {
//                                var11 = Math.cos(Math.toRadians(x));
//                            } else if (str.equals( "tan")) {
//                                var11 = Math.tan(Math.toRadians(x));
//                            } else if (str.equals( "log")) {
//                                var11 = Math.log10(x);
//                            } else {
//                                if (!str.equals("ln")) {
//                                    try {
//                                        throw (Throwable)(new RuntimeException("Unknown function: " + func));
//                                    } catch (Throwable throwable) {
//                                        throwable.printStackTrace();
//                                    }
//                                }
//
//                                var11 = Math.log(x);
//                            }
//
//                            x = var11;
//                        } else {
//                            while(true) {
//                                if ((this.ch < 48 || this.ch > 57) && this.ch != 46) {
//                                    func = str;
//                                    int var5 = this.pos;
//                                    boolean var6 = false;
//                                    if (func == null) {
//                                        throw new NullPointerException("null cannot be cast to non-null type java.lang.String");
//                                    }
//
//                                    var10000 = func.substring(startPos, var5);
//                                    //Intrinsics.checkNotNullExpressionValue(var10000, "(this as java.lang.Strin…ing(startIndex, endIndex)");
//                                    func = var10000;
//                                    boolean var8 = false;
//                                    x = Double.parseDouble(func);
//                                    break;
//                                }
//
//                                this.nextChar();
//                            }
//                        }
//                    }
//
//                    if (this.eat(94)) {
//                        x = Math.pow(x, this.parseFactor());
//                    }
//
//                    return x;
//                }
//            }
//        })).parse();
//    }
    @Override
     public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            flag = true;
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
            flag = false;
        }
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("input", input.getText().toString());
        outState.putString("output", output.getText().toString());
    }
}
Leave a Comment