tanha70
یک شنبه 24 مرداد 1389, 17:00 عصر
سلام دوستان
بالاخره بعد از سرچ زیاد تو اینترنت و تلاشهای خودم تونستم یه ماشین حساب ساده بنویسم
هرکدوم از دوستان که سوالی در مورد این برنامه داشت با من تماس بگیره
با کمال میل جواب میدم.
برنامه دارای دو فایل کلاس هست که هردم رو براتون میذارم.
فایل اصلی
package mycalculator;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Main extends JFrame {
// Fonts
private static final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 20);
// Boolean
private boolean _startNumber = true;
// Panels
JPanel buttonPanel = new JPanel(new GridLayout(5, 3, 2, 2));
JPanel opPanel = new JPanel(new GridLayout(5, 1, 2, 2));
JPanel clearPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel content = new JPanel(new BorderLayout(5, 5));
// Strings
private String []buttonOrder = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "", "0", "."};
private String []opOrder = {"+", "-", "*", "/", "="};
private String _previousOp = "=";
// Buttons
JButton clearButton = new JButton("Clear");
// Text Fields
JTextField _displayField = new JTextField(12);
// other Classes
private CalcLogic _logic = new CalcLogic();
// Constructor
public Main(){
super("Simple Calculator");
_displayField.setFont(BIGGER_FONT);
clearButton.setFont(BIGGER_FONT);
clearButton.addActionListener(new ClearListener());
// Adding number keys to panel
for (int i = 0; i < buttonOrder.length; i++) {
JButton b = new JButton(buttonOrder[i]);
b.setFont(BIGGER_FONT);
b.addActionListener(new NumListener());
buttonPanel.add(b);
}
// Adding operator keys
for (int i = 0; i < opOrder.length; i++) {
JButton b = new JButton(opOrder[i]);
b.setFont(BIGGER_FONT);
b.addActionListener(new OpListener());
opPanel.add(b);
}
// Adding panels to content panel
clearPanel.add(clearButton);
content.add(_displayField, BorderLayout.NORTH);
content.add(buttonPanel, BorderLayout.CENTER);
content.add(opPanel, BorderLayout.EAST);
content.add(clearPanel, BorderLayout.SOUTH);
content.setBorder(BorderFactory.createEmptyBorder( 10, 10, 10, 10));
this.setContentPane(content);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public static void main(String[] args){
// Set the Look and Feel to that of system we're running on.
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
} catch (Exception e) {
}
Main Window = new Main();
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
Window.setVisible(true);
}
private void actionClear(){
_startNumber = true;
_displayField.setText("");
_previousOp = "=";
_logic.setTotal("0");
}
class OpListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(_startNumber){
actionClear();
JOptionPane.showMessageDialog(null, "Error - No Operator");
}
else{
_startNumber = true;
try {
String displayText = _displayField.getText();
if(_previousOp.equals("="))
_logic.setTotal(displayText);
else if(_previousOp.equals("+")){
_logic.add(displayText);
}
else if(_previousOp.equals("-")){
_logic.subtract(displayText);
}
else if(_previousOp.equals("*")){
_logic.multiply(displayText);
}
else if(_previousOp.equals("/")){
_logic.divide(displayText);
}
_displayField.setText(_logic.getTotalString());
} catch (NumberFormatException ex) {
actionClear();
JOptionPane.showMessageDialog(null, ex);
}
_previousOp = e.getActionCommand();
}// end if_startnumber
}// end method
}// end class
class NumListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String digit = e.getActionCommand();
if(_startNumber) {
_displayField.setText(digit);
_startNumber = false;
}else{
_displayField.setText(_displayField.getText() + digit);
}
}
}
class ClearListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
actionClear();
}
}
}
اینم از فایل کلاس دوم
package mycalculator;
public class CalcLogic {
private double _currentTotal;
public CalcLogic() {
_currentTotal = 0;
}
public String getTotalString() {
return "" + _currentTotal;
}
public void setTotal(String n) {
_currentTotal = convertToNumber(n);
}
public void add(String n) {
_currentTotal += convertToNumber(n);
}
public void subtract(String n) {
_currentTotal -= convertToNumber(n);
}
public void multiply(String n) {
_currentTotal *= convertToNumber(n);
}
public void divide(String n) {
_currentTotal /= convertToNumber(n);
}
private double convertToNumber(String n) {
return Double.parseDouble(n);
}
}
tanha70
جمعه 12 شهریور 1389, 09:29 صبح
سلام دوستان
برنامه ماشین حساب قبلی که یک برنامه ساده بود رو توسعه دادم و این دفعه یه ماشین حساب مهندسی نوشتم
سورس برنامه رو میذارم برای دانلود
از دوستان تقاضا دارم با این برنامه کار کنند و باگ هاشو بدست بیارن و به من بگن تا در ورژن های بعدی این نواقص رو از بین ببرم
در ضمن هرکدوم از دوستان سوالی درباره چگونگی نوشتن این برنامه داره با من مطرح کنه خوشحال میشم به سوالاتتون جواب بدم.
سورس کلاس Main
/************************************************** **********************************
/Author: Hamed Bahmani (Student of software engineering in Guilan University) /
/Date: 1389/6/12 - 9/3/2010 /
/E-mail: Hamed.Bahmani@Gmail.com /
/Web Site : HamedWeb.ir /
************************************************** **********************************/
package engineeringcalculator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
public class Main extends JFrame {
// String
private String previousOp = "=";
// Boolean
boolean startNumber = true;
// Font
private static final Font BIGGER_FONT = new Font("monspaced", Font.PLAIN, 12);
// Radio Buttons
ButtonGroup choice = new ButtonGroup();
public JRadioButton degrees = new JRadioButton("Degrees", true);
public JRadioButton radians = new JRadioButton("Radians", false);
// Number Buttons
JButton []numBtn = new JButton[12];
String []numName = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "HB", "0", "."};
// Right Operation Buttons
JButton []ropBtn = new JButton[8];
String []ropName = {"/", "±", "*", "%", "-", "1/x", "+", "="};
// Left Operation Buttons
JButton []lopBtn = new JButton[33];
String []lopName = {"ON", "OFF", "Clear",
"←", "n!", "Memory",
"sin", "cos", "tan",
"asin", "acos", "atan",
"sinh", "cosh", "tanh",
"x^2", "x^3", "x^y",
"√x", "∛x", "√(y&x)",
"Ln", "Log", "exp",
"2^x", "10^x", "mod",
"ceil", "floor", "rint",
"π", "e", "round"};
// Text Fields
JTextField displayField = new JTextField("0");
//Panels
JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
JPanel contentPanel = new JPanel(new BorderLayout(5, 5));
JPanel topPanel = new JPanel(new BorderLayout(5, 5));
JPanel numPanel = new JPanel(new GridLayout(4, 3, 5, 5));
JPanel ropPanel = new JPanel(new GridLayout(4, 2, 5, 5));
JPanel lopPanel = new JPanel(new GridLayout(11, 3, 5, 5));
JPanel botPanel = new JPanel(new BorderLayout(5, 5));
JPanel choicePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
// Objects
Calculator calculate = new Calculator();
public Main() {
super("Hamed's Engineering Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
choice.add(radians);
choice.add(degrees);
choicePanel.add(degrees);
choicePanel.add(radians);
// Adding Number Buttons
for (int i = 0; i < numBtn.length; i++) {
numBtn[i] = new JButton(numName[i]);
if(numName[i].equals("HB")) {
numBtn[i].setEnabled(false);
}
else {
numBtn[i].addActionListener(new numListener());
}
numBtn[i].setFont(BIGGER_FONT);
numPanel.add(numBtn[i]);
}
// Adding Right Operation Buttons
for (int i = 0; i < ropBtn.length; i++) {
ropBtn[i] = new JButton(ropName[i]);
if(ropName[i].equals("±")) {
ropBtn[i].addActionListener(new LOPListener());
} else if(ropName[i].equals("1/x")) {
ropBtn[i].addActionListener(new LOPListener());
} else if(ropName[i].equals("%")) {
ropBtn[i].addActionListener(new percentOpen());
}
else {
ropBtn[i].addActionListener(new ROPListener());
}
ropBtn[i].setFont(BIGGER_FONT);
ropPanel.add(ropBtn[i]);
}
// Adding Left Operation Listener
for (int i = 0; i < lopBtn.length; i++) {
lopBtn[i] = new JButton(lopName[i]);
if(lopName[i].equals("OFF")){
lopBtn[i].addActionListener(new OnOff());
} else if(lopName[i].equals("ON")) {
lopBtn[i].addActionListener(new OnOff());
} else if(lopName[i].equals("Clear")) {
lopBtn[i].addActionListener(new ActionClear());
} else if(lopName[i].equals("x^y") || lopName[i].equals("√(y&x)") || lopName[i].equals("√(y&x)")) {
lopBtn[i].addActionListener(new ROPListener());
} else if(lopName[i].equals("π") || lopName[i].equals("e")) {
lopBtn[i].addActionListener(new numListener());
} else if(lopName[i].equals("←")) {
lopBtn[i].addActionListener(new BackSpace());
} else if(lopName[i].equals("n!")) {
lopBtn[i].addActionListener(new LOPListener());
} else if(lopName[i].equals("Memory")) {
lopBtn[i].addActionListener(new MemoryOpen());
}
else {
lopBtn[i].addActionListener(new LOPListener());
}
lopBtn[i].setFont(BIGGER_FONT);
lopPanel.add(lopBtn[i]);
}
// Display Field
displayField.setFont(BIGGER_FONT);
displayField.setHorizontalAlignment(JTextField.RIG HT);
// Top Panel
topPanel.add(displayField, BorderLayout.NORTH);
topPanel.add(choicePanel, BorderLayout.CENTER);
topPanel.add(lopPanel, BorderLayout.SOUTH);
// Bottom Panel
botPanel.add(numPanel, BorderLayout.CENTER);
botPanel.add(ropPanel, BorderLayout.EAST);
// Main Panel
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(botPanel, BorderLayout.SOUTH);
mainPanel.setBorder(BorderFactory.createEmptyBorde r(10, 10, 10, 10) );
mainPanel.setBackground(Color.PINK);
// Adding Main Panel
add(mainPanel);
this.pack();
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);
}
public static void main(String[] args){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
} catch (Exception unused) {
JOptionPane.showMessageDialog(null, unused);
}
Main window = new Main();
}
private void actionClear() {
startNumber = true; // Expecting number, not op.
displayField.setText("0");
previousOp = "=";
calculate.setTotal("0");
}
private void showResult(double r) {
displayField.setText("" + r);
}
class numListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String digit = e.getActionCommand();
Double PI = Math.PI;
Double E = Math.E;
if(digit.equals("π")) {
digit = PI.toString();
} else if(digit.equals("e")) {
digit = E.toString();
}
if (startNumber) {
displayField.setText(digit);
startNumber = false;
}
else
displayField.setText(displayField.getText() + digit);
}
}
class ROPListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (startNumber) {
actionClear();
JOptionPane.showMessageDialog(null, "Error: Please enter a number.\nghalat kardi.");
} else {
startNumber = true;
try {
String displayText = displayField.getText();
if (previousOp.equals("=")) {
calculate.setTotal(displayText);
} else if (previousOp.equals("+")) {
calculate.add(displayText);
} else if (previousOp.equals("-")) {
calculate.subtract(displayText);
} else if (previousOp.equals("*")) {
calculate.multiply(displayText);
} else if (previousOp.equals("/")) {
calculate.divide(displayText);
} else if (previousOp.equals("x^y")) {
calculate.pow(displayText);
} else if (previousOp.equals("√(y&x)")) {
calculate.sqrty(displayText);
} else if (previousOp.equals("mod")) {
calculate.mod(displayText);
}
displayField.setText("" + calculate.getTotalString());
} catch (NumberFormatException ex) {
actionClear();
displayField.setText("Error: " + ex);
}
previousOp = e.getActionCommand();
}
}
}
class LOPListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
String displayText = displayField.getText();
double ans;
double x = Double.parseDouble(displayText);
previousOp = e.getActionCommand();
if(startNumber){
actionClear();
JOptionPane.showMessageDialog(null, "Error - No Operator");
} else {
try {
if (previousOp.equals("=")) {
calculate.setTotal(displayText);
} else if (previousOp.equals("sin")) {
calculate.sin(displayText, degrees);
} else if (previousOp.equals("cos")) {
calculate.cos(displayText, degrees);
} else if (previousOp.equals("tan")) {
calculate.tan(displayText, degrees);
} else if (previousOp.equals("asin")) {
calculate.asin(displayText, degrees);
} else if (previousOp.equals("acos")) {
calculate.acos(displayText, degrees);
} else if (previousOp.equals("atan")) {
calculate.atan(displayText, degrees);
} else if (previousOp.equals("sinh")) {
calculate.sinh(displayText, degrees);
} else if (previousOp.equals("cosh")) {
calculate.cosh(displayText, degrees);
} else if (previousOp.equals("tanh")) {
calculate.tanh(displayText, degrees);
} else if (previousOp.equals("x^2")) {
calculate.pow2(displayText);
} else if (previousOp.equals("x^3")) {
calculate.pow3(displayText);
} else if (previousOp.equals("√x")) {
calculate.sqrt(displayText);
} else if (previousOp.equals("∛x")) {
calculate.cbrt(displayText);
} else if (previousOp.equals("Ln")) {
calculate.log(displayText);
} else if (previousOp.equals("Log")) {
calculate.log10(displayText);
} else if (previousOp.equals("exp")) {
calculate.exp(displayText);
} else if (previousOp.equals("2^x")) {
calculate.pow2x(displayText);
} else if (previousOp.equals("10^x")) {
calculate.pow10x(displayText);
} else if (previousOp.equals("ceil")) {
calculate.ceil(displayText);
} else if (previousOp.equals("floor")) {
calculate.floor(displayText);
} else if (previousOp.equals("rint")) {
calculate.rint(displayText);
} else if (previousOp.equals("round")) {
calculate.round(displayText);
} else if (previousOp.equals("±")) {
calculate.plusMines(displayText);
} else if (previousOp.equals("1/x")) {
calculate.inverse(displayText);
} else if (previousOp.equals("n!")) {
calculate.factorial(displayText);
}
displayField.setText("" + calculate.getTotalString());
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
previousOp = e.getActionCommand();
}
}
}
class OnOff implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
try {
if(source == lopBtn[1]) {
for (int i = 0; i < numBtn.length; i++) {
numBtn[i].setEnabled(false);
}
for (int i = 1; i < lopBtn.length; i++) {
lopBtn[i].setEnabled(false);
}
for (int i = 0; i < ropBtn.length; i++) {
ropBtn[i].setEnabled(false);
}
} else if (source == lopBtn[0]){
for (int i = 0; i < numBtn.length; i++) {
if (numName[i].equals("HB"))numBtn[i].setEnabled(false);
else numBtn[i].setEnabled(true);
}
for (int i = 0; i < lopBtn.length; i++) {
lopBtn[i].setEnabled(true);
}
for (int i = 0; i < ropBtn.length; i++) {
ropBtn[i].setEnabled(true);
}
}
} catch (Exception e) {
}
}
}
class ActionClear implements ActionListener {
public void actionPerformed(ActionEvent e) {
actionClear();
}
}
class MemoryOpen implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
Memory obj = new Memory();
if (source == lopBtn[5]) {
obj.setVisible(true);
}
}
}
class Memory extends JFrame implements ActionListener {
JLabel []Lbl = new JLabel[10];
String []LblName = {"Memory 1: ", "Memory 2: ", "Memory 3: ", "Memory 4: ", "Memory 5: ", "Memory 6: ", "Memory 7: ", "Memory 8: ", "Memory 9: ", "Memory 10: "};
JTextField []tf = new JTextField[10];
JButton []MemBtn1 = new JButton[10];
JButton []MemBtn2 = new JButton[10];
String []MemName1 = {"M+", "M+", "M+", "M+", "M+", "M+", "M+", "M+", "M+", "M+"};
String []MemName2 = {"M-", "M-", "M-", "M-", "M-", "M-", "M-", "M-", "M-", "M-"};
JPanel MemPanel1 = new JPanel(new GridLayout(10, 1, 5, 5));
JPanel MemPanel2 = new JPanel(new GridLayout(10, 1, 5, 5));
JPanel LabelPanel = new JPanel(new GridLayout(10, 1, 5, 5));
JPanel tfPanel = new JPanel(new GridLayout(10, 1, 5, 5));
JPanel btnPanel = new JPanel(new BorderLayout(5, 5));
JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
public Memory() {
super("Memory");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
for (int i = 0; i < Lbl.length; i++) {
Lbl[i] = new JLabel(LblName[i]);
Lbl[i].setFont(BIGGER_FONT);
LabelPanel.add(Lbl[i]);
}
for (int i = 0; i < tf.length; i++) {
tf[i] = new JTextField();
tf[i].setFont(BIGGER_FONT);
tf[i].setEditable(true);
tfPanel.add(tf[i]);
}
for (int i = 0; i < MemBtn1.length; i++) {
MemBtn1[i] = new JButton(MemName1[i]);
MemBtn1[i].addActionListener(this);
MemBtn1[i].setFont(BIGGER_FONT);
MemPanel1.add(MemBtn1[i]);
}
for (int i = 0; i < MemBtn2.length; i++) {
MemBtn2[i] = new JButton(MemName2[i]);
MemBtn2[i].addActionListener(this);
MemBtn2[i].setFont(BIGGER_FONT);
MemPanel2.add(MemBtn2[i]);
}
btnPanel.add(MemPanel1, BorderLayout.WEST);
btnPanel.add(MemPanel2, BorderLayout.EAST);
mainPanel.add(LabelPanel, BorderLayout.WEST);
mainPanel.add(tfPanel, BorderLayout.CENTER);
mainPanel.add(btnPanel, BorderLayout.EAST);
add(mainPanel);
setSize(400, 400);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
for (int i = 0; i < MemBtn1.length; i++) {
if(source == MemBtn1[i]) {
tf[i].setText(displayField.getText());
} else if (source == MemBtn2[i]) {
displayField.setText(tf[i].getText());
startNumber = false;
}
}// end of for
}// end of method
}// end of class
class BackSpace implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == lopBtn[3]) {
String displayText = displayField.getText();
StringBuffer sb = new StringBuffer(displayText);
int size = displayText.length();
if(size > 0) {
sb.deleteCharAt(size-1);
displayText = sb.toString();
displayField.setText(displayText);
} else {
JOptionPane.showMessageDialog(null, "There is nothing to delete.");
}
}
}
}
class Percent extends JFrame implements ActionListener {
JButton calc1 = new JButton("=");
JButton calc2 = new JButton("=");
JButton calc3 = new JButton("=");
JTextField []tf = new JTextField[9];
JLabel qMark1 = new JLabel("?");
JLabel qMark2 = new JLabel("?");
JLabel qMark3 = new JLabel("?");
JLabel []textLabel = new JLabel[5];
String []textName = {"What is", "% of", "is what percent of", "is", "% of what",};
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
Percent() {
super("Percentage Calculator");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
for (int i = 0; i < textLabel.length; i++) {
textLabel[i] = new JLabel(textName[i]);
}
for (int i = 0; i < tf.length; i++) {
tf[i] = new JTextField(12);
}
// Panel 1
panel1.add(textLabel[0]);
panel1.add(tf[0]);
panel1.add(textLabel[1]);
panel1.add(tf[1]);
panel1.add(qMark1);
panel1.add(calc1);
panel1.add(tf[2]);
// Panel 2
panel2.add(tf[3]);
panel2.add(textLabel[2]);
panel2.add(tf[4]);
panel2.add(qMark2);
panel2.add(calc2);
panel2.add(tf[5]);
// Panel 3
panel3.add(tf[6]);
panel3.add(textLabel[3]);
panel3.add(tf[7]);
panel3.add(textLabel[4]);
panel3.add(qMark3);
panel3.add(calc3);
panel3.add(tf[8]);
// Main Panel
mainPanel.add(panel1, BorderLayout.NORTH);
mainPanel.add(panel2, BorderLayout.CENTER);
mainPanel.add(panel3, BorderLayout.SOUTH);
// Add Action listener
calc1.addActionListener(this);
calc2.addActionListener(this);
calc3.addActionListener(this);
add(mainPanel);
pack();
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
String displayText1, displayText2;
try {
if(source == calc1) {
displayText1 = tf[0].getText();
displayText2 = tf[1].getText();
double ans = calculate.p1(displayText1, displayText2);
tf[2].setText(""+ans);
}
else if(source == calc2) {
displayText1 = tf[3].getText();
displayText2 = tf[4].getText();
double ans = calculate.p2(displayText1, displayText2);
tf[5].setText(""+ans);
}
else if(source == calc3) {
displayText1 = tf[6].getText();
displayText2 = tf[7].getText();
double ans = calculate.p3(displayText1, displayText2);
tf[8].setText(""+ans);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
class percentOpen implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Percent obj = new Percent();
Object source = evt.getSource();
if (source == ropBtn[3]) {
obj.setVisible(true);
}
}
}
}
سورس کلاس Calculator در تاپیک بعدی
tanha70
جمعه 12 شهریور 1389, 09:31 صبح
اینم از سورس کلاس Calculator
/************************************************** **********************************
/Author: Hamed Bahmani (Student of software engineering in Guilan University) /
/Date: 1389/6/12 - 9/3/2010 /
/E-mail: Hamed.Bahmani@Gmail.com /
/Web Site : HamedWeb.ir /
************************************************** **********************************/
package engineeringcalculator;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
public class Calculator {
private double currentTotal;
private double secondTotal;
private boolean flag = false;
public Calculator() {
currentTotal = 0;
}
private double convertToNumber(String hb) {
return Double.parseDouble(hb);
}
String getTotalString() {
return "" + currentTotal;
}
private double fact(double n) {
if( n <= 1 )
return 1;
else
return n * fact( n - 1 );
}
void setTotal(String hb) {
currentTotal = convertToNumber(hb);
}
void divide(String hb) {
currentTotal /= convertToNumber(hb);
}
void multiply(String hb) {
currentTotal *= convertToNumber(hb);
}
void subtract(String hb) {
currentTotal -= convertToNumber(hb);
}
void add(String hb) {
currentTotal += convertToNumber(hb);
}
void sin(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
if(d.isSelected()) {
currentTotal = Math.toRadians(currentTotal);
}
currentTotal = Math.sin(currentTotal);
getTotalString();
}
void cos(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
if(d.isSelected()) {
currentTotal = Math.toRadians(currentTotal);
}
currentTotal = Math.cos(currentTotal);
getTotalString();
}
void tan(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
if(d.isSelected()) {
currentTotal = Math.toRadians(currentTotal);
}
currentTotal = Math.tan(currentTotal);
getTotalString();
}
void asin(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
currentTotal = Math.asin(currentTotal);
if(d.isSelected()) {
currentTotal = Math.toDegrees(currentTotal);
}
getTotalString();
}
void acos(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
currentTotal = Math.acos(currentTotal);
if(d.isSelected()) {
currentTotal = Math.toDegrees(currentTotal);
}
getTotalString();
}
void atan(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
currentTotal = Math.atan(currentTotal);
if(d.isSelected()) {
currentTotal = Math.toDegrees(currentTotal);
}
getTotalString();
}
void sinh(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
if(d.isSelected()) {
currentTotal = Math.toRadians(currentTotal);
}
currentTotal = Math.sinh(currentTotal);
getTotalString();
}
void cosh(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
if(d.isSelected()) {
currentTotal = Math.toRadians(currentTotal);
}
currentTotal = Math.cosh(currentTotal);
getTotalString();
}
void tanh(String hb, JRadioButton d) {
currentTotal = convertToNumber(hb);
if(d.isSelected()) {
currentTotal = Math.toRadians(currentTotal);
}
currentTotal = Math.tanh(currentTotal);
getTotalString();
}
void pow2(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.pow(currentTotal, 2);
getTotalString();
}
void pow3(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.pow(currentTotal, 3);
getTotalString();
}
void pow(String hb) {
if(flag) {
secondTotal = convertToNumber(hb);
} else {
currentTotal = convertToNumber(hb);
flag = true;
}
currentTotal = Math.pow(currentTotal, secondTotal);
}
void sqrt(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.sqrt(currentTotal);
getTotalString();
}
void cbrt(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.cbrt(currentTotal);
getTotalString();
}
void sqrty(String hb) {
if(flag) {
secondTotal = convertToNumber(hb);
secondTotal = 1/secondTotal;
} else {
currentTotal = convertToNumber(hb);
flag = true;
}
currentTotal = Math.pow(currentTotal, secondTotal);
}
void log(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.log(currentTotal);
getTotalString();
}
void log10(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.log10(currentTotal);
getTotalString();
}
void exp(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.exp(currentTotal);
getTotalString();
}
void pow2x(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.pow(2, currentTotal);
getTotalString();
}
void pow10x(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.pow(10, currentTotal);
getTotalString();
}
void mod(String hb) {
try{
if(flag) {
secondTotal = convertToNumber(hb);
} else {
currentTotal = convertToNumber(hb);
flag = true;
}
currentTotal = Math.IEEEremainder(currentTotal, secondTotal);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
void ceil(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.ceil(currentTotal);
getTotalString();
}
void floor(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.floor(currentTotal);
getTotalString();
}
void rint(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.rint(currentTotal);
getTotalString();
}
void round(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = Math.round(currentTotal);
getTotalString();
}
void plusMines(String hb) {
currentTotal = convertToNumber(hb);
if(Math.signum(currentTotal) == 1 || Math.signum(currentTotal) == -1)
currentTotal = (-1)*currentTotal;
getTotalString();
}
void inverse(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = (1/currentTotal);
getTotalString();
}
void factorial(String hb) {
currentTotal = convertToNumber(hb);
currentTotal = fact(currentTotal);
getTotalString();
}
double p1(String h, String b) {
currentTotal = convertToNumber(h);
secondTotal = convertToNumber(b);
return (currentTotal*secondTotal)/100;
}
double p2(String h, String b) {
currentTotal = convertToNumber(h);
secondTotal = convertToNumber(b);
return (currentTotal*100)/secondTotal;
}
double p3(String h, String b) {
currentTotal = convertToNumber(h);
secondTotal = convertToNumber(b);
return (currentTotal*100)/secondTotal;
}
}
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.