Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Meno a priezvisko:
- POKYNY:
- (1) Subor premenujte na Priezvisko_Meno_ID_pisomka2.cpp (pouzite vase udaje bez diakritiky).
- (2) Implementujte funkcie, metody, konstruktory a operatory tak, aby splnali popis (pri ich deklaraciach).
- (3) Cela implementacia musi byt v tomto jednom subore.
- (4) Odovzdajte len tento (spravne premenovany) zdrojovy subor.
- (5) Program musi byt kompilovatelny.
- (6) Globalne a staticke premenne su zakazane.
- (7) V ziadnom pripade nemente deklaracie funkcii, ktore mate za ulohu naprogramovat
- (nemente nazvy, navratove hodnoty ani typ a pocet parametrov v zadanych funkciach).
- Nemente implementacie zadanych datovych typov, ani implementacie hotovych pomocnych funkcii
- (ak nie je v zadani ulohy uvedene inak).
- (8) V pripade potreby mozete kod doplnit o dalsie pomocne funkcie alebo struktury.
- (9) Vase riesenie otestujte (vo funkcii 'main' a pomocou doplnenych pomocnych funkcii alebo struktur).
- Testovaci kod ale nebude hodnoteny.
- (10) Funkcia 'main' musi byt v zdrojovom kode posledna.
- */
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <list>
- #include <algorithm>
- #include <cctype>
- #include <string>
- using namespace std;
- //-------------------------------------------------------------------------------------------------
- // 1. ULOHA (3 body)
- //-------------------------------------------------------------------------------------------------
- /*
- Do triedy 'Rectangle' doplnte:
- a) metodu 'getArea', ktora vrati obsah obdlznika
- Testovaci kod pre tuto ulohu musi byt kompilovatelny. Odkomentujte ho.
- b) kopirovaci konstruktor,
- ktory v novovytvorenom objekte nastavi sirku aj vysku o 1 vacsiu ako su v objekte, ktoreho kopia sa vytvara.
- c) operator porovnania ('operator==')
- Dva objekty triedy 'Rectangle' su rovnake, ak maju rovnake sirky a rovnake vysky.
- Mozete ho implementova ako clensky, alebo globalny.
- Testovaci kod pre tuto ulohu musi byt kompilovatelny. Odkomentujte ho.
- Poznamka: atributy su verejne len kvoli testovaciemu kodu.
- */
- class Rectangle {
- public:
- int width; // sirka obdlznika
- int height; // vyska obdlznika
- Rectangle(int width, int height) : width(width) , height(height) {
- }
- // TODO 1a), 1b), 1c)
- //double getArea(){}
- double getArea(){
- return (double)width * (double)height;
- }
- Rectangle(const Rectangle &x) { width = x.width + 1; height= x.height + 1; }
- };
- //-------------------------------------------------------------------------------------------------
- // 2. ULOHA (1 bod)
- //-------------------------------------------------------------------------------------------------
- /*
- Implementujte funkciu 'transformToUpper' tak, aby skonvertovala vsetky pismena vo vektore 'text' na velke.
- Vektor 'text' moze obsahovat lubovolne znaky, nie len pismena.
- Odporucanie: pouzite 'std::transform' a 'toUpper'.
- */
- // pomocna funkcia
- int toUpper(unsigned char c) noexcept {
- return std::toupper(c);
- }
- void transformToUpper(vector<char> & text) noexcept {
- for (int i = 0; i < text.size(); i++)
- {
- text[i] = toUpper(text[i]);
- }
- }
- //-------------------------------------------------------------------------------------------------
- // 3. ULOHA (1 bod)
- //-------------------------------------------------------------------------------------------------
- /*
- Funkcia 'divide' vykona celociselne delenie a vrati vysledok.
- Doplnte implementaciu funkcie 'divide' tak, aby v pripade delenia nulou, vyhodila vynimku typu 'DivisionByZeroException'.
- Poznamka: funkcia vynimku vyhodi, ale nezachytava.
- PARAMETRE:
- [in] divident - delenec
- [in] divisor - delitel
- RETURN:
- Podiel (divident/divisor)
- EXCEPTION:
- Ak je 'divisor' (delitel) nula, tak vyhodi vynimku typu 'DivisionByZeroException'.
- Odkomentujte testovaci kod.
- */
- class DivisionByZeroException {
- };
- int divide(int divident, int divisor) {
- if (divisor == 0)
- {
- throw DivisionByZeroException();
- }
- else
- {
- int podiel = 0;
- podiel = divident / divisor;
- return podiel;
- }
- //return divident / divisor;
- }
- //-------------------------------------------------------------------------------------------------
- // 4. ULOHA ( 2 body)
- //-------------------------------------------------------------------------------------------------
- // Datove typy pre 4. a 5. ulohu
- // Uzol binarneho vyhladavacieho stromu
- struct Node {
- int value; // hodnota uzla
- Node *left; // mensie prvky
- Node *right; // vacsie prvky
- };
- // Strom
- struct Tree {
- Node *root; // koren stromu
- };
- /*
- Implementujte funkciu 'all', ktora vrati usporiadany zoznam vsetkych prvkov v strome 'tree'.
- Usporiadanie vystupneho zoznamu je od najmensieho prvku po najvacsi.
- Usporiadanie dosiahnite prechodom stromu do hlbky, v poradi inorder.
- Pre kazdy uzol stromu plati, ze v jeho lavom podstrome su prvky s mensou hodnotou, v pravom podstrome prvky s vacsou hodnotou.
- Ak sa rozhodnete pre rekurzivnu implementaciu, tak mozete vytvorit a zavolat dalsiu funkciu,
- v ktorej bude prevazna cast implementacie.
- */
- list<int> all(const Tree * tree) {
- /*list<int> zoznam;
- if (tree->root)
- {
- if()
- }*/
- return list<int>(); // tento riadok zmente podla zadania, je tu len kvoli kompilacii
- }
- //-------------------------------------------------------------------------------------------------
- // 5. ULOHA ( 3 body)
- //-------------------------------------------------------------------------------------------------
- /*
- Implementujte funkciu 'add', ktora prida novy uzol s hodnotou 'value' do binarneho vyhladavacieho stromu 'tree'.
- Ak sa 'value' v strome 'tree' uz nachadza, tak funkcia neprida novy uzol (nezmeni strom).
- Ak sa rozhodnete pre rekurzivnu implementaciu, tak mozete vytvorit a zavolat dalsiu funkciu,
- v ktorej bude prevazna cast implementacie.
- */
- Node* addNodeRecursive(Node* root, const int value)
- {
- if (root)
- {
- if (value < root->value)
- {
- root->left = addNodeRecursive(root->left, value);
- }
- else if (value > root->value)
- {
- root->right = addNodeRecursive(root->right, value);
- }
- else
- {
- throw string{ "Note: uzol sa uz v strome nachadza" };
- }
- return root;
- }
- return new Node{ value, nullptr, nullptr };
- }
- void add(Tree* tree, const int value)
- {
- try {
- tree->root = addNodeRecursive(tree->root, value);
- }
- catch (const string& e)
- {
- cout << e << endl;
- }
- }
- //-------------------------------------------------------------------------------------------------
- // TESTOVANIE
- //-------------------------------------------------------------------------------------------------
- // pomocny operator na vypis obsahu vektora znakov
- ostream& operator<<(ostream& out, const vector<char> vector) {
- for_each(vector.begin(), vector.end(), [&out](const char letter)->void{ out << letter; });
- return out;
- }
- // pomocny operator na vypis obsahu zoznamu cisiel
- ostream& operator<<(ostream& out, const list<int> list) {
- for_each(list.begin(), list.end(), [&out](const int number)->void{ out << number << " "; });
- return out;
- }
- // ...........................................................
- // TEST ULOHY 1 a
- // ...........................................................
- void testUloha1a() {
- cout << "TESTY ULOHA 1a (getArea):" << endl;
- cout << "--------------------------------------------------" << endl;
- Rectangle r1(10, 20);
- Rectangle r2(2, 4);
- cout << "odkomentujte testovaci kod (a tento vypis vymazte)" << endl << endl;
- cout << "r1.getArea(): " << r1.getArea() << " (spravna hodnota: 200) " << endl;
- cout << "r2.getArea(): " << r2.getArea() << " (spravna hodnota: 8) " << endl << endl;
- }
- // ...........................................................
- // TEST ULOHY 1 b
- // ...........................................................
- void testUloha1b() {
- cout << "TESTY ULOHA 1b (kopirovaci konstruktor):" << endl;
- cout << "--------------------------------------------------" << endl;
- Rectangle r1(10, 20);
- Rectangle r2(2, 4);
- Rectangle r3(r1);
- Rectangle r4(r3);
- Rectangle r5(r2);
- cout << "r3.width = " << r3.width << " (spravna hodnota: 11) " << endl;
- cout << "r3.height = " << r3.height << " (spravna hodnota: 21) " << endl << endl;
- cout << "r4.width = " << r4.width << " (spravna hodnota: 12) " << endl;
- cout << "r4.height = " << r4.height << " (spravna hodnota: 22) " << endl << endl;
- cout << "r5.width = " << r5.width << " (spravna hodnota: 3) " << endl;
- cout << "r5.height = " << r5.height << " (spravna hodnota: 5) " << endl << endl;
- }
- // ...........................................................
- // TEST ULOHY 1 c
- // ...........................................................
- void testUloha1c() {
- cout << "TESTY ULOHA 1c (operator==):" << endl;
- cout << "--------------------------------------------------" << endl;
- Rectangle r1(1, 2);
- Rectangle r2(2, 2);
- Rectangle r3(2, 1);
- Rectangle r4(1, 2);
- cout << "odkomentujte testovaci kod (a tento vypis vymazte)" << endl << endl;
- //cout << "(r1 == r1): " << setw(5) << left << (r1 == r1) << " (spravna hodnota: true) " << endl;
- //cout << "(r1 == r2): " << setw(5) << left << (r1 == r2) << " (spravna hodnota: false) " << endl;
- //cout << "(r1 == r3): " << setw(5) << left << (r1 == r3) << " (spravna hodnota: false) " << endl;
- //cout << "(r1 == r4): " << setw(5) << left << (r1 == r4) << " (spravna hodnota: true) " << endl << endl;
- }
- // ...........................................................
- // TEST ULOHY 2
- // ...........................................................
- void testUloha2() {
- cout << "TESTY ULOHA 2 (transformToUpper):" << endl;
- cout << "--------------------------------------------------" << endl;
- vector<char> a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
- cout << "a) pred volanim transformToUpper: " << a << endl;
- transformToUpper(a);
- cout << " po zavolani transformToUpper : " << a << endl << endl;
- vector<char> b = {'A', '1', 'b', '?', '/', 'C', 'd', 'E'};
- cout << "b) pred volanim transformToUpper: " << b << endl;
- transformToUpper(b);
- cout << " po zavolani transformToUpper : " << b << endl << endl;
- vector<char> c;
- cout << "c) pred volanim transformToUpper: " << c << endl;
- transformToUpper(c);
- cout << " po zavolani transformToUpper : " << c << endl << endl;
- }
- // ...........................................................
- // TEST ULOHY 3
- // ...........................................................
- void testUloha3() {
- cout << "TESTY ULOHA 3 (divide):" << endl;
- cout << "--------------------------------------------------" << endl;
- cout << "odkomentujte testovaci kod (a tento vypis vymazte)" << endl << endl;
- cout << "test vratenia spravnej hodnoty: " << endl;
- cout << " divide( 12, 5): " << setw(2) << right << divide(12, 5) << " (spravna hodnota: 2)" << endl;
- cout << " divide(100, -20): " << setw(2) << right << divide(100, -20) << " (spravna hodnota: -5)" << endl << endl;
- cout << "test vyhodenia vynimky: " << endl;
- try {
- divide(10, 0);
- cout << " FAILED: volanie 'divide(10, 0)' nevyhodilo vynimku" << endl << endl;
- }
- catch(const DivisionByZeroException& ) {
- cout << " PASSED: volanie 'divide(10, 0)' vyhodilo spravny typ vynimky" << endl << endl;
- }
- catch(...) {
- cout << " FAILED: volanie 'divide(10, 0)' vyhodilo nespravny typ vynimky" << endl << endl;
- }
- }
- // ...........................................................
- // TEST ULOHY 4
- // ...........................................................
- Tree* createTreeA() {
- return new Tree{nullptr};
- }
- Tree* createTreeB() {
- return new Tree{
- new Node { 10,
- nullptr,
- nullptr
- }
- };
- }
- Tree* createTreeC() {
- return new Tree{
- new Node { 10,
- new Node { 5,
- nullptr,
- nullptr
- },
- nullptr
- }
- };
- }
- Tree* createTreeD() {
- return new Tree{
- new Node { 10,
- nullptr,
- new Node { 15,
- nullptr,
- nullptr
- }
- }
- };
- }
- Tree* createTreeE() {
- return new Tree{
- new Node { 10,
- new Node { 5,
- nullptr,
- nullptr
- },
- new Node{ 15,
- nullptr,
- nullptr
- }
- }
- };
- }
- Tree* createTreeF() {
- return new Tree{
- new Node { 10,
- new Node{ 5,
- new Node { 2,
- nullptr,
- nullptr
- },
- new Node { 8,
- nullptr,
- nullptr
- }
- },
- new Node{ 15,
- new Node { 12,
- nullptr,
- nullptr
- },
- new Node { 20,
- nullptr,
- nullptr
- }
- }
- }
- };
- }
- Tree* createTreeG() {
- return new Tree{
- new Node { 10,
- new Node{ 5,
- new Node { 2,
- new Node { 1,
- nullptr,
- nullptr
- },
- new Node { 3,
- nullptr,
- nullptr
- }
- },
- new Node { 8,
- new Node { 7,
- nullptr,
- nullptr
- },
- new Node { 9,
- nullptr,
- nullptr
- }
- }
- },
- new Node{ 15,
- new Node { 12,
- nullptr,
- nullptr
- },
- new Node { 20,
- new Node { 18,
- nullptr,
- nullptr
- },
- new Node { 25,
- nullptr,
- nullptr
- }
- }
- }
- }
- };
- }
- Tree* createTreeH() {
- return new Tree{
- new Node { 10,
- new Node{ 5,
- new Node { 2,
- new Node { 1,
- nullptr,
- nullptr
- },
- new Node { 3,
- nullptr,
- new Node { 4,
- nullptr,
- nullptr
- }
- }
- },
- new Node { 8,
- new Node { 7,
- nullptr,
- nullptr
- },
- new Node { 9,
- nullptr,
- nullptr
- }
- }
- },
- new Node{ 15,
- new Node { 12,
- nullptr,
- nullptr
- },
- new Node { 20,
- new Node { 18,
- new Node { 17,
- nullptr,
- nullptr
- },
- nullptr
- },
- new Node { 25,
- nullptr,
- nullptr
- }
- }
- }
- }
- };
- }
- void testUloha4() {
- cout << "TESTY ULOHA 4 (all):" << endl;
- cout << "--------------------------------------------------" << endl;
- const Tree *treeA = createTreeA();
- const list<int> listA = all(treeA);
- cout << "a) Vas vystup: " << listA << endl;
- cout << " Spravny vystup: " << endl << endl;
- const Tree *treeB = createTreeB();
- const list<int> listB = all(treeB);
- cout << "b) Vas vystup: " << listB << endl;
- cout << " Spravny vystup: 10" << endl << endl;
- const Tree *treeC = createTreeC();
- const list<int> listC = all(treeC);
- cout << "c) Vas vystup: " << listC << endl;
- cout << " Spravny vystup: 5 10" << endl << endl;
- const Tree *treeD = createTreeD();
- const list<int> listD = all(treeD);
- cout << "d) Vas vystup: " << listD << endl;
- cout << " Spravny vystup: 10 15" << endl << endl;
- const Tree *treeE = createTreeE();
- const list<int> listE = all(treeE);
- cout << "e) Vas vystup: " << listE << endl;
- cout << " Spravny vystup: 5 10 15" << endl << endl;
- const Tree *treeF = createTreeF();
- const list<int> listF = all(treeF);
- cout << "f) Vas vystup: " << listF << endl;
- cout << " Spravny vystup: 2 5 8 10 12 15 20" << endl << endl;
- const Tree *treeG = createTreeG();
- const list<int> listG = all(treeG);
- cout << "g) Vas vystup: " << listG << endl;
- cout << " Spravny vystup: 1 2 3 5 7 8 9 10 12 15 18 20 25" << endl << endl;
- const Tree *treeH = createTreeH();
- const list<int> listH = all(treeH);
- cout << "h) Vas vystup: " << listH << endl;
- cout << " Spravny vystup: 1 2 3 4 5 7 8 9 10 12 15 17 18 20 25" << endl << endl;
- }
- // ...........................................................
- // TEST ULOHY 5
- // ...........................................................
- string testLeaf(const Node *node, const int value) {
- if(node == nullptr) {
- return "FAILED: uzol neexistuje";
- }
- else if(node->value != value) {
- return "FAILED: chybna hodnota uzla";
- }
- else if(node->left != nullptr) {
- return "FAILED: left nie je nullptr";
- }
- else if(node->right != nullptr) {
- return "FAILED: right nie je nullptr";
- }
- else {
- return "PASSED: uzol je listom so spravnou hodnotou";
- }
- }
- void testUloha5() {
- cout << "TESTY ULOHA 5 (add):" << endl;
- cout << "--------------------------------------------------" << endl;
- Tree *treeA = createTreeA();
- add(treeA, 10);
- cout << "treeA->root " << testLeaf(treeA->root, 10) << endl;
- Tree *treeB1 = createTreeB();
- add(treeB1, 5);
- cout << "treeB1->root->left " << testLeaf(treeB1->root->left, 5) << endl;
- Tree *treeB2 = createTreeB();
- add(treeB2, 15);
- cout << "treeB2->root->right " << testLeaf(treeB2->root->right, 15) << endl;
- Tree *treeB3 = createTreeB();
- add(treeB3, 10);
- cout << "treeB3->root " << testLeaf(treeB3->root, 10) << endl;
- Tree *treeC1 = createTreeC();
- add(treeC1, 15);
- cout << "treeC1->root->right " << testLeaf(treeC1->root->right, 15) << endl;
- Tree *treeD1 = createTreeD();
- add(treeD1, 5);
- cout << "treeD1->root->left " << testLeaf(treeD1->root->left, 5) << endl;
- Tree *treeC2 = createTreeC();
- add(treeC2, 2);
- cout << "treeC2->root->left->left " << testLeaf(treeC2->root->left->left, 2) << endl;
- Tree *treeD2 = createTreeD();
- add(treeD2, 20);
- cout << "treeD2->root->right->right " << testLeaf(treeD2->root->right->right, 20) << endl;
- Tree *treeE1 = createTreeE();
- add(treeE1, 2);
- cout << "treeE1->root->left->left " << testLeaf(treeE1->root->left->left, 2) << endl;
- Tree *treeE2 = createTreeE();
- add(treeE2, 8);
- cout << "treeE2->root->left->right " << testLeaf(treeE2->root->left->right, 8) << endl;
- Tree *treeE3 = createTreeE();
- add(treeE3, 12);
- cout << "treeE3->root->right->left " << testLeaf(treeE3->root->right->left, 12) << endl;
- Tree *treeE4 = createTreeE();
- add(treeE4, 20);
- cout << "treeE4->root->right->right " << testLeaf(treeE4->root->right->right, 20) << endl;
- Tree *treeE5 = createTreeE();
- add(treeE5, 10);
- cout << "treeE5->root " << (treeE5->root->value == 10 ? "PASSED" : "FAILED: chybna hodnota") << endl;
- cout << "treeE5->root->left " << testLeaf(treeE5->root->left, 5) << endl;
- cout << "treeE5->root->right " << testLeaf(treeE5->root->right, 15) << endl;
- Tree *treeE6 = createTreeE();
- add(treeE6, 5);
- cout << "treeE6->root " << (treeE6->root->value == 10 ? "PASSED" : "FAILED: chybna hodnota") << endl;
- cout << "treeE6->root->left " << testLeaf(treeE6->root->left, 5) << endl;
- cout << "treeE6->root->right " << testLeaf(treeE6->root->right, 15) << endl;
- Tree *treeE7 = createTreeE();
- add(treeE7, 15);
- cout << "treeE7->root " << (treeE7->root->value == 10 ? "PASSED" : "FAILED: chybna hodnota") << endl;
- cout << "treeE7->root->left " << testLeaf(treeE7->root->left, 5) << endl;
- cout << "treeE7->root->right " << testLeaf(treeE7->root->right, 15) << endl;
- Tree *treeG1 = createTreeG();
- add(treeG1, 0);
- cout << "treeG1->root->left->left->left->left" << endl << " " << testLeaf(treeG1->root->left->left->left->left, 0) << endl;
- Tree *treeG2 = createTreeG();
- add(treeG2, 4);
- cout << "treeG2->root->left->left->right->right " << endl << " " << testLeaf(treeG2->root->left->left->right->right, 4) << endl;
- Tree *treeG3 = createTreeG();
- add(treeG3, 17);
- cout << "treeG3->root->right->right->left->left " << endl << " " << testLeaf(treeG3->root->right->right->left->left, 17) << endl;
- Tree *treeG4 = createTreeG();
- add(treeG4, 30);
- cout << "treeG4->root->right->right->right->right " << endl << " " << testLeaf(treeG4->root->right->right->right->right, 30) << endl;
- Tree *treeG5 = createTreeG();
- add(treeG5, 18);
- cout << "treeG5->root->right->right->left " << endl << " " << testLeaf(treeG5->root->right->right->left, 18) << endl;
- }
- // tu mozete doplnit pomocne testovacie funkcie a struktury
- int main() {
- cout << boolalpha; // aby true a false vypisoval ako "true" a "false", nie 1 a 0
- testUloha1a();
- testUloha1b();
- testUloha1c();
- testUloha2();
- testUloha3();
- testUloha4();
- testUloha5();
- // tu mozete doplnit testovaci kod
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement