Sanady

Domaca 6

Nov 10th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.66 KB | None | 0 0
  1. /*
  2. Meno a priezvisko:
  3.  
  4. POKYNY:
  5. (1)  Subor premenujte na Priezvisko_Meno_ID_du06.cpp (pouzite vase udaje bez diakritiky).
  6. (2)  Implementujte funkcie tak, aby splnali popis pri ich deklaraciach.
  7. (3)  Cela implementacia musi byt v tomto jednom subore.
  8. (4)  Odovzdajte len tento (spravne premenovany) zdrojovy subor.
  9. (5)  Program musi byt kompilovatelny.
  10. (6)  Globalne a staticke premenne su zakazane.
  11. (7)  V ziadnom pripade nemente deklaracie funkcii, ktore mate za ulohu naprogramovat
  12.      (nemente nazvy, navratove hodnoty ani typ a pocet parametrov v zadanych funkciach).
  13.      Nemente implementacie zadanych datovych typov, ani implementacie hotovych pomocnych funkcii
  14.      (ak nie je v zadani ulohy uvedene inak).
  15. (8)  V pripade potreby mozete kod doplnit o dalsie pomocne funkcie alebo struktury.
  16. (9)  Vase riesenie otestujte (vo funkcii 'main' a pomocou doplnenych pomocnych funkcii alebo struktur).
  17.      Testovaci kod ale nebude hodnoteny.
  18. (10) Funkcia 'main' musi byt v zdrojovom kode posledna.
  19. */
  20.  
  21. #include <iostream>
  22. #include <cstdlib>
  23. #include <cstring>
  24. #include <cassert>
  25.  
  26. using namespace std;
  27.  
  28. //-------------------------------------------------------------------------------------------------
  29. // ULOHA (0.1 boda)
  30. //-------------------------------------------------------------------------------------------------
  31. /*
  32.     Do deklaracie funkcie doplnte implicitne hodnoty parametrov.
  33.     Pre parameter 'a' nech je implicitna hodnota 10,
  34.     pre parameter 'b' nech je implicitna hodnota 20.
  35. */
  36. int sucet(int a = 10, int b = 20) {
  37.     return a+b;
  38. }
  39.  
  40. //-------------------------------------------------------------------------------------------------
  41. // ULOHA (0.9 boda)
  42. //-------------------------------------------------------------------------------------------------
  43. /*
  44.     Trieda 'String' reprezentuje textovy retazec. Doplnte jej implementaciu podla zadania nizsie.
  45.  
  46.     Trieda implementuje textovy retazec, polom prvkov typu 'char', zakoncenym hodnotou '\0' (ako v jazyku C).
  47.     Adresa tohoto pola je ulozena v atribute 'data'.
  48.     Atribut 'data' musi byt sukromny (v casti 'private').
  49.     Ak do implementacie triedy budete pridavat dalsie atributy, tak atribut 'data' musi zostat ako prvy atribut
  50.     (pridanie dalsich atributov nie je nutne).
  51.  
  52.     Vytvorte verejne (v casti public) konstruktory, destruktory a metody (kazda poduloha je za 0.1 boda):
  53.  
  54.     a)  Vytvorte konstruktor bez parametrov.
  55.         Tento konstruktor vytvori objekt reprezentujuci prazdny textovy retazec.
  56.  
  57.     b)  Vytvorte konstruktor s parametrom typu 'const char *'.
  58.         Tento konstruktor vytvori objekt reprezentujuci textovy retazec, ktory je kopiou vstupneho parametra.
  59.  
  60.     c)  Vytvorte kopirovaci konstruktor. Tento konstruktor vytvori hlboku kopiu.
  61.  
  62.     d)  Vytvorte metodu 'size_t getLength() const', ktora vrati pocet znakov v textovom retazci (bez '\0').
  63.  
  64.     e)  Vytvorte metodu 'char getChar(const size_t index) const'.
  65.         Vstupny parameter je indexom znaku v textovom retazci (prvy znak je na pozicii s indexom nula).
  66.         Metoda vratich znak, ktory sa nachadza na mieste urcenom indexom.
  67.         Ak je 'index' mimo rozsahu, alebo textovy retazec neobsahuje ziadne znaky, tak metoda vrati '\0'.
  68.  
  69.     f)  Vytvorte metodu 'const char * toCString() const'.
  70.         Metoda vrati smernik na C-ckovsku reprezentaciu textoveho retazca.
  71.         Implementacia je jednoducha, metoda vrati adresu v atribute 'data' (kopiu adresy).
  72.         Poznamka: Kedze (konstantny) typ navratvej hodnoty zabranuje zmene obsahu textoveho retazca, nevytvarajte kopiu textu.
  73.  
  74.     g)  Vytvorte metodu 'void set(const char *text)',
  75.         ktora nastavi novu hodnotu textoveho retazca podla vstupneho parametra.
  76.         Nezabudnite dealokovat nepotrebnu pamat.
  77.  
  78.     h)  Vytvorte metodu 'void append(const char *text)', ktora prida na koniec 'text', ktory je vstupnym parametrom.
  79.         Nezabudnite dealokovat nepotrebnu pamat.
  80.  
  81.     i)  Vytvorte destruktor, ktory v pripade potreby dealokuje pamat.
  82.  
  83.     Pre alokaciu a dealokaciu poli pouzivajte new[] a delete[].
  84.  
  85.     Funkcia 'basicTestString' je urcena na test spravnej deklaracie konstruktorov, metod a destruktora.
  86.     Postupne v nej odkomentuj jednotlive riadky. Tieto musia byt po dokonceni vypracovania kompilovatelne.
  87.     Funkcia testuje spravnost funkcnosti len ciastocne. Vytvorte dalsie testy pre overenenie funkcnosti.
  88. */
  89.  
  90. class String { // Prve tri riadky deklaracie triedy musia zostat nezmenene
  91.     private:
  92.         char *data; // obsah textoveho retazca (ak pridate dalsie atributy, tak tento musi zostat prvym atributom)
  93.     public:
  94.         String()
  95.         {
  96.             this->data = new char;
  97.             this->data[0] = '\0';
  98.         }
  99.  
  100.         String(const char *input)
  101.         {
  102.             this->data = new char[strlen(input) + 1];
  103.             strcpy(this->data, input);
  104.             cout << this->data;
  105.         }
  106.  
  107.         String(const String& str)
  108.         {
  109.             this->data = new char[strlen(str.data) + 1];
  110.             strcpy(this->data, str.data);
  111.         }
  112.  
  113.         size_t getLength() const
  114.         {
  115.             return strlen(this->data);
  116.         }
  117.  
  118.         char getChar(const size_t index) const
  119.         {
  120.             if((index >= getLength()) || index < 0)
  121.             {
  122.                 return '\0';
  123.             }
  124.             else
  125.             {
  126.                 return this->data[index];
  127.             }
  128.         }
  129.  
  130.         const char* toCString() const
  131.         {
  132.             return this->data;
  133.         }
  134.  
  135.         void set(const char *setter)
  136.         {
  137.             delete[] this->data;
  138.             this->data = new char[strlen(setter) + 1];
  139.             strcpy(this->data, setter);
  140.         }
  141.  
  142.         void append(const char *app)
  143.         {
  144.             if(getLength() > 0)
  145.             {
  146.                 char *tmp = new char[strlen(this->data) + 1];
  147.                 strcpy(tmp, this->data);
  148.                 delete[] this->data;
  149.                 this->data = new char[strlen(tmp) + strlen(app) + 1];
  150.                 strcpy(this->data, tmp);
  151.                 strcpy(this->data, app);
  152.                 delete[] tmp;
  153.             }
  154.             else
  155.             {
  156.                 set(app);
  157.             }
  158.         }
  159.  
  160.         ~String()
  161.         {
  162.             delete[] this->data;
  163.         }
  164.     // TODO (tu pridajte kod implementujuci triedu)
  165. };
  166.  
  167. //-------------------------------------------------------------------------------------------------
  168. // TESTOVANIE
  169. //-------------------------------------------------------------------------------------------------
  170.  
  171. void basicTestString() {
  172.     // a)
  173.     String str1;
  174.  
  175.     //b)
  176.     String str2("hello world");
  177.  
  178.     // c)
  179.     String str3(str2);
  180.  
  181.     // d)
  182.     size_t length1 = str1.getLength();
  183.     assert(length1 == 0);
  184.     size_t length2 = str2.getLength();
  185.     assert(length2 == 11);
  186.  
  187.     // e)
  188.     char letter1 = str1.getChar(0);
  189.     assert(letter1 == '\0');
  190.     char letter2 = str2.getChar(0);
  191.     assert(letter2 == 'h');
  192.     char letter3 = str2.getChar(1000);
  193.     assert(letter3 == '\0');
  194.  
  195.     // f)
  196.     const char *cstr1 = str1.toCString();
  197.     assert(cstr1[0] == '\0');
  198.     const char *cstr2 = str2.toCString();
  199.     assert(std::strcmp(cstr2, "hello world") == 0);
  200.  
  201.     // g)
  202.     str2.set("HELLO WORLD AGAIN");
  203.     assert(std::strcmp(str2.toCString(), "HELLO WORLD AGAIN") == 0);
  204.  
  205.     // h)
  206.     str2.append("dalsi text");
  207.     assert(std::strcmp(str2.toCString(), "HELLO WORLD AGAINdalsi text") == 0);
  208.  
  209.     assert(std::strcmp(str3.toCString(), "hello world") == 0);
  210. }
  211.  
  212. // tu mozete doplnit pomocne testovacie funkcie a struktury
  213.  
  214. int main() {
  215.  
  216.     // tu mozete doplnit testovaci kod
  217.  
  218.     basicTestString();
  219.  
  220.     return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment