-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework6Programming
More file actions
119 lines (104 loc) · 3.56 KB
/
Copy pathHomework6Programming
File metadata and controls
119 lines (104 loc) · 3.56 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
public class PostfixTester
{
/**
* Reads and evaluates multiple postfix expressions.
*/
public static void main(String[] args)
{
String expression, again;
int result;
Scanner in = new Scanner(System.in);
do
{
PostfixEvaluator evaluator = new PostfixEvaluator();
System.out.println("Enter a valid post-fix expression one token " +
"at a time with a space between each token (e.g. 5 4 + 3 2 1 - + *)");
System.out.println("Each token must be an integer or an operator (+,-,*,/)");
expression = in.nextLine();
result = evaluator.evaluate(expression);
System.out.println();
System.out.println("That expression equals " + result);
System.out.println("The Expression Tree for that expression is: ");
System.out.println(evaluator.getTree());
System.out.print("Evaluate another expression [Y/N]? ");
again = in.nextLine();
System.out.println();
}
while (again.equalsIgnoreCase("y"));
}
}
/**
* PostfixEvaluator this modification of our stack example uses a
* stack to create an expression tree from a VALID integer postfix expression
* and then uses a recursive method from the ExpressionTree class to
* evaluate the tree.
*
* @author Temirlan
* @version 4.0
*/
public class PostfixEvaluator
{
private String expression;
private Stack<ExpressionTree> treeStack;
/**
* Sets up this evalutor by creating a new stack.
*/
public PostfixEvaluator()
{
treeStack = new Stack<ExpressionTree>();
}
/**
* Retrieves and returns the next operand off of this tree stack.
*
* @param treeStack the tree stack from which the operand will be returned
* @return the next operand off of this tree stack
*/
private ExpressionTree getOperand(Stack<ExpressionTree> treeStack)
{
ExpressionTree temp;
temp = treeStack.pop();
return temp;
}
/**
* Evaluates the specified postfix expression by building and evaluating
* an expression tree.
*
* @param expression string representation of a postfix expression
* @return value of the given expression
*/
public int evaluate(String expression)
{
ExpressionTree operand1, operand2;
char operator;
String tempToken;
Scanner parser = new Scanner(expression);
while (parser.hasNext())
{
tempToken = parser.next();
operator = tempToken.charAt(0);
if ((operator == '+') || (operator == '-') || (operator == '*') ||
(operator == '/'))
{
operand1 = getOperand(treeStack);
operand2 = getOperand(treeStack);
treeStack.push(new ExpressionTree
(new ExpressionTreeOp(1,operator,0), operand2, operand1));
}
else
{
treeStack.push(new ExpressionTree(new ExpressionTreeOp
(2,' ',Integer.parseInt(tempToken)), null, null));
}
}
return (treeStack.peek()).evaluateTree();
}
/**
* Returns the expression tree associated with this postfix evaluator.
*
* @return string representing the expression tree
*/
public String getTree()
{
return (treeStack.peek()).printTree();
}
}