A pair of Friends: Chen Shu-yun http://www.cnblogs.com/babybluecsj/
II. (1) I used my friend's last 5.1 job for unit testing. I found that when the divisor is zero, no exception was thrown during the operation, so we added
1. if
(fh2==
‘/‘
){
if
(b==
0
){
throw
new
Exception(
"The divisor cannot be 0! "
);
}
answer=a/b;
}
2.
try
{
res=Calculator.Onefhnum(
1
,
1
,
3
);
}
catch
(Exception e) {
e.printStackTrace();
Assert.fail(
"Throw an exception and test failed! "
);
}
This code; (2). The Code of the last job is also found to be incorrect. The last one of the three operators is the divisor, but it is not written:
If (fh2 = '/') // The first is except {If (fh3 = '+') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B + C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B + c-d;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B + C * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B + C/d;} If (fh3 = '-') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B-C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = a/B-c-d;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = a/B-c * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B-C/d;} If (fh3 = '*') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B * C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B * c-d;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} Answer = A/B * C * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B * C/d;} If (fh3 = '/') {If (fh4 = '+ ') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B/C + D;} If (fh4 = '-') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B/C-D;} If (fh4 = '*') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} Answer = A/B/C * D;} If (fh4 = '/') {If (B = 0) {Throw new exception ("the divisor cannot be 0! ");} If (C = 0) {Throw new exception (" the divisor cannot be 0! ");} If (D = 0) {Throw new exception (" the divisor cannot be 0! ") ;}Answer = A/B/C/D ;}} return answer ;}
(3) Here, we also add the exact decimal point of the answer:
static
double
answer=
0
.0d;
public
static
double
Onefhnum(
int
a,
int
b,
int
e)
throws
Exception{
// Method of an operator
public
static
double
Twofhnum(
int
a,
int
b,
int
c,
int
e,
int
h)
throws
Exception{
// Two operators
public
static
double
Threefhnum(
int
a,
int
b,
int
c,
int
d,
int
e,
int
h,
int
f)
throws
Exception{
// Methods of the three operators
(4). Due to time, the following code is added:int
precision =
0
;
if
(NUM_MAX_OPERATOR == precision) {
// Here I can process strings with a maximum of three operators :)
}
else
if
(NUM_SCALE == precision) {
// Here I can process a string with a value range of-100 to 100 :)
}
else
if
(NUM_PRECISION == precision) {
// Here I can handle strings whose precision is the first digit after the decimal point :)
}
After this code, you do not know how to add or process information in parentheses.
Four arithmetic testing and encapsulation (improved) 5.2 5.3