Skip to content

Instantly share code, notes, and snippets.

@ngnguyen1
Created March 24, 2012 05:47
Show Gist options
  • Save ngnguyen1/2178752 to your computer and use it in GitHub Desktop.
Save ngnguyen1/2178752 to your computer and use it in GitHub Desktop.
Calculator
// Author Nduy.nga
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class bai14 extends Applet implements ActionListener
{
Button Nut7,Nut8,Nut9,NutChia,Nut4,Nut5,Nut6,NutNhan,
Nut1,Nut2,Nut3,NutTru,Nut0,NutThapPhan,NutBang,NutCong,ext;
TextField txtName;
GridBagLayout gb;
GridBagConstraints gbc;
int num1;
String s="";
public void init()
{
gb = new GridBagLayout();
setLayout(gb);
gbc = new GridBagConstraints();
txtName = new TextField(20);
Nut7 = new Button(" "+" "+" "+" "+"7"+" "+" "+" "+" ");
Nut7.addActionListener(this);
Nut8 = new Button(" "+" "+" "+" "+"8"+" "+" "+" "+" ");
Nut8.addActionListener(this);
Nut9 = new Button(" "+" "+" "+" "+"9"+" "+" "+" "+" ");
Nut9.addActionListener(this);
NutChia = new Button(" "+" "+"/"+" "+" ");
NutChia.addActionListener(this);
Nut4 = new Button(" "+" "+" "+" "+"4"+" "+" "+" "+" ");
Nut4.addActionListener(this);
Nut5 = new Button(" "+" "+" "+" "+"5"+" "+" "+" "+" ");
Nut5.addActionListener(this);
Nut6 = new Button(" "+" "+" "+" "+"6"+" "+" "+" "+" ");
Nut6.addActionListener(this);
NutNhan = new Button(" "+" "+"*"+" "+" ");
NutNhan.addActionListener(this);
Nut1 = new Button(" "+" "+" "+" "+"1"+" "+" "+" "+" ");
Nut1.addActionListener(this);
Nut2 = new Button(" "+" "+" "+" "+"2"+" "+" "+" "+" ");
Nut2.addActionListener(this);
Nut3 = new Button(" "+" "+" "+" "+"3"+" "+" "+" "+" ");
Nut3.addActionListener(this);
NutTru = new Button(" "+" "+"-"+" "+" ");
NutTru.addActionListener(this);
Nut0 = new Button(" "+" "+" "+" "+"0"+" "+" "+" "+" ");
Nut0.addActionListener(this);
NutThapPhan = new Button(" "+" "+" "+" "+"."+" "+" "+" "+" ");
NutThapPhan.addActionListener(this);
NutBang = new Button(" "+" "+" "+" "+"="+" "+" "+" "+" ");
NutBang.addActionListener(this);
NutCong = new Button(" "+" "+"+"+" "+" ");
NutCong.addActionListener(this);
ext = new Button(" "+" "+"Exit"+" "+" ");
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(txtName,0,0,1,7);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut7,1,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut8,1,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut9,1,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(NutChia,1,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut4,2,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut5,2,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut6,2,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(NutNhan,2,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut1,3,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut2,3,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut3,3,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(NutTru,3,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(Nut0,4,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(NutThapPhan,4,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(NutBang,4,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(NutCong,4,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(ext,5,2,1,1);
}
public void addComponent(Component comp,int row,int col, int nrow, int ncol)
{
gbc.gridx = col;
gbc.gridy = row;
gbc.gridwidth = ncol;
gbc.gridheight = nrow;
gb.setConstraints(comp,gbc);
add(comp);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == Nut0)
{
num1 = 0;
s+=num1;
txtName.setText(s);
//System.out.println(s);
}
if (ae.getSource() == Nut1)
{
num1 = 1;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut2)
{
num1 = 2;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut3)
{
num1 = 3;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut4)
{
num1 = 4;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut5)
{
num1 = 5;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut6)
{
num1 = 6;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut7)
{
num1 = 7;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut8)
{
num1 = 8;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == Nut9)
{
num1 = 9;
s+=num1;
txtName.setText(s);
}
if (ae.getSource() == ext)
{
System.exit(0);
}
if (ae.getSource() == NutThapPhan )
{
s+=".";
txtName.setText(s);
}
if (ae.getSource() == NutCong)
{
s+="+";
txtName.setText(s);
}
if (ae.getSource() == NutTru)
{
s+="-";
txtName.setText(s);
}
if (ae.getSource() == NutNhan)
{
s+="*";
txtName.setText(s);
}
if (ae.getSource() == NutChia)
{
s+="/";
txtName.setText(s);
}
if (ae.getSource() == NutBang)
{
float so1=0,so2=0,kq=0;
String op="",s1="",s2="";
StringTokenizer st=new StringTokenizer(s,"+-*/",true);
if(st.hasMoreTokens())
{
so1=Float.parseFloat(st.nextToken());
//if(st.hasMoreTokens())
op=st.nextElement().toString();
//if(st.hasMoreElements())
so2=Float.parseFloat(st.nextToken());
}
else
{
so1=Float.parseFloat(s);
}
if(op.equals("+"))
{
kq=so1+so2;
txtName.setText(""+kq);
}
else if (op.equals("*"))
{
kq=so1*so2;
txtName.setText("" +kq);
}
else if (op.equals("-"))
{
kq=so1-so2;
txtName.setText("" +kq);
}
else if (op.equals("/"))
{
kq=so1/so2;
txtName.setText("" +kq);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment