This a GUI java program that can calculate an equation like in scientific calculator
but only limited to only a few operation.
by: Mohamad Fikri Bin Ramlan
Muhammad Amir Zaki Bin Zainal Alam
*/
Class 'calculation'
public class calculation
{
public double calculate(double num1, double num2, String operator)
{
if(operator.equals("+"))
return num1 + num2;
else if(operator.equals("-"))
return num1 - num2;
else if(operator.equals("*"))
return num1 * num2;
else if(operator.equals("/"))
return num1 / num2;
else if(operator.equals("^"))
return Math.pow(num1, num2);
else
return Math.log(num1)/Math.log(num2);
}
public double calculate2(double num, String operator)
{
if(operator.equals("s"))
return Math.sin(Math.toRadians(num));
else if(operator.equals("c"))
return Math.cos(Math.toRadians(num));
else if(operator.equals("t"))
return Math.tan(Math.toRadians(num));
else if(operator.equals("l"))
return Math.log(num)/Math.log(10);
else
{
if(num==0)
return 1;
else
return num*calculate2(num-1, operator);
}
}
}
Class 'calc'
import javax.swing.*;
import java.util.*;
public class calc
{
public String process(String tet)
{
String input = "" + tet + ";";
String num[] = new String[99], precedent[] = {"+*s^!(", "-/c", " t", " l"}, sctl = "sctl";
boolean next=false, isDigit=false, allType[] = new boolean[99], sign=false;
int countNum = 0;
Stack operandStack = new Stack(), operatorStack = new Stack();
ArrayList all = new ArrayList();
num[0] = new String("");
for(int c=0, a=0; c
{
char check = input.charAt(c);
if((check>=48&&check<58)||check=='.')//check for digit and .
{
if(next==false)
{
num[countNum] += new String(check + "");
}
else
{
num[++countNum] = new String(check + "");
}
next=false;
isDigit=true;
}
else//check for other than number
{
if(isDigit==true)//check for negative sign
{
if(sign==true)
all.add("-" + num[countNum]);
else
all.add(num[countNum]);
allType[a] = true;
sign=false;
a++;
}
if(c==0 && check=='-')//set negative sign
sign=true;
else if((c!=0 && check=='-') && (allType[all.size()-1]==false && !all.get(all.size()-1).toString().equals(")")))
sign=true; //set negative sign
else if(check!=';' && check!=',')//check for operator
{
all.add(check);
allType[a] = false;
a++;
}
next=true;
isDigit=false;
}//end separate the operand and operator
}
for(int c=0; c
{
if(allType[c]==false)
{
String operator = all.get(c).toString();
boolean enter=false, bracket=true;
while(enter==false && !operatorStack.isEmpty())
{
if(operator.equals(")")&&bracket==true)//check for the operation in bracket
{
while(!operatorStack.peek().toString().equals("("))
{
String operate = operatorStack.pop().toString();
double num2 = Double.parseDouble(operandStack.pop().toString());
double num1 = Double.parseDouble(operandStack.pop().toString());
calculation raw = new calculation();
operandStack.push(raw.calculate(num1, num2, operate));
}
operatorStack.pop();
bracket=false;
enter=true;
}
else
{
String peeking = operatorStack.peek().toString();
int temp1 = precedent[0].indexOf(operator), //get the precedent from 1st set
temp2 = precedent[0].indexOf(peeking); //get the precedent from the operatorStack
for(int count=1; temp1 == -1&&count<4; count++)//get the precedent if not in 1st set
temp1 = precedent[count].indexOf(operator);
for(int count=1; temp2 == -1&&count<4; count++)//get precedent if no equal value
temp2 = precedent[count].indexOf(peeking);
if(temp1==2 && temp1>temp2)//check for log sin cos and tan precedent level
{
operatorStack.push(operator);//arrange the operator precedent in the stack
enter=true;
}
else if(temp1>temp2 || peeking.equals("("))//check for higher precedent or opening bracket
{
operatorStack.push(operator);//arrange the operator precedent in the stack
enter=true;
}
else//condition if the status is not operator
{
String operate = operatorStack.pop().toString();
calculation raw = new calculation();
if(sctl.indexOf(peeking)!=-1 || operate.equals("!"))//check for equation of one operand
{
double num1 = Double.parseDouble(operandStack.pop().toString());
operandStack.push(raw.calculate2(num1, operate));
}
else//condition for equation of two operand
{
double num2 = Double.parseDouble(operandStack.pop().toString());
double num1 = Double.parseDouble(operandStack.pop().toString());
operandStack.push(raw.calculate(num1, num2, operate));
}
enter=false;
}
}
}
if(operatorStack.isEmpty() && !operator.equals(")"))
operatorStack.push(operator);
}
else
{
operandStack.push(all.get(c));
}
}
while(!operatorStack.isEmpty())
{
String peeking = operatorStack.peek().toString();
String operate = operatorStack.pop().toString();
calculation raw = new calculation();
if(sctl.indexOf(peeking)!=-1||operate.equals("!"))
{
double num1 = Double.parseDouble(operandStack.pop().toString());
operandStack.push(raw.calculate2(num1, operate));
}
else
{
double num2 = Double.parseDouble(operandStack.pop().toString());
double num1 = Double.parseDouble(operandStack.pop().toString());
operandStack.push(raw.calculate(num1, num2, operate));
}
}
return "" + operandStack.peek();
}
}
Class 'calculator'
/*Author:Mohamad Fikri & Muhammad Amir Zaki
* development date: semester 4, year 2010
* adjusted by: Mohamad Fikri
*/
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.text.DecimalFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Calculator extends JFrame implements ActionListener
{
boolean check = false;
char divide= (char) 247;
String totalEquation = "";
double ans = 0;
DateFormat dateFormat = new SimpleDateFormat("yy/MM/dd");
Date date = new Date();
private Stack stackUp = new Stack();
private Stack stackDown = new Stack();
private ImageIcon iNum[], iPoint, iEquals, iClear, iExit, iPlus, iMinus,
iTimes, iDiv, iParL, iParR, iBck, iSin, iCos, iTan,
iLog, iFac, iFlu, iComa, iPow, iAns;
private JTextField fEquation, fAnswer;
private JPanel pNumbers, pOperator, pMain, pOutput;
private JButton bEquals, bClear, bExit, bPlus, bMinus, bTimes, bDiv, bPoint,
bNum[], bParL, bParR, bBck, bSin, bCos, bTan, bLog, bFac,
bFlu, bComa, bPow, bAns;
private Color color;
public Calculator()
{
super("Calculator by ThumBuck & Ikazrima");
iNum = new ImageIcon[10];
for(int i = 0; i < 10; i++)
{
iNum[i] = new ImageIcon("resources/n" + i + ".jpg");
}
iPoint = new ImageIcon("resources/nP.jpg");
iEquals = new ImageIcon("resources/n=.jpg");
iClear = new ImageIcon("resources/nC.jpg");
iExit = new ImageIcon("resources/nEx.jpg");
iPlus = new ImageIcon("resources/n+.jpg");
iMinus = new ImageIcon("resources/n-.jpg");
iTimes = new ImageIcon("resources/nX.jpg");
iDiv = new ImageIcon("resources/nDiv.jpg");
iParL = new ImageIcon("resources/n(.jpg");
iParR = new ImageIcon("resources/n).jpg");
iBck = new ImageIcon("resources/nBck.jpg");
iSin = new ImageIcon("resources/nSin.jpg");
iCos = new ImageIcon("resources/nCos.jpg");
iTan = new ImageIcon("resources/nTan.jpg");
iLog = new ImageIcon("resources/nLog.jpg");
iFac = new ImageIcon("resources/n!.jpg");
iFlu = new ImageIcon("resources/nFlu.jpg");
iComa = new ImageIcon("resources/nK.jpg");
iPow = new ImageIcon("resources/nPow.jpg");
iAns = new ImageIcon("resources/nAns.jpg");
fEquation = new JTextField();
fAnswer = new JTextField();
pNumbers = new JPanel(new GridLayout(4,3));
pOperator = new JPanel(new GridLayout(3,6));
pOutput = new JPanel(new GridLayout(2,1));
bNum = new JButton[10];
for(int i = 0; i < 10; i++)
{
bNum[i] = new JButton(iNum[i]);
bNum[i].setBackground(Color.BLACK);
}
bEquals = new JButton(iEquals);
bClear = new JButton(iClear);
bExit = new JButton(iExit);
bPlus = new JButton(iPlus);
bMinus = new JButton(iMinus);
bTimes = new JButton(iTimes);
bDiv = new JButton(iDiv);
bPoint = new JButton(iPoint);
bParL = new JButton(iParL);
bParR = new JButton(iParR);
bBck = new JButton(iBck);
bSin = new JButton(iSin);
bCos = new JButton(iCos);
bTan = new JButton(iTan);
bLog = new JButton(iLog);
bFac = new JButton(iFac);
bFlu = new JButton(iFlu);
bComa = new JButton(iComa);
bPow = new JButton(iPow);
bAns = new JButton(iAns);
bEquals.setBackground(Color.BLACK);
bClear.setBackground(Color.BLACK);
bExit.setBackground(Color.BLACK);
bPlus.setBackground(Color.BLACK);
bMinus.setBackground(Color.BLACK);
bTimes.setBackground(Color.BLACK);
bDiv.setBackground(Color.BLACK);
bPoint.setBackground(Color.BLACK);
bParL.setBackground(Color.BLACK);
bParR.setBackground(Color.BLACK);
bBck.setBackground(Color.BLACK);
bFlu.setBackground(Color.BLACK);
bSin.setBackground(Color.BLACK);
bCos.setBackground(Color.BLACK);
bTan.setBackground(Color.BLACK);
bLog.setBackground(Color.BLACK);
bFac.setBackground(Color.BLACK);
bComa.setBackground(Color.BLACK);
bPow.setBackground(Color.BLACK);
bAns.setBackground(Color.BLACK);
pOperator.setBackground(Color.BLACK);
pNumbers.setBackground(Color.BLACK);
fEquation.setBorder(new TitledBorder("Equation"));
fEquation.setBackground(Color.BLACK);
fEquation.setForeground(Color.WHITE);
fEquation.setEditable(false);
fEquation.setFont(new Font("LucidaSans", Font.PLAIN, 15));
fAnswer.setBorder(new TitledBorder("Answer"));
fAnswer.setBackground(Color.BLACK);
fAnswer.setForeground(Color.WHITE);
fAnswer.setEditable(false);
fAnswer.setFont(new Font("LucidaSans", Font.PLAIN, 15));
pOutput.add(fEquation, BorderLayout.NORTH);
pOutput.add(fAnswer, BorderLayout.CENTER);
pNumbers.add(bNum[7]);
pNumbers.add(bNum[8]);
pNumbers.add(bNum[9]);
pNumbers.add(bNum[4]);
pNumbers.add(bNum[5]);
pNumbers.add(bNum[6]);
pNumbers.add(bNum[1]);
pNumbers.add(bNum[2]);
pNumbers.add(bNum[3]);
pNumbers.add(bNum[0]);
pNumbers.add(bPoint);
pNumbers.add(bComa);
pOperator.add(bSin);
pOperator.add(bCos);
pOperator.add(bTan);
pOperator.add(bLog);
pOperator.add(bFac);
pOperator.add(bParL);
pOperator.add(bPlus);
pOperator.add(bMinus);
pOperator.add(bTimes);
pOperator.add(bDiv);
pOperator.add(bPow);
pOperator.add(bParR);
pOperator.add(bExit);
pOperator.add(bFlu);
pOperator.add(bBck);
pOperator.add(bClear);
pOperator.add(bAns);
pOperator.add(bEquals);
this.add(BorderLayout.NORTH, pOutput);
this.add(BorderLayout.CENTER, pOperator);
this.add(BorderLayout.SOUTH, pNumbers);
this.setSize(290, 460);
//this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setLocation(10, 10);
this.setVisible(true);
bEquals.addActionListener(this);
bClear.addActionListener(this);
bExit.addActionListener(this);
bPlus.addActionListener(this);
bMinus.addActionListener(this);
bTimes.addActionListener(this);
bDiv.addActionListener(this);
bPoint.addActionListener(this);
bComa.addActionListener(this);
bParL.addActionListener(this);
bParR.addActionListener(this);
bBck.addActionListener(this);
bFlu.addActionListener(this);
bPow.addActionListener(this);
fEquation.addActionListener(this);
bSin.addActionListener(this);
bCos.addActionListener(this);
bTan.addActionListener(this);
bLog.addActionListener(this);
bFac.addActionListener(this);
bAns.addActionListener(this);
for(int i = 0; i < 10; i++)
{
bNum[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent e)
{
DecimalFormat df = new DecimalFormat("0.00");
if (e.getSource() == bExit)
{
System.exit(0);
}
if (e.getSource() == bPlus)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
if(fEquation.getText().equals(""))
{
totalEquation = ans + "+";
fEquation.setText(ans + "+");
}
else
{
totalEquation = totalEquation + "+";
fEquation.setText(fEquation.getText() + "+");
}
}
if (e.getSource() == bMinus)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
if(fEquation.getText().equals(""))
{
totalEquation = ans + "-";
fEquation.setText(ans + "-");
}
else
{
totalEquation = totalEquation + "-";
fEquation.setText(fEquation.getText() + "-");
}
}
if (e.getSource() == bTimes)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
if(fEquation.getText().equals(""))
{
totalEquation = ans + "*";
fEquation.setText(ans + "x");
}
else
{
totalEquation = totalEquation + "*";
fEquation.setText(fEquation.getText() + "x");
}
}
if (e.getSource() == bDiv)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
if(fEquation.getText().equals(""))
{
totalEquation = ans + "/";
fEquation.setText("" + ans + divide);
}
else
{
totalEquation = totalEquation + "/";
fEquation.setText(fEquation.getText() + divide);
}
}
if (e.getSource() == bSin)
{
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "s";
fEquation.setText(fEquation.getText() + "sin ");
}
if (e.getSource() == bCos)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "c";
fEquation.setText(fEquation.getText() + "cos ");
}
if (e.getSource() == bTan)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "t";
fEquation.setText(fEquation.getText() + "tan ");
}
if (e.getSource() == bLog)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "l";
fEquation.setText(fEquation.getText() + "log ");
}
if (e.getSource() == bFac)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
if(fEquation.getText().equals(""))
{
totalEquation = ans + "!";
fEquation.setText(ans + "! ");
}
else
{
totalEquation = totalEquation + "!";
fEquation.setText(fEquation.getText() + "! ");
}
}
if (e.getSource() == bPow)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
if(fEquation.getText().equals(""))
{
totalEquation = ans + "^";
fEquation.setText(ans + "^");
}
else
{
totalEquation = totalEquation + "^";
fEquation.setText(fEquation.getText() + "^");
}
}
if (e.getSource() == bParL)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "(";
fEquation.setText(fEquation.getText() + "(");
}
if (e.getSource() == bParR)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + ")";
fEquation.setText(fEquation.getText() + ")");
}
if (e.getSource() == bNum[0])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "0";
fEquation.setText(fEquation.getText() + "0");
}
if (e.getSource() == bNum[1])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "1";
fEquation.setText(fEquation.getText() + "1");
}
if (e.getSource() == bNum[2])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "2";
fEquation.setText(fEquation.getText() + "2");
}
if (e.getSource() == bNum[3])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "3";
fEquation.setText(fEquation.getText() + "3");
}
if (e.getSource() == bNum[4])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "4";
fEquation.setText(fEquation.getText() + "4");
}
if (e.getSource() == bNum[5])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "5";
fEquation.setText(fEquation.getText() + "5");
}
if (e.getSource() == bNum[6])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "6";
fEquation.setText(fEquation.getText() + "6");
}
if (e.getSource() == bNum[7])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "7";
fEquation.setText(fEquation.getText() + "7");
}
if (e.getSource() == bNum[8])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "8";
fEquation.setText(fEquation.getText() + "8");
}
if (e.getSource() == bNum[9])
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + "9";
fEquation.setText(fEquation.getText() + "9");
}
if (e.getSource() == bPoint)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
if(fEquation.getText().equals(""))
{
totalEquation = 0 + ".";
fEquation.setText("0.");
}
else
{
totalEquation = totalEquation + ".";
fEquation.setText(fEquation.getText() + ".");
}
}
if (e.getSource() == bComa)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + ",";
fEquation.setText(fEquation.getText() + ",");
}
if (e.getSource() == bAns)
{
if(check == true)
{
totalEquation = "";
fEquation.setText("");
}
check = false;
totalEquation = totalEquation + ans;
fEquation.setText(fEquation.getText() + ans);
}
if (e.getSource() == bEquals)
{
if(!fEquation.getText().equals(""))
{
calc obj = new calc();
ans = Double.parseDouble(obj.process(totalEquation));
String ntah = "" + ans;
fAnswer.setText(ntah);
String temp = "[" + date.toString() + "] " + fEquation.getText();
stackUp.push(temp);
}
check = true;
}
if (e.getSource() == bFlu)
{
stackUp = new Stack();
stackDown = new Stack();
}
if (e.getSource() == bClear)
{
fEquation.setText("");
}
if (e.getSource() == bBck)
{
int temp = fEquation.getText().length();
String text = "";
for(int i = 0; i < (temp-1); i++)
text += fEquation.getText().charAt(i);
fEquation.setText("" + text);
}
}
public static void main()
{
Calculator c = new Calculator();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


No comments:
Post a Comment