Repository navigation
Expand file tree
/
Copy pathFractionCalculator.java
More file actions
140 lines (119 loc) · 3.43 KB
/
Copy pathFractionCalculator.java
File metadata and controls
140 lines (119 loc) · 3.43 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.util.Scanner;
import java.util.Arrays;
class FractionCalculator {
public static Fraction evaluate(Fraction fraction, String str) {
char c;
int num = 1;
int denom = 1;
String operator = "";
Fraction newresult = new Fraction(1,1);
String[] data = str.split(" "); // split string into array parts to determine the fraction and the operator.
for (int i = 0; i < data.length; i++) {
if (data[i].length() < 3) { // could be a negative integer or an operator but not a fraction.
for (int j = 0; j < data[i].length(); j++) {
c = data[i].charAt(j);
if (Character.isDigit(c)) {
num = Integer.parseInt(data[i]);
}
if (c == '+') { // determine the operator.
operator = "addition";
} else if (c == '*') {
operator = "multiply";
} else if (c == '/') {
operator = "divide";
} else if (c == '-') {
operator = "subtract";
}
}
} else { // 3 or more characters assumed to be a fraction.
for (int j = 0; j < data[i].length(); j++) {
c = data[i].charAt(j);
if (c == '/') {
num = Integer.parseInt(data[i].substring(0,j));
denom = Integer.parseInt(data[i].substring(j+1));
}
}
}
}
Fraction fraction2 = new Fraction(num,denom);
if (operator.equals("addition")) {
newresult = fraction.add(fraction2);
}
if (operator.equals("subtract")) {
newresult = fraction.subtract(fraction2);
}
if (operator.equals("divide")) {
newresult = fraction.divide(fraction2);
}
if (operator.equals("multiply")) {
newresult = fraction.multiply(fraction2);
}
return newresult;
}
public static void main(String[] args) {
int num = 1;
int denom = 1;
boolean finished = false;
boolean negateval = false;
boolean absoluteval = false;
Fraction newresult = new Fraction(num,denom);
System.out.println("Geoff's simple Fraction Calculator (limited features).");
while (!finished) {
System.out.print(">");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String[] inputstring = s.split(" ");
//if not a fraction then could be an int (0-9 (allowing for negative)), 'q' or 'c'.
if (inputstring[0].length() < 3) {
if (inputstring[0].equals("q")) {
System.out.println("Goodbye!");
finished = true;
}
if (inputstring[0].equals("n")) { // negate stored value.
negateval = true;
num = newresult.getNumerator();
denom = newresult.getDenominator();
}
if (inputstring[0].equals("a")) { // absolute value
absoluteval = true;
}
if (inputstring[0].equals("c")) { // clear calculator
num = 1;
denom = 1;
}
else { //value stored becomes the first operand.
num = newresult.getNumerator();
denom = newresult.getDenominator();
}
}
// a fraction has > 2 character length; get num & denom.
else {
for (int j = 0; j < inputstring[0].length(); j++) {
char c = inputstring[0].charAt(j);
if (c == '/') {
num = Integer.parseInt(inputstring[0].substring(0,j));
denom = Integer.parseInt(inputstring[0].substring(j+1));
s = s.substring(inputstring[0].length()+1);
}
}
}
Fraction result = new Fraction(num,denom);
if (negateval == true) {
newresult = result.negate();
}
else if (absoluteval == true) {
newresult = result.absolute();
}
else {
newresult = evaluate(result,s);
}
if (finished == true) {
}
else {
System.out.println(newresult);
negateval = false;
absoluteval = false;
}
}
}
}