-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
70 lines (56 loc) · 2.19 KB
/
Main.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
package zone.mafu;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] tempargs) {
ArrayList<String> args = new ArrayList<>(); //easier to work with em in an arraylist... at least I think so
Scanner sc = new Scanner(System.in);
String answer = "";
while(!answer.equals("y") && !answer.equals("n")) {
System.out.println("Show work? y/n");
answer = sc.next();
}
for(int i = 0; i < tempargs.length; i++){
args.add(tempargs[i]);
}
if(answer.equals("y"))
System.out.println(args);
int placeHolder = 0;
int sum = 0;
while(args.size() > 2) {
while (!isOperand(args.get(placeHolder))) {
placeHolder++;
}
switch (args.get(placeHolder)) {
case "+":
sum += Integer.parseInt(args.get(placeHolder - 2)) + Integer.parseInt(args.get(placeHolder - 1));
break;
case "-":
sum += Integer.parseInt(args.get(placeHolder - 2)) - Integer.parseInt(args.get(placeHolder - 1));
break;
case "*":
sum += Integer.parseInt(args.get(placeHolder - 2)) * Integer.parseInt(args.get(placeHolder - 1));
break;
case "/":
sum += Integer.parseInt(args.get(placeHolder - 2)) / Integer.parseInt(args.get(placeHolder - 1));
break;
}
args.remove(placeHolder - 2);
args.remove(placeHolder - 2);
args.remove(placeHolder - 2);
args.add(placeHolder - 2, Integer.toString(sum));
if(answer.equals("y")){
System.out.println(sum);
System.out.println(args);
}
placeHolder = 0; //resetting placeholder
sum = 0; //resetting sum
}
System.out.println("Answer: " + args.get(0));
}
public static boolean isOperand(String s){
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/"))
return true;
return false;
}
}