forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
TreeCalc.cpp.html
106 lines (91 loc) · 11 KB
/
TreeCalc.cpp.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>TreeCalc.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">// Insert your header information here</span></i>
<i><span style="color:#9A1900">// TreeCalc.cpp: CS 2150 Tree Calculator method implementations</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000">"TreeCalc.h"</span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// Constructor</span></i>
TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">TreeCalc</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Destructor - frees memory</span></i>
TreeCalc<span style="color:#990000">::~</span><b><span style="color:#000000">TreeCalc</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Deletes tree/frees memory</span></i>
<span style="color:#009900">void</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">cleanTree</span></b><span style="color:#990000">(</span>TreeNode<span style="color:#990000">*</span> tree<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Gets data from user</span></i>
<i><span style="color:#9A1900">// DO NOT MODIFY</span></i>
<span style="color:#009900">void</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">readInput</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">string</span> response<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Enter elements one by one in postfix notation"</span> <span style="color:#990000"><<</span> endl
<span style="color:#990000"><<</span> <span style="color:#FF0000">"Any non-numeric or non-operator character,"</span>
<span style="color:#990000"><<</span> <span style="color:#FF0000">" e.g. #, will terminate input"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Enter first element: "</span><span style="color:#990000">;</span>
cin <span style="color:#990000">>></span> response<span style="color:#990000">;</span>
<i><span style="color:#9A1900">//while input is legal</span></i>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span><b><span style="color:#000000">isdigit</span></b><span style="color:#990000">(</span>response<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">])</span> <span style="color:#990000">||</span> response<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'/'</span> <span style="color:#990000">||</span> response<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'*'</span>
<span style="color:#990000">||</span> response<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'-'</span> <span style="color:#990000">||</span> response<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'+'</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">insert</span></b><span style="color:#990000">(</span>response<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Enter next element: "</span><span style="color:#990000">;</span>
cin <span style="color:#990000">>></span> response<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Puts value in tree stack</span></i>
<span style="color:#009900">void</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">insert</span></b><span style="color:#990000">(</span><b><span style="color:#0000FF">const</span></b> string<span style="color:#990000">&</span> val<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// insert a value into the tree</span></i>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Prints data in prefix form</span></i>
<span style="color:#009900">void</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">printPrefix</span></b><span style="color:#990000">(</span>TreeNode<span style="color:#990000">*</span> tree<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// print the tree in prefix format</span></i>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Prints data in infix form</span></i>
<span style="color:#009900">void</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">printInfix</span></b><span style="color:#990000">(</span>TreeNode<span style="color:#990000">*</span> tree<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// print tree in infix format with appropriate parentheses</span></i>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">//Prints data in postfix form</span></i>
<span style="color:#009900">void</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">printPostfix</span></b><span style="color:#990000">(</span>TreeNode<span style="color:#990000">*</span> tree<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// print the tree in postfix form</span></i>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Prints tree in all 3 (post, in, pre) forms</span></i>
<i><span style="color:#9A1900">// DO NOT MODIFY</span></i>
<span style="color:#009900">void</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">printOutput</span></b><span style="color:#990000">()</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>expressionStack<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#990000">!=</span> <span style="color:#993399">0</span> <span style="color:#990000">&&</span> expressionStack<span style="color:#990000">.</span><b><span style="color:#000000">top</span></b><span style="color:#990000">()</span> <span style="color:#990000">!=</span> NULL<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
TreeNode<span style="color:#990000">*</span> tree <span style="color:#990000">=</span> expressionStack<span style="color:#990000">.</span><b><span style="color:#000000">top</span></b><span style="color:#990000">();</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Expression tree in postfix expression: "</span><span style="color:#990000">;</span>
<b><span style="color:#000000">printPostfix</span></b><span style="color:#990000">(</span>tree<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Expression tree in infix expression: "</span><span style="color:#990000">;</span>
<b><span style="color:#000000">printInfix</span></b><span style="color:#990000">(</span>tree<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Expression tree in prefix expression: "</span><span style="color:#990000">;</span>
<b><span style="color:#000000">printPrefix</span></b><span style="color:#990000">(</span>tree<span style="color:#990000">);</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span> <b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Size is 0."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// Evaluates tree, returns value</span></i>
<i><span style="color:#9A1900">// private calculate() method</span></i>
<span style="color:#009900">int</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">calculate</span></b><span style="color:#990000">(</span>TreeNode<span style="color:#990000">*</span> tree<span style="color:#990000">)</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// Traverse the tree and calculates the result</span></i>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">//Calls calculate, sets the stack back to a blank stack</span></i>
<i><span style="color:#9A1900">// public calculate() method. Hides private data from user</span></i>
<span style="color:#009900">int</span> TreeCalc<span style="color:#990000">::</span><b><span style="color:#000000">calculate</span></b><span style="color:#990000">()</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// call private calculate method here</span></i>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>