📄 mycalc.java
字号:
convertNumsToDouble() ;
answer = Math.pow( dNum1 , dNum2 ) ;
setResults() ;
MyPrint( "1c- powerOp: The value of answer is " + answer +
" The value of opCode is " + opCode );
}
}
else {
if ( checkFirstNum() ) {
MyPrint( "1d- powerOp: The value of answer is " + answer +
" The value of opCode is " + opCode );
pendingOpCode = 4 ;
}
}
opCode = 4 ;
} /** ********** End of powerOp() **************/
/**
* divOp()
* Version 1.1
* Changed 9/10/2003 - Fixed chained operation errors
*/
private void multOp() {
if ( pendingOp ) {
MyPrint( "1a- multOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
opCodeMethod() ;
opCode = -1 ;
}
if ( !firstNumSet ) {
pendingOp = true ;
if ( opCode == 99 ) {
firstNum = "" + answer ;
firstNumSet = true ;
MyPrint( "1b- multOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
}
if ( checkFirstNum() ) {
opCode = 0 ;
MyPrint( "1c- multOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
pendingOpCode = 0 ;
}
}
else if ( opCode == 0 ) {
pendingOp = false ;
secondNum = enterBox.getText() ;
pendingOp = true ;
MyPrint( "2a- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
if ( secondNum.equals( "" ) ) {
secondNum = "1.0" ;
MyPrint( "2b- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
}
MyPrint( "3a- multOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 * dNum2 ;
setResults() ;
MyPrint( "3b- multOp(): opCode is " + opCode + " The value of answer is " + answer );
dNum1 = answer ;
secondNum = "0.0" ;
secondNumSet = true ;
}
else {
pendingOp = true ;
pendingOpCode = 0 ;
opCode = 0 ;
result = true ;
MyPrint( "3a- multOp(): opCode is " + opCode + " The value of answer is " + answer );
}
} /** ********** End of multOp() **************/
/** /**
* divOp() - divides the two entered numbers and checks for a zero divisor
* Version 1.1
* Changed 9/10/2003 - Fixed chained operation errors
*/
private void divOp() {
MyPrint( "1a- divOp(): opCode is " + opCode + " firstNumSet =s " + firstNumSet );
if ( pendingOp ) {
MyPrint( "1aa- divOp(): opCode is " + opCode );
opCodeMethod() ;
opCode = -1 ;
}
if ( !firstNumSet ) {
pendingOp = true ;
if ( opCode == 99 ) {
firstNum = "" + answer ;
firstNumSet = true ;
MyPrint( "1b- divOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
}
if ( checkFirstNum() ) {
MyPrint( "1c- divOp(): opCode is " + opCode + " The value of firstNum is " + firstNum );
pendingOpCode = 1 ;
opCode = 1 ;
}
}
else if ( firstNumSet ) {
if ( pendingOp ) {
opCodeMethod() ;
opCode = 1 ;
}
secondNum = enterBox.getText() ;
MyPrint( "2a- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
if ( secondNum.equals( "" ) || opCode < 0 ) {
MyPrint( "2b- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
}
else {
convertNumsToDouble() ;
if (dNum2 != 0.0 ) {
answer = dNum1 / dNum2 ;
setResults() ;
MyPrint( "2- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
}
else {
JOptionPane.showMessageDialog( this, "The number " +
dNum2 + " cannot be used as a denometer" ,
"Attempted to Divide by Zero",
JOptionPane.ERROR_MESSAGE );
firstNumSet = false ;
firstNum = "" ;
opCode = -1 ;
}
}
opCode = 1 ;
}
//enterBox.setText( "" ) ;
result = true ;
} /** ********** End of divOp() **************/
/** subOp() - subtracts the second number from the first number
*
* Version 1.1
* Changed 9/10/2003 - Fixed chained operation errors
* */
private void subOp() {
if ( pendingOp ) {
MyPrint( "subOp() 1a: In pendingOp code. The opcode value is " + opCode ) ;
opCodeMethod() ;
opCode = 2 ;
pendingOp = true ;
pendingOpCode = 2 ;
}
else if( !firstNumSet ) {
firstNum = enterBox.getText() ;
pendingOp = true ;
pendingOpCode = 2 ;
opCode = 2 ;
firstNumSet = true ;
result = true ;
MyPrint( "1b- subOp()-checkFirstNum(): opCode is " + opCode + " The value of answer is " + answer );
}
else if ( opCode == 2 ) {
pendingOp = false ;
if ( secondNum.equals( "0.0" ) ) {
firstNum = "" + answer ;
}
secondNum = enterBox.getText() ;
if ( secondNum.equals( "" ) ) {
secondNum = "0.0" ;
}
MyPrint( "2a- subOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 - dNum2 ;
setResults() ;
MyPrint( "2b- subOp(): opCode is " + opCode + " The value of answer is " + answer );
secondNum = "0.0" ;
opCode = -1 ;
secondNumSet = true ;
}
else {
pendingOp = true ;
pendingOpCode = 2 ;
opCode = 2 ;
result = true ;
MyPrint( "3a- subOp(): opCode is " + opCode + " The value of answer is " + answer );
}
} /** ********** End of subOp() **************/
/**
* plusOp() - adds the two entered numbers
* Version 1.1
* Changed 9/10/2003 - Fixed chained operation errors
*/
private void plusOp() {
if ( pendingOp ) {
MyPrint( "subOp() 1a: In pendingOp code. The opcode value is " + opCode ) ;
opCodeMethod() ;
opCode = 3 ;
pendingOp = true ;
pendingOpCode = 3 ;
}
else if( !firstNumSet ) {
firstNum = enterBox.getText() ;
pendingOp = true ;
pendingOpCode = 3 ;
opCode = 3 ;
firstNumSet = true ;
result = true ;
MyPrint( "1b- plusOp()-checkFirstNum(): opCode is " + opCode + " The value of answer is " + answer );
}
else if ( opCode == 3 ) {
pendingOp = false ;
MyPrint( "2aa- plusOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
if ( secondNum.equals( "0.0" ) ) {
firstNum = "" + answer ;
}
secondNum = enterBox.getText() ;
if ( secondNum.equals( "" ) ) {
secondNum = "0.0" ;
}
MyPrint( "2a- plusOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 + dNum2 ;
setResults() ;
MyPrint( "2b- plusOp(): opCode is " + opCode + " The value of answer is " + answer );
secondNum = "0.0" ;
opCode = -1 ;
secondNumSet = true ;
}
else {
pendingOp = true ;
pendingOpCode = 3 ;
opCode = 3 ;
result = true ;
MyPrint( "3a- plusOp(): opCode is " + opCode + " The value of answer is " + answer );
}
} /** ********** End of plusOp() **************/
/** convertNumsToDouble() - converts numbers entered from string to double */
private void convertNumsToDouble() {
if ( !firstNum.equals( "" ) )
dNum1 = Double.parseDouble( firstNum ) ;
if ( !secondNum.equals( "" ) )
dNum2 = Double.parseDouble( secondNum ) ;
MyPrint( "convertNumsToDouble(): The value of dNum1 is " + dNum1 );
MyPrint( "convertNumsToDouble(): The value of dNum2 is " + dNum2 );
} /** ********** End of convertNumsToDouble() **************/
/** PlusMinus() - toggles the sign of the entered number */
private void PlusMinus() {
MyPrint( "PlusMinus: The value of enterBox is " + enterBox.getText() );
if ( !enterBox.getText().equals( "" ) ) {
answer = Double.parseDouble( enterBox.getText() ) * minusOne ;
MyPrint( "1a- PlusMinus: The value of answer is " + answer );
setResults() ;
}
else if ( !firstNum.equals( "" ) ) {
answer = Double.parseDouble( firstNum ) * minusOne ;
setResults() ;
MyPrint( "1b- PlusMinus: The value of firstNum is " + firstNum );
}
result = true ;
} /** ********** End of PlusMinus() **************/
/** MySqrt() - computes the square root of the entered number */
private void MySqrt() {
MyPrint( "1a- MySqrt(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( opCode == 99 ) {
answer = Math.sqrt( answer ) ;
setResults() ;
}
else if ( !enterBox.getText().equals( "" ) ) {
MyPrint( "1b- MySqrt(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( pendingOp )
opCodeMethod() ;
answer = Double.parseDouble( enterBox.getText() ) ;
answer = Math.sqrt( answer ) ;
setResults() ;
}
else if ( !firstNum.equals( "" ) ) {
MyPrint( "1c- MySqrt(): opCode is " + opCode + " firstNum is " + firstNum ) ;
answer = Double.parseDouble( firstNum ) ;
answer = Math.sqrt( answer ) ;
setResults() ;
}
//opCode = -1 ;
} /** ********** End of MySqrt() **************/
/** OneDivX() - computes the value of one divided by the entered number */
private void OneDivX() {
MyPrint( "1a- OneDivX(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( opCode == 99 ) {
answer = ( 1.0 / answer ) ;
setResults() ;
}
else if ( !enterBox.getText().equals( "" ) ) {
answer = Double.parseDouble( enterBox.getText() ) ;
answer = ( 1.0 / answer ) ;
setResults() ;
}
else if ( firstNumSet ) {
answer = Double.parseDouble( firstNum ) ;
answer = ( 1.0 / answer ) ;
setResults() ;
}
}
/** ********** End of OneDivX() **************/
/** sysExit() - causes the application to exit */
private void sysExit() {
System.exit( 0 );
} /** ********** End of sysExit() **************/
/** MyPrint() - toggles debug aid on or off*/
private void MyPrint( String str ) {
if ( myDiag )
System.out.println( str );
} /** ********** End of MyPrint() **************/
/** main() */
public static void main( String args[] ) {
final MyCalc app = new MyCalc () ; /** creates an instance of MyCalc */
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
app.sysExit() ;
}
}
);
}
/** ********** End of main() **************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -