会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
grand's home
.NET
博客园
首页
新随笔
联系
订阅
管理
公告
记事本写的科学计算器(源代码)
标题
记事本写的科学计算器(源代码)
关键字
记事本写的科学计算器(源代码)
出处
选择自
hongweijin
的 Blog
作者Blog:
http://blog.csdn.net/hongweijin/
/** */
/**
* Calculator
* A shareware calculator
*
*
@author
{
@link
http://blog.csdn.net/hongweijin
Bingbing Li}
*
* @recitation 0101 Matt Carlson
*
* @date 12/7/2005 12:08AM
*
*/
import
javax.swing.
*
;
import
javax.swing.border.
*
;
import
java.awt.
*
;
import
java.awt.event.
*
;
public
class
Calculator
extends
JFrame
implements
ActionListener
{
public
static
final
int
WIDTH
=
800
;
//
width of the window
public
static
final
int
HEIGHT
=
600
;
//
height of the window
public
static
final
int
BUTTON_WIDTH
=
80
;
//
width of the buttons
public
static
final
int
BUTTON_HEIGHT
=
60
;
//
height of the buttons
private
CardLayout dealer;
//
card layout
private
JPanel deckPanel;
//
used to card layout
private
JTextField result;
//
the calculate result
private
JCheckBoxMenuItem scientificMode;
//
the menu item of the mode
private
Box vPanel1;
//
the first line buttons of the
//
scientific mode.
private
JButton mod;
//
the modular button
private
JButton xey;
//
the button of Yth root of X
private
JButton ms;
//
save button
private
JButton mr;
//
release the stored value
private
JButton mc;
//
clear the stored value
private
double
head
=
0.0
;
//
the first number of the equation
private
double
tail
=
0.0
;
//
the second number of the equation
private
boolean
substitution
=
true
;
//
whether substituted the answer or not
private
String operatedCommand
=
"
NOTHING
"
;
//
the current operated command
private
String preOperatedCommand
=
"
NOTHING
"
;
//
remember the pre-operated command
private
double
variableMemory
=
0.0
;
//
the variable of the memory
private
JButton jButtonR;
//
the register's button
private
JButton jButtonE;
//
the edit's button
private
JTextField nameField;
//
the field of the name
private
JTextField countryField;
//
the field of the country
private
JTextField zipField;
//
the field of the zip
private
JTextField stateField;
//
the field of the state
private
JTextField cityField ;
//
the field of the city
private
JTextField streetAddressField;
//
the field of the address
private
JTextField first;
//
the first part of the key
private
JTextField second;
//
the second part of the key
private
JTextField third;
//
the third part of the key
private
JTextField fourth;
//
the fourth part of the key
public
Calculator()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(
"
Normal Calculator
"
);
Container contentPre
=
getContentPane();
//
the container of the window
dealer
=
new
CardLayout();
//
create a new card layout
deckPanel
=
new
JPanel();
deckPanel.setLayout(dealer);
Box content
=
Box.createVerticalBox();
//
create a new vertical Box
Box registration
=
Box.createVerticalBox();
/** */
/**
**************************************************
* menu
* file
****************************************************
*/
JMenu fileMenu
=
new
JMenu(
"
File
"
);
JMenuItem exitItemOfFile
=
new
JMenuItem(
"
Exit
"
);
exitItemOfFile.addActionListener(
this
);
fileMenu.add(exitItemOfFile);
/** */
/**
* menu
* settings
*/
JMenu settingsMenu
=
new
JMenu(
"
Settings
"
);
JMenuItem registrationInfo
=
new
JMenuItem(
"
Registration Info
"
);
registrationInfo.addActionListener(
this
);
settingsMenu.add(registrationInfo);
scientificMode
=
new
JCheckBoxMenuItem(
"
Scientific Mode
"
);
scientificMode.addActionListener(
this
);
settingsMenu.add(scientificMode);
JMenuBar jMenuBar
=
new
JMenuBar();
jMenuBar.add(fileMenu);
jMenuBar.add(settingsMenu);
setJMenuBar(jMenuBar);
/** */
/**
**************************************************
* textFiled panel
****************************************************
*/
result
=
new
JTextField(
"
0
"
);
//
the initiated value of the answer
result.setBackground(Color.CYAN);
//
set the back ground color with cyan
result.setHorizontalAlignment(JTextField.RIGHT);
//
set the horizontal alignment
content.add(result);
content.add(paintButtons());
deckPanel.add(
"
cal
"
, content);
//
add the calculators card to the layout
registration.add(paintRegistration());
//
add the register window
deckPanel.add(
"
reg
"
, registration);
//
add the register window to the layout
contentPre.add(deckPanel);
//
add the cards to the container
}
/** */
/**
* paint the buttons of the two models.
*
*/
public
Box paintButtons()
{
/** */
/**
**************************************************
* Buttons
****************************************************
*/
/** */
/**
* line1
*/
vPanel1
=
Box.createVerticalBox();
//
add the backwards's button
JButton backwards
=
new
JButton(
"
1/X
"
);
backwards.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
backwards.addActionListener(
this
);
vPanel1.add(backwards);
//
add the factorial button
JButton factorial
=
new
JButton(
"
X!
"
);
factorial.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
factorial.addActionListener(
this
);
vPanel1.add(factorial);
//
add the square's button
JButton square
=
new
JButton(
"
<html>X<sup>2</sup></html>
"
);
square.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
square.addActionListener(
this
);
square.setActionCommand(
"
sqr
"
);
vPanel1.add(square);
//
add the square root's button
JButton squareRoot
=
new
JButton(
"
\u221a
"
);
squareRoot.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
squareRoot.addActionListener(
this
);
vPanel1.add(squareRoot);
//
add the power's button
JButton power
=
new
JButton(
"
<html>X<sup>Y</sup></html>
"
);
power.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
power.addActionListener(
this
);
power.setActionCommand(
"
pow
"
);
vPanel1.add(power);
/** */
/**
* line2
*/
Box vPanel2
=
Box.createVerticalBox();
//
add the modular button
mod
=
new
JButton(
"
Mod
"
);
mod.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mod.addActionListener(
this
);
vPanel2.add(mod);
//
add the seven button
JButton seven
=
new
JButton(
"
7
"
);
seven.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
seven.addActionListener(
this
);
vPanel2.add(seven);
//
add the four button
JButton four
=
new
JButton(
"
4
"
);
four.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
four.addActionListener(
this
);
vPanel2.add(four);
//
add the one button
JButton one
=
new
JButton(
"
1
"
);
one.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
one.addActionListener(
this
);
vPanel2.add(one);
//
add the zero button
JButton zero
=
new
JButton(
"
0
"
);
zero.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
zero.addActionListener(
this
);
vPanel2.add(zero);
/** */
/**
* line3
*/
Box vPanel3
=
Box.createVerticalBox();
//
add the Yth root of X button
xey
=
new
JButton(
"
XeY
"
);
xey.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
xey.addActionListener(
this
);
vPanel3.add(xey);
//
add the eight button
JButton eight
=
new
JButton(
"
8
"
);
eight.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
eight.addActionListener(
this
);
vPanel3.add(eight);
//
add the five button
JButton five
=
new
JButton(
"
5
"
);
five.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
five.addActionListener(
this
);
vPanel3.add(five);
//
add the two button
JButton two
=
new
JButton(
"
2
"
);
two.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
two.addActionListener(
this
);
vPanel3.add(two);
//
add the dot button
JButton dot
=
new
JButton(
"
.
"
);
dot.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
dot.addActionListener(
this
);
vPanel3.add(dot);
/** */
/**
* line4
*/
Box vPanel4
=
Box.createVerticalBox();
//
add the MS button
ms
=
new
JButton(
"
MS
"
);
ms.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
ms.addActionListener(
this
);
vPanel4.add(ms);
//
add the nine button
JButton nine
=
new
JButton(
"
9
"
);
nine.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
nine.addActionListener(
this
);
vPanel4.add(nine);
//
add the six button
JButton six
=
new
JButton(
"
6
"
);
six.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
six.addActionListener(
this
);
vPanel4.add(six);
//
add the three button
JButton three
=
new
JButton(
"
3
"
);
three.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
three.addActionListener(
this
);
vPanel4.add(three);
//
add the plusMinus button
JButton plusMinus
=
new
JButton(
"
\u00b1
"
);
plusMinus.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plusMinus.addActionListener(
this
);
vPanel4.add(plusMinus);
/** */
/**
* line5
*/
Box vPanel5
=
Box.createVerticalBox();
//
add the MR button
mr
=
new
JButton(
"
MR
"
);
mr.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mr.addActionListener(
this
);
vPanel5.add(mr);
//
add the division button
JButton division
=
new
JButton(
"
\u00F7
"
);
division.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
division.addActionListener(
this
);
vPanel5.add(division);
//
add the multiplication button
JButton multiplication
=
new
JButton(
"
\u00d7
"
);
multiplication.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
multiplication.addActionListener(
this
);
vPanel5.add(multiplication);
//
add the subtract button
JButton subtract
=
new
JButton(
"
-
"
);
subtract.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
subtract.addActionListener(
this
);
vPanel5.add(subtract);
//
add the plus button
JButton plus
=
new
JButton(
"
+
"
);
plus.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
plus.addActionListener(
this
);
vPanel5.add(plus);
/** */
/**
* line6
*/
Box vPanel6
=
Box.createVerticalBox();
//
add the MC button
mc
=
new
JButton(
"
MC
"
);
mc.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
mc.addActionListener(
this
);
vPanel6.add(mc);
//
add the C button
JButton c
=
new
JButton(
"
C
"
);
c.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
c.addActionListener(
this
);
vPanel6.add(c);
//
add a vertical strut
Component verticalStrut
=
Box.createVerticalStrut(BUTTON_HEIGHT);
vPanel6.add(verticalStrut);
//
add the enter button
JButton enter
=
new
JButton(
"
=
"
);
enter.setMaximumSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT
*
2
));
enter.setPreferredSize(
new
Dimension(BUTTON_WIDTH, BUTTON_HEIGHT
*
2
));
enter.addActionListener(
this
);
vPanel6.add(enter);
/** */
/**
* Buttons panel
*/
Box buttonsPanel
=
Box.createHorizontalBox();
buttonsPanel.add(vPanel1);
buttonsPanel.add(vPanel2);
buttonsPanel.add(vPanel3);
buttonsPanel.add(vPanel4);
buttonsPanel.add(vPanel5);
buttonsPanel.add(vPanel6);
/** */
/**
********************************************************
* the initial state is normal calculator
**********************************************************
*/
vPanel1.setVisible(
false
);
mod.setVisible(
false
);
xey.setVisible(
false
);
ms.setVisible(
false
);
mr.setVisible(
false
);
mc.setVisible(
false
);
return
buttonsPanel;
}
/** */
/**
* paint the registration window.
*
*/
public
Box paintRegistration()
{
Box registration
=
Box.createVerticalBox();
/**/
/*
* title
*/
JLabel titleRegistration
=
new
JLabel(
"
Bingbing's Calculator Registration
"
);
registration.add(titleRegistration);
/**/
/*
* information
*/
JPanel information
=
new
JPanel();
information.setLayout(
new
GridLayout(
6
,
2
));
//
Name
JLabel name
=
new
JLabel(
"
Name:
"
);
nameField
=
new
JTextField();
information.add(name);
information.add(nameField);
//
Street Address
JLabel streetAddress
=
new
JLabel(
"
Street Address:
"
);
streetAddressField
=
new
JTextField();
information.add(streetAddress);
information.add(streetAddressField);
//
City
JLabel city
=
new
JLabel(
"
City:
"
);
cityField
=
new
JTextField();
information.add(city);
information.add(cityField);
//
State
JLabel state
=
new
JLabel(
"
State:
"
);
stateField
=
new
JTextField();
information.add(state);
information.add(stateField);
//
Zip
JLabel zip
=
new
JLabel(
"
Zip:
"
);
zipField
=
new
JTextField();
information.add(zip);
information.add(zipField);
//
Country
JLabel country
=
new
JLabel(
"
Country:
"
);
countryField
=
new
JTextField();
information.add(country);
information.add(countryField);
registration.add(information);
/**/
/*
* Registration Code
*/
Box registrationCode
=
Box.createVerticalBox();
JPanel registrationCodePanel
=
new
JPanel();
registrationCodePanel.setLayout(
new
FlowLayout());
//
registrationCodePanel.setTitle("Registration Code");
registrationCodePanel.setBorder(
new
LineBorder(Color.red,
1
));
registrationCode.add(registrationCodePanel);
first
=
new
JTextField(
3
);
registrationCodePanel.add(first);
JLabel rail1
=
new
JLabel(
"
-
"
);
registrationCodePanel.add(rail1);
second
=
new
JTextField(
3
);
registrationCodePanel.add(second);
JLabel rail2
=
new
JLabel(
"
-
"
);
registrationCodePanel.add(rail2);
third
=
new
JTextField(
3
);
JLabel rail3
=
new
JLabel(
"
-
"
);
registrationCodePanel.add(third);
registrationCodePanel.add(rail3);
fourth
=
new
JTextField(
3
);
registrationCodePanel.add(fourth);
/**/
/*
* buttons
*/
JPanel buttonsReg
=
new
JPanel();
buttonsReg.setLayout(
new
FlowLayout());
jButtonR
=
new
JButton(
"
Register
"
);
jButtonE
=
new
JButton(
"
Edit
"
);
JButton jButtonRe
=
new
JButton(
"
Return
"
);
jButtonR.addActionListener(
this
);
jButtonE.addActionListener(
this
);
jButtonRe.addActionListener(
this
);
buttonsReg.add(jButtonR);
buttonsReg.add(jButtonE);
buttonsReg.add(jButtonRe);
registrationCode.add(buttonsReg);
Box registrationAndPic
=
Box.createHorizontalBox();
registrationAndPic.add(registrationCode);
jButtonE.setVisible(
false
);
/**/
/*
* image
*/
JLabel imageLabel
=
new
JLabel();
ImageIcon greatwall
=
new
ImageIcon(
"
greatwall.jpg
"
);
imageLabel.setIcon(greatwall);
registrationAndPic.add(imageLabel);
registration.add(registrationAndPic);
return
registration;
}
/** */
/**
* performe the action which sent by the window.
*
*
@param
e catch the action event of the window.
*/
public
void
actionPerformed(ActionEvent e)
{
String actionCommand
=
e.getActionCommand();
//
exit the system
if
(actionCommand.equals(
"
Exit
"
))
System.exit(
0
);
//
return to the calculator
else
if
(actionCommand.equals(
"
Return
"
))
{
dealer.show(deckPanel,
"
cal
"
);
setTitle(
"
Calculator
"
);
validate();
pack();
}
//
check the register information
else
if
(actionCommand.equals(
"
Register
"
))
{
long
check
=
0
;
Boolean valid
=
true
;
if
(nameField.getText().trim().length()
!=
0
)
{
check
=
calChecksum(nameField.getText().trim());
if
(check
%
3
!=
Long.parseLong(first.getText().trim()))
valid
=
false
;
else
if
(check
%
5
!=
Long.parseLong(second.getText().trim()))
valid
=
false
;
else
if
(check
%
7
!=
Long.parseLong(third.getText().trim()))
valid
=
false
;
else
if
(check
%
11
!=
Long.parseLong(fourth.getText().trim()))
valid
=
false
;
//
if the information is valid, we will change the buttons
//
and set the text field uneditable.
if
(valid)
{
jButtonE.setVisible(
true
);
jButtonR.setVisible(
false
);
nameField.setEditable(
false
);
countryField.setEditable(
false
);
zipField.setEditable(
false
);
stateField.setEditable(
false
);
cityField.setEditable(
false
);
streetAddressField.setEditable(
false
);
first.setEditable(
false
);
second.setEditable(
false
);
third.setEditable(
false
);
fourth.setEditable(
false
);
validate();
}
}
}
//
make the text field editable
else
if
(actionCommand.equals(
"
Edit
"
))
{
jButtonE.setVisible(
false
);
jButtonR.setVisible(
true
);
nameField.setEditable(
true
);
countryField.setEditable(
true
);
zipField.setEditable(
true
);
stateField.setEditable(
true
);
cityField.setEditable(
true
);
streetAddressField.setEditable(
true
);
first.setEditable(
true
);
second.setEditable(
true
);
third.setEditable(
true
);
fourth.setEditable(
true
);
validate();
}
//
turn to registration window
else
if
(actionCommand.equals(
"
Registration Info
"
))
{
dealer.show(deckPanel,
"
reg
"
);
setTitle(
"
Registration
"
);
validate();
pack();
}
//
turn to scientific model
else
if
(actionCommand.equals(
"
Scientific Mode
"
))
{
dealer.show(deckPanel,
"
cal
"
);
vPanel1.setVisible(scientificMode.isSelected());
mod.setVisible(scientificMode.isSelected());
xey.setVisible(scientificMode.isSelected());
ms.setVisible(scientificMode.isSelected());
mr.setVisible(scientificMode.isSelected());
mc.setVisible(scientificMode.isSelected());
if
(scientificMode.isSelected())
setTitle(
"
Scientific Model
"
);
else
setTitle(
"
Normal Model
"
);
validate();
pack();
}
//
post the result
else
if
(actionCommand.equals(
"
=
"
))
{
//
if the pre-operated command was "=", return the pre-step
if
(
!
preOperatedCommand.equals(
"
=
"
))
tail
=
Double.parseDouble(result.getText().trim());
//
if the two numbel are 0, we don't need calculate the result
if
(tail
!=
0.0
||
head
!=
0.0
)
{
//
division
if
(operatedCommand.equals(
"
\u00F7
"
))
{
if
(Math.abs(tail)
>
0.0000000000000000001
)
{
head
=
head
/
tail;
result.setText(Double.toString(head));
}
else
{
JOptionPane.showMessageDialog(
this
,
"
Cannot divide by zero.
"
);
head
=
0.0
;
tail
=
0.0
;
}
substitution
=
true
;
}
//
multiplacation
else
if
(operatedCommand.equals(
"
\u00d7
"
))
{
head
=
head
*
tail;
result.setText(Double.toString(head));
substitution
=
true
;
}
//
sub
else
if
(operatedCommand.equals(
"
-
"
))
{
head
=
head
-
tail;
result.setText(Double.toString(head));
substitution
=
true
;
}
//
plus
else
if
(operatedCommand.equals(
"
+
"
))
{
head
=
head
+
tail;
result.setText(Double.toString(head));
substitution
=
true
;
}
//
pow
else
if
(operatedCommand.equals(
"
pow
"
))
{
head
=
Math.pow(head, tail);
result.setText(Double.toString(head));
substitution
=
true
;
}
//
the Yth root of the X
else
if
(operatedCommand.equals(
"
XeY
"
))
{
head
=
Math.pow(head,
1
/
tail);
result.setText(Double.toString(head));
substitution
=
true
;
}
//
modular
else
if
(operatedCommand.equals(
"
Mod
"
))
{
head
=
head
%
tail;
result.setText(Double.toString(head));
substitution
=
true
;
}
}
preOperatedCommand
=
"
=
"
;
}
/** */
/**
***********************************************************
* set the action of the number
*************************************************************
*/
else
if
(actionCommand.equals(
"
0
"
))
{
if
(
!
(result.getText().equals(
"
0
"
)
||
result.getText().equals(
"
-0
"
)))
{
if
(substitution)
{
result.setText(
"
0
"
);
}
else
result.setText(result.getText()
+
"
0
"
);
}
}
else
if
(actionCommand.equals(
"
1
"
))
{
if
(substitution)
{
result.setText(
"
1
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
1
"
);
}
else
if
(actionCommand.equals(
"
2
"
))
{
if
(substitution)
{
result.setText(
"
2
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
2
"
);
}
else
if
(actionCommand.equals(
"
3
"
))
{
if
(substitution)
{
result.setText(
"
3
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
3
"
);
}
else
if
(actionCommand.equals(
"
4
"
))
{
if
(substitution)
{
result.setText(
"
4
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
4
"
);
}
else
if
(actionCommand.equals(
"
5
"
))
{
if
(substitution)
{
result.setText(
"
5
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
5
"
);
}
else
if
(actionCommand.equals(
"
6
"
))
{
if
(substitution)
{
result.setText(
"
6
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
6
"
);
}
else
if
(actionCommand.equals(
"
7
"
))
{
if
(substitution)
{
result.setText(
"
7
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
7
"
);
}
else
if
(actionCommand.equals(
"
8
"
))
{
if
(substitution)
{
result.setText(
"
8
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
8
"
);
}
else
if
(actionCommand.equals(
"
9
"
))
{
if
(substitution)
{
result.setText(
"
9
"
);
substitution
=
false
;
}
else
result.setText(result.getText()
+
"
9
"
);
}
else
if
(actionCommand.equals(
"
.
"
))
{
if
(result.getText().length()
==
0
)
result.setText(
"
0.
"
);
if
(
!
(result.getText().contains(
"
.
"
)))
result.setText(result.getText()
+
"
.
"
);
}
else
if
(actionCommand.equals(
"
\u00b1
"
))
{
if
(result.getText().charAt(
0
)
!=
'
-
'
)
result.setText(
"
-
"
+
result.getText());
else
result.setText(result.getText().substring(
1
));
}
else
if
(actionCommand.equals(
"
C
"
))
{
result.setText(
"
0
"
);
substitution
=
true
;
}
/** */
/**
***********************************************************
* set the action of the arithmetic
*************************************************************
*/
//
division
else
if
(actionCommand.equals(
"
\u00F7
"
))
{
head
=
Double.parseDouble(result.getText().trim());
operatedCommand
=
"
\u00F7
"
;
substitution
=
true
;
preOperatedCommand
=
"
\u00F7
"
;
}
//
multiplication
else
if
(actionCommand.equals(
"
\u00d7
"
))
{
head
=
Double.parseDouble(result.getText().trim());
operatedCommand
=
"
\u00d7
"
;
substitution
=
true
;
preOperatedCommand
=
"
\u00d7
"
;
}
//
sub
else
if
(actionCommand.equals(
"
-
"
))
{
head
=
Double.parseDouble(result.getText().trim());
operatedCommand
=
"
-
"
;
substitution
=
true
;
preOperatedCommand
=
"
-
"
;
}
//
plus
else
if
(actionCommand.equals(
"
+
"
))
{
head
=
Double.parseDouble(result.getText().trim());
operatedCommand
=
"
+
"
;
substitution
=
true
;
preOperatedCommand
=
"
+
"
;
}
//
backwards
else
if
(actionCommand.equals(
"
1/X
"
))
{
head
=
Double.parseDouble(result.getText().trim());
if
(head
!=
0
)
result.setText(Double.toString(
1
/
head));
else
{
JOptionPane.showMessageDialog(
this
,
"
Cannot divide by zero.
"
);
head
=
0.0
;
tail
=
0.0
;
}
}
//
factorial
else
if
(actionCommand.equals(
"
X!
"
))
{
head
=
Double.parseDouble(result.getText().trim());
result.setText(Double.toString(factorial(head)));
}
//
square
else
if
(actionCommand.equals(
"
sqr
"
))
{
head
=
Double.parseDouble(result.getText().trim());
result.setText(Double.toString(head
*
head));
}
//
square root
else
if
(actionCommand.equals(
"
\u221a
"
))
{
head
=
Double.parseDouble(result.getText().trim());
if
(head
>=
0.0
)
result.setText(Double.toString(Math.sqrt(head)));
else
{
JOptionPane.showMessageDialog(
this
,
"
Invalid input for function
"
);
head
=
0.0
;
tail
=
0.0
;
}
}
//
power
else
if
(actionCommand.equals(
"
pow
"
))
{
head
=
Double.parseDouble(result.getText().trim());
operatedCommand
=
"
pow
"
;
substitution
=
true
;
preOperatedCommand
=
"
pow
"
;
}
//
the Yth of the X
else
if
(actionCommand.equals(
"
XeY
"
))
{
head
=
Double.parseDouble(result.getText().trim());
operatedCommand
=
"
XeY
"
;
substitution
=
true
;
preOperatedCommand
=
"
XeY
"
;
}
//
modular
else
if
(actionCommand.equals(
"
Mod
"
))
{
head
=
Double.parseDouble(result.getText().trim());
operatedCommand
=
"
Mod
"
;
substitution
=
true
;
preOperatedCommand
=
"
Mod
"
;
}
//
MS
else
if
(actionCommand.equals(
"
MS
"
))
{
variableMemory
=
Double.parseDouble(result.getText().trim());
}
//
MR
else
if
(actionCommand.equals(
"
MR
"
))
{
result.setText(Double.toString(variableMemory));
}
//
MC
else
if
(actionCommand.equals(
"
MC
"
))
{
variableMemory
=
0.0
;
}
}
/** */
/**
* calculates and returns the factorial of the value
*
*
@param
value value of the root of the factorial
*
*/
public
double
factorial(
double
value)
{
if
(value
<=
1.0
)
return
value;
else
return
value
*
factorial(value
-
1
);
}
/** */
/**
* calculates and returns the check sum of the message.
*
*
@param
message message of the name whick entered by the user.
*/
public
long
calChecksum(String message)
{
long
check
=
0
;
for
(
int
i
=
0
;i
<
message.length();i
++
)
check
+=
message.charAt(i);
return
check;
}
public
static
void
main(String[] args)
{
Calculator calculator
=
new
Calculator();
calculator.pack();
calculator.setVisible(
true
);
}
}
posted on
2007-03-06 14:07
蓝色代码
阅读(
892
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 蓝色代码
Powered by .NET 8.0 on Kubernetes