-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
222 lines (206 loc) · 4.29 KB
/
main.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <fstream>
#include"BinarySearchTree.h"
using namespace std;
string inputCommands[] = { "INT","STRING", "tree", "size", "clear", "print", "add","remove", "find"};
int NUM_COMMANDS = 9;
const string TRUE_STRING = " true";
const string FALSE_STRING = " false";
const string FOUND_STRING = " found";
const string NOT_FOUND_STRING = " not found";
struct inputReturns
{
int commandNumber = 0;//indicates which command from prompt was given
string inputInformation = "";//indicates what data to manipulate in tree
};
inputReturns getInputValues(string line);//function returns values for a specific line of code
int main(int argc, char* argv[])
{
ifstream inputFile(argv[1]);
ofstream outputFile(argv[2]);
string inputLine = "";
inputReturns myReturns;
BinarySearchTree<int> intTree;
BinarySearchTree<string> stringTree;
bool editingInt = true;
int inputConversion = 0;
while (!inputFile.eof())
{
getline(inputFile, inputLine);
myReturns = getInputValues(inputLine);
//string inputCommands[] = { "INT","STRING", "tree", "size", "clear", "print", "add","remove", "find"};
outputFile << inputLine;
switch (myReturns.commandNumber)
{
case 0://case INT
editingInt = true;
outputFile << TRUE_STRING << endl;
break;
case 1://case STRING
editingInt = false;
outputFile << TRUE_STRING << endl;
break;
case 2://case tree
outputFile << ":" << endl;
break;
case 3://case size
if (editingInt)
{
outputFile << " " << intTree.size() << endl;
}
else
{
outputFile << " " << stringTree.size() << endl;
}
break;
case 4://case clear
if (editingInt)
{
intTree.removeWholeTree();
}
else
{
stringTree.removeWholeTree();
}
outputFile << TRUE_STRING << endl;
break;
case 5://case print
if (editingInt)
{
outputFile << ":";
outputFile << intTree.toString();
}
else
{
outputFile << ":";
outputFile << stringTree.toString();
}
outputFile << endl;
break;
case 6://case add
if (editingInt)
{
inputConversion = stoi(myReturns.inputInformation);
if (intTree.addNode(inputConversion))
{
outputFile << TRUE_STRING;
}
else
{
outputFile << FALSE_STRING;
}
}
else
{
if (stringTree.addNode(myReturns.inputInformation))
{
outputFile << TRUE_STRING;
}
else
{
outputFile << FALSE_STRING;
}
}
outputFile << endl;
break;
case 7://case remove
if (editingInt)
{
inputConversion = stoi(myReturns.inputInformation);
if (intTree.removeNode(inputConversion))
{
outputFile << TRUE_STRING;
}
else
{
outputFile << FALSE_STRING;
}
}
else
{
if (stringTree.removeNode(myReturns.inputInformation))
{
outputFile << TRUE_STRING;
}
else
{
outputFile << FALSE_STRING;
}
}
outputFile << endl;
break;
case 8://case find
if (editingInt)
{
inputConversion = stoi(myReturns.inputInformation);
if (intTree.find(inputConversion))
{
outputFile << FOUND_STRING;
}
else
{
outputFile << NOT_FOUND_STRING;
}
}
else
{
if (stringTree.find(myReturns.inputInformation))
{
outputFile << FOUND_STRING;
}
else
{
outputFile << NOT_FOUND_STRING;
}
}
outputFile << endl;
break;
default:
break;
}
}
inputFile.close();
outputFile.close();
return 0;
}
inputReturns getInputValues(string line)//returns input values from text document
{
inputReturns myReturn;
string command = "";
bool getMore = false;
unsigned int i = 0;//counter variable
while (i < line.size())
{
command += line.at(i);
i++;
if (i < line.size())
{
if (line.at(i) == ' ')
{
break;
}
}
}
for (int j = 0; j < NUM_COMMANDS; j++)
{
if (command == inputCommands[j])
{
myReturn.commandNumber = j;
break;
}
}
if (myReturn.commandNumber <= 5)
{
return myReturn;
}
else
{
i++;//ignores space
while (i < line.size())
{
myReturn.inputInformation += line.at(i);
i++;
}
return myReturn;
}
}