Skip to content

Commit

Permalink
226088
Browse files Browse the repository at this point in the history
  • Loading branch information
mk5135795 committed Apr 13, 2016
1 parent d3cc321 commit d44f16a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
25 changes: 24 additions & 1 deletion Slav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,34 @@ void Slav::init()
Slav::Slav()
{
static int amountOfNames = (init(), names.size());
//Deklarowana jest zmienna statyczna czyli taka która istnieje "do konca"
//tzn nawet po wyjsciu z bliku w ktorym zostala zadeklarowana wciaz znajduje sie na stosie
//wciaz mozna sie do niej odwolac tylko w obrebie bloku w ktorym zostala zadeklarowana.
//najpierw wywoływana jest funkcja modyfikująca zmienna name,
//a nastepnie zmiennej przypisywana jest wartosc name.size()
//tzw leniwa inicjalizacja
//jako ze zmienna jest statyczna cala definicja wykonywana jest tylko raz,
//wiec dzieki polaczeniu funkcji operatorem(), obydwie sa wykonywane tylko raz
//obydwie sa ze soba powiazane i nie ma potrzeby wywolywac i przypisywac ich wielokrotnie
//zmienna jest definiowana ale uznalem ze przejzysciej bedzie podzielic ja na deklaracje i inicjalizacje
_name = names[rand() % amountOfNames];
_id = _counter++;
sex_map.insert(pair<gender, Slav *>(this->gender(), this);
}

vector<Slav *> Slav::get_sex_vec(sex g){
vector<Slav *> tmp;
for(map<gender, Slav *>::iterator i(sex_map.begin()); i != sex_map.end(); i++)
if(i->first == g)
tmp.push_back(i->second);
return tmp;
}

sex Slav::gender(){
return (_name.back() == 'e' || _name.back() == 'a') ? female : male;
}

string Slav::description()
{
return string(" ") + _name + " [" + to_string(_id) + "]";
}
}
7 changes: 6 additions & 1 deletion Slav.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@ class Slav

static std::vector <string> names;
static int _counter;
static map<gender, Slav *> sex_map;

string _name;
int _id;

public:

Slav();

static int counter() { return _counter; }

static vector<Slav *> get_sex_vec(sex g);

string name() { return _name; }
int id() { return _id; }
enum sex {male, female};

string description();
sex gender();
};
46 changes: 38 additions & 8 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#include "Slav.h"

Expand Down Expand Up @@ -42,27 +43,47 @@ void containers(Slav * slavs, int n)
vector <Slav *> vectorOfSlavs;
set <Slav *> setOfSlavs;
map <Slav *, Slav *> mapOfSlavs;


vector<Slav *>::iterator v_it_slv;
set<Slav *>::iterator s_it_slv;
map<Slav *, Slav *>::iterator m_it_slv;

printf("# Containers\n");
REPORT_CONTAINERS;
printf("## vector\n");

// Umieść Słowian w losowej kolejności w wektorze.
for(int i(0); i < n; i++)
{
v_it_slv = vectorOfSlavs.begin() + rand() % (vectorOfSlavs.size() + 1);
vectorOfSlavs.insert(v_it_slv, &slavs[i]);
}

// Wykorzystując iterator i funkcję description(), wyświetl wszystkich Słowian w wektorze

for(v_it_slv = vectorOfSlavs.begin(); v_it_slv != vectorOfSlavs.end(); v_it_slv++)
vectorOfSlavs[vectorOfSlavs.begin() - v_it_slv]->description();
REPORT_CONTAINERS;
printf("## set\n");

// Przenieś wszystkich Słowian z wektoru do zbioru.

for(int i(0); i < n; i++)
setOfSlavs.insert(s_it_slv++, vectorOfSlavs[n]);
//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

s_it_slv = setOfSlavs.begin();
for(int i(0); i < n/2; i++)
{
mapOfSlavs.insert(pair<Slav *, Slav *>(*setOfSlavs.begin(), *setOfSlavs.begin() + 1));
setOfSlavs.erase(setOfSlavs.begin());
setOfSlavs.erase(setOfSlavs.begin());
}

// Wykorzystując iterator, wyświetl wszystkie pary Słowian

for(m_it_slv = mapOfSlavs.begin(); m_it_slv != mapOfSlavs.end(); m_it_slv++)
printf("%s - %s\n", (m_it_slv->first)->description(), (m_it_slv->second)->description());//#############################################################################################
REPORT_CONTAINERS;
}

Expand All @@ -76,16 +97,25 @@ void adapters(Slav * slavs, int n)
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.back());
queueOfSlavs.pop();
}
REPORT_ADAPTERS;

// Wyświetl Słowian zdejmowanych ze stosu.

for(int i(0); i < n; i++)
{
printf("%s", (stackOfSlavs.top())->description());
stackOfSlavs.pop();
}
REPORT_ADAPTERS;
}

Expand Down

0 comments on commit d44f16a

Please sign in to comment.