Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. // Nazwa pliku wykonywalnego: StringStory
  2.  
  3. // Prosze dopisac kod, dodac nowe pliki, tak aby program wykonywal
  4. // sie, a wynik jego dzialania byl taki sam jak podany na końcu tego
  5. // pliku.
  6.  
  7. // !!! UWAGA !!!
  8. // Przy wykonaniu tego zadania nie wolno korzystac z std::string!
  9.  
  10. // Prosze napisac klase MyString. Klasa ta posluzyc ma do ulatwienia
  11. // operacji na napisach. W tym celu do klasy dodano operatory + i *
  12. // a takze mozna obiekty tej klasy wypisywac funkcją Print().
  13. // Dostepne sa takze naturalne operatory = i == oraz [].
  14. // Trudnoscia w zadaniu jest sprawne zarzadzanie pamiecia.
  15.  
  16. // Prosze zwrocic uwage na dobre zarzadzanie pamiecia, zwlaszcza na
  17. // zwolnienie pamieci na koniec programu.
  18.  
  19. // Pliku Main.cpp, nie wolno modyfikowac.
  20.  
  21. // Ostateczny program powinien byc przyjazny dla programisty (miec
  22. // czytelny i dobrze napisany kod).
  23.  
  24. // Przy wykonaniu zadania nie wolno korzystac z internetu, notatek,
  25. // ani zadnych innych materialow (w tym wlasnych wczesniej
  26. // przygotowanych plikow oprocz makefile)
  27.  
  28. // Kody zrodlowe musza znajdowac sie w katalogu do ktorego nikt oprocz
  29. // wlasciciela nie ma praw dostepu.
  30.  
  31. // Rozwiazanie (czyli dodane pliki, makefile (lub CMakeLists.txt) i Main.cpp)
  32. // nalezy wgrac do UPEL
  33.  
  34.  
  35. #include <iostream>
  36. #include "MyString.h"
  37.  
  38. using namespace std;
  39.  
  40. int main() {
  41. MyString s0("Witaj Swiecie");
  42. MyString s1("Witaj swiecie");
  43. const MyString s2("Witaj swiecie");
  44.  
  45. if (s0 == s1 )
  46. cout << " s0 i s1 sa identyczne" << endl;
  47. if (s1 == s2 )
  48. cout << " s1 i s2 sa identyczne" << endl;
  49. if (s2 == s1 )
  50. cout << " s2 i s1 sa identyczne" << endl;
  51. // Uwaga: kolejnosc zmiennych w wyr == zmieniona aby wymuscic poprawnosc const
  52. s1.Print();
  53. MyString s3 = s2+" tu i teraz,";
  54. s3.Print();
  55. MyString s4 = s1 + ". " + s1 + " - " + s2 + " !!!";
  56. s4.Print();
  57.  
  58. MyString s5 = 2 * MyString("*");
  59. // s5.Print();
  60. /* MyString s6 = s5 + (const MyString&)s5*4;
  61. s6.Print();
  62. s6 = s1;
  63. s6.Print();
  64. s6[6] = 'S';
  65. s6.Print();
  66. */
  67. }
  68. /* wynik
  69. brachwal@vbox:[build]$ ./StringStory
  70. s1 i s2 sa identyczne
  71. s2 i s1 sa identyczne
  72. Witaj swiecie
  73. Witaj swiecie tu i teraz,
  74. Witaj swiecie. Witaj swiecie - Witaj swiecie !!!
  75. **********
  76. Witaj swiecie
  77. Witaj Swiecie
  78. */
  79.  
  80.  
  81. #ifndef MY_STRING
  82. #define MY_STRING
  83. #include <iostream>
  84. #include <string.h>
  85. using std::cout;
  86. using std::endl;
  87.  
  88. class MyString
  89. {
  90. char* s;
  91.  
  92. public:
  93. friend inline ::MyString operator*(int n,const MyString& W);
  94. MyString();
  95. MyString(const char* p);
  96. bool operator==(const MyString& P) const;
  97. void Print();
  98. MyString operator+(const MyString& W) const;
  99. ~MyString();
  100. };
  101. inline MyString MyString::operator*(int _n, const MyString& _W)
  102. {
  103. MyString _K;
  104. size_t _p = strlen(_W.s)*_n;
  105. _K.s = new char[5];
  106. cout << _K.s << endl;
  107. for (int _i=0;_i<2;_i++)
  108. {
  109. strcat (_K.s,_W.s);
  110. }
  111. cout <<_K.s<<"fdsfds\n";
  112. return _K;
  113. }
  114. #endif // mystring
  115.  
  116.  
  117.  
  118.  
  119. #include <iostream>
  120. #include "MyString.h"
  121. #include <string.h>
  122. using namespace std;
  123. MyString::MyString(){}
  124. MyString::MyString(const char* p)
  125. {
  126. int n=strlen(p);
  127. s = new char[n+1];
  128. s = strcpy(s,p);
  129. }
  130. bool MyString::operator==(const MyString& P) const
  131. {
  132. size_t n=strlen(this->s);
  133. size_t p=strlen(P.s);
  134.  
  135. if (n!=p) return 1;
  136. else
  137. {
  138. if (strncmp(this->s,P.s,n)!=0) return 0;
  139. }
  140. return 1;
  141. }
  142. void MyString::Print()
  143. {
  144. cout << s << endl;
  145. }
  146. MyString MyString::operator+(const MyString& W) const
  147. {
  148. MyString K;
  149. size_t n = strlen(s)+strlen(W.s);
  150. K.s = new char[n];
  151. cout <<"... "<< K.s <<" ..." <<endl;
  152. // char* l = this->s;
  153. // delete[] this->s;
  154. strcat(K.s,this->s);
  155. strcat(K.s,W.s);
  156. return K;
  157. }
  158. MyString::~MyString()
  159. {
  160. delete[] s;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement