程序填空题:Int Operator
This program reads a line of prefix integer expression with one leading intergal operator and two integer values, and evaluates the result. A prefix integer expression is like:
```
add 5 3
```
The result of the expression above is: `8`.
Now, fill in the blanks:
```Java
import java.util.Scanner;
enum IntOp {
@@[add()](1) { // for operator add
int eval(int i1, int i2) {
return @@[i1+i2](1);
}
},
@@[ sub()](1) { // for operator sub
int eval(int i1, int i2) {
return @@[i1-i2](1);
}
};
int eval(int i1, int i2) {
return 0;
};
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String op = in.next();
int i1 = in.nextInt();
int i2 = in.nextInt();
System.out.println(IntOp.@@[valueOf(op)](1).eval(i1, i2));
in.close();
}
}
```
答案:
第1空:add()
第2空:i1+i2
第3空: sub()
第4空:i1-i2
第5空:valueOf(op)
```
add 5 3
```
The result of the expression above is: `8`.
Now, fill in the blanks:
```Java
import java.util.Scanner;
enum IntOp {
@@[add()](1) { // for operator add
int eval(int i1, int i2) {
return @@[i1+i2](1);
}
},
@@[ sub()](1) { // for operator sub
int eval(int i1, int i2) {
return @@[i1-i2](1);
}
};
int eval(int i1, int i2) {
return 0;
};
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String op = in.next();
int i1 = in.nextInt();
int i2 = in.nextInt();
System.out.println(IntOp.@@[valueOf(op)](1).eval(i1, i2));
in.close();
}
}
```
答案:
第1空:add()
第2空:i1+i2
第3空: sub()
第4空:i1-i2
第5空:valueOf(op)