Untitled
unknown
plain_text
22 days ago
12 kB
1
Indexable
Never
package com.example.calculator; import android.content.Context; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.Stack; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; 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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); input = findViewById(R.id.input); output = findViewById(R.id.output); // initializing all our variables. btnAC = findViewById(R.id.btnAC); btnDel = findViewById(R.id.btnDel); btnMoNgoac = findViewById(R.id.btnMoNgoac); btnDongNgoac = findViewById(R.id.btnDongNgoac); // 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); 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. btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "1"); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "2"); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "3"); } }); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "4"); } }); btn5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "5"); } }); btn6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "6"); } }); btn7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "7"); } }); btn8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "8"); } }); btn9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "9"); } }); btn0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "0"); } }); 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() + "/"); } }); 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() + ")"); } }); btnPoint.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { input.setText(input.getText().toString() + "."); } }); // 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"); // } // }); 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() + "*"); } } }); // 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)); // } // } // }); 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",""); res = "="+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(""); } }); // 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+"!"); // } // } // }); } int factorial(int n){ 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; } }
Leave a Comment