import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
JTextArea txt1,txt2;
Font font;
JButton[] button;
String buttonString[]={ "7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","C","+","sqrt" };
String str;
int top_opr=-1;
int top_ope=-1;
double operand[];
String operator[];
void push_ope(double tmp)
{
operand[++top_ope]=tmp;
}
void push_opr(String str)
{
operator[++top_opr]=str;
}
double pop_ope()
{
try
{
return operand[top_ope--];
}
catch(ArrayIndexOutOfBoundsException ae)
{
return 0;
}
}
String pop_opr()
{
try
{
return operator[top_opr--];
}
catch(ArrayIndexOutOfBoundsException ae)
{
return "";
}
}
public Calculator()
{
this.setLayout(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
operand=new double[25];
operator=new String[25];
font = new Font("Times new Roman", Font.BOLD, 20);
txt1=new JTextArea(1,500);
txt1.setFont(font);
// txt1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
add(txt1);
txt1.setEditable(false);
txt1.setBounds(0, 0, 490, 25);
txt2=new JTextArea(1,500);
txt2.setFont(font);
// txt2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
add(txt2);
txt2.setEditable(false);
txt2.setBounds(0,26,490,24);
font = new Font("Times new Roman", Font.BOLD, 24);
button=new JButton[20];
for(int i = 0; i<20; i++)
{
button[i] = new JButton();
button[i].setText(buttonString[i]);
button[i].setFont(font);
button[i].addActionListener(this);
}
int x=0;
for(int i=0;i<=19;i++)
{
if(i<=4)
button[i].setBounds(x, 50, 100, 50);
else
if(i>4&&i<=9)
button[i].setBounds(x,100,100,50);
else
if(i>9&&i<=14)
button[i].setBounds(x, 150,100,50);
else
button[i].setBounds(x,200,100,50);
x+=100;
add(button[i]);
if(i==4 || i==9 || i==14)
x=0;
}
}
void evaluate(String str)
{
double d1,d2,tmp=0;
d1=pop_ope();
d2=pop_ope();
if(str.equals("/"))
tmp=d2/d1;
if(str.equals("%"))
tmp=d2%d1;
if(str.equals("*"))
tmp=d2*d1;
if(str.equals("+"))
tmp=d2+d1;
if(str.equals("-"))
tmp=d2-d1;
push_ope(tmp);
}
public void actionPerformed(ActionEvent ae)
{
//Handle Numbers
if(ae.getSource()==button[0])
{
txt2.append(buttonString[0]);
txt1.append(buttonString[0]);
}
if(ae.getSource()==button[1])
{
txt2.append(buttonString[1]);
txt1.append(buttonString[1]);
}
if(ae.getSource()==button[2])
{
txt2.append(buttonString[2]);
txt1.append(buttonString[2]);
}
if(ae.getSource()==button[3])
{
push_ope(Double.parseDouble(txt2.getText()) );
txt2.setText("");
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[3]);
txt1.append(buttonString[3]);
}
if(ae.getSource()==button[4])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[4]);
txt1.append(buttonString[4]);
}
if(ae.getSource()==button[5])
{
txt2.append(buttonString[5]);
txt1.append(buttonString[5]);
}
if(ae.getSource()==button[6])
{
txt2.append(buttonString[6]);
txt1.append(buttonString[6]);
}
if(ae.getSource()==button[7])
{
txt2.append(buttonString[7]);
txt1.append(buttonString[7]);
}
if(ae.getSource()==button[8])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[8]);
txt1.append(buttonString[8]);
}
if(ae.getSource()==button[10])
{
txt2.append(buttonString[10]);
txt1.append(buttonString[10]);
}
if(ae.getSource()==button[11])
{
txt2.append(buttonString[11]);
txt1.append(buttonString[11]);
}
if(ae.getSource()==button[12])
{
txt2.append(buttonString[12]);
txt1.append(buttonString[12]);
}
if(ae.getSource()==button[13])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
txt1.append(buttonString[13]);
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[13]);
}
if(ae.getSource()==button[15])
{
txt2.append(buttonString[15]);
txt1.append(buttonString[15]);
}
if(ae.getSource()==button[16])
{
txt2.append(buttonString[16]);
txt1.append(buttonString[16]);
}
if(ae.getSource()==button[18])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
txt1.append(buttonString[18]);
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[18]);
}
if(ae.getSource()==button[9])
{
str=txt2.getText();
double i=(1.0)/Integer.parseInt(str);
txt1.append(Double.toString(i));
}
if(ae.getSource()==button[17])
{
txt1.setText("");
txt2.setText("");
top_opr=-1;
top_ope=-1;
}
if(ae.getSource()==button[14])
{
push_ope( Double.parseDouble(txt2.getText()) );
while((top_opr)>=0)
evaluate(pop_opr());
txt1.setText("");
txt2.setText("");
txt1.setText(Double.toString(operand[0]));
}
}
public static void main(String args[])
{
Calculator c=new Calculator();
Toolkit tool=Toolkit.getDefaultToolkit();
Dimension size=tool.getScreenSize();
final int HEIGHT=279;
final int WIDTH=500;
c.setBounds(size.width/2-WIDTH/2,size.height/2-HEIGHT/2,WIDTH,HEIGHT);
c.setTitle("Calculator");
c.setResizable(false);
c.setVisible(true);
}
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
JTextArea txt1,txt2;
Font font;
JButton[] button;
String buttonString[]={ "7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","C","+","sqrt" };
String str;
int top_opr=-1;
int top_ope=-1;
double operand[];
String operator[];
void push_ope(double tmp)
{
operand[++top_ope]=tmp;
}
void push_opr(String str)
{
operator[++top_opr]=str;
}
double pop_ope()
{
try
{
return operand[top_ope--];
}
catch(ArrayIndexOutOfBoundsException ae)
{
return 0;
}
}
String pop_opr()
{
try
{
return operator[top_opr--];
}
catch(ArrayIndexOutOfBoundsException ae)
{
return "";
}
}
public Calculator()
{
this.setLayout(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
operand=new double[25];
operator=new String[25];
font = new Font("Times new Roman", Font.BOLD, 20);
txt1=new JTextArea(1,500);
txt1.setFont(font);
// txt1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
add(txt1);
txt1.setEditable(false);
txt1.setBounds(0, 0, 490, 25);
txt2=new JTextArea(1,500);
txt2.setFont(font);
// txt2.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
add(txt2);
txt2.setEditable(false);
txt2.setBounds(0,26,490,24);
font = new Font("Times new Roman", Font.BOLD, 24);
button=new JButton[20];
for(int i = 0; i<20; i++)
{
button[i] = new JButton();
button[i].setText(buttonString[i]);
button[i].setFont(font);
button[i].addActionListener(this);
}
int x=0;
for(int i=0;i<=19;i++)
{
if(i<=4)
button[i].setBounds(x, 50, 100, 50);
else
if(i>4&&i<=9)
button[i].setBounds(x,100,100,50);
else
if(i>9&&i<=14)
button[i].setBounds(x, 150,100,50);
else
button[i].setBounds(x,200,100,50);
x+=100;
add(button[i]);
if(i==4 || i==9 || i==14)
x=0;
}
}
void evaluate(String str)
{
double d1,d2,tmp=0;
d1=pop_ope();
d2=pop_ope();
if(str.equals("/"))
tmp=d2/d1;
if(str.equals("%"))
tmp=d2%d1;
if(str.equals("*"))
tmp=d2*d1;
if(str.equals("+"))
tmp=d2+d1;
if(str.equals("-"))
tmp=d2-d1;
push_ope(tmp);
}
public void actionPerformed(ActionEvent ae)
{
//Handle Numbers
if(ae.getSource()==button[0])
{
txt2.append(buttonString[0]);
txt1.append(buttonString[0]);
}
if(ae.getSource()==button[1])
{
txt2.append(buttonString[1]);
txt1.append(buttonString[1]);
}
if(ae.getSource()==button[2])
{
txt2.append(buttonString[2]);
txt1.append(buttonString[2]);
}
if(ae.getSource()==button[3])
{
push_ope(Double.parseDouble(txt2.getText()) );
txt2.setText("");
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[3]);
txt1.append(buttonString[3]);
}
if(ae.getSource()==button[4])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[4]);
txt1.append(buttonString[4]);
}
if(ae.getSource()==button[5])
{
txt2.append(buttonString[5]);
txt1.append(buttonString[5]);
}
if(ae.getSource()==button[6])
{
txt2.append(buttonString[6]);
txt1.append(buttonString[6]);
}
if(ae.getSource()==button[7])
{
txt2.append(buttonString[7]);
txt1.append(buttonString[7]);
}
if(ae.getSource()==button[8])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[8]);
txt1.append(buttonString[8]);
}
if(ae.getSource()==button[10])
{
txt2.append(buttonString[10]);
txt1.append(buttonString[10]);
}
if(ae.getSource()==button[11])
{
txt2.append(buttonString[11]);
txt1.append(buttonString[11]);
}
if(ae.getSource()==button[12])
{
txt2.append(buttonString[12]);
txt1.append(buttonString[12]);
}
if(ae.getSource()==button[13])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
txt1.append(buttonString[13]);
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[13]);
}
if(ae.getSource()==button[15])
{
txt2.append(buttonString[15]);
txt1.append(buttonString[15]);
}
if(ae.getSource()==button[16])
{
txt2.append(buttonString[16]);
txt1.append(buttonString[16]);
}
if(ae.getSource()==button[18])
{
push_ope( Double.parseDouble(txt2.getText()) );
txt2.setText("");
txt1.append(buttonString[18]);
if(top_ope>=1 && ( operator[top_opr].equals("*") || operator[top_opr].equals("/") || operator[top_opr].equals("%") ) )
{
evaluate(pop_opr());
}
push_opr(buttonString[18]);
}
if(ae.getSource()==button[9])
{
str=txt2.getText();
double i=(1.0)/Integer.parseInt(str);
txt1.append(Double.toString(i));
}
if(ae.getSource()==button[17])
{
txt1.setText("");
txt2.setText("");
top_opr=-1;
top_ope=-1;
}
if(ae.getSource()==button[14])
{
push_ope( Double.parseDouble(txt2.getText()) );
while((top_opr)>=0)
evaluate(pop_opr());
txt1.setText("");
txt2.setText("");
txt1.setText(Double.toString(operand[0]));
}
}
public static void main(String args[])
{
Calculator c=new Calculator();
Toolkit tool=Toolkit.getDefaultToolkit();
Dimension size=tool.getScreenSize();
final int HEIGHT=279;
final int WIDTH=500;
c.setBounds(size.width/2-WIDTH/2,size.height/2-HEIGHT/2,WIDTH,HEIGHT);
c.setTitle("Calculator");
c.setResizable(false);
c.setVisible(true);
}
}
No comments:
Post a Comment