import java.awt.*; import java.lang.*; import java.util.*; import java.applet.Applet; public class Calculator extends Applet { TextField Disp; /* Display Area for inputs and results */ Checkbox InvBox; /* Inverse Function CheckBox */ StringBuffer Expr; /* String for the Number on Disp */ Stack S1, S2; /* Stacks for operands and operators */ double memValue=0; /* Memory Value */ boolean newValue=true; public void init() { Expr = new StringBuffer(); S1 = new Stack(); S2 = new Stack(); Panel CalTopPanel = new Panel(); Panel CalBtmPanel = new Panel(); setLayout(new GridLayout(0, 1, 20, 20)); add(CalTopPanel); add(CalBtmPanel); CalTopPanel.setLayout(new GridLayout(0, 1, 20, 10)); Panel CalDisPanel = new Panel(); CalDisPanel.setFont(new Font("Helvetica", Font.PLAIN, 20)); CalDisPanel.setLayout(new CardLayout()); CalDisPanel.add(Disp = new TextField("0.")); Disp.setEditable(false); Disp.setBackground(Color.white); Disp.setForeground(Color.blue); CalTopPanel.add(CalDisPanel); /* Display Area */ Panel CalNumPanel = new Panel(); CalNumPanel.setLayout(new GridLayout(4, 3, 20, 15)); CalNumPanel.setFont(new Font("Helvetica", Font.PLAIN, 14)); /* Add number buttons */ CalNumPanel.add(new Button("7")); CalNumPanel.add(new Button("8")); CalNumPanel.add(new Button("9")); CalNumPanel.add(new Button("4")); CalNumPanel.add(new Button("5")); CalNumPanel.add(new Button("6")); CalNumPanel.add(new Button("1")); CalNumPanel.add(new Button("2")); CalNumPanel.add(new Button("3")); CalNumPanel.add(new Button("0")); CalNumPanel.add(new Button(".")); CalNumPanel.add(new Button("+/-")); Panel CalOprPanel = new Panel(); CalOprPanel.setLayout(new GridLayout(0, 2, 20, 15)); CalOprPanel.setFont(new Font("Helvetica", Font.PLAIN, 14)); /* Add operator buttons */ CalOprPanel.add(new Button("(")); CalOprPanel.add(new Button(")")); CalOprPanel.add(new Button("*")); CalOprPanel.add(new Button("/")); CalOprPanel.add(new Button("+")); CalOprPanel.add(new Button("-")); CalOprPanel.add(new Button("=")); CalOprPanel.add(new Button("1/X")); CalBtmPanel.setLayout(new GridLayout(0, 2, 20, 20)); CalBtmPanel.add(CalNumPanel); CalBtmPanel.add(CalOprPanel); Panel CalMemPanel = new Panel(); CalMemPanel.setLayout(new GridLayout(1, 0, 20, 20)); CalMemPanel.setFont(new Font("Helvetica", Font.PLAIN, 14)); /* Add memory buttons */ CalMemPanel.add(new Button("MC")); CalMemPanel.add(new Button("MR")); CalMemPanel.add(new Button("M+")); CalMemPanel.add(new Button("M-")); Panel CalMthPanel = new Panel(); CalMthPanel.setLayout(new GridLayout(1, 0, 20, 20)); CalMthPanel.setFont(new Font("Helvetica", Font.PLAIN, 14)); /* Add math function buttons */ CalMthPanel.add(new Button("SIN")); CalMthPanel.add(new Button("COS")); CalMthPanel.add(new Button("X^2")); CalMthPanel.add(new Button("EXP")); Panel CalCtlPanel = new Panel(); CalCtlPanel.setFont(new Font("Helvetica", Font.PLAIN, 14)); CalCtlPanel.setLayout(new GridLayout(1,0,20,20)); /* Add control buttons */ CalCtlPanel.add(InvBox = new Checkbox("INV. F()")); CalCtlPanel.add(new Button("CA")); CalCtlPanel.add(new Button("C")); CalCtlPanel.add(new Button("Del")); CalTopPanel.add(CalCtlPanel); CalTopPanel.add(CalMthPanel); CalTopPanel.add(CalMemPanel); }/* end of init() */ /* Uniary operation function */ public boolean doUniary(Object arg) { double V1=0; int Op; Op = (((String)arg).charAt(0)); V1 = Double.valueOf(Expr.toString()).doubleValue(); if (InvBox.getState()) { switch(Op) { case '1': /* 1/X */ V1 = 1.0 / V1; break; case 'S': /* ArcSin() */ V1 = (180.0/Math.PI)*Math.asin(V1); break; case 'C': /* ArcCos() */ V1 = (180.0/Math.PI)*Math.acos(V1); break; case 'X': /* Sqrt() */ V1 = Math.sqrt(V1); break; case 'E': /* Log() */ V1 = Math.log(V1); break; } } else { switch(Op) { case '1': /* 1/X */ V1 = 1.0 / V1; break; case 'S': /* Sin() */ V1 = Math.sin(V1*Math.PI/180.0); break; case 'C': /* Cos() */ V1 = Math.cos(V1*Math.PI/180.0); break; case 'X': /* X^2 */ V1 = V1*V1; break; case 'E': /* Exp() */ V1 = Math.exp(V1); break; } } Expr.setLength(0); Expr.append((new Double(V1)).toString()); Disp.setText(Expr.toString()); return true; } /* Binary operation function */ public boolean doPostfix(int Op) { double V1=0, V2=0; if(!S1.empty()) V1 = ((Double)S1.pop()).doubleValue(); /* Pop top operand */ if(!S1.empty()) V2 = ((Double)S1.pop()).doubleValue(); /* Pop second operand */ switch(Op) { case '+': V1 = V2 + V1; break; case '-': V1 = V2 - V1; break; case '*': V1 = V2 * V1; break; case '/': V1 = V2 / V1; break; } S1.push(new Double(V1)); /* push result back to top of stack */ Expr.setLength(0); Expr.append((new Double(V1)).toString()); Disp.setText(Expr.toString()); /* Display result */ return true; } /* push operators and operands into two stacks according to precedence */ public boolean doInfix(Object arg) { int Op; if("=".equals(arg)) { S1.push(Double.valueOf(Expr.toString())); while(!S2.empty()) { Op = (((String)S2.peek()).charAt(0)); if ( Op == '(' ) return false; Op = (((String)S2.pop()).charAt(0)); doPostfix(Op); } } else if("+".equals(arg)) { S1.push(Double.valueOf(Expr.toString())); while(!S2.empty()) { Op = (((String)S2.peek()).charAt(0)); if ( Op == '(' ) break; Op = (((String)S2.pop()).charAt(0)); doPostfix(Op); } S2.push(arg); } else if("-".equals(arg)) { S1.push(Double.valueOf(Expr.toString())); while(!S2.empty()) { Op = (((String)S2.peek()).charAt(0)); if ( Op == '(' ) break; Op = (((String)S2.pop()).charAt(0)); doPostfix(Op); } S2.push(arg); } else if("*".equals(arg)) { S1.push(Double.valueOf(Expr.toString())); while(!S2.empty()) { Op = (((String)S2.peek()).charAt(0)); if ( Op == '(' || Op == '+' || Op == '-' ) break; Op = (((String)S2.pop()).charAt(0)); doPostfix(Op); } S2.push(arg); } else if("/".equals(arg)) { S1.push(Double.valueOf(Expr.toString())); while(!S2.empty()) { Op = (((String)S2.peek()).charAt(0)); if ( Op == '(' || Op == '+' || Op == '-' ) break; Op = (((String)S2.pop()).charAt(0)); doPostfix(Op); } S2.push(arg); } else if("(".equals(arg)) { S2.push(arg); } else if(")".equals(arg)) { S1.push(Double.valueOf(Expr.toString())); while(!S2.empty()) { Op = (((String)S2.pop()).charAt(0)); if ( Op == '(' ) break; doPostfix(Op); } } return true; } public void clearAll() { while (!S1.empty()) S1.pop(); while (!S2.empty()) S2.pop(); } public boolean action(Event evt, Object arg) { if(evt.target instanceof Button) { /* binary operator buttons */ if("=".equals(arg) || "+".equals(arg) || "-".equals(arg) || "*".equals(arg) || "/".equals(arg) || "(".equals(arg) || ")".equals(arg)) { doInfix(arg); newValue=true; } /* uniary operator buttons */ else if("1/X".equals(arg) || "SIN".equals(arg) || "COS".equals(arg) || "X^2".equals(arg) || "EXP".equals(arg)) { doUniary(arg); newValue=true; } /* memory buttons */ else if("MC".equals(arg)) { memValue=0; newValue=true; } else if("MR".equals(arg)) { Expr.setLength(0); Expr.append((new Double(memValue)).toString()); Disp.setText(Expr.toString()); } else if("M+".equals(arg)) { memValue+=Double.valueOf(Expr.toString()).doubleValue(); newValue=true; } else if("M-".equals(arg)) { memValue-=Double.valueOf(Expr.toString()).doubleValue(); newValue=true; } /* control buttons */ else if("CA".equals(arg)) { Expr.setLength(0); Disp.setText(Expr.toString()); newValue=true; clearAll(); } else if("C".equals(arg)) { Expr.setLength(0); Disp.setText(Expr.toString()); newValue=true; } else if("Del".equals(arg)) { if(Expr.length() > 0) { Expr.setLength(Expr.length()-1); } Disp.setText(Expr.toString()); } /* change sign button */ else if("+/-".equals(arg)) { if(Expr.length() == 0 && newValue) { newValue=false; } if(Expr.length() == 0 || ( Expr.charAt(0) != '-' && Expr.charAt(0) != ' ' ) ) { Expr.insert(0,'-'); } else if( Expr.charAt(0) != '-' && Expr.charAt(0) == ' ' ) { Expr.setCharAt(0,'-'); } else { Expr.setCharAt(0,' '); } Disp.setText(Expr.toString()); } /* number buttons */ else { if(newValue) { newValue=false; Expr.setLength(0); } Expr.append(arg.toString()); Disp.setText(Expr.toString()); } return true; } return false; } public boolean keyDown(java.awt.Event evt, int key) { switch ((char) key) { /* number keys */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '.': if(newValue) { newValue=false; Expr.setLength(0); } Expr.append((char) key); Disp.setText(Expr.toString()); break; /* binary operator keys */ case '+': case '-': case '*': case '/': case '=': case '(': case ')': doInfix(new String((new StringBuffer()).append((char) key))); newValue=true; break; /* delete key and backspace key */ case (char) 010: case (char) 0177: if(Expr.length() > 0) { Expr.setLength(Expr.length()-1); } Disp.setText(Expr.toString()); } return true; } public void paint(Graphics g) { Dimension d = size(); g.drawRect(0, 0, d.width - 1, d.height - 1); /* Draw a box around */ } public Insets insets() { return new Insets(10, 10, 10, 10); /* Leave margin */ } public Dimension PreferredSize() { return new Dimension(400, 400); } public static void main(String args[]) { Frame f = new Frame("Calculator"); Calculator ex1 = new Calculator(); ex1.init(); f.add("Center", ex1); f.pack(); f.resize(f.preferredSize()); f.show(); } }