|
-
Feb 12th, 2005, 08:23 AM
#1
Thread Starter
Frenzied Member
calculator with more than one operator
Ok, I know I had another post of this, but I don't feel like finding it..What I was wondering, was could someone look at this simple pseudo code and tell me if you think it will work...
Code:
user enters first number
//do nothing
user selects an operator
//get string of first number and subtract the operator from it
user enters second number
// do nothing
useer selects second operator
//have a variable that kept track of the previos operator
//perform the previous operation using the previous operator
//change the previous operator to the current operator
user enters another number
// do nothing
user selects calculate
//perform the last operation
The only problem I can see with this, is that there would be a lot of methods with if else if , statements, and there should be some way that I could lessen those statements.
-
Feb 12th, 2005, 04:15 PM
#2
Thread Starter
Frenzied Member
Re: calculator with more than one operator
never mind, I got it all figured out..It's amazing how much logic goes into just a simple calculator..
-
Feb 14th, 2005, 05:34 AM
#3
Addicted Member
Re: calculator with more than one operator
when you've finished it can i have a butchers at the code? just out of interest, having never written a calculator before....
How are you doing it? are you using a stack based approach?
-
Feb 14th, 2005, 06:16 AM
#4
Thread Starter
Frenzied Member
Re: calculator with more than one operator
Right now, I'm not using a stack approach..That was coming some time in the future..What I was doing, was trying to use the most simple methods and simple data structures as possible...Now, that is what makes my code very confusing(I think)..I personally don't like how it's written, but this was my first time at writing one of these. I'll post the code, but there as still a couple of minor problems, and don't forget, this calculator is very very simple!
-
Feb 14th, 2005, 06:19 AM
#5
Thread Starter
Frenzied Member
Re: calculator with more than one operator
err it was too long to add to the previous post.. I'll put it in an attachment.
-
Feb 14th, 2005, 06:21 AM
#6
Thread Starter
Frenzied Member
Re: calculator with more than one operator
ahh it's not letting me add an attachment.
-
Feb 14th, 2005, 06:25 AM
#7
Thread Starter
Frenzied Member
Re: calculator with more than one operator
Here, I'll just post half on this post and half on another
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.text.*;
public class Calculator extends JFrame implements ActionListener, UndoableEditListener
{
JMenuItem exit;
JMenuItem clear;
JMenuItem copy;
JMenuItem paste;
JMenuItem undo;
JMenuItem redo;
JMenuItem aboutCalc;
JRadioButtonMenuItem rb0;
JRadioButtonMenuItem rb2;
JRadioButtonMenuItem rb4;
JRadioButtonMenuItem rb6;
JRadioButtonMenuItem rb8;
JButton[] btnNums;
JButton btnBack;
JButton btnClear;
JButton btnAddMem;
JButton btnPasteMem;
JButton btnCalculate;
String[] strNames;
String strOperand1;
String strOperand2;
double dOperand1;
double dOperand2;
double memoryVar;
double answer;
final String STR_MULTIPLY = "MULTIPLY";
final String STR_DIVIDE = "DIVIDE";
final String STR_ADD = "ADD";
final String STR_SUBTRACT = "SUBTRACT";
final String STR_SQUARE = "SQUARE";
String whichOperator;
String strPrevOperator;
boolean prevOperator;
DecimalFormat dec;
Document doc;
JTextField txtDisplay;
ArrayList operands;
UndoManager undoManager;
public Calculator()
{
JPanel menuPanel = new JPanel();
BorderLayout br = new BorderLayout();
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(exit = new JMenuItem("Exit"));
exit.addActionListener(this);
JMenu editMenu = new JMenu("Edit");
editMenu.add(undo = new JMenuItem("Undo"));
undo.addActionListener(this);
editMenu.add(redo = new JMenuItem("Redo"));
redo.addActionListener(this);
editMenu.add(copy = new JMenuItem("AddM"));
copy.addActionListener(this);
editMenu.add(paste = new JMenuItem("PasteM"));
paste.addActionListener(this);
editMenu.add(clear = new JMenuItem("Clear"));
clear.addActionListener(this);
JMenu formatMenu = new JMenu("Format");
ButtonGroup bg = new ButtonGroup();
rb0 = new JRadioButtonMenuItem("###",false);
rb0.addActionListener(this);
rb2 = new JRadioButtonMenuItem("###.##",true);
rb2.addActionListener(this);
rb4 = new JRadioButtonMenuItem("###.####",false);
rb4.addActionListener(this);
rb6 = new JRadioButtonMenuItem("###.######",false);
rb6.addActionListener(this);
bg.add(rb0);
bg.add(rb2);
bg.add(rb4);
bg.add(rb6);
formatMenu.add(rb0);
formatMenu.add(rb2);
formatMenu.add(rb4);
formatMenu.add(rb6);
JMenu aboutMenu = new JMenu("About");
aboutMenu.add(aboutCalc = new JMenuItem("About Calculator"));
aboutCalc.addActionListener(this);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
menuBar.add(aboutMenu);
setJMenuBar(menuBar);
undoManager = new UndoManager();
undoManager.setLimit(1500);
/* construct a JPanel and the textfield that
* will show the inputed values and outputed
* results
*/
JPanel jpDisplay = new JPanel();
jpDisplay.setLayout(new BorderLayout());
txtDisplay = new JTextField(15);
txtDisplay.setHorizontalAlignment(JTextField.RIGHT);
doc = (Document)txtDisplay.getDocument();
doc.addUndoableEditListener(this);
jpDisplay.add(txtDisplay);
dec = new DecimalFormat("###,###,###.##");
/* contstruct a JPanel that will contain a back
* button and a clear button.
*/
JPanel jpRow2 = new JPanel();
btnAddMem = new JButton("M+");
btnAddMem.addActionListener(this);
btnPasteMem = new JButton("M-");
btnPasteMem.addActionListener(this);
btnBack = new JButton("back");
btnBack.addActionListener(this);
jpRow2.add(btnAddMem);
jpRow2.add(btnPasteMem);
jpRow2.add(btnBack);
/* construct a string array with all the names of the
* buttons, then in a for loop create the new buttons
* and add their name and an actionListener to them.
*/
String[] strNames = {"7","8", "9","/", "4", "5", "6","*", "1",
"2", "3","+", "0", "x^y", ".", "-"};
btnNums = new JButton[16];
JPanel jpButtons = new JPanel();
jpButtons.setLayout(new GridLayout(4,4,4,4));
for (int i = 0; i < 16; i++)
{
btnNums[i] = new JButton(strNames[i]);
btnNums[i].addActionListener(this);
jpButtons.add(btnNums[i]);
}
/* construct the final JPanel and add a
* calculate button to it.
*/
JPanel jpLastRow = new JPanel();
btnCalculate = new JButton("calculate");
btnCalculate.addActionListener(this);
btnClear = new JButton("clear");
btnClear.addActionListener(this);
jpLastRow.add(btnCalculate);
jpLastRow.add(btnClear);
operands = new ArrayList();
/* set the contentPane and create the layout
* add the panels to the container
*/
Container content = getContentPane();
setSize(240,290);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(content);
setLayout(new FlowLayout());
setResizable(false);
content.add(menuPanel, BorderLayout.NORTH);
content.add(jpDisplay, BorderLayout.NORTH);
content.add(jpRow2);
content.add(jpButtons);
content.add(jpLastRow);
setTitle("Mini Calculator");
setVisible(true);
}
public void undoableEditHappened(UndoableEditEvent uee)
{
undoManager.addEdit(uee.getEdit());
}
public void actionPerformed(ActionEvent ae)
{
// if any of the array buttons was clicked append the button's text
for (int i =0; i < 16; i++)
{
if (ae.getSource() == btnNums[i])
{
if (ae.getSource() == btnNums[3] || ae.getSource() == btnNums[7] || ae.getSource() == btnNums[11] ||
ae.getSource() == btnNums[13] || ae.getSource() == btnNums[15])
{
txtDisplay.setText(txtDisplay.getText() + "");
}
else
{
txtDisplay.setText(txtDisplay.getText()
+ btnNums[i].getText());
}
}
}
/* if the backspace button was clicked, then create substring
* to to subtract the farthest character to the right
*/
if (ae.getSource() == btnBack)
{
String strAll = txtDisplay.getText();
int length = strAll.length();
String strShowThis = strAll.substring(0,length - 1);
txtDisplay.setText(strShowThis);
}
else if (ae.getSource() == btnClear || ae.getSource() == clear)
{
txtDisplay.setText(null);
dOperand1 = 0;
dOperand2 = 0;
answer = 0;
prevOperator = false;
}
else if (ae.getSource() == btnCalculate)
{
String strText = txtDisplay.getText();
try
{
int equation = Integer.parseInt(strText);
txtDisplay.setText("" + equation);
}
catch (ArithmeticException aee)
{
aee.printStackTrace();
}
}
else if (ae.getSource() == btnAddMem || ae.getSource() == copy)
{
memoryVar = Double.parseDouble(txtDisplay.getText());
}
else if (ae.getSource() == btnPasteMem || ae.getSource() == paste)
{
txtDisplay.setText(memoryVar + "");
}
else if (ae.getSource() == exit)
{
System.exit(0);
}
/* start the process of finding which operator was
* chosen, and the process the equation in a new
* method
*/
if (ae.getSource() == btnNums[3])
{
processDivide();
}
else if (ae.getSource() == btnNums[7])
{
processMultiply();
}
else if (ae.getSource() == btnNums[11])
{
processAdd();
}
else if (ae.getSource() == btnNums[13])
{
processSquare();
}
else if (ae.getSource() == btnNums[15])
{
processSubtract();
}
else if (ae.getSource() == undo)
{
processUndo();
}
else if (ae.getSource() == redo)
{
processRedo();
}
else if (ae.getSource() == aboutCalc)
{
processAbout();
}
else
{
}
-
Feb 14th, 2005, 06:26 AM
#8
Thread Starter
Frenzied Member
Re: calculator with more than one operator
Code:
if (ae.getSource() == rb0)
{
dec = new DecimalFormat("###,###,###.0");
}
else if (ae.getSource() == rb2)
{
dec = new DecimalFormat("###,###,###.##");
}
else if (ae.getSource() == rb4)
{
dec = new DecimalFormat("###,###,###.####");
}
else if (ae.getSource() == rb6)
{
dec = new DecimalFormat("###,###,###.######");
}
else
{
}
// and finally process the calculation
if (ae.getSource() == btnCalculate)
{
processCalc();
}
else
{
}
}
public void processAdd()
{
if (prevOperator == false)
{
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand1 = Double.parseDouble(value);
txtDisplay.setText("");
prevOperator = true;
strPrevOperator = STR_ADD;
whichOperator = STR_ADD;
}
else if (prevOperator == true)
{
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand2 = Double.parseDouble(value);
prevOperator = true;
whichOperator = STR_ADD;
if (strPrevOperator == STR_ADD)
{
dOperand1 = dOperand1 + dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SUBTRACT)
{
dOperand1 = dOperand1 - dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_MULTIPLY)
{
dOperand1 = dOperand1 * dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_DIVIDE)
{
dOperand1 = dOperand1 / dOperand2;
txtDisplay.setText("");
}
}
else if (strPrevOperator == STR_SQUARE)
{
dOperand1 = Math.pow(dOperand1,dOperand2);
txtDisplay.setText("");
}
else
{
}
whichOperator = STR_ADD;
strPrevOperator = STR_ADD;
prevOperator = true;
}
public void processSubtract()
{
if (prevOperator == false)
{
whichOperator = STR_SUBTRACT;
strPrevOperator = STR_SUBTRACT;
prevOperator = true;
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand1 = Double.parseDouble(value);
txtDisplay.setText("");
}
else if (prevOperator == true)
{
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand2 = Double.parseDouble(value);
txtDisplay.setText("");
if (strPrevOperator == STR_ADD)
{
dOperand1 = dOperand1 + dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SUBTRACT)
{
dOperand1 = dOperand1 - dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_MULTIPLY)
{
dOperand1 = dOperand1 * dOperand2;
System.out.println("strprevoperator = strmultiply");
System.out.println("doperand1 after multiplecation = " + dOperand1);
txtDisplay.setText("");
}
else if (strPrevOperator == STR_DIVIDE)
{
dOperand1 = dOperand1 / dOperand2;
txtDisplay.setText("");
}
}
else if (strPrevOperator == STR_SQUARE)
{
dOperand1 = Math.pow(dOperand1,dOperand2);
txtDisplay.setText("");
}
else
{
}
whichOperator = STR_SUBTRACT;
strPrevOperator = STR_SUBTRACT;
prevOperator = true;
}
public void processMultiply()
{
if (prevOperator == false)
{
whichOperator = STR_MULTIPLY;
strPrevOperator = STR_MULTIPLY;
prevOperator = true;
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand1 = Double.parseDouble(value);
System.out.println("processMultiply");
System.out.println("dOperand1 = " + dOperand1);
txtDisplay.setText("");
}
else if (prevOperator == true)
{
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand2 = Double.parseDouble(value);
txtDisplay.setText("");
if (strPrevOperator == STR_ADD)
{
dOperand1 = dOperand1 + dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SUBTRACT)
{
dOperand1 = dOperand1 - dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_MULTIPLY)
{
dOperand1 = dOperand1 * dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_DIVIDE)
{
dOperand1 = dOperand1 / dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SQUARE)
{
dOperand1 = Math.pow(dOperand1,dOperand2);
txtDisplay.setText("");
}
}
else
{
}
whichOperator = STR_MULTIPLY;
strPrevOperator = STR_MULTIPLY;
prevOperator = true;
}
public void processDivide()
{
if (prevOperator == false)
{
whichOperator = STR_DIVIDE;
strPrevOperator = STR_DIVIDE;
prevOperator = true;
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand1 = Double.parseDouble(value);
txtDisplay.setText("");
}
else if (prevOperator == true)
{
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand2 = Double.parseDouble(value);
txtDisplay.setText("");
if (strPrevOperator == STR_ADD)
{
dOperand1 = dOperand1 + dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SUBTRACT)
{
dOperand1 = dOperand1 - dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_MULTIPLY)
{
dOperand1 = dOperand1 * dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_DIVIDE)
{
dOperand1 = dOperand1 / dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SQUARE)
{
dOperand1 = Math.pow(dOperand1,dOperand2);
txtDisplay.setText("");
}
}
else
{
}
whichOperator = STR_DIVIDE;
strPrevOperator = STR_DIVIDE;
prevOperator = true;
}
public void processSquare()
{
if ( prevOperator = false)
{
whichOperator = STR_SQUARE;
strOperand1 = txtDisplay.getText();
int x = strOperand1.length();
String s = strOperand1.substring(0,x);
dOperand1 = Double.parseDouble(s);
txtDisplay.setText("");
}
else if (prevOperator == true)
{
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand2 = Double.parseDouble(value);
txtDisplay.setText("");
if (strPrevOperator == STR_ADD)
{
dOperand1 = dOperand1 + dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SUBTRACT)
{
dOperand1 = dOperand1 - dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_MULTIPLY)
{
dOperand1 = dOperand1 * dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_DIVIDE)
{
dOperand1 = dOperand1 / dOperand2;
txtDisplay.setText("");
}
else if (strPrevOperator == STR_SQUARE)
{
dOperand1 = Math.pow(dOperand1,dOperand2);
txtDisplay.setText("");
}
}
else
{
}
txtDisplay.setText("");
whichOperator = STR_SQUARE;
strPrevOperator = STR_SQUARE;
prevOperator = true;
}
public void processCalc()
{
String wholeText = txtDisplay.getText();
int length = wholeText.length();
String value = wholeText.substring(0,length);
dOperand2 = Double.parseDouble(value);
if (whichOperator == STR_ADD)
{
answer = dOperand1 + dOperand2;
txtDisplay.setText(dec.format(answer)+"");
dOperand1 = answer;
}
else if (whichOperator == STR_SUBTRACT)
{
answer = dOperand1 - dOperand2;
txtDisplay.setText(dec.format(answer)+"");
dOperand1 = answer;
}
else if (whichOperator == STR_MULTIPLY)
{
answer = dOperand1 * dOperand2;
txtDisplay.setText(dec.format(answer)+"");
dOperand1 = answer;
}
else if (whichOperator == STR_DIVIDE)
{
answer = dOperand1 / dOperand2;
txtDisplay.setText(dec.format(answer)+"");
dOperand1 = answer;
}
else if (whichOperator == STR_SQUARE)
{
String o2 = txtDisplay.getText();
Double d2 = Double.parseDouble(o2);
answer = Math.pow(dOperand1,d2);
txtDisplay.setText("" + dec.format(answer)+"");
}
else
{
}
}
public void processUndo()
{
try
{
/* call the UndoManagers undo method
*/
undoManager.undo();
}
catch (CannotUndoException cre)
{
}
}
public void processRedo()
{
try
{
/* call the UndoManagers redo() method
*/
undoManager.redo();
}
catch (CannotRedoException cre)
{
}
}
public void processAbout()
{
String message = "Written and Tested by: *****" + "\nDate Developed: Febuary 8th, 2005\n"
+ " Copyright 2005\n All rights reserverd";
JOptionPane.showMessageDialog(null, message);
}
public static void main(String[] args)
{
Calculator calc = new Calculator();
}
}
That worked, but I don't know why I couldn't post attachments..Just append the code in one file.
-
Feb 14th, 2005, 06:26 AM
#9
Thread Starter
Frenzied Member
Re: calculator with more than one operator
by, the way, let me know what you think.
-
Feb 14th, 2005, 06:41 AM
#10
Addicted Member
Re: calculator with more than one operator
i'm impressed, my only thoughts are that althought you wanted to keep it simple, which is a good idea, this method actually makes it a little more compelx because of the amount of string parsing you have to do, but it does work so hat off to you,
so what is the future of this? stack based, RPN, simple language parsing?
Why a calculator?
Andy
-
Feb 14th, 2005, 06:49 AM
#11
Thread Starter
Frenzied Member
Re: calculator with more than one operator
ahh the future..Well first I'm going to add a lot more functions too it, because obviously it's way to simple right now..One of the thing I really want to do, is create some kind of method that will calculate the determinate of a matrix...Also, I think there is definately a stack based approach in the future..As you said, it's very confusing right now, and a lot of repetiive code could be cut down by using advanced data structures and approaces. Maybe some kind of tree parser would be the best...I kind of liked starting out with this simple approach though, I learned a lot from it and it took some serious time to figure out all the logic!
As for why I'm doing a calculator, I really don't know. I've been trying to write more usefull programs lately. When I first started programming everything was a do-nothing application, and so is everything we do in my ap comp sci class. Right now, I just like picking something that will take a lot of time and reasearching, that way I learn something from making it.
-
Feb 14th, 2005, 06:57 AM
#12
Addicted Member
Re: calculator with more than one operator
i had the same problem, so when i started at my company there were the odd project floating around, but i managed to avoid them all and write a data base explorer, that used JDBC and all its whizz bang features to allow you to explore teh connection, data structures etc... taught me more about programming than i ever learnt at university doing my Computer Science degree.
I wouldn;t mind seeing your later versions when you get them up and running..
Andy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|