-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackApps.java
103 lines (98 loc) · 2.21 KB
/
StackApps.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
//Part of the Stack Project
//By: Cameron Gonzalez
//Helped by: Dr. Hong
import java.util.Stack;
import java.util.Scanner;
public class StackApps
{
public static String int2Str(int aI)
{
Stack<Integer> myStack = new Stack<Integer>();
if(aI == 0)
{
return "0";
}
String str = "";
if(aI < 0)
{
str = "-";
aI = -aI;
}
while(aI > 0)
{
int remainder = aI%10;
myStack.push(remainder);
aI = aI/10;
}
while(!myStack.isEmpty())
{
str += (char)(myStack.pop() + '0');//ask why this works
}
return str;
}
public static double evalPostfix(String ex)
{
Stack<Double> myStack = new Stack<Double>();
String nextToken;
final String OPERATOR_PATTERN = "[-+*/]";
final String INTEGER_PATTERN = "\\d+"; //integer operand pattern
final String REAL_PATTERN = "\\d+\\.\\d*"; //real number operand pattern
final String OPERAND_PATTERN = REAL_PATTERN + "|" + INTEGER_PATTERN;
final String TOKEN_PATTERN = OPERAND_PATTERN + "|" + OPERATOR_PATTERN;
ex.replaceAll("\\s+", "");
Scanner sc = new Scanner(ex);
while((nextToken = sc.findInLine(TOKEN_PATTERN)) != null)
{
if((nextToken.equals("-") || nextToken.equals("+") || nextToken.equals("*") || nextToken.equals("/")))
{
double RHS = myStack.pop();
//if there are no extra operators (normal execution)
if(!myStack.isEmpty())
{
double LHS = myStack.pop();
double result = 0;
if(nextToken.equals("-"))
{
result = LHS - RHS;
}
else if(nextToken.equals("+"))
{
result = LHS + RHS;
}
else if(nextToken.equals("*"))
{
result = LHS * RHS;
}
else
{
result = LHS / RHS;
}
myStack.push(result);
}
//if the stack is empty (in other words, extra operators)
else
{
sc.close();
return Double.NaN;
}
}
//pushing a operand into the stack
else
{
myStack.push(Double.parseDouble(nextToken));
}
}
sc.close();
//if there are extra operands
if(myStack.size() != 1)
{
return Double.NaN;
}
//normal execution
else
{
double answer = myStack.pop();
return answer;
}
}
}