📄 calculator.java
字号:
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -