//Written by Jen Chen //Updated on 07/17/03 //Copyrighted by Jen Chen import javax.swing.*; import javax.swing.JTextField; import java.awt.*; import java.awt.Container; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.*; import java.awt.Font; public class Calculator implements ActionListener{ // static JTextArea jtext = new JTextArea(1, 20); static JTextField jtext = new JTextField(); static JRadioButton degree = new JRadioButton("degree"); static JRadioButton radian = new JRadioButton("radian"); static JButton ext = new JButton("Exit"); static JButton equal = new JButton("="); static JButton mult = new JButton("*"); static JButton div = new JButton("/"); static JButton add = new JButton("+"); static JButton subt = new JButton("-"); static JButton decimal = new JButton("."); static JButton add_sub = new JButton("+/-"); static JButton modulo = new JButton("%"); static JButton sqroot = new JButton("sqrt"); static JButton reciprocal = new JButton("1/x"); static JButton backspace = new JButton("Delete"); static JButton valuePi = new JButton("PI"); static JButton clear = new JButton("Clear"); static JButton sine = new JButton("sin"); static JButton cosine = new JButton("cos"); static JButton tangent = new JButton("tan"); static JButton cotangent = new JButton("cot"); static JButton secant = new JButton("sec"); static JButton cosecant = new JButton("csc"); static JButton square = new JButton("x^2"); static JButton cube = new JButton("x^3"); static JButton powerOf = new JButton("x^y"); static JButton logBaseTen = new JButton("log"); static JButton logNatural = new JButton("ln"); static JButton factorial = new JButton("n!"); static JButton zero = new JButton("0"); static JButton one = new JButton("1"); static JButton two = new JButton("2"); static JButton three = new JButton("3"); static JButton four = new JButton("4"); static JButton five = new JButton("5"); static JButton six = new JButton("6"); static JButton seven = new JButton("7"); static JButton eight = new JButton("8"); static JButton nine = new JButton("9"); static JButton naturalE = new JButton("E"); static JFrame fr = new JFrame("JC calculator"); String st = "", tmp = ""; static myStack stackNum = new myStack(); static myStack stackOperator = new myStack(); double trigvalue = 0.0; public Calculator(){ Container cont = fr.getContentPane(); cont.setLayout(new BorderLayout(3, 2)); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(jtext, BorderLayout.NORTH); cont.add(panel, BorderLayout.NORTH); JPanel panel_4 = new JPanel(); panel_4.setLayout(new BorderLayout()); panel_4.add(panel, BorderLayout.NORTH); panel_4.add(degree, BorderLayout.EAST); panel_4.add(radian, BorderLayout.WEST); cont.add(panel_4, BorderLayout.NORTH); degree.addActionListener(this); radian.addActionListener(this); JPanel panel_2 = new JPanel(); panel_2.setMaximumSize(new Dimension(2,3)); panel_2.setLayout(new GridLayout(1, 3)); //one row, three columns. panel_2.add(naturalE, BorderLayout.CENTER); panel_2.add(valuePi, BorderLayout.CENTER); panel_2.add(backspace, BorderLayout.CENTER); panel_2.add(clear, BorderLayout.CENTER); cont.add(panel_2, BorderLayout.CENTER); valuePi.addActionListener(this); backspace.addActionListener(this); clear.addActionListener(this); naturalE.addActionListener(this); JPanel panel_3 = new JPanel(); // panel_3.setLayout(new GridLayout(4, 8)); //4 rows, 8 columns. panel_3.setLayout(new GridLayout(8, 4)); //8 rows, 4 columns. panel_3.add(seven); panel_3.add(eight); panel_3.add(nine); panel_3.add(div); panel_3.add(four); panel_3.add(five); panel_3.add(six); panel_3.add(mult); panel_3.add(one); panel_3.add(two); panel_3.add(three); panel_3.add(subt); panel_3.add(zero); panel_3.add(add_sub); panel_3.add(decimal); panel_3.add(add); panel_3.add(equal); panel_3.add(sine); panel_3.add(secant); panel_3.add(powerOf); panel_3.add(modulo); panel_3.add(cosine); panel_3.add(cosecant); panel_3.add(factorial); panel_3.add(reciprocal); panel_3.add(tangent); panel_3.add(square); panel_3.add(logNatural); panel_3.add(sqroot); panel_3.add(cotangent); panel_3.add(cube); panel_3.add(logBaseTen); cont.add(panel_3, BorderLayout.SOUTH); zero.addActionListener(this); one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); five.addActionListener(this); six.addActionListener(this); seven.addActionListener(this); eight.addActionListener(this); nine.addActionListener(this); div.addActionListener(this); sqroot.addActionListener(this); mult.addActionListener(this); modulo.addActionListener(this); subt.addActionListener(this); reciprocal.addActionListener(this); add_sub.addActionListener(this); decimal.addActionListener(this); add.addActionListener(this); equal.addActionListener(this); sine.addActionListener(this); cosine.addActionListener(this); tangent.addActionListener(this); cotangent.addActionListener(this); secant.addActionListener(this); cosecant.addActionListener(this); square.addActionListener(this); cube.addActionListener(this); factorial.addActionListener(this); powerOf.addActionListener(this); logNatural.addActionListener(this); logBaseTen.addActionListener(this); }//end Calculator constructor() public void actionPerformed(ActionEvent e){ // String st = "", tmp = ""; boolean decimalpress = false; //make sure that the decimal is presses ONCE ONLY for any number! jtext.setHorizontalAlignment(JTextField.RIGHT); if(e.getSource()== zero){ st = st + "0"; jtext.setText(st); } else if(e.getSource() == one){ st = st + "1"; jtext.setText(st); } else if(e.getSource() == two){ st = st + "2"; jtext.setText(st); } else if(e.getSource() == three){ st = st + "3"; jtext.setText(st); } else if(e.getSource() == four){ st = st + "4"; jtext.setText(st); } else if(e.getSource() == five){ st = st + "5"; jtext.setText(st); } else if(e.getSource() == six){ st = st + "6"; jtext.setText(st); } else if(e.getSource() == seven){ st = st + "7"; jtext.setText(st); } else if(e.getSource() == eight){ st = st + "8"; jtext.setText(st); } else if(e.getSource() == nine){ st = st + "9"; jtext.setText(st); } else if(e.getSource() == decimal){ st = st + "."; jtext.setText(st); } if(e.getSource() == sqroot){ jtext.setText(String.valueOf(Math.sqrt(Double.parseDouble(jtext.getText())))); } if(e.getSource() == square){ double square = Double.parseDouble(jtext.getText()); square *= square; jtext.setText(String.valueOf(square)); } if(e.getSource() == cube){ double cube = Double.parseDouble(jtext.getText()); cube = cube*cube*cube; jtext.setText(String.valueOf(cube)); } if(e.getSource() == powerOf){ stackOperator.push("^"); stackNum.push(jtext.getText()); jtext.setText(jtext.getText()); st = ""; } if(e.getSource() == factorial){ double tmp = 1.0, fac; String st = jtext.getText(); int indexVal = st.indexOf('.'); int beforeIndex = st.length() - indexVal; boolean allZero = true; try{ if(indexVal != -1){ for(int j = (indexVal + 1); j < st.length(); j++) if(st.charAt(j) != '0' ){ jtext.setText("Invalid input"); allZero = false; } if(allZero == true) st = st.substring(0, indexVal) ; } if(allZero == true || indexVal == -1){ fac = Double.parseDouble(st.trim()); if(fac < 0) jtext.setText("N/A"); else{ for(int i = 1; i <= fac; i++) tmp = tmp * i; jtext.setText(String.valueOf(tmp)); }//end of inner IF() }//end of outer if() } catch(Exception er){System.out.println(er);} } if(e.getSource() == logNatural){ double tmp = Double.parseDouble(jtext.getText()); tmp = Math.log(tmp); jtext.setText(String.valueOf(tmp)); st = ""; } if(e.getSource() == add){ stackOperator.push("+"); stackNum.push(jtext.getText()); st = ""; } if(e.getSource() == subt){ stackOperator.push("-"); stackNum.push(jtext.getText()); st = ""; } if(e.getSource() == mult){ stackOperator.push("*"); stackNum.push(jtext.getText()); st = ""; } if(e.getSource() == div){ stackOperator.push("/"); stackNum.push(jtext.getText()); st = ""; } if(e.getSource() == modulo){ stackOperator.push("%"); stackNum.push(jtext.getText()); st = ""; } if(e.getSource() == reciprocal){ jtext.setText(String.valueOf(1/Double.parseDouble(jtext.getText()))); } if(e.getSource() == add_sub){ jtext.setText(String.valueOf(-1*Double.parseDouble(jtext.getText()))); st = ""; } if(e.getSource() == backspace){ try{ st = jtext.getText(); st = st.substring(0, st.length() - 1); stackNum.push(jtext.getText()); jtext.setText(st); } catch(Exception er){} } if(e.getSource() == naturalE){ stackNum.push(String.valueOf(Math.E)); jtext.setText(String.valueOf(Math.E)); st = ""; } if(e.getSource() == valuePi){ stackNum.push(String.valueOf(Math.PI)); jtext.setText(String.valueOf(Math.PI)); st = ""; } if(degree.isSelected()){ try{ trigvalue = Double.parseDouble(jtext.getText()); trigvalue = Math.toRadians(trigvalue); } catch(Exception er){} radian.setSelected(false); radian.setContentAreaFilled(false); } if(radian.isSelected()){ try{ trigvalue = Double.parseDouble(jtext.getText()); } catch(Exception er){} degree.setSelected(false); degree.setContentAreaFilled(false); st = ""; }//end of IF() if(e.getSource() == sine){ jtext.setText(String.valueOf(Math.sin(trigvalue))); st = ""; } if(e.getSource() == cosine){ jtext.setText(String.valueOf(Math.cos(trigvalue))); st = ""; } if(e.getSource() == tangent){ jtext.setText(String.valueOf(Math.tan(trigvalue))); st = ""; } if(e.getSource() == cotangent){ jtext.setText(String.valueOf(Math.cos(trigvalue)/Math.sin(trigvalue))); st = ""; } if(e.getSource() == secant){ jtext.setText(String.valueOf(1/Math.cos(trigvalue))); st = ""; } if(e.getSource() == cosecant){ jtext.setText(String.valueOf(1/Math.sin(trigvalue))); st = ""; } if (e.getSource() == equal){ double num, num2, sum, product, difference, quotient, remainder, inverse; char sign; sign = stackOperator.pop().toString().charAt(0); stackNum.push(jtext.getText()); switch (sign){ case '+': // while(! stackNum.isEmpty()) // st = st.concat(stackNum.pop().toString() + ", "); num2 = Double.parseDouble(stackNum.pop().toString()); num = Double.parseDouble(stackNum.pop().toString()); sum = num + num2; jtext.setText(String.valueOf(sum)); break; case '-': num2 = Double.parseDouble(stackNum.pop().toString()); num = Double.parseDouble(stackNum.pop().toString()); difference = num - num2; jtext.setText(String.valueOf(difference)); break; case '*': num2 = Double.parseDouble(stackNum.pop().toString()); num = Double.parseDouble(stackNum.pop().toString()); product = num * num2; jtext.setText(String.valueOf(product)); break; case '/': num2 = Double.parseDouble(stackNum.pop().toString()); num = Double.parseDouble(stackNum.pop().toString()); quotient = num / num2; jtext.setText(String.valueOf(quotient)); break; case '%': num2 = Double.parseDouble(stackNum.pop().toString()); num = Double.parseDouble(stackNum.pop().toString()); remainder = num % num2; jtext.setText(String.valueOf(remainder)); break; case '^': num2 = Double.parseDouble(stackNum.pop().toString()); num = Double.parseDouble(stackNum.pop().toString()); jtext.setText(String.valueOf(Math.pow(num, num2))); break; }//end of SWITCH() // jtext.setText(st); } if (e.getSource() == clear){ st = ""; jtext.setText(""); } // jtext.setText(String.valueOf(result)); }//end of actionPerformed() public static void main(String[] args) { Calculator calc = new Calculator(); fr.setSize(330, 320); fr.setLocation(100, 100); degree.setSelected(true); fr.setVisible(true); fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }//end of main() }//end of class Calculator{}