-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.cpp
220 lines (183 loc) · 4.91 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
//*****************************************************************************************************
//
// This program demonstrates the AList class by instantiating an array-based list of Stock
// objects and integers and performing it's methods.
//
// Other files required:
// 1. aList.h - header file for the AList class
// 2. stock.h - header file for the Stock class (includes implementation file: stock.cpp)
//
//*****************************************************************************************************
#include "aList.h"
#include "stock.h"
#include <iostream>
using namespace std;
//*****************************************************************************************************
void searchStock(const AList<Stock> &stockList, const Stock &s);
void searchInt(const AList<int> &intList, const int &s);
void checkStock(const AList<Stock> &stockList);
void checkInt(const AList<int> &intList);
//*****************************************************************************************************
int main() {
AList<int> intList(10);
AList<Stock> stockList(5);
Stock s1("Apple Inc.", "AAPL", 150.10);
Stock s2("Microsoft Corp.", "MSFT", 300.20);
Stock s3("Google LLC", "GOOGL", 2800.50);
Stock s4("Amazon Inc.", "AMZN", 3400.40);
Stock s5("Facebook Inc.", "FB", 355.60);
Stock s6("Tesla Inc.", "TSLA", 1100.70);
Stock s7(s1);
Stock s8("Webster University", "WBST", 100100100100.10);
checkInt(intList); // empty
for (int i = 0; i < 10; ++i)
if (i % 2 == 0)
intList.insert(i);
else
intList.insert(i * 10);
checkInt(intList); // full
intList.print();
intList.insert(100);
checkInt(intList); // resize
intList.print();
searchInt(intList, 10);
searchInt(intList, 100);
searchInt(intList, 20); // not found
checkStock(stockList); // empty
stockList.insert(s1);
stockList.insert(s2);
stockList.insert(s3);
stockList.insert(s4);
stockList.insert(s5);
checkStock(stockList); // full
stockList.print();
stockList.insert(s6);
stockList.insert(s7);
checkStock(stockList); // resize
stockList.print();
searchStock(stockList, s1); // original found
searchStock(stockList, s7); // copy found (notice same index as s1)
searchStock(stockList, s6);
searchStock(stockList, s3);
searchStock(stockList, s8); // not found
return 0;
}
//*****************************************************************************************************
void searchStock(const AList<Stock> &stockList, const Stock &s) {
int searchIndex = stockList.binarySearch(s);
string sName = s.getName();
if (searchIndex != -1)
cout << sName << " at index: " << searchIndex << endl;
else
cerr << sName << " not found\n";
}
//*****************************************************************************************************
void searchInt(const AList<int> &intList, const int &s) {
int searchIndex = intList.binarySearch(s);
if (searchIndex != -1)
cout << s << " at index: " << searchIndex << endl;
else
cerr << s << " not found\n";
}
//*****************************************************************************************************
void checkStock(const AList<Stock> &stockList) {
cout << "\nvalues: " << stockList.listSize() << endl;
cout << "capacity: " << stockList.listCap() << endl;
if (stockList.isFull())
cerr << "stock list full\n";
else if (stockList.isEmpty())
cerr << "stock list empty\n";
}
//*****************************************************************************************************
void checkInt(const AList<int> &intList) {
cout << "\nvalues: " << intList.listSize() << endl;
cout << "capacity: " << intList.listCap() << endl;
if (intList.isFull())
cerr << "integer list full\n";
else if (intList.isEmpty())
cerr << "integer list empty\n";
}
//*****************************************************************************************************
/*
values: 0
capacity: 10
integer list empty
values: 10
capacity: 10
integer list full
90
70
50
30
10
8
6
4
2
0
values: 11
capacity: 20
100
90
70
50
30
10
8
6
4
2
0
10 at index: 5
100 at index: 0
20 not found
values: 0
capacity: 5
stock list empty
values: 5
capacity: 5
stock list full
Microsoft Corp.
MSFT
300.2
Google LLC
GOOGL
2800.5
Facebook Inc.
FB
355.6
Amazon Inc.
AMZN
3400.4
Apple Inc.
AAPL
150.1
values: 7
capacity: 15
Tesla Inc.
TSLA
1100.7
Microsoft Corp.
MSFT
300.2
Google LLC
GOOGL
2800.5
Facebook Inc.
FB
355.6
Amazon Inc.
AMZN
3400.4
Apple Inc.
AAPL
150.1
Apple Inc.
AAPL
150.1
Apple Inc. at index: 5
Apple Inc. at index: 5
Tesla Inc. at index: 0
Google LLC at index: 2
Webster University not found
*/