Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.12 KB | None | 0 0
  1. import java.awt.event.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.lang.*;
  5. public class ButtonHandler implements ActionListener{
  6.     static String currentDisplay;
  7.     static String type;
  8.     static String lastOp;
  9.     final String pi = "3.14159265";
  10.     static String saved = "0";
  11.     static int operatorNum;
  12.     static int xorVar = 0;
  13.     static double num1 = 1;
  14.     static int num2 = 1;
  15.     static double result = 0;
  16.     static int resultHex = 0;
  17.     static boolean operated = false;
  18.     static boolean start = true;
  19.     static boolean point;
  20.     static boolean hexa = false;
  21.     static boolean hexStart = true;
  22.     public void actionPerformed(ActionEvent e){
  23.         String command = e.getActionCommand();
  24.         doStuff(command);
  25.     }
  26.     public void doStuff(String command){
  27.         if(type == "Standard"){
  28.             currentDisplay = StandardCalculator.getCalcText();
  29.         }else{
  30.             currentDisplay = ScientificCalculator.getCalcText();
  31.         }
  32.         if(hexa){
  33.             num2 = Integer.parseInt(currentDisplay, 16);
  34.         }else{
  35.             num1 = Double.parseDouble(currentDisplay);
  36.         }
  37.         //operators
  38.         if(Double.parseDouble(command) >= 10){
  39.             operatorNum = Integer.parseInt(command);
  40.             switch(operatorNum){
  41.                 case 10: // =
  42.                     if(lastOp == "bAdd"){
  43.                         if(hexa){
  44.                             addCalcHex(num2);
  45.                         }else{
  46.                             addCalc(num1);
  47.                         }
  48.                     }
  49.                     else if(lastOp == "bSub"){
  50.                         if(hexa){
  51.                             subCalcHex(num2);
  52.                         }else{
  53.                             subCalc(num1);
  54.                         }
  55.                     }
  56.                     else if(lastOp == "bMul"){
  57.                         if(hexa){
  58.                             mulCalcHex(num2);
  59.                         }else{
  60.                             mulCalc(num1);
  61.                         }
  62.                     }
  63.                     else if(lastOp == "bDiv"){
  64.                         if(hexa){
  65.                             divCalcHex(num2);
  66.                         }else{
  67.                             divCalc(num1);
  68.                         }
  69.                     }
  70.                     else if(lastOp == "bSqrt") sqrtCalc(num1);
  71.                     else if(lastOp == "bSMod") modCalc(num1);
  72.                     else if(lastOp == "bSXor") xorCalc(currentDisplay);
  73.                     else if(lastOp == "bSAnd") andCalc(currentDisplay);
  74.                     else if(lastOp == "bSOr") orCalc(currentDisplay);
  75.                     hexStart = true;
  76.                     operated = true;
  77.                     start = true;
  78.                     break;
  79.                 case 11: // + ADD
  80.                     if(hexa){
  81.                         addCalcHex(num2);
  82.                     }else{
  83.                         addCalc(num1);
  84.                     }
  85.                     break;
  86.                 case 12: // - SUBTRACT
  87.                     if(hexa){
  88.                         subCalcHex(num2);
  89.                     }else{
  90.                         subCalc(num1);
  91.                     }
  92.                     break;
  93.                 case 13: // * MULTIPLY
  94.                     if(hexa){
  95.                         mulCalcHex(num2);
  96.                     }else{
  97.                         mulCalc(num1);
  98.                     }
  99.                     break;
  100.                 case 14: // / DIVIDE
  101.                     if(hexa){
  102.                         divCalcHex(num2);
  103.                     }else{
  104.                         divCalc(num1);
  105.                     }
  106.                     break;
  107.                 case 15: //   SQUARE ROOT
  108.                     sqrtCalc(num1);
  109.                     break;
  110.                 case 16: //    %
  111.                     //percCalc(num1);
  112.                     break;
  113.                 case 17: //    RECIPROCAL
  114.                     recCalc(num1);
  115.                     break;
  116.                 case 18: //    BCE
  117.                     currentDisplay = "0";
  118.                     lastOp = null;
  119.                     break;
  120.                 case 19: //    BC
  121.                     currentDisplay = "0";
  122.                     result = 0;
  123.                     num1 = 0;
  124.                     start = true;
  125.                     hexStart = true;
  126.                     break;
  127.                 case 20: //    BACKSPACE
  128.                     if(currentDisplay.length() == 1){
  129.                         currentDisplay = "0";
  130.                     }else{
  131.                         currentDisplay = currentDisplay.substring(0,currentDisplay.length()-1);
  132.                     }
  133.                     break;
  134.                 case 21: //    . POINT
  135.                     int x = 0;
  136.                     point = false;
  137.                     for(int i = 0; i < currentDisplay.length(); i++){
  138.                         if(currentDisplay.charAt(i)=='.'){
  139.                             point = true;
  140.                         }
  141.                     }
  142.                     if(!point){
  143.                         currentDisplay = currentDisplay + '.';
  144.                     }
  145.                     break;
  146.                 case 22: //    MS
  147.                         saved = currentDisplay;
  148.                     break;
  149.                 case 23: //    MR
  150.                         currentDisplay = saved;
  151.                     break;
  152.                 case 24: //    M+
  153.                         saved = "" + (Double.parseDouble(saved) + num1);
  154.                     break;
  155.                 case 25: //    M+
  156.                         saved = "0";
  157.                     break;
  158.                 case 26: //    +/-
  159.                         currentDisplay = "" + Double.parseDouble(currentDisplay) * (-1);
  160.                     break;
  161.                 case 27: //    Mod
  162.                         modCalc(num1);
  163.                     break;
  164.                 case 28: //    int
  165.                         int p = 0;
  166.                         point = false;
  167.                         for(int i = 0; i < currentDisplay.length(); i++){
  168.                             if(currentDisplay.charAt(i)=='.'){
  169.                                 point = true;
  170.                                 p = i;
  171.                             }
  172.                         }
  173.                         if(point){
  174.                             currentDisplay = currentDisplay.substring(0,p);
  175.                         }
  176.                     break;
  177.                 case 29: //    pi
  178.                             currentDisplay = pi;
  179.                     break;
  180.                 case 30: //    hex
  181.                             ScientificCalculator.b2.setEnabled(true);
  182.                             ScientificCalculator.b3.setEnabled(true);
  183.                             ScientificCalculator.b4.setEnabled(true);
  184.                             ScientificCalculator.b5.setEnabled(true);
  185.                             ScientificCalculator.b6.setEnabled(true);
  186.                             ScientificCalculator.b7.setEnabled(true);
  187.                             ScientificCalculator.b8.setEnabled(true);
  188.                             ScientificCalculator.b9.setEnabled(true);
  189.                             ScientificCalculator.bSPi.setEnabled(false);
  190.                             ScientificCalculator.bSA.setEnabled(true);
  191.                             ScientificCalculator.bSB.setEnabled(true);
  192.                             ScientificCalculator.bSC.setEnabled(true);
  193.                             ScientificCalculator.bSD.setEnabled(true);
  194.                             ScientificCalculator.bSE.setEnabled(true);
  195.                             ScientificCalculator.bSF.setEnabled(true);
  196.                             currentDisplay = Integer.toHexString(Integer.parseInt(currentDisplay));
  197.                             hexa = true;
  198.                     break;
  199.                 case 31: //    dec
  200.                             ScientificCalculator.b2.setEnabled(true);
  201.                             ScientificCalculator.b3.setEnabled(true);
  202.                             ScientificCalculator.b4.setEnabled(true);
  203.                             ScientificCalculator.b5.setEnabled(true);
  204.                             ScientificCalculator.b6.setEnabled(true);
  205.                             ScientificCalculator.b7.setEnabled(true);
  206.                             ScientificCalculator.b8.setEnabled(true);
  207.                             ScientificCalculator.b9.setEnabled(true);
  208.                             ScientificCalculator.bSPi.setEnabled(true);
  209.                             ScientificCalculator.bSA.setEnabled(false);
  210.                             ScientificCalculator.bSB.setEnabled(false);
  211.                             ScientificCalculator.bSC.setEnabled(false);
  212.                             ScientificCalculator.bSD.setEnabled(false);
  213.                             ScientificCalculator.bSE.setEnabled(false);
  214.                             ScientificCalculator.bSF.setEnabled(false);
  215.                             currentDisplay = "" + Integer.parseInt(currentDisplay, 16);
  216.                             hexa = false;
  217.                     break;
  218.                 case 32: //    oct
  219.                             ScientificCalculator.b2.setEnabled(true);
  220.                             ScientificCalculator.b3.setEnabled(true);
  221.                             ScientificCalculator.b4.setEnabled(true);
  222.                             ScientificCalculator.b5.setEnabled(true);
  223.                             ScientificCalculator.b6.setEnabled(true);
  224.                             ScientificCalculator.b7.setEnabled(true);
  225.                             ScientificCalculator.b8.setEnabled(false);
  226.                             ScientificCalculator.b9.setEnabled(false);
  227.                             ScientificCalculator.bSPi.setEnabled(false);
  228.                             ScientificCalculator.bSA.setEnabled(false);
  229.                             ScientificCalculator.bSB.setEnabled(false);
  230.                             ScientificCalculator.bSC.setEnabled(false);
  231.                             ScientificCalculator.bSD.setEnabled(false);
  232.                             ScientificCalculator.bSE.setEnabled(false);
  233.                             ScientificCalculator.bSF.setEnabled(false);
  234.                             currentDisplay = Integer.toOctalString(Integer.parseInt(currentDisplay));
  235.                             hexa = false;
  236.                     break;
  237.                 case 33: //    bin
  238.                             ScientificCalculator.b2.setEnabled(false);
  239.                             ScientificCalculator.b3.setEnabled(false);
  240.                             ScientificCalculator.b4.setEnabled(false);
  241.                             ScientificCalculator.b5.setEnabled(false);
  242.                             ScientificCalculator.b6.setEnabled(false);
  243.                             ScientificCalculator.b7.setEnabled(false);
  244.                             ScientificCalculator.b8.setEnabled(false);
  245.                             ScientificCalculator.b9.setEnabled(false);
  246.                             ScientificCalculator.bSPi.setEnabled(false);
  247.                             ScientificCalculator.bSA.setEnabled(false);
  248.                             ScientificCalculator.bSB.setEnabled(false);
  249.                             ScientificCalculator.bSC.setEnabled(false);
  250.                             ScientificCalculator.bSD.setEnabled(false);
  251.                             ScientificCalculator.bSE.setEnabled(false);
  252.                             ScientificCalculator.bSF.setEnabled(false);
  253.                             currentDisplay = Integer.toBinaryString(Integer.parseInt(currentDisplay));
  254.                             hexa = false;
  255.                     break;
  256.                 case 34: //    Not
  257.                             currentDisplay = "" +((Double.parseDouble(currentDisplay)+1) * (-1));
  258.                             operated = true;
  259.                     break;
  260.                 case 35: //    Xor
  261.                             xorCalc(currentDisplay);
  262.                     break;
  263.                 case 36: //    And
  264.                             andCalc(currentDisplay);
  265.                     break;
  266.                 case 37: //    Or
  267.                             orCalc(currentDisplay);
  268.                     break;
  269.                 case 38: //    A
  270.                             if(hexStart)currentDisplay = "A";
  271.                             else currentDisplay = currentDisplay + "A";
  272.                             hexStart = false;
  273.                     break;
  274.                 case 39: //    B
  275.                             if(hexStart)currentDisplay = "B";
  276.                             else currentDisplay = currentDisplay + "B";
  277.                             hexStart = false;
  278.                     break;
  279.                 case 40: //    C
  280.                             if(hexStart)currentDisplay = "C";
  281.                             else currentDisplay = currentDisplay + "C";
  282.                             hexStart = false;
  283.                     break;
  284.                 case 41: //    D
  285.                             if(hexStart)currentDisplay = "D";
  286.                             else currentDisplay = currentDisplay + "D";
  287.                             hexStart = false;
  288.                     break;
  289.                 case 42: //    E
  290.                             if(hexStart)currentDisplay = "E";
  291.                             else currentDisplay = currentDisplay + "E";
  292.                             hexStart = false;
  293.                     break;
  294.                 case 43: //    F
  295.                             if(hexStart)currentDisplay = "F";
  296.                             else currentDisplay = currentDisplay + "F";
  297.                             hexStart = false;
  298.                     break;
  299.                 default:
  300.                     System.out.println("wala");
  301.             }
  302.         }
  303.         else if(num1 == 0 && currentDisplay.length() == 2 && currentDisplay.charAt(1) == '.'){
  304.             currentDisplay = currentDisplay + command;
  305.         }
  306.         else if(operated){
  307.             operated = false;
  308.             currentDisplay = command;
  309.             hexStart = false;
  310.         }
  311.         else if(hexa){
  312.             if(num2 == 0){
  313.                 operated = false;
  314.                 currentDisplay = command;
  315.                 hexStart = false;
  316.             }else{
  317.             currentDisplay = currentDisplay + command;
  318.             }
  319.         }
  320.         else if(num1 == 0){
  321.             operated = false;
  322.             currentDisplay = command;
  323.             hexStart = false;
  324.         }else{
  325.             currentDisplay = currentDisplay + command;
  326.         }
  327.        
  328.        
  329.         System.out.println(num2);
  330.         //System.out.println(currentDisplay.charAt(currentDisplay.length()-1));
  331.         if(currentDisplay.length()>2 && operated){
  332.             if(currentDisplay.charAt(0)!='0' && currentDisplay.charAt(currentDisplay.length()-1) == '0' && currentDisplay.charAt(currentDisplay.length()-2) == '.'){
  333.                 currentDisplay = currentDisplay.substring(0,currentDisplay.length()-2);
  334.             }
  335.         }
  336.         if(type == "Standard"){
  337.             StandardCalculator.setCalcText(currentDisplay);
  338.         }else{
  339.             ScientificCalculator.setCalcText(currentDisplay);
  340.         }
  341.     }
  342.     public void addCalc(double x){
  343.         if(lastOp == "bSub"){
  344.             subCalc(x);
  345.         }
  346.         else if(lastOp == "bMul"){
  347.             mulCalc(x);
  348.         }
  349.         else if(lastOp == "bDiv"){
  350.             divCalc(x);
  351.         }
  352.         else if(start){
  353.             currentDisplay = "" + x;
  354.             result = x;
  355.         }
  356.         else{
  357.             result = result + x;
  358.             currentDisplay = "" + result;
  359.         }
  360.         lastOp = "bAdd";
  361.         start = false;
  362.         operated = true;
  363.     }
  364.     public void subCalc(double x){
  365.         if(lastOp == "bAdd"){
  366.             addCalc(x);
  367.         }
  368.         else if(lastOp == "bMul"){
  369.             mulCalc(x);
  370.         }
  371.         else if(lastOp == "bDiv"){
  372.             divCalc(x);
  373.         }
  374.         else if(start){
  375.             currentDisplay = "" + x;
  376.             result =x;
  377.         }
  378.         else{
  379.             result = result - x;
  380.             currentDisplay = "" + result;
  381.         }
  382.         lastOp = "bSub";
  383.         start = false;
  384.         operated = true;
  385.     }
  386.     public void mulCalc(double x){
  387.         if(lastOp == "bAdd"){
  388.             addCalc(x);
  389.         }
  390.         else if(lastOp == "bSub"){
  391.             subCalc(x);
  392.         }
  393.         else if(lastOp == "bDiv"){
  394.             divCalc(x);
  395.         }
  396.         else if(start){
  397.             currentDisplay = "" + x;
  398.             result = x;
  399.         }
  400.         else{
  401.             result = result * x;
  402.             currentDisplay = "" + result;
  403.         }
  404.         lastOp = "bMul";
  405.         start = false;
  406.         operated = true;
  407.     }
  408.     public void divCalc(double x){
  409.         if(lastOp == "bAdd"){
  410.             addCalc(x);
  411.         }
  412.         else if(lastOp == "bSub"){
  413.             subCalc(x);
  414.         }
  415.         else if(lastOp == "bMul"){
  416.             mulCalc(x);
  417.         }
  418.         else if(start){
  419.             currentDisplay = "" + x;
  420.             result =x;
  421.         }
  422.         else{
  423.             result = result / x;
  424.             currentDisplay = "" + result;
  425.         }
  426.         lastOp = "bDiv";
  427.         start = false;
  428.         operated = true;
  429.     }
  430.     public void sqrtCalc(double x){
  431.         result = Math.sqrt(x);
  432.         currentDisplay = "" + result;
  433.         lastOp = "bSqrt";
  434.         start = true;
  435.         operated = true;
  436.     }
  437.     public void recCalc(double x){
  438.         result = 1/x;
  439.         currentDisplay = "" + result;
  440.         lastOp = "bRecip";
  441.         start = true;
  442.         operated = true;
  443.     }
  444.     public void modCalc(double x){
  445.         if(lastOp == "bAdd"){
  446.             addCalc(x);
  447.         }
  448.         if(lastOp == "bSub"){
  449.             subCalc(x);
  450.         }
  451.         else if(lastOp == "bMul"){
  452.             mulCalc(x);
  453.         }
  454.         else if(lastOp == "bDiv"){
  455.             divCalc(x);
  456.         }
  457.         else if(start){
  458.             currentDisplay = "" + x;
  459.             result = x;
  460.         }
  461.         else{
  462.             result = result % x;
  463.             currentDisplay = "" + result;
  464.         }
  465.         lastOp = "bSMod";
  466.         start = false;
  467.         operated = true;
  468.     }
  469.     public void xorCalc(String x){
  470.         if(start){
  471.             currentDisplay = "" + x;
  472.             xorVar = Integer.parseInt(x);
  473.         }
  474.         else{
  475.             xorVar = xorVar ^ Integer.parseInt(x);
  476.             currentDisplay = "" + xorVar;
  477.         }
  478.         lastOp = "bSXor";
  479.         start = false;
  480.         operated = true;
  481.     }
  482.     public void andCalc(String x){
  483.         if(start){
  484.             currentDisplay = "" + x;
  485.             xorVar = Integer.parseInt(x);
  486.         }
  487.         else{
  488.             xorVar = xorVar & Integer.parseInt(x);
  489.             currentDisplay = "" + xorVar;
  490.         }
  491.         lastOp = "bSAnd";
  492.         start = false;
  493.         operated = true;
  494.     }
  495.     public void orCalc(String x){
  496.         if(start){
  497.             currentDisplay = "" + x;
  498.             xorVar = Integer.parseInt(x);
  499.         }
  500.         else{
  501.             xorVar = xorVar | Integer.parseInt(x);
  502.             currentDisplay = "" + xorVar;
  503.         }
  504.         lastOp = "bSOr";
  505.         start = false;
  506.         operated = true;
  507.     }
  508.     public void addCalcHex(int x){
  509.         if(lastOp == "bSub"){
  510.             subCalcHex(x);
  511.         }
  512.         else if(lastOp == "bMul"){
  513.             mulCalcHex(x);
  514.         }
  515.         else if(lastOp == "bDiv"){
  516.             divCalcHex(x);
  517.         }
  518.         else if(start){
  519.             currentDisplay = "" + currentDisplay;
  520.             resultHex = x;
  521.         }      
  522.         else{
  523.             resultHex = resultHex + x;
  524.             currentDisplay = "" + Integer.toHexString(resultHex);
  525.         }
  526.         lastOp = "bAdd";
  527.         start = false;
  528.         operated = true;
  529.         hexStart = true;
  530.         System.out.println(x);
  531.     }
  532.     public void subCalcHex(int x){
  533.         if(lastOp == "bAdd"){
  534.             addCalcHex(x);
  535.         }
  536.         else if(lastOp == "bMul"){
  537.             mulCalcHex(x);
  538.         }
  539.         else if(lastOp == "bDiv"){
  540.             divCalcHex(x);
  541.         }
  542.         else if(start){
  543.             currentDisplay = "" + currentDisplay;
  544.             resultHex = x;
  545.         }
  546.         else{
  547.             resultHex = resultHex - x;
  548.             currentDisplay = "" + Integer.toHexString(resultHex);
  549.         }
  550.         lastOp = "bSub";
  551.         start = false;
  552.         operated = true;
  553.         hexStart = true;
  554.     }
  555.     public void mulCalcHex(int x){
  556.         if(lastOp == "bAdd"){
  557.             addCalcHex(x);
  558.         }
  559.         else if(lastOp == "bSub"){
  560.             subCalcHex(x);
  561.         }
  562.         else if(lastOp == "bDiv"){
  563.             divCalcHex(x);
  564.         }
  565.         else if(start){
  566.             currentDisplay = "" + currentDisplay;
  567.             resultHex = x;
  568.         }
  569.         else{
  570.             resultHex = resultHex * x;
  571.             currentDisplay = "" + Integer.toHexString(resultHex);
  572.         }
  573.         lastOp = "bMul";
  574.         start = false;
  575.         operated = true;
  576.         hexStart = true;
  577.     }
  578.     public void divCalcHex(int x){
  579.         if(lastOp == "bAdd"){
  580.             addCalcHex(x);
  581.         }
  582.         else if(lastOp == "bSub"){
  583.             subCalcHex(x);
  584.         }
  585.         else if(lastOp == "bMul"){
  586.             mulCalcHex(x);
  587.         }
  588.         else if(start){
  589.             currentDisplay = "" + currentDisplay;
  590.             resultHex =  x;
  591.         }
  592.         else{
  593.             resultHex = resultHex / x;
  594.             currentDisplay = "" + Integer.toHexString(resultHex);
  595.         }
  596.         lastOp = "bDiv";
  597.         start = false;
  598.         operated = true;
  599.         hexStart = true;
  600.     }
  601.     //determine calculator type
  602.     static void calcType(String whatType){
  603.         if(whatType == "Standard"){
  604.             type = "Standard";
  605.         }else{
  606.             type = "Scientific";
  607.         }
  608.     }
  609.    
  610. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement