-
Notifications
You must be signed in to change notification settings - Fork 0
/
FractionCalculatorAdvanced.java
240 lines (200 loc) · 10 KB
/
FractionCalculatorAdvanced.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Part 4 - Hacker Problem - FractionCalculatorAdvanced
*/
import java.util.Scanner;
public class FractionCalculatorAdvanced {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
System.out.println(" \t NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
System.out.println(" \t NN NN");
System.out.println(" \t NN FRACTION CALCULATOR ADVANCED NN");
System.out.println(" \t NN NN");
System.out.println(" \t NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN");
System.out.println();
System.out.println(" \t This program allows the user to perform standard");
System.out.println(" \t mathematical functions on regular fractions. It");
System.out.println(" \t add, subtract, multiply and divide fractions until");
System.out.println(" \t you type Q to quit.");
System.out.println();
System.out.println("Valid operations are of the form \"[FRAC] [OPERATION] [FRAC]\".");
System.out.println("[FRAC] can be either a single integer or two integers separated by \"/\".");
System.out.println("[OPERATION] can be +, -, *, / or =.");
System.out.println("-------------------------------------------------------------------------");
System.out.println();
int quit = 0;
while (quit == 0) {
System.out.print("Please enter your fractional computation (Q to quit): ");
String convolute = input.nextLine();
if (convolute.equalsIgnoreCase("q")) { quit = 1; continue; }
int exit = 0;
while (exit == 0) {
convolute = verifyInput(convolute);
// Parse String for further testing...
String[] choppy = convolute.split(" ");
String elem1 = choppy[0];
String elem2 = choppy[1];
String elem3 = choppy[2];
if (!(validFraction(elem1))) {
System.out.print("Your first element is not a typical fraction. Please use integer form a/b or a. Re-type full operation: ");
convolute = input.nextLine();
continue;
}
if (!(validOperator(elem2))) {
System.out.print("Your operator must be: +, -, /, *, =. Please re-enter your whole equation: ");
convolute = input.nextLine();
continue;
}
if (!(validFraction(elem3))) {
System.out.print("Your third element is not a typical fraction. Please use integer form a/b or a. Re-type full operation: ");
convolute = input.nextLine();
continue;
}
exit = 1;
}
// Parse and cast.
String[] choppy = convolute.split(" ");
if (!(choppy[0].contains("/"))) { choppy[0] = choppy[0] + "/1"; }
if (!(choppy[2].contains("/"))) { choppy[2] = choppy[2] + "/1"; }
// Cast fraction1
String[] finechop = choppy[0].split("/");
int[] frac1 = new int[2];
frac1[0] = Integer.parseInt(finechop[0]);
frac1[1] = Integer.parseInt(finechop[1]);
Fraction fraction1 = new Fraction(frac1[0], frac1[1]);
// Cast fraction2
finechop = choppy[2].split("/");
int[] frac2 = new int[2];
frac2[0] = Integer.parseInt(finechop[0]);
frac2[1] = Integer.parseInt(finechop[1]);
Fraction fraction2 = new Fraction(frac2[0], frac2[1]);
// Equation is: fraction1 choppy[1] fraction2, process normally.
if (choppy[1].equals("+")) {
Fraction sum = fraction1.add(fraction2);
System.out.print(fraction1.toString() + " + " + fraction2.toString() + " = " + sum.toString());
Fraction sumLow = new Fraction(sum.getNumerator(), sum.getDenominator());
sumLow.toLowestTerms();
if (!(sum.toString().equals(sumLow.toString()))) { System.out.print(" -> " + sumLow.toString()); }
System.out.print("\n");
} else if (choppy[1].equals("-")) {
Fraction rem = fraction1.subtract(fraction2);
System.out.println(fraction1.toString() + " - " + fraction2.toString() + " = " + rem.toString());
Fraction remLow = new Fraction(rem.getNumerator(), rem.getDenominator());
remLow.toLowestTerms();
if (!(rem.toString().equals(remLow.toString()))) { System.out.print(" -> " + remLow.toString()); }
System.out.print("\n");
} else if (choppy[1].equals("/")) {
Fraction quotient = fraction1.divide(fraction2);
System.out.println(fraction1.toString() + " / " + fraction2.toString() + " = " + quotient.toString());
Fraction quoLow = new Fraction(quotient.getNumerator(), quotient.getDenominator());
quoLow.toLowestTerms();
if (!(quotient.toString().equals(quoLow.toString()))) { System.out.print(" -> " + quoLow.toString()); }
System.out.print("\n");
} else if (choppy[1].equals("*")) {
Fraction product = fraction1.multiply(fraction2);
System.out.println(fraction1.toString() + " * " + fraction2.toString() + " = " + product.toString());
Fraction pdtLow = new Fraction(product.getNumerator(), product.getDenominator());
pdtLow.toLowestTerms();
if (!(product.toString().equals(pdtLow.toString()))) { System.out.print(" -> " + pdtLow.toString()); }
System.out.print("\n");
} else {
boolean answer = fraction1.equals(fraction2);
System.out.println(fraction1.toString() + " = " + fraction2.toString() + " is " + answer);
}
System.out.println("-------------------------------------------------------------------------");
}
}
public static String verifyInput(String in) {
int exit = 0;
while (exit == 0) {
// Must include spaces.
if (isSpaced(in)) {
// Parse
String[] choppy = in.split(" ");
if (choppy.length == 3) {
exit = 1;
} else {
System.out.print("Your entry must contain exactly three (3) items [FRAC] [OPERATION] [FRAC], try again: ");
in = input.nextLine();
}
} else {
System.out.print("Your computation must include spaces to separate the fractions and operation, try again: ");
in = input.nextLine();
}
}
return in;
}
public static boolean isSpaced(String in) {
if (in.contains(" ")) {
return true;
} else {
return false;
}
}
public static String getOperation() {
System.out.print("Please enter an operation (+, -, /, *, = or Q to quit): ");
String perform = input.nextLine();
if (!(perform.equals("+")) && !(perform.equals("-")) && !(perform.equals("/")) && !(perform.equals("*")) && !(perform.equals("=")) && !(perform.equalsIgnoreCase("Q"))) {
while (!(perform.equals("+")) && !(perform.equals("-")) && !(perform.equals("/")) && !(perform.equals("*")) && !(perform.equals("=")) && !(perform.equalsIgnoreCase("Q"))) {
System.out.print("Invalid input (+, -, /, *, = or Q to quit): ");
perform = input.nextLine();
}
}
if (perform.equalsIgnoreCase("q")) {
System.exit(0);
}
return perform;
}
public static boolean validFraction(String check) {
if (check.contains("/")) {
String[] data = check.split("/");
if (data[0].matches("-?\\d+")) {
if (data[1].matches("-?\\d+")) {
int data1 = Integer.parseInt(data[1]);
if (data1 > 0) {
return true;
} else { // Negative denom. passed!
return false;
}
} else { // Non-integer denom. passed!
return false;
}
} else { // Non-integer numerator passed!
return false;
}
} else {
if (check.matches("-?\\d+")) {
return true;
} else { // Non-integer data passed!
return false;
}
}
}
public static boolean validOperator(String in) {
if ((in.equals("+")) || (in.equals("-")) || (in.equals("/")) || (in.equals("*")) || (in.equals("="))) {
return true;
} else {
return false;
}
}
public static Fraction getFraction() {
System.out.print("Please enter a fraction (a/b) or integer (a): ");
String usrIn = input.nextLine();
if (!validFraction(usrIn)) {
while (!validFraction(usrIn)) {
System.out.print("Invalid Fraction. Please enter (a/b) or (a), where a and b are integers and b is not zero: ");
usrIn = input.nextLine();
}
}
// Convert to integers to pass as Fraction
int[] toInts = new int[2];
if (usrIn.contains("/")) {
String[] split = usrIn.split("/");
toInts[0] = Integer.parseInt(split[0]);
toInts[1] = Integer.parseInt(split[1]);
} else {
toInts[0] = Integer.parseInt(usrIn);
toInts[1] = 1;
}
return new Fraction(toInts[0], toInts[1]);
}
}