-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolishNotation.java
More file actions
60 lines (55 loc) · 2.16 KB
/
polishNotation.java
File metadata and controls
60 lines (55 loc) · 2.16 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
//Author: ONYEKA NWAMBA
import java.util.Scanner;
public class polishNotation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the operators");
System.out.println("for operators +, -, *, and !");
System.out.println("Leave spaces between all operators and digits " +
" if using 2 + numbers");
System.out.print("expression: ");
System.out.println("value = " + evaluateEXP(scanner));
}
//input contains the expression user has entered
public static int evaluateEXP(Scanner scanner) {
//checks if there is another digit after current one
//allows expression to be looked at from right to left
if (scanner.hasNextInt())
return scanner.nextInt();
//if there is another digit after current one then
//operands and operators are established
char operator = scanner.next().charAt(0);
int operand1 = evaluateEXP(scanner);
int operand2 = 0;
//only take second operand for factorial if operator is not unary
if (operator != '!') {
operand2 = evaluateEXP(scanner);
}
return evaluateOP(operator, operand1, operand2);
}
//operator has to be one of +, - , * or ! otherwise error is given
public static int evaluateOP(char operator, int operand1, int operand2) {
if (operator=='+') {
return operand1 + operand2;
} else if(operator=='-') {
return operand1 - operand2;
} else if (operator=='*') {
return operand1 * operand2;
} else if (operator=='/') {
return operand1 / operand2;
} else if (operator=='!') {
//if ! used then uses factorial method
return factorial(operand1);
} else {
//RunTimeException allows to return an error string in a int "type" method
throw new RuntimeException("operator not allowed for this language");
}
}
private static int factorial(int n) {
if(n==1) {
return 1;
} else {
return factorial(n - 1) * n;
}
}
}