import java.awt.*; import java.awt.event.*; public class Calc1 extends Frame implements ActionListener { TextField txtVal; Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bp,bm,bpw,bAdd,bSub,bMul,bDiv,bCls,bEqu; Button bsin,bcos,btan,bsqr,bx,bc; int a=1,b=1,Op=0,n=1; double O=0.0,ans=0.0,x; Calc1() { setCursor(new Cursor(Cursor.HAND_CURSOR)); setFont(new Font("Dialog",0,18)); setTitle("Calculater By MVC"); txtVal=new TextField(18); txtVal.setFont(new Font("Dialog",0,18)); b1=new Button("1"); b2=new Button("2"); b3=new Button("3"); b4=new Button("4"); b5=new Button("5"); b6=new Button("6"); b7=new Button("7"); b8=new Button("8"); b9=new Button("9"); b0=new Button("0"); bp=new Button("."); bm=new Button("+/-"); bpw=new Button("^"); bMul=new Button("*"); bAdd=new Button("+"); bSub=new Button("-"); bDiv=new Button("/"); bCls=new Button("C"); bEqu=new Button("="); bsin=new Button("sin"); bcos=new Button("cos"); btan=new Button("tan"); bsqr=new Button("sqr"); bx=new Button("1/x"); bc=new Button("c"); bEqu.setBackground(new Color(0,0,111)); add(txtVal); txtVal.setBackground(new Color(204,204,204)); txtVal.setEditable(false); txtVal.setText("0"); add(bc); add(bsin); add(b7); add(b8); add(b9); add(bAdd); add(bcos); add(b4); add(b5); add(b6); add(bSub); add(btan); add(b1); add(b2); add(b3); add(bMul); add(bsqr); add(b0); add(bp); add(bm); add(bDiv); add(bx); add(bpw); add(bCls); add(bEqu); bAdd.addActionListener(this); bc.addActionListener(this); bSub.addActionListener(this); bMul.addActionListener(this); bDiv.addActionListener(this); bCls.addActionListener(this); bEqu.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); b0.addActionListener(this); bp.addActionListener(this); bm.addActionListener(this); bpw.addActionListener(this); bsin.addActionListener(this); bcos.addActionListener(this); btan.addActionListener(this); bsqr.addActionListener(this); bx.addActionListener(this); this.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent W) { System.exit(0); } }); } public void actionPerformed(ActionEvent A) { if (A.getSource()==b0) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("0"); else if (a==1) txtVal.setText(txtVal.getText()+"0"); else { txtVal.setText("0"); a=1; } } else if (A.getSource()==b1) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("1"); else if (a==1) txtVal.setText(txtVal.getText()+"1"); else { txtVal.setText("1"); a=1; } } else if (A.getSource()==b2) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("2"); else if (a==1) txtVal.setText(txtVal.getText()+"2"); else { txtVal.setText("2"); a=1; } } else if (A.getSource()==b3) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("3"); else if (a==1) txtVal.setText(txtVal.getText()+"3"); else { txtVal.setText("3"); a=1; } } else if (A.getSource()==b4) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("4"); else if (a==1) txtVal.setText(txtVal.getText()+"4"); else {txtVal.setText("4"); a=1; } } else if (A.getSource()==b5) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("5"); else if (a==1) txtVal.setText(txtVal.getText()+"5"); else {txtVal.setText("5"); a=1; } } else if (A.getSource()==b6) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("6"); else if (a==1) txtVal.setText(txtVal.getText()+"6"); else { txtVal.setText("6"); a=1; } } else if (A.getSource()==b7) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("7"); else if (a==1) txtVal.setText(txtVal.getText()+"7"); else { txtVal.setText("7"); a=1; } } else if (A.getSource()==b8) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("8"); else if (a==1) txtVal.setText(txtVal.getText()+"8"); else { txtVal.setText("8"); a=1; } } else if (A.getSource()==b9) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("9"); else if (a==1) txtVal.setText(txtVal.getText()+"9"); else { txtVal.setText("9"); a=1; } } else if (A.getSource()==bp) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("."); else if (a==1) txtVal.setText(txtVal.getText()+"."); else { txtVal.setText("."); a=1; } } else if (A.getSource()==bm) { if (txtVal.getText().length()==1&&txtVal.getText().equals("0")) txtVal.setText("0"); if ((txtVal.getText()).charAt(0)=='-') { StringBuffer S=new StringBuffer(txtVal.getText()); S.setCharAt(0, ' ' ); txtVal.setText(String.valueOf(S)); } if ((txtVal.getText()).charAt(0)!='-') { txtVal.setText("-"+txtVal.getText()); } else { txtVal.setText("0"); } } else if (A.getSource()==bAdd) { if (a==1&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=1;n=2; } else if (a==1&&n==2) { ans=add(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); O=Double.parseDouble(txtVal.getText()); a=2;Op=1; } else if (a==2&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=1; } } else if (A.getSource()==bSub) { if (a==1&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=2;n=2; } else if (a==1&&n==2) { ans=sub(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); O=Double.parseDouble(txtVal.getText()); a=2;Op=2; } else if (a==2&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=2; } } else if (A.getSource()==bMul) { if (a==1&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=3;n=2; } else if (a==1&&n==2) {ans=mul(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); O=Double.parseDouble(txtVal.getText()); a=2;Op=3; } else if (a==2&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=2; } } else if (A.getSource()==bDiv) { if (a==1&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=4;n=2; }else if (a==1&&n==2) { ans=div(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); O=Double.parseDouble(txtVal.getText()); a=2;Op=4; } else if (a==2&&n==1) { O=Double.parseDouble(txtVal.getText()); a=2;Op=4; } } else if (A.getSource()==bpw) { O=Double.parseDouble(txtVal.getText()); Op=5; a=2; } else if (A.getSource()==bsin) { O=Double.parseDouble(txtVal.getText()); txtVal.setText(Double.toString(Math.sin(Math.PI/(180/O)))); a=2; } else if (A.getSource()==bcos) { O=Double.parseDouble(txtVal.getText()); txtVal.setText(Double.toString(Math.sin(Math.PI/(180/O)))); a=2; } else if (A.getSource()==bsin) { O=Double.parseDouble(txtVal.getText()); txtVal.setText(Double.toString(Math.sin(Math.PI/(180/O)))); a=2; } else if (A.getSource()==bsqr) { O=Double.parseDouble(txtVal.getText()); txtVal.setText(Double.toString(Math.sqrt(0))); a=2; } else if (A.getSource()==bx) { O=Double.parseDouble(txtVal.getText()); txtVal.setText(Double.toString(1/O)); a=2; } else if (A.getSource()==bEqu) { if (Op==1) { ans=add(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); Op=0; a=2;n=1; } if (Op==2) { ans=sub(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); Op=0; a=2;n=1; } if (Op==3) { ans=mul(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); Op=0; a=2;n=1; } if (Op==4) { ans=div(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); Op=0; a=2;n=1; } if (Op==5) { ans=pow(O,Double.parseDouble(txtVal.getText())); txtVal.setText(Double.toString(ans)); Op=0; a=2;n=1; } } if (A.getSource()==bCls) { txtVal.setText("0");a=1;O=0;} } double add(double A,double B) { return A+B; } double sub(double A,double B) { return A-B; } double mul(double A,double B) { return A*B; } double div(double A,double B) { return A/B; } double pow(double A,double B) { x=A; for (int i=1;i