From 60d90aad81225d250111747801a2f58a44b20e78 Mon Sep 17 00:00:00 2001 From: Xsenonn <225974@student.pwr.edu.pl> Date: Thu, 21 Apr 2016 20:30:48 +0200 Subject: [PATCH] =?UTF-8?q?Rozwi=C4=85zanie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++-- Slav.cpp | 6 ++++++ Slav.h | 10 +++++++++ main.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index c05bca8..f8e6e38 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,3 @@ all: - g++ *.cpp -o lab3 - ./lab3 3 \ No newline at end of file + g++ -std=c++11 *.cpp -o lab3 + ./lab3 9 \ No newline at end of file diff --git a/Slav.cpp b/Slav.cpp index 67f2636..fbacd02 100644 --- a/Slav.cpp +++ b/Slav.cpp @@ -1,5 +1,6 @@ #include "Slav.h" #include +#include using namespace std; @@ -26,4 +27,9 @@ Slav::Slav() string Slav::description() { return string(" ") + _name + " [" + to_string(_id) + "]"; +} + +gender Slav::Sex() +{ + return (_name[_name.length()-1]=='a') ? woman : man; } \ No newline at end of file diff --git a/Slav.h b/Slav.h index 04be0e7..a90e0b8 100644 --- a/Slav.h +++ b/Slav.h @@ -1,9 +1,17 @@ #pragma once #include #include +#include +#include +#include +#include +#include + using namespace std; +enum gender {man, woman}; + class Slav { private: @@ -24,4 +32,6 @@ class Slav int id() { return _id; } string description(); + + gender Sex(); }; diff --git a/main.cpp b/main.cpp index 42c9a59..138ef8c 100644 --- a/main.cpp +++ b/main.cpp @@ -22,9 +22,11 @@ void showMeAdapterSizes(queue , stack ); void containers(Slav *, int); void adapters(Slav *, int); +void Show_Sex(Slav*, int); int main(int argc, char const *argv[]) { + srand(time(NULL)); int n = 2 * atoi(argv[1]); Slav *slavs = new Slav[n]; cout << "# Generated Slavs" << endl; @@ -33,6 +35,7 @@ int main(int argc, char const *argv[]) containers(slavs, n); adapters(slavs, n); + Show_Sex(slavs, n); delete [] slavs; } @@ -48,21 +51,53 @@ void containers(Slav * slavs, int n) printf("## vector\n"); // Umieść Słowian w losowej kolejności w wektorze. + vectorOfSlavs.push_back(slavs); + for(int i=1 ; i::iterator itr; + for( itr = vectorOfSlavs.begin();itr != vectorOfSlavs.end(); itr++) + { + cout << (*itr) -> description() << endl; + } REPORT_CONTAINERS; printf("## set\n"); // Przenieś wszystkich Słowian z wektoru do zbioru. + for(int i = n-1; i>=0 ; i--) + { + setOfSlavs.insert(vectorOfSlavs[i]); + vectorOfSlavs.pop_back(); + } + 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 itr_s = setOfSlavs.begin(); + set< Slav* >::iterator itr_removal_s = setOfSlavs.begin(); + while(itr_s != setOfSlavs.end()) + { + while(itr_s !=setOfSlavs.end()) + { + mapOfSlavs[*itr_s++] = *itr_s++; + setOfSlavs.erase(itr_removal_s++); + setOfSlavs.erase(itr_removal_s++); + } + } // Wykorzystując iterator, wyświetl wszystkie pary Słowian - + map ::iterator itr_m = mapOfSlavs.begin(); + + while(itr_m != mapOfSlavs.end()) + { + cout << (itr_m -> first) -> description() << " -> " <<(itr_m -> second) -> description() << endl; + itr_m++; + } + REPORT_CONTAINERS; } @@ -76,18 +111,42 @@ 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. + while(queueOfSlavs.empty() == 0) + { + stackOfSlavs.push(queueOfSlavs.front()); + queueOfSlavs.pop(); + } + REPORT_ADAPTERS; // Wyświetl Słowian zdejmowanych ze stosu. + while(stackOfSlavs.empty() == 0) + { + cout << stackOfSlavs.top() -> description() << endl; + stackOfSlavs.pop(); + } + REPORT_ADAPTERS; } +void Show_Sex(Slav *slavs, int n) +{ + map < gender, vector< Slav* > > mapOfGender; + for(int i = 0; i vector, set set, map map) {