-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyInfArith.java
More file actions
76 lines (67 loc) · 2.27 KB
/
Copy pathMyInfArith.java
File metadata and controls
76 lines (67 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import arbitraryarithmetic.AFloat;
import arbitraryarithmetic.AInteger;
public class MyInfArith {
public static void main(String[] args){
if(args.length!=4){
System.out.println("Incorrect usage.");
System.out.println("Usage: <int/float> <operation> <number1> <number2>");
System.exit(1);
}
boolean int_ops = false;
boolean float_ops = false;
if(args[0].equals("int")){
int_ops = true;
float_ops = false;
} else if(args[0].equals("float")){
int_ops = false;
float_ops = true;
} else{
System.out.println("Unexpected argument at position : 0 (expected only 'int' or 'float')");
System.exit(1);
}
boolean add = false;
boolean multiply = false;
boolean subtract = false;
if(args[1].equals("add")){
add = true;
} else if(args[1].equals("subtract")){
subtract = true;
} else if(args[1].equals("multiply")){
multiply = true;
} else if(args[1].equals("divide")){
//do nothing
} else{
System.out.println("Unexpected argument at position : 1 (expected 'add', 'subtract', 'multiply' or 'divide')");
System.exit(1);
}
if(int_ops){
AInteger num1 = new AInteger(args[2]);
AInteger num2 = new AInteger(args[3]);
AInteger result;
if(add){
result = num1.add(num2);
} else if(subtract){
result = num1.sub(num2);
} else if(multiply){
result = num1.multiply(num2);
} else {
result = num1.divide(num2);
}
result.print_int();
} else if(float_ops){
AFloat num1 = new AFloat(args[2]);
AFloat num2 = new AFloat(args[3]);
AFloat result;
if(add){
result = num1.add(num2);
} else if(subtract){
result = num1.sub(num2);
} else if(multiply){
result = num1.multiply(num2);
} else {
result = num1.divide(num2);
}
result.print_float();
}
}
}