📄 mycalc.java
字号:
MyPrint( "The Div button was pressed. And pendingOp is " + pendingOp );
divOp() ;
}
else if ( e.getSource() == bSub ) {
MyPrint( "The Sub button was pressed. And pendingOp is " + pendingOp );
subOp() ;
}
else if ( e.getSource() == bPlus ) {
MyPrint( "The Plus button was pressed. And pendingOp is " + pendingOp );
plusOp() ;
}
else if ( e.getSource() == bCEnt ) {
MyPrint( "The CEnt button was pressed." );
enterBox.setText( "" ) ;
}
else if ( e.getSource() == bSqrt ) {
MyPrint( "The bSqrt button was pressed. And pendingOp is " + pendingOp );
MySqrt() ;
}
else if ( e.getSource() == bOneDivX ) {
MyPrint( "The bOneDivX button was pressed. And pendingOp is " + pendingOp );
OneDivX() ;
}
else if ( e.getSource() == bCAll ) {
MyPrint( "The CAll button was pressed." );
enterBox.setText( "" ) ;
firstNum = "" ;
firstNumSet = false ;
secondNum = "" ;
answer= 0.0 ;
opCode = -1 ;
}
else if ( e.getSource() == bEquals ) {
MyPrint( "The Equals button was pressed. And pendingOp is " + pendingOp +
" The opCode value is " + opCode );
if ( pendingOp ) {
opCodeMethod() ;
opCode = -1 ;
}
secondNum = enterBox.getText() ;
opCodeMethod() ;
firstNum = "" + answer ;
result = true ;
}
else if ( e.getSource() == bPlusMinus ) {
MyPrint( "The PlusMinus button was pressed." );
opCode = -1 ;
PlusMinus() ;
}
else if ( e.getSource() == bExp ) {
MyPrint( "The Power button was pressed." );
secondNumSet = false ;
powerOp() ;
}
else if ( e.getSource() == bMPlus ) {
MyPrint( "1a: The MPlus button was pressed. pendingOp =s " + pendingOp);
if ( dArrayPtr < 10 ) { /** array hold 10 entries */
if ( !pendingOp ) {
if ( opCode == 99 ) {
//enterBox.setText( "" );
MyPrint( "1b: The value of opCode is " + opCode +
" firstNum =s " + firstNum + " answer =s " + answer );
pendingOpCode = -1 ;
firstNumSet = true ;
firstNum = "" + answer ;
dArray [ dArrayPtr ] =
Double.parseDouble( "" + answer ) ;
dArrayPtr++ ;
enterBox.setText( firstNum ) ;
MyPrint( "1c: The value of dArrayPtr is " + dArrayPtr );
}
else {
/** This code is entered if the =s button was not just pressed */
firstNum = "" + enterBox.getText( );
firstNumSet = true ;
dArray [ dArrayPtr ] =
Double.parseDouble( "" + firstNum ) ;
dArrayPtr++ ;
MyPrint( "1d: The value of dArrayPtr is " + dArrayPtr );
}
}
else if ( !enterBox.getText().equals( "" ) ) {
dArray [ dArrayPtr ] =
Double.parseDouble( enterBox.getText() ) ;
dArrayPtr++ ;
firstNumSet = true ;
firstNum = "" + answer ;
MyPrint( "1d: The value of dArrayPtr is " + dArrayPtr );
}
else {
JOptionPane.showMessageDialog( this, "A number must be entered befor clicking the M+ button" ,
"Attempted to store a blank number.",
JOptionPane.ERROR_MESSAGE );
}
}
result = true ;
}
else if ( e.getSource() == bMMinus ) {
/** This code retrieves values from the array stack */
MyPrint( "The MMinus button was pressed." ) ;
if ( dArrayPtr > 0 ) {
enterBox.setText( "" + dArray [ dArrayPtr-1 ] ) ;
firstNum = "" + answer ;
answer = dArray [ dArrayPtr-1 ] ;
dArrayPtr-- ;
MyPrint( "1a: The retrieved value is " + answer ) ;
}
else {
enterBox.setText( "" ) ;
MyPrint( "1b: There are no more values to retrieve." ) ;
JOptionPane.showMessageDialog( this, "There are no more values to retrieve using M-." ,
"No More Values.",
JOptionPane.ERROR_MESSAGE );
}
/**
* Code changed on 9/10/2003 to allow result to be a
* first operand.
*/
if ( pendingOp ) {
secondNumSet = true ;
secondNum = "" + answer ;
}
else {
MyPrint( "1a- MMinus: The value of answer is " + answer );
secondNum = "" ;
opCode = -1 ;
dNum2 = 0.0 ;
dNum1 = answer ;
secondNumSet = false ;
firstNum = "" + answer ;
firstNumSet = true ;
pendingOp = false ;
enterBox.setText( "" + answer ) ;
}
}
else if ( e.getSource() == bHelp ) {
MyPrint( "The Help button was pressed." );
File hd = new File("help_doc.html");
//File net = new File("Netscp.exe");
String helpStr = hd.getAbsolutePath() ;
Runtime rt = Runtime.getRuntime();
/* String[] callAndArgs = { "c:\\Program Files\\Internet Explorer\\IExplore.exe" ,
"" +
E:\MyExamples\SimpleCalc\Calc\help_doc.html }; */
String[] callAndArgs = { "c:\\Program Files\\Internet Explorer\\IExplore.exe" ,
"" + helpStr };
try {
Process child = rt.exec( callAndArgs );
child.waitFor();
MyPrint ("Process exit code is: " +
child.exitValue());
}
catch(IOException e2) {
MyPrint (
"IOException starting process!");
}
catch(InterruptedException e3) {
System.err.println(
"Interrupted waiting for process!");
}
}
else if ( e.getSource() == bFact ) {
MyPrint( "1a- bFact(): opCode is " + opCode + " firstNum is " + firstNum ) ;
if ( opCode == 99 ) {
tempInt = ( int ) answer ;
}
else if ( !enterBox.getText().equals( "" ) ) {
MyPrint( "1b- bFact(): The value of enterBox.getText() is " + enterBox.getText() );
tempInt = Integer.parseInt( enterBox.getText() ) ;
MyPrint( "1c- bFact(): The value of tempInt is " + tempInt );
}
else if ( firstNumSet ) {
MyPrint( "1d- bFact() The value of firstNum is " + firstNum ) ;
tempInt = (int)Double.parseDouble( firstNum ) ;
MyPrint( "1e- bFact(): The value of tempInt is " + tempInt );
}
MyPrint( "1f- bFact(): The value of tempInt is " + tempInt );
tempInt2 = 1 ;
for( int ii = 1 ; ii < tempInt + 1; ii++ ) {
tempInt2 *= ii ;
}
enterBox.setText( "" + tempInt2 ) ;
/**
* Code changed on 9/10/2003 to allow result to be a
* first operand.
*/
answer = (double)tempInt2 ;
MyPrint( "1g- factorial: The value of answer is " + answer );
secondNum = "" ;
opCode = -1 ;
dNum2 = 0.0 ;
dNum1 = answer ;
secondNumSet = false ;
firstNum = "" + answer ;
firstNumSet = true ;
pendingOp = false ;
}
else if ( e.getSource() == bAbout ) {
MyPrint( "The About button was pressed." );
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = { "c:\\Program Files\\Internet Explorer\\IExplore.exe" ,
"http://sumtotalz.com/TotalAppsWorks/index.html " };
try {
Process child = rt.exec(callAndArgs);
child.waitFor();
MyPrint ("Process exit code is: " +
child.exitValue());
}
catch(IOException e2) {
MyPrint( "IOException starting process!");
}
catch(InterruptedException e3) {
MyPrint( "Interrupted waiting for process!");
}
}
} /** ***** End of actionPerformed() ****************/
/** checkFirstNum - checks for a number and ensures that it is not null */
private boolean checkFirstNum( ) {
boolean retCode = false ;
if ( !firstNumSet ) {
if ( !enterBox.getText().equals( "" ) ) {
firstNum = enterBox.getText() ;
firstNumSet = true ;
result = true ;
retCode = true ;
MyPrint( "1a- In checkFirstNum( ) -- firstNum =s " + firstNum);
}
}
else if ( !firstNum.equals( "" ) ) {
result = true ;
retCode = true ;
MyPrint( "1b- In checkFirstNum( ) firstNumSet is " + firstNumSet );
}
return retCode ;
} /** ***** End of checkFirstNum() ****************/
/** setResults() - enters the result of an operation in the textbox and the firstNum */
private void setResults() {
enterBox.setText( "" + answer );
firstNum = "" + answer ;
firstNumSet = true ;
result = true ;
pendingOp = false ;
} /** ***** End of checkFirstNum() ****************/
/** opCodeMethod() - determines if there is a pending op to perform
* - if so, that operation is performed
* - if not, the current operation is performed
* */
private void opCodeMethod() {
int currentOpCode = -1 ;
MyPrint( "In opCodeMethod() with an opCode of " + opCode );
/** Check if there is a pending operation that must
* be performed before the current operation is
* performed. */
if ( pendingOp ) {
currentOpCode = pendingOpCode ;
pendingOp = false ;
}
else {
currentOpCode = opCode ;
}
switch ( currentOpCode ) {
case 0 :
multOp() ;
break ;
case 1 :
divOp() ;
break ;
case 2 :
MyPrint( "opCodeMethod(): opCode =s " + currentOpCode ) ;
subOp() ;
break ;
case 3 :
MyPrint( "opCodeMethod(): opCode =s " + currentOpCode ) ;
plusOp() ;
break ;
case 4 :
MyPrint( "Calling PowerOp method." );
MyPrint( "Case 4: the value of firstNum is " +
firstNum + " the value of secondNum is " +
secondNum ) ;
secondNumSet = true ;
powerOp() ;
break ;
case 5 :
break ;
default :
setResults() ;
firstNumSet = true ;
secondNum = "0.0" ;
dNum2 = 0.0 ;
pendingOp = false ;
opCode = 99 ;
MyPrint( "Case default: firstNum =s " + firstNum);
break ;
}
if ( !( opCode == 99 ) )
opCode = -1 ;
} /** ********** End of opCodeMethod() **************/
/** powerOp() - raises the first number to the power of the second */
private void powerOp() {
MyPrint( "1a- powerOp: The value of firstNum is " + firstNum +
" The value of secondNum is " + secondNum
+
" The value of answer is " + answer );
if ( opCode == 99 ) {
setResults() ;
}
if ( firstNumSet ) {
if ( pendingOp ) {
opCodeMethod() ;
pendingOp = false ;
secondNumSet = false ;
}
if ( secondNumSet ) {
MyPrint( "1b- powerOp: The value of firstNum is " + firstNum +
" The value of secondNum is " + secondNum
+
" The value of answer is " + answer );
secondNum = enterBox.getText() ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -