Advertisement
Sajib_Ahmed

Calculator

Feb 28th, 2021
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.44 KB | None | 0 0
  1. package com.example.calculator;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.TextView;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.  
  11.     TextView result, result2;
  12.     int num1, num2;
  13.     String pi = String.valueOf(3.141592654);
  14.     char c = '0';
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         result = (TextView) findViewById(R.id.Output);
  21.         result2 = (TextView) findViewById(R.id.Output2);
  22.     }
  23.  
  24.     public void Del(View view) {
  25.         String s = result.getText().toString();
  26.         if (s.length() != 0) {
  27.             s = s.substring(0, s.length() - 1);
  28.             result.setText(s);
  29.         }
  30.  
  31.     }
  32.  
  33.     public void AC(View view) {
  34.         result.setText("");
  35.         result2.setText("");
  36.     }
  37.  
  38.     public void numzero(View view) {
  39.         String s = result.getText().toString();
  40.         if (s.length() != 0) {
  41.             result.setText(s + "0");
  42.         }
  43.     }
  44.  
  45.     public void numdot(View view) {
  46.         result.setText(result.getText() + ".");
  47.     }
  48.  
  49.     public void numone(View view) {
  50.         result.setText(result.getText() + "1");
  51.     }
  52.  
  53.     public void numtwo(View view) {
  54.         result.setText(result.getText() + "2");
  55.     }
  56.  
  57.     public void numthree(View view) {
  58.         result.setText(result.getText() + "3");
  59.     }
  60.  
  61.     public void numfour(View view) {
  62.         result.setText(result.getText() + "4");
  63.     }
  64.  
  65.     public void numfive(View view) {
  66.         result.setText(result.getText() + "5");
  67.     }
  68.  
  69.     public void numsix(View view) {
  70.         result.setText(result.getText() + "6");
  71.     }
  72.  
  73.     public void numseven(View view) {
  74.         result.setText(result.getText() + "7");
  75.     }
  76.  
  77.     public void numeight(View view) {
  78.         result.setText(result.getText() + "8");
  79.     }
  80.  
  81.     public void numnine(View view) {
  82.         result.setText(result.getText() + "9");
  83.     }
  84.  
  85.     public void numplus(View view) {
  86.         result.setText(result.getText() + "+");
  87.     }
  88.  
  89.     public void numdiv(View view) {
  90.         result.setText(result.getText() + "/");
  91.     }
  92.  
  93.     public void numminus(View view) {
  94.         result.setText(result.getText() + "-");
  95.     }
  96.  
  97.     public void nummulti(View view) {
  98.         result.setText(result.getText() + "*");
  99.     }
  100.  
  101.     public void fb1(View view) {
  102.         result.setText(result.getText() + "(");
  103.     }
  104.  
  105.     public void fb2(View view) {
  106.         result.setText(result.getText() + ")");
  107.     }
  108.     public void ncr1(View view){
  109.         num1=Integer.parseInt(result.getText().toString());
  110.         result2.setText(result.getText());
  111.         result.setText("");
  112.         c='1';
  113.  
  114.     }
  115.     public void nprf(View view){
  116.         num1=Integer.parseInt(result.getText().toString());
  117.         result2.setText(result.getText());
  118.         result.setText("");
  119.         c='2';
  120.  
  121.     }
  122.  
  123.     public void pi1(View view) {
  124.         result2.setText(result.getText());
  125.         result.setText(result.getText() + pi);
  126.     }
  127.  
  128.     public void squr(View view) {
  129.         String s = result.getText().toString();
  130.         Double val = Math.sqrt(Double.parseDouble(s));
  131.         result.setText(String.valueOf(val));
  132.     }
  133.  
  134.     public void sinf(View view) {
  135.         result.setText(result.getText() + "sin(");
  136.     }
  137.  
  138.     public void cosf(View view) {
  139.         result.setText(result.getText() + "cos(");
  140.     }
  141.  
  142.     public void tanf(View view) {
  143.         result.setText(result.getText() + "tan(");
  144.     }
  145.  
  146.     public void facto(View view) {
  147.         int s = Integer.parseInt(result.getText().toString());
  148.         int fact = factorial(s);
  149.         result.setText(String.valueOf(fact));
  150.         result2.setText(s + "!");
  151.     }
  152.     public void lnf(View view)
  153.     {
  154.         result.setText(result.getText()+"ln(");
  155.     }
  156.     public void logf(View view)
  157.     {
  158.         result.setText(result.getText()+"log(");
  159.     }
  160.     public void power(View view)
  161.     {
  162.         Double s = Double.parseDouble(result.getText().toString());
  163.         Double Squares = (s*s);
  164.         result.setText(String.valueOf(Squares));
  165.         result2.setText(s + "²");
  166.     }
  167.     public void percent(View view)
  168.     {
  169.         Double s = Double.parseDouble(result.getText().toString());
  170.         Double p = (s/100);
  171.         result.setText(String.valueOf(p));
  172.         result2.setText(s + "%");
  173.     }
  174.     int ncrs(int n, int r)
  175.     {
  176.         return factorial(n)/(factorial(r)*factorial(n-r));
  177.     }
  178.     int nprs(int n, int r)
  179.     {
  180.         return factorial(n)/(factorial(n-r));
  181.     }
  182.     int factorial(int n)
  183.     {
  184.         return (n==1||n==0)?1 : n*factorial(n-1);
  185.     }
  186.     public void expression(View view)
  187.     {
  188.         result.setText(result.getText() + "e");
  189.     }
  190.     public void numequal(View view)
  191.     {
  192.         if(c=='0') {
  193.             String values = result.getText().toString();
  194.             Double fresult = eval(values);
  195.             result.setText(String.valueOf(fresult));
  196.             result2.setText(values);
  197.         }
  198.         if(c=='1')
  199.         {
  200.             num2=Integer.parseInt(result.getText().toString());
  201.             int fout=ncrs(num1,num2);
  202.             result.setText(String.valueOf(fout));
  203.             c='0';
  204.  
  205.         }
  206.         if(c=='2')
  207.         {
  208.             num2=Integer.parseInt(result.getText().toString());
  209.             int fout=nprs(num1,num2);
  210.             result.setText(String.valueOf(fout));
  211.             c='0';
  212.  
  213.         }
  214.  
  215.     }
  216.     public static double eval(final String str)
  217.     {
  218.         return new Object() {
  219.             int pos = -1, ch;
  220.  
  221.             void nextChar() {
  222.                 ch = (++pos < str.length()) ? str.charAt(pos) : -1;
  223.             }
  224.  
  225.             boolean eat(int charToEat) {
  226.                 while (ch == ' ') nextChar();
  227.                 if (ch == charToEat) {
  228.                     nextChar();
  229.                     return true;
  230.                 }
  231.                 return false;
  232.             }
  233.  
  234.             double parse() {
  235.                 nextChar();
  236.                 double x = parseExpression();
  237.                 if (pos < str.length()) throw new RuntimeException("Unexpected: " + (char)ch);
  238.                 return x;
  239.             }
  240.  
  241.  
  242.  
  243.             double parseExpression() {
  244.                 double x = parseTerm();
  245.                 for (;;) {
  246.                     if      (eat('+')) x += parseTerm();
  247.                     else if (eat('-')) x -= parseTerm();
  248.                     else return x;
  249.                 }
  250.             }
  251.  
  252.             double parseTerm() {
  253.                 double x = parseFactor();
  254.                 for (;;) {
  255.                     if      (eat('*')) x *= parseFactor();
  256.                     else if (eat('/')) x /= parseFactor();
  257.                     else return x;
  258.                 }
  259.             }
  260.  
  261.             double parseFactor() {
  262.                 if (eat('+')) return parseFactor();
  263.                 if (eat('-')) return -parseFactor();
  264.  
  265.                 double x;
  266.                 int startPos = this.pos;
  267.                 if (eat('(')) {
  268.                     x = parseExpression();
  269.                     eat(')');
  270.                 } else if ((ch >= '0' && ch <= '9') || ch == '.') { // numbers
  271.                     while ((ch >= '0' && ch <= '9') || ch == '.') nextChar();
  272.                     x = Double.parseDouble(str.substring(startPos, this.pos));
  273.                 } else if (ch >= 'a' && ch <= 'z') {
  274.                     while (ch >= 'a' && ch <= 'z') nextChar();
  275.                     String func = str.substring(startPos, this.pos);
  276.                     x = parseFactor();
  277.                     if (func.equals("sqrt")) x = Math.sqrt(x);
  278.                     else if (func.equals("sin")) x = Math.sin(Math.toRadians(x));
  279.                     else if (func.equals("cos")) x = Math.cos(Math.toRadians(x));
  280.                     else if (func.equals("tan")) x = Math.tan(Math.toRadians(x));
  281.                     else if (func.equals("log")) x = Math.log10(x);
  282.                     else if (func.equals("ln")) x = Math.log(x);
  283.                     else if(func.equals("e")) x=Math.exp(x);
  284.                     else throw new RuntimeException("Unknown function: " + func);
  285.                 } else {
  286.                     throw new RuntimeException("Unexpected: " + (char)ch);
  287.                 }
  288.  
  289.                 if (eat('^')) x = Math.pow(x, parseFactor());
  290.  
  291.                 return x;
  292.             }
  293.         }.parse();
  294.     }
  295.  
  296. }
  297.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement