Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.scientificcalculator;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageButton;
- public class MainActivity extends AppCompatActivity {
- EditText input, sign;
- float value1, value2;
- String facto;
- int i,num;
- long factorial;
- boolean summation, subtraction, multiplication, divition, hasdot, log, ln, xn, root, not, sin, cos, tan;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- input = findViewById(R.id.input);
- sign = findViewById(R.id.sign);
- }
- public void zero(View view) {
- input.setText(input.getText()+"0");
- }
- public void one(View view) {
- input.setText(input.getText()+"1");
- }
- public void two(View view) {
- input.setText(input.getText()+"2");
- }
- public void three(View view) {
- input.setText(input.getText()+"3");
- }
- public void four(View view) {
- input.setText(input.getText()+"4");
- }
- public void five(View view) {
- input.setText(input.getText()+"5");
- }
- public void six(View view) {
- input.setText(input.getText()+"6");
- }
- public void seven(View view) {
- input.setText(input.getText()+"7");
- }
- public void eight(View view) {
- input.setText(input.getText()+"8");
- }
- public void nine(View view) {
- input.setText(input.getText()+"9");
- }
- public void dot(View view) {
- if(!hasdot){
- if(input.getText().equals("")){
- input.setText("0.");
- }
- else {
- input.setText(input.getText()+".");
- }
- hasdot=true;
- }
- }
- public void sum(View view) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- summation = true;
- sign.setText("+");
- input.setText("");
- hasdot = false;
- }
- catch (Exception e){
- }
- }
- public void sub(View view) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- subtraction = true;
- sign.setText("-");
- input.setText("");
- hasdot = false;
- }
- catch (Exception e){
- }
- }
- public void multi(View view) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- multiplication = true;
- sign.setText("*");
- input.setText("");
- hasdot = false;
- }
- catch (Exception e){
- }
- }
- public void divi(View view) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- divition = true;
- sign.setText("/");
- input.setText("");
- hasdot = false;
- }
- catch (Exception e){
- }
- }
- public void delete(View view) {
- try{
- if(input.getText().equals("")){
- input.setText(null);
- }
- else {
- int len = input.getText().length();
- String s = input.getText().toString();
- if(s.charAt(len-1)=='.'){
- input.setText(input.getText().subSequence(0,input.getText().length()-1));
- hasdot=false;
- }
- else {
- input.setText(input.getText().subSequence(0,input.getText().length()-1));
- }
- }
- }
- catch (Exception e){
- }
- }
- public void clear(View view) {
- input.setText("");
- sign.setText("");
- hasdot=false;
- }
- public void log(View view) {
- log = true;
- sign.setText("log");
- input.setText("");
- }
- public void ln(View view) {
- ln = true;
- sign.setText("ln");
- input.setText("");
- }
- public void xn(View view) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- xn = true;
- sign.setText("^");
- input.setText("");
- }
- catch (Exception e){
- }
- }
- public void root(View view) {
- root = true;
- sign.setText("√");
- input.setText("");
- }
- public void not(View view) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- facto = input.getText()+"";
- not = true;
- sign.setText("!");
- input.setText("");
- }
- catch (Exception e){
- }
- }
- public void sin(View view) {
- sin = true;
- sign.setText("sin");
- input.setText("");
- }
- public void cos(View view) {
- cos = true;
- sign.setText("cos");
- input.setText("");
- }
- public void tan(View view) {
- tan = true;
- sign.setText("ten");
- input.setText("");
- }
- public void equal(View view) {
- try{
- if (summation == true) {
- value2 = Float.parseFloat(input.getText() + "");
- input.setText(value1 + value2 + "");
- summation = false;
- sign.setText(null);
- } else if (subtraction == true) {
- value2 = Float.parseFloat(input.getText() + "");
- input.setText(value1 - value2 + "");
- subtraction = false;
- sign.setText(null);
- } else if (multiplication == true) {
- value2 = Float.parseFloat(input.getText() + "");
- input.setText(value1 * value2 + "");
- multiplication = false;
- sign.setText(null);
- } else if (divition == true) {
- value2 = Float.parseFloat(input.getText() + "");
- try {
- input.setText(value1 / value2 + "");
- divition = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- } else if (log == true) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- input.setText(Math.log10(value1) + "");
- log = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- } else if (ln == true) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- input.setText(Math.log(value1) + "");
- ln = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- } else if (xn == true) {
- try {
- value2 = Float.parseFloat(input.getText() + "");
- input.setText(Math.pow(value1, value2) + "");
- xn = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- } else if (root == true) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- input.setText(Math.sqrt(value1) + "");
- root = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- } else if (not == true) {
- num = Integer.parseInt(facto);
- factorial = 1;
- for (int i = 1; i <= num; ++i) {
- factorial *= i;
- }
- input.setText(factorial + "");
- sign.setText(null);
- not = false;
- } else if (sin == true) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- input.setText(Math.sin(value1) + "");
- sin = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- } else if (cos == true) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- input.setText(Math.cos(value1) + "");
- cos = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- } else if (tan == true) {
- try {
- value1 = Float.parseFloat(input.getText() + "");
- input.setText(Math.tan(value1) + "");
- tan = false;
- sign.setText(null);
- } catch (Exception e) {
- }
- }
- }
- catch (Exception e){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement