forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
TreeCalc.cpp
92 lines (77 loc) · 2.45 KB
/
TreeCalc.cpp
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
// Insert your header information here
// TreeCalc.cpp: CS 2150 Tree Calculator method implementations
#include "TreeCalc.h"
#include <iostream>
using namespace std;
// Constructor
TreeCalc::TreeCalc() {
}
// Destructor - frees memory
TreeCalc::~TreeCalc() {
}
// Deletes tree/frees memory
void TreeCalc::cleanTree(TreeNode* tree) {
}
// Gets data from user
// DO NOT MODIFY
void TreeCalc::readInput() {
string response;
cout << "Enter elements one by one in postfix notation" << endl
<< "Any non-numeric or non-operator character,"
<< " e.g. #, will terminate input" << endl;
cout << "Enter first element: ";
cin >> response;
//while input is legal
while (isdigit(response[0]) || response[0] == '/' || response[0] == '*'
|| response[0] == '-' || response[0] == '+') {
insert(response);
cout << "Enter next element: ";
cin >> response;
}
}
// Puts value in tree stack
void TreeCalc::insert(const string& val) {
// insert a value into the tree
}
// Prints data in prefix form
void TreeCalc::printPrefix(TreeNode* tree) const {
// print the tree in prefix format
}
// Prints data in infix form
void TreeCalc::printInfix(TreeNode* tree) const {
// print tree in infix format with appropriate parentheses
}
//Prints data in postfix form
void TreeCalc::printPostfix(TreeNode* tree) const {
// print the tree in postfix form
}
// Prints tree in all 3 (post, in, pre) forms
// DO NOT MODIFY
void TreeCalc::printOutput() const {
if (expressionStack.size() != 0 && expressionStack.top() != NULL) {
TreeNode* tree = expressionStack.top();
cout << "Expression tree in postfix expression: ";
printPostfix(tree);
cout << endl;
cout << "Expression tree in infix expression: ";
printInfix(tree);
cout << endl;
cout << "Expression tree in prefix expression: ";
printPrefix(tree);
cout << endl;
} else {
cout << "Size is 0." << endl;
}
}
// Evaluates tree, returns value
// private calculate() method
int TreeCalc::calculate(TreeNode* tree) const {
// Traverse the tree and calculates the result
return 0;
}
//Calls calculate, sets the stack back to a blank stack
// public calculate() method. Hides private data from user
int TreeCalc::calculate() {
// call private calculate method here
return 0;
}