-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmain.cpp
194 lines (145 loc) · 4.54 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
// Kontenery
#include <vector> // Tablica
#include <set> // Zbiór
#include <map> // Słownik
// Adaptery
#include <queue> // Kolejka
#include <stack> // Stos
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include "Slav.h"
#define REPORT_ADAPTERS showMeAdapterSizes(queueOfSlavs,stackOfSlavs)
#define REPORT_CONTAINERS showMeContainerSizes(vectorOfSlavs,setOfSlavs,mapOfSlavs)
using namespace std;
void showMeContainerSizes(vector <Slav *>, set <Slav *>, map <Slav *, Slav*>);
void showMeAdapterSizes(queue <Slav *>, stack <Slav *>);
void sortByGender(Slav *, int);
void containers(Slav *, int);
void adapters(Slav *, int);
int main(int argc, char const *argv[])
{
srand(time(NULL)); //Losowy seed dla późniejszego losowania słowian
int n = 2 * atoi(argv[1]);
Slav *slavs = new Slav[n];
cout << "# Generated Slavs" << endl;
for (int i = 0; i < n; ++i)
cout << slavs[i].description() << endl;
containers(slavs, n);
adapters(slavs, n);
delete [] slavs;
}
void containers(Slav * slavs, int n)
{
vector <Slav *> vectorOfSlavs;
set <Slav *> setOfSlavs;
map <Slav *, Slav *> mapOfSlavs;
printf("# Containers\n");
REPORT_CONTAINERS;
printf("## vector\n");
// Umieść Słowian w losowej kolejności w wektorze.
vectorOfSlavs.push_back(NULL);
for(int i = 0; i < n; i++)
vectorOfSlavs.insert(vectorOfSlavs.begin() + rand() % vectorOfSlavs.size(), slavs + i);
vectorOfSlavs.pop_back();
// Wykorzystując iterator i funkcję description(), wyświetl wszystkich Słowian w wektorze.
vector <Slav *>::iterator itVectorSlavs = vectorOfSlavs.begin(); //Tworzymy iterator
for(; itVectorSlavs != vectorOfSlavs.end(); itVectorSlavs++)
cout << (*itVectorSlavs)->description() << endl;
REPORT_CONTAINERS;
printf("## set\n");
// Przenieś wszystkich Słowian z wektoru do zbioru.
for(itVectorSlavs = vectorOfSlavs.begin(); itVectorSlavs != vectorOfSlavs.end(); itVectorSlavs++)
setOfSlavs.insert(*itVectorSlavs);
vectorOfSlavs.clear();
REPORT_CONTAINERS;
printf("## map\n");
// Stwórz słownik tworzący pary Słowian, z tych znajdujących się w zbiorze, czyszcząc zbiór
set <Slav *>::iterator itSetSlavs = setOfSlavs.begin();
while(itSetSlavs != setOfSlavs.end())
{
mapOfSlavs[*itSetSlavs] =*(itSetSlavs = setOfSlavs.erase(itSetSlavs));
itSetSlavs = setOfSlavs.erase(itSetSlavs);
}
// Wykorzystując iterator, wyświetl wszystkie pary Słowian
map <Slav*, Slav*>::iterator itMapSlavs = mapOfSlavs.begin();
for(; itMapSlavs != mapOfSlavs.end(); itMapSlavs++)
cout << (itMapSlavs->first)->description() << " :::: " << (itMapSlavs->second)->description() << endl;
REPORT_CONTAINERS;
}
void adapters(Slav * slavs, int n)
{
queue <Slav *> queueOfSlavs;
stack <Slav *> stackOfSlavs;
printf("# Adapters\n");
REPORT_ADAPTERS;
printf("## queue\n");
// Umieść Słowian w kolejce.
for(int i = 0; i<n; i++)
queueOfSlavs.push(slavs + i);
REPORT_ADAPTERS;
printf("## stack\n");
// Przenieś Słowian z kolejki do stosu
for(int i = 0; i<n; i++)
{
stackOfSlavs.push(queueOfSlavs.front());
queueOfSlavs.pop();
}
REPORT_ADAPTERS;
// Wyświetl Słowian zdejmowanych ze stosu.
for(int i = 0; i<n; i++)
{
cout << (*stackOfSlavs.top()).description() << endl;
stackOfSlavs.pop();
}
REPORT_ADAPTERS;
}
void sortByGender(Slav* slavs, int n)
{
printf("## Sort by gender\n");
map<_gender, vector<Slav*>> mapOfSlavsSorted;
for(int i = 0; i<n; i++)
{
if((slavs + i)->gender() == male)
mapOfSlavsSorted[male].push_back(slavs + i);
else
mapOfSlavsSorted[female].push_back(slavs + i);
}
vector<Slav *>::iterator itVectorSlavsSorted = mapOfSlavsSorted[male].begin();
int i = 0;
_gender genderOfSlav;
while(itVectorSlavsSorted != mapOfSlavsSorted[male].end())
{
cout << (*itVectorSlavsSorted)->description() << endl;
itVectorSlavsSorted++;
i++;
}
if(i == 0)
cout << "Brak mezczyzn" << endl;
itVectorSlavsSorted = mapOfSlavsSorted[female].begin();
i = 0;
while(itVectorSlavsSorted != mapOfSlavsSorted[female].end())
{
cout << (*itVectorSlavsSorted)->description() << endl;
itVectorSlavsSorted++;
i++;
}
if(i == 0)
cout << "Brak kobiet" << endl;
}
void showMeContainerSizes(vector <Slav *> vector, set <Slav *> set, map <Slav *, Slav*> map)
{
printf("[vector_size = %lu, set_size = %lu, map_size = %lu, existingSlavs = %i]\n",
vector.size(),
set.size(),
map.size(),
Slav::counter());
}
void showMeAdapterSizes(queue <Slav *> queue, stack <Slav *> stack)
{
printf("[queue_size = %lu, stack_size = %lu, existingSlavs = %i]\n",
queue.size(),
stack.size(),
Slav::counter());
}