Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package calculator; //can't get this to work.
- /**
- * This class is the part of the calculator program.
- * It does all of the processing of the data after each button is pressed.
- * The other part of the program is in the class CalcGUI.
- * @author me */
- public class CalcMain {
- /**
- * name of calculator GUI object
- */
- private static CalcGUI myCalc;
- /**
- * current operator
- */
- private static char op;
- /**
- * registers or variables for storing the numbers that have been entered
- */
- private static double x,y;
- /** flag to indicate data entry */
- private static boolean dataEntry = false;
- //flag for decimal entry
- /** flag to track how many decimal places we're working with.<br>
- * 1 = none<br>
- * 0 = decimal key has just been pressed<br>
- * -1, -2, -3 ... where the next number is placed in the display
- */
- private static int decimal = 1;
- /**
- * main function creates CalcGUI. That's it.
- * @param args command-line arguments (not used)
- */
- public static void main(String args[]) {
- myCalc = new CalcGUI();
- }
- /**
- * Processes each button as it is pressed.<br>
- * <b>Called from:</b> CalcGUI<br>
- * <b>Calls:</b> setDisplay() in CalcGUI to set the display of the calculator<br>
- * <b>Assumptions:</b> each each button has unique first character which is used to identify it.
- * @param s the string that comprises the button text
- */
- static void sendButton(String s) {
- // myCalc.setDisplay("5.22");
- char c = (char)s.charAt(0);
- // System.out.println(c);
- switch (c) {
- case 'C': //clear
- myCalc.setDisplay("0.0");
- x=0.0;
- dataEntry=false;
- break;
- case '.':
- decimal=0;
- break;
- case '1': case '2': case '3': case '4': case '5':
- case '6': case '7': case '8': case '9': case '0':
- if (dataEntry) {
- if (decimal < 1) {
- decimal--;
- if (x > 0)
- x = x + Character.getNumericValue(c) * Math.pow(10,decimal);
- else
- x = x - Character.getNumericValue(c) * Math.pow(10,decimal);
- } else
- if (x > 0)
- x = x*10.0 + Character.getNumericValue(c);
- else
- x = x*10.0 - Character.getNumericValue(c);
- }
- else {
- x = Character.getNumericValue(c);
- }
- dataEntry=true;
- myCalc.setDisplay(x);
- break;
- case '\u00B1': // +/-
- x = -1 * x;
- // System.out.println(x + "CHS");
- myCalc.setDisplay(x);
- break;
- case '\u221A':
- x = Math.sqrt(x); //results in NaN if sqrt of negative number
- myCalc.setDisplay(x);
- dataEntry=false;
- break;
- case '+': case '-': case 'x':
- op = c;
- y=x;
- dataEntry = false;
- break;
- case '\u00F7':
- op = '/';
- y=x;
- dataEntry = false;
- break;
- case '=':
- //perform calculation
- switch (op) {
- case '+':
- x = y + x;
- break;
- case '-':
- x = y - x;
- break;
- case 'x':
- x = y * x;
- break;
- case '/':
- x = y / x; //divide by zero results in Infinity. Nice.
- break;
- }
- myCalc.setDisplay(x);
- dataEntry=false;
- break;
- default: //for testing
- //myCalc.setDisplay(s);
- }
- if (!dataEntry) decimal = 1;
- // System.out.println(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement