Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.event.*;
- import java.awt.*;
- import javax.swing.*;
- import java.lang.*;
- public class ButtonHandler implements ActionListener{
- static String currentDisplay;
- static String type;
- static String lastOp;
- final String pi = "3.14159265";
- static String saved = "0";
- static int operatorNum;
- static int xorVar = 0;
- static double num1 = 1;
- static int num2 = 1;
- static double result = 0;
- static int resultHex = 0;
- static boolean operated = false;
- static boolean start = true;
- static boolean point;
- static boolean hexa = false;
- static boolean hexStart = true;
- public void actionPerformed(ActionEvent e){
- String command = e.getActionCommand();
- doStuff(command);
- }
- public void doStuff(String command){
- if(type == "Standard"){
- currentDisplay = StandardCalculator.getCalcText();
- }else{
- currentDisplay = ScientificCalculator.getCalcText();
- }
- if(hexa){
- num2 = Integer.parseInt(currentDisplay, 16);
- }else{
- num1 = Double.parseDouble(currentDisplay);
- }
- //operators
- if(Double.parseDouble(command) >= 10){
- operatorNum = Integer.parseInt(command);
- switch(operatorNum){
- case 10: // =
- if(lastOp == "bAdd"){
- if(hexa){
- addCalcHex(num2);
- }else{
- addCalc(num1);
- }
- }
- else if(lastOp == "bSub"){
- if(hexa){
- subCalcHex(num2);
- }else{
- subCalc(num1);
- }
- }
- else if(lastOp == "bMul"){
- if(hexa){
- mulCalcHex(num2);
- }else{
- mulCalc(num1);
- }
- }
- else if(lastOp == "bDiv"){
- if(hexa){
- divCalcHex(num2);
- }else{
- divCalc(num1);
- }
- }
- else if(lastOp == "bSqrt") sqrtCalc(num1);
- else if(lastOp == "bSMod") modCalc(num1);
- else if(lastOp == "bSXor") xorCalc(currentDisplay);
- else if(lastOp == "bSAnd") andCalc(currentDisplay);
- else if(lastOp == "bSOr") orCalc(currentDisplay);
- hexStart = true;
- operated = true;
- start = true;
- break;
- case 11: // + ADD
- if(hexa){
- addCalcHex(num2);
- }else{
- addCalc(num1);
- }
- break;
- case 12: // - SUBTRACT
- if(hexa){
- subCalcHex(num2);
- }else{
- subCalc(num1);
- }
- break;
- case 13: // * MULTIPLY
- if(hexa){
- mulCalcHex(num2);
- }else{
- mulCalc(num1);
- }
- break;
- case 14: // / DIVIDE
- if(hexa){
- divCalcHex(num2);
- }else{
- divCalc(num1);
- }
- break;
- case 15: // SQUARE ROOT
- sqrtCalc(num1);
- break;
- case 16: // %
- //percCalc(num1);
- break;
- case 17: // RECIPROCAL
- recCalc(num1);
- break;
- case 18: // BCE
- currentDisplay = "0";
- lastOp = null;
- break;
- case 19: // BC
- currentDisplay = "0";
- result = 0;
- num1 = 0;
- start = true;
- hexStart = true;
- break;
- case 20: // BACKSPACE
- if(currentDisplay.length() == 1){
- currentDisplay = "0";
- }else{
- currentDisplay = currentDisplay.substring(0,currentDisplay.length()-1);
- }
- break;
- case 21: // . POINT
- int x = 0;
- point = false;
- for(int i = 0; i < currentDisplay.length(); i++){
- if(currentDisplay.charAt(i)=='.'){
- point = true;
- }
- }
- if(!point){
- currentDisplay = currentDisplay + '.';
- }
- break;
- case 22: // MS
- saved = currentDisplay;
- break;
- case 23: // MR
- currentDisplay = saved;
- break;
- case 24: // M+
- saved = "" + (Double.parseDouble(saved) + num1);
- break;
- case 25: // M+
- saved = "0";
- break;
- case 26: // +/-
- currentDisplay = "" + Double.parseDouble(currentDisplay) * (-1);
- break;
- case 27: // Mod
- modCalc(num1);
- break;
- case 28: // int
- int p = 0;
- point = false;
- for(int i = 0; i < currentDisplay.length(); i++){
- if(currentDisplay.charAt(i)=='.'){
- point = true;
- p = i;
- }
- }
- if(point){
- currentDisplay = currentDisplay.substring(0,p);
- }
- break;
- case 29: // pi
- currentDisplay = pi;
- break;
- case 30: // hex
- ScientificCalculator.b2.setEnabled(true);
- ScientificCalculator.b3.setEnabled(true);
- ScientificCalculator.b4.setEnabled(true);
- ScientificCalculator.b5.setEnabled(true);
- ScientificCalculator.b6.setEnabled(true);
- ScientificCalculator.b7.setEnabled(true);
- ScientificCalculator.b8.setEnabled(true);
- ScientificCalculator.b9.setEnabled(true);
- ScientificCalculator.bSPi.setEnabled(false);
- ScientificCalculator.bSA.setEnabled(true);
- ScientificCalculator.bSB.setEnabled(true);
- ScientificCalculator.bSC.setEnabled(true);
- ScientificCalculator.bSD.setEnabled(true);
- ScientificCalculator.bSE.setEnabled(true);
- ScientificCalculator.bSF.setEnabled(true);
- currentDisplay = Integer.toHexString(Integer.parseInt(currentDisplay));
- hexa = true;
- break;
- case 31: // dec
- ScientificCalculator.b2.setEnabled(true);
- ScientificCalculator.b3.setEnabled(true);
- ScientificCalculator.b4.setEnabled(true);
- ScientificCalculator.b5.setEnabled(true);
- ScientificCalculator.b6.setEnabled(true);
- ScientificCalculator.b7.setEnabled(true);
- ScientificCalculator.b8.setEnabled(true);
- ScientificCalculator.b9.setEnabled(true);
- ScientificCalculator.bSPi.setEnabled(true);
- ScientificCalculator.bSA.setEnabled(false);
- ScientificCalculator.bSB.setEnabled(false);
- ScientificCalculator.bSC.setEnabled(false);
- ScientificCalculator.bSD.setEnabled(false);
- ScientificCalculator.bSE.setEnabled(false);
- ScientificCalculator.bSF.setEnabled(false);
- currentDisplay = "" + Integer.parseInt(currentDisplay, 16);
- hexa = false;
- break;
- case 32: // oct
- ScientificCalculator.b2.setEnabled(true);
- ScientificCalculator.b3.setEnabled(true);
- ScientificCalculator.b4.setEnabled(true);
- ScientificCalculator.b5.setEnabled(true);
- ScientificCalculator.b6.setEnabled(true);
- ScientificCalculator.b7.setEnabled(true);
- ScientificCalculator.b8.setEnabled(false);
- ScientificCalculator.b9.setEnabled(false);
- ScientificCalculator.bSPi.setEnabled(false);
- ScientificCalculator.bSA.setEnabled(false);
- ScientificCalculator.bSB.setEnabled(false);
- ScientificCalculator.bSC.setEnabled(false);
- ScientificCalculator.bSD.setEnabled(false);
- ScientificCalculator.bSE.setEnabled(false);
- ScientificCalculator.bSF.setEnabled(false);
- currentDisplay = Integer.toOctalString(Integer.parseInt(currentDisplay));
- hexa = false;
- break;
- case 33: // bin
- ScientificCalculator.b2.setEnabled(false);
- ScientificCalculator.b3.setEnabled(false);
- ScientificCalculator.b4.setEnabled(false);
- ScientificCalculator.b5.setEnabled(false);
- ScientificCalculator.b6.setEnabled(false);
- ScientificCalculator.b7.setEnabled(false);
- ScientificCalculator.b8.setEnabled(false);
- ScientificCalculator.b9.setEnabled(false);
- ScientificCalculator.bSPi.setEnabled(false);
- ScientificCalculator.bSA.setEnabled(false);
- ScientificCalculator.bSB.setEnabled(false);
- ScientificCalculator.bSC.setEnabled(false);
- ScientificCalculator.bSD.setEnabled(false);
- ScientificCalculator.bSE.setEnabled(false);
- ScientificCalculator.bSF.setEnabled(false);
- currentDisplay = Integer.toBinaryString(Integer.parseInt(currentDisplay));
- hexa = false;
- break;
- case 34: // Not
- currentDisplay = "" +((Double.parseDouble(currentDisplay)+1) * (-1));
- operated = true;
- break;
- case 35: // Xor
- xorCalc(currentDisplay);
- break;
- case 36: // And
- andCalc(currentDisplay);
- break;
- case 37: // Or
- orCalc(currentDisplay);
- break;
- case 38: // A
- if(hexStart)currentDisplay = "A";
- else currentDisplay = currentDisplay + "A";
- hexStart = false;
- break;
- case 39: // B
- if(hexStart)currentDisplay = "B";
- else currentDisplay = currentDisplay + "B";
- hexStart = false;
- break;
- case 40: // C
- if(hexStart)currentDisplay = "C";
- else currentDisplay = currentDisplay + "C";
- hexStart = false;
- break;
- case 41: // D
- if(hexStart)currentDisplay = "D";
- else currentDisplay = currentDisplay + "D";
- hexStart = false;
- break;
- case 42: // E
- if(hexStart)currentDisplay = "E";
- else currentDisplay = currentDisplay + "E";
- hexStart = false;
- break;
- case 43: // F
- if(hexStart)currentDisplay = "F";
- else currentDisplay = currentDisplay + "F";
- hexStart = false;
- break;
- default:
- System.out.println("wala");
- }
- }
- else if(num1 == 0 && currentDisplay.length() == 2 && currentDisplay.charAt(1) == '.'){
- currentDisplay = currentDisplay + command;
- }
- else if(operated){
- operated = false;
- currentDisplay = command;
- hexStart = false;
- }
- else if(hexa){
- if(num2 == 0){
- operated = false;
- currentDisplay = command;
- hexStart = false;
- }else{
- currentDisplay = currentDisplay + command;
- }
- }
- else if(num1 == 0){
- operated = false;
- currentDisplay = command;
- hexStart = false;
- }else{
- currentDisplay = currentDisplay + command;
- }
- System.out.println(num2);
- //System.out.println(currentDisplay.charAt(currentDisplay.length()-1));
- if(currentDisplay.length()>2 && operated){
- if(currentDisplay.charAt(0)!='0' && currentDisplay.charAt(currentDisplay.length()-1) == '0' && currentDisplay.charAt(currentDisplay.length()-2) == '.'){
- currentDisplay = currentDisplay.substring(0,currentDisplay.length()-2);
- }
- }
- if(type == "Standard"){
- StandardCalculator.setCalcText(currentDisplay);
- }else{
- ScientificCalculator.setCalcText(currentDisplay);
- }
- }
- public void addCalc(double x){
- if(lastOp == "bSub"){
- subCalc(x);
- }
- else if(lastOp == "bMul"){
- mulCalc(x);
- }
- else if(lastOp == "bDiv"){
- divCalc(x);
- }
- else if(start){
- currentDisplay = "" + x;
- result = x;
- }
- else{
- result = result + x;
- currentDisplay = "" + result;
- }
- lastOp = "bAdd";
- start = false;
- operated = true;
- }
- public void subCalc(double x){
- if(lastOp == "bAdd"){
- addCalc(x);
- }
- else if(lastOp == "bMul"){
- mulCalc(x);
- }
- else if(lastOp == "bDiv"){
- divCalc(x);
- }
- else if(start){
- currentDisplay = "" + x;
- result =x;
- }
- else{
- result = result - x;
- currentDisplay = "" + result;
- }
- lastOp = "bSub";
- start = false;
- operated = true;
- }
- public void mulCalc(double x){
- if(lastOp == "bAdd"){
- addCalc(x);
- }
- else if(lastOp == "bSub"){
- subCalc(x);
- }
- else if(lastOp == "bDiv"){
- divCalc(x);
- }
- else if(start){
- currentDisplay = "" + x;
- result = x;
- }
- else{
- result = result * x;
- currentDisplay = "" + result;
- }
- lastOp = "bMul";
- start = false;
- operated = true;
- }
- public void divCalc(double x){
- if(lastOp == "bAdd"){
- addCalc(x);
- }
- else if(lastOp == "bSub"){
- subCalc(x);
- }
- else if(lastOp == "bMul"){
- mulCalc(x);
- }
- else if(start){
- currentDisplay = "" + x;
- result =x;
- }
- else{
- result = result / x;
- currentDisplay = "" + result;
- }
- lastOp = "bDiv";
- start = false;
- operated = true;
- }
- public void sqrtCalc(double x){
- result = Math.sqrt(x);
- currentDisplay = "" + result;
- lastOp = "bSqrt";
- start = true;
- operated = true;
- }
- public void recCalc(double x){
- result = 1/x;
- currentDisplay = "" + result;
- lastOp = "bRecip";
- start = true;
- operated = true;
- }
- public void modCalc(double x){
- if(lastOp == "bAdd"){
- addCalc(x);
- }
- if(lastOp == "bSub"){
- subCalc(x);
- }
- else if(lastOp == "bMul"){
- mulCalc(x);
- }
- else if(lastOp == "bDiv"){
- divCalc(x);
- }
- else if(start){
- currentDisplay = "" + x;
- result = x;
- }
- else{
- result = result % x;
- currentDisplay = "" + result;
- }
- lastOp = "bSMod";
- start = false;
- operated = true;
- }
- public void xorCalc(String x){
- if(start){
- currentDisplay = "" + x;
- xorVar = Integer.parseInt(x);
- }
- else{
- xorVar = xorVar ^ Integer.parseInt(x);
- currentDisplay = "" + xorVar;
- }
- lastOp = "bSXor";
- start = false;
- operated = true;
- }
- public void andCalc(String x){
- if(start){
- currentDisplay = "" + x;
- xorVar = Integer.parseInt(x);
- }
- else{
- xorVar = xorVar & Integer.parseInt(x);
- currentDisplay = "" + xorVar;
- }
- lastOp = "bSAnd";
- start = false;
- operated = true;
- }
- public void orCalc(String x){
- if(start){
- currentDisplay = "" + x;
- xorVar = Integer.parseInt(x);
- }
- else{
- xorVar = xorVar | Integer.parseInt(x);
- currentDisplay = "" + xorVar;
- }
- lastOp = "bSOr";
- start = false;
- operated = true;
- }
- public void addCalcHex(int x){
- if(lastOp == "bSub"){
- subCalcHex(x);
- }
- else if(lastOp == "bMul"){
- mulCalcHex(x);
- }
- else if(lastOp == "bDiv"){
- divCalcHex(x);
- }
- else if(start){
- currentDisplay = "" + currentDisplay;
- resultHex = x;
- }
- else{
- resultHex = resultHex + x;
- currentDisplay = "" + Integer.toHexString(resultHex);
- }
- lastOp = "bAdd";
- start = false;
- operated = true;
- hexStart = true;
- System.out.println(x);
- }
- public void subCalcHex(int x){
- if(lastOp == "bAdd"){
- addCalcHex(x);
- }
- else if(lastOp == "bMul"){
- mulCalcHex(x);
- }
- else if(lastOp == "bDiv"){
- divCalcHex(x);
- }
- else if(start){
- currentDisplay = "" + currentDisplay;
- resultHex = x;
- }
- else{
- resultHex = resultHex - x;
- currentDisplay = "" + Integer.toHexString(resultHex);
- }
- lastOp = "bSub";
- start = false;
- operated = true;
- hexStart = true;
- }
- public void mulCalcHex(int x){
- if(lastOp == "bAdd"){
- addCalcHex(x);
- }
- else if(lastOp == "bSub"){
- subCalcHex(x);
- }
- else if(lastOp == "bDiv"){
- divCalcHex(x);
- }
- else if(start){
- currentDisplay = "" + currentDisplay;
- resultHex = x;
- }
- else{
- resultHex = resultHex * x;
- currentDisplay = "" + Integer.toHexString(resultHex);
- }
- lastOp = "bMul";
- start = false;
- operated = true;
- hexStart = true;
- }
- public void divCalcHex(int x){
- if(lastOp == "bAdd"){
- addCalcHex(x);
- }
- else if(lastOp == "bSub"){
- subCalcHex(x);
- }
- else if(lastOp == "bMul"){
- mulCalcHex(x);
- }
- else if(start){
- currentDisplay = "" + currentDisplay;
- resultHex = x;
- }
- else{
- resultHex = resultHex / x;
- currentDisplay = "" + Integer.toHexString(resultHex);
- }
- lastOp = "bDiv";
- start = false;
- operated = true;
- hexStart = true;
- }
- //determine calculator type
- static void calcType(String whatType){
- if(whatType == "Standard"){
- type = "Standard";
- }else{
- type = "Scientific";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement