-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathmain.cpp
More file actions
432 lines (362 loc) · 8.8 KB
/
main.cpp
File metadata and controls
432 lines (362 loc) · 8.8 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//*****************************************************************************************************
//
// This program reads stock data from a file and instantiates a binary search tree to store the
// stock objects. It then displays a menu of options to demonstrate the BST class methods.
//
// Other files required:
// 1. bst.h - header file for the BST class (includes node struct: node.h)
// 2. stock.h - header file for the Stock class (includes implementation file: stock.cpp)
// 3. Stock.txt - text file containing stock data
//
//*****************************************************************************************************
#include "bst.h"
#include "stock.h"
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
using namespace std;
//*****************************************************************************************************
bool readStocks(BST<Stock> &stockTree);
void traverseBST(BST<int> &intTree);
void displayMenu(BST<Stock> &stockTree);
//*****************************************************************************************************
int main() {
BST<int> intTree;
BST<Stock> stockTree;
int randVal;
srand(static_cast<unsigned>(time(0)));
for (int i = 0; i < 10; ++i) {
randVal = rand() % 5000 + 1;
intTree.insert(randVal);
}
traverseBST(intTree);
cout << "\nHeight: " << intTree.height() << endl;
if (readStocks(stockTree))
displayMenu(stockTree);
return 0;
}
//*****************************************************************************************************
bool readStocks(BST<Stock> &stockTree) {
ifstream inFile("Stock.txt");
bool success = true;
if (inFile.is_open()) {
string name,
symbol;
double price;
while (getline(inFile, name) && getline(inFile, symbol) && inFile >> price) {
inFile.ignore(numeric_limits<streamsize>::max(), '\n');
Stock stock(name, symbol, price);
stockTree.insert(stock);
}
} else {
cerr << "\nError: file not found\n";
success = false;
}
inFile.close();
return success;
}
//*****************************************************************************************************
void traverseBST(BST<int> &intTree) {
cout << "In-order: " << endl;
intTree.inOrder();
cout << "\nPre-order: " << endl;
intTree.preOrder();
cout << "\nPost-order: " << endl;
intTree.postOrder();
}
//*****************************************************************************************************
void displayMenu(BST<Stock> &stockTree) {
ofstream outFile("Stock.txt");
string name,
symbol;
double price;
char choice;
do {
cout << "\nMenu Options:\n"
<< "a) Display a stock's name given its symbol\n"
<< "b) Display a stock's price given its symbol\n"
<< "c) Insert a new stock\n"
<< "d) Display all stocks\n"
<< "e) Quit\n"
<< "Enter your choice: ";
cin >> choice;
choice = tolower(choice);
switch (choice) {
case 'a':
case 'b': {
cout << "\nEnter stock symbol: ";
cin >> symbol;
Stock *pStock = stockTree.search(Stock("", symbol));
if (pStock == nullptr)
cerr << "Error: stock not found\n";
else if (choice == 'a')
cout << "Stock name: " << pStock->getName() << endl;
else
cout << "Stock price: " << pStock->getPrice() << endl;
delete pStock;
pStock = nullptr;
} break;
case 'c':
cin.ignore(numeric_limits<streamsize>::max(), '\n');
do {
cout << "\nEnter stock name: ";
getline(cin, name);
if (name.empty())
cerr << "Error: invalid input\n";
} while (name.empty());
do {
cout << "Enter stock symbol: ";
getline(cin, symbol);
// find_first_of returns position of the first character that matches
// if no such character is found, the function returns string::npos (not a position)
// makes sure the symbol is not empty and is only one word
if (symbol.empty() || symbol.find_first_of(" \t\n\v\f\r") != string::npos)
cerr << "Error: invalid input\n\n";
} while (symbol.empty() || symbol.find_first_of(" \t\n\v\f\r") != string::npos);
cout << "Enter stock price: ";
while (!(cin >> price) || price < 0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Error: invalid input\n\n";
cout << "Enter stock price: ";
}
stockTree.insert(Stock(name, symbol, price));
break;
case 'd':
cout << "\n\tStocks"
<< "\n----------------------" << endl;
stockTree.inOrder();
break;
case 'e':
stockTree.inOrder(outFile);
outFile.close();
break;
default:
cerr << "\nError: invalid choice\n";
}
} while (choice != 'e');
}
//*****************************************************************************************************
/*
In-order:
449
563
1143
2716
3758
3763
3875
4221
4428
4593
Pre-order:
3758
449
563
1143
2716
3763
4593
4428
3875
4221
Post-order:
2716
1143
563
449
4221
3875
4428
4593
3763
3758
Height: 6
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: d
Stocks
----------------------
Apple
AAPL
121.73
Advanced Micro Devices
AMD
84.51
Intel
INTC
60.78
Motorola Inc.
MOT
17.49
Microsoft Corp.
MSFT
28.11
NVIDIA
NVDA
548.58
Sony
SNE
105.81
Tesla
TSLA
564.33
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: c
Enter stock name: Computer Science Club
Enter stock symbol: CSC
Enter stock price: 1010.10
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: b
Enter stock symbol: CSC
Stock price: 1010.1
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: a
Enter stock symbol: CSC
Stock name: Computer Science Club
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: e
****************************************************************************************************
In-order:
642
1022
2651
2896
3133
3778
3846
4457
4739
4764
Pre-order:
642
3846
3133
2896
1022
2651
3778
4739
4457
4764
Post-order:
2651
1022
2896
3778
3133
4457
4764
4739
3846
642
Height: 6
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: A
Enter stock symbol: TEST
Error: stock not found
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: B
Enter stock symbol: TEST
Error: stock not found
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: C
Enter stock name:
Error: invalid input
Enter stock name: Computer Science Club
Enter stock symbol:
Error: invalid input
Enter stock symbol: CSC WBST
Error: invalid input
Enter stock symbol: CSC
Enter stock price: -100
Error: invalid input
Enter stock price: a
Error: invalid input
Enter stock price: 1010.10
Menu Options:
a) Display a stock's name given its symbol
b) Display a stock's price given its symbol
c) Insert a new stock
d) Display all stocks
e) Quit
Enter your choice: e
****************************************************************************************************
In-order:
693
1098
1352
1456
1740
1750
2081
2116
2234
2765
Pre-order:
1456
1098
693
1352
2116
1740
1750
2081
2765
2234
Post-order:
693
1352
1098
2081
1750
1740
2234
2765
2116
1456
Height: 5
Error: file not found
*/