Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <conio.h>
  6. #include <string>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. class String {
  12. char *s;
  13. int len;
  14. int static Stan; // liczba inicjalizowana na 0 przy starcie programu, kazda obiekt ma referencje do tej samej danej,
  15. // tj. jesli zmienisz ja na 5 to wszystkie obiekty tez beda mialy
  16. // wartosc 5, w tym przpyadku zmienna jest prywatna wiec nie mozesz jej zmienic z zewnatrz klasy
  17. public:
  18. String(void); // domyslny konstruktor: np. String abc;
  19. String(int n); // konstruktor z intem np. String abc(6);
  20. String(const char *p); // konstruktor cstringa ...
  21. String(const String& str); // konstruktor przyjmujacy inny obiekt tej samej klasy, np.
  22. // String abc;
  23. // String def(abc);
  24.  
  25. ~String() { delete s; } // destruktor
  26. int lenght() const { return(len); }
  27. String& operator =(const String &str);// operator podstawiania, czyli np.
  28. // String abc;
  29. // String def;
  30. // abc = def;
  31. void print() const { cout << s << "\nLenght: " << len << "\n"; }
  32. void operator !() const { print(); } // print, negacja
  33. // String abc;
  34. // !abc;
  35. operator char *() const { return(s); } // operator konwersji // operator dereferencji, czyli *abc;
  36. static void switch_stan(void); // statyczna metoda moze byc wywolana z obiektem lub bez, np:
  37. // String::switch_stan();
  38. // albo String asd;
  39. // asd.switch_stan();
  40. String& operator ++(void); // Zamienia małe litery na duże i zwraca nowy string
  41. // prefix operator czyli ++abc;
  42. String operator ++(int); // Zamienia małe litery na duże i zwraca stary string
  43. // postfix operator czyli abc++;
  44. String& operator --(void); // Zamienia duże litery na małe i zwraca nowy string
  45. String operator --(int); // Zamienia duże litery na małe i zwraca stary string
  46. friend String operator +(const String str1, const String str2); // tu jest friend dlatego zeby mozna bylo uzywac w globalnym scopie
  47. // czyli String abc; String def; abc + def;
  48. // rownie dobrze mozna ja zadeklarowac poza klasa, wtedy bez przedrostka friend
  49. friend String operator -(const String &d, const char &c);
  50.  
  51. };
  52.  
  53. // domyslny konstruktor
  54. String::String(void)
  55. {
  56. s = new char[81]; /// domyslna dlugosc stringa to 81 znakow ( wlasciwie 80, bo ostatni to null terminator czyli \0
  57. s[0] = 0; // przerwanie łańcucha
  58. len = 80;
  59. if (Stan)
  60. {
  61. cout << "\nPracuje konstruktor domniemany:\n";
  62. print();
  63. }
  64. }
  65.  
  66. String::String(int n)
  67. {
  68. s = new char[n + 1];
  69. s[0] = 0; // przerwanie łańcucha
  70. len = n;
  71. if (Stan)
  72. {
  73. cout << "\nPracuje konstruktor z jednym argumentem:\n";
  74. print();
  75. }
  76. }
  77.  
  78. String::String(const char *p)
  79. {
  80. len = (int)strlen(p); // rzutowanie
  81. s = new char[len + 1];
  82. strcpy(s, p);
  83. if (Stan)
  84. {
  85. cout << "\nPracuje operator konwersji:\n";
  86. print();
  87. }
  88. }
  89.  
  90. String::String(const String& str)
  91. {
  92. s = 0;
  93. (*this) = str;
  94. if (Stan)
  95. {
  96. cout << "\nPracuje konstruktor kopiujacy:\n";
  97. print();
  98. }
  99. }
  100.  
  101. String& String::operator =(const String &str)
  102. {
  103. len = str.len;
  104. if (s) delete s; // bez sensu, robimy realokacje za kazdym razem, mozna by robic ja tylko wtedy kiedy nasz bufor s jest krotszy niz dlugosc
  105. // argumentu str
  106. s = new char[len + 1];
  107. strcpy(s, str.s);
  108. if (Stan)
  109. {
  110. cout << "\nPracuje przeciazony operator podstawiania:\n";
  111. print();
  112. }
  113. return *this;
  114. }
  115.  
  116.  
  117. int String::Stan = 0;
  118.  
  119. void String::switch_stan(void)
  120. {
  121. cout << "\nStan = ";
  122. cout << (Stan ? "gadamy." : "siedzimy cicho."); // operator trojskladnikowy, czyli jesli Stan jest true to printuje gadamy, jesli Stan
  123. // jest false to wyprintuje "siedzimy cicho"
  124. cout << " Zmiana? (t/n) ";
  125. int p = _getch(); // tutaj czekamy na znak wpisany przez uzytkownika w konsoli
  126. if (p == 't' || p == 'T') // jesli uzytkownik wpisze t lub T to odwracamy liczbe Stan z 1 na 0 lub 0 na 1
  127. Stan ^= 1;
  128. cout << endl;
  129. }
  130.  
  131. String& String::operator ++(void) // Zamienia małe litery na duże i zwraca nowy string
  132. {
  133. char mm = 'A' - 'a'; // z ascii A=65, a=97, 65-97 = -32, -32 = 256 - 32 = 224
  134. // ziomek tutaj chyba chcial zrobic 'a' - 'A' co by bylo prostsze
  135. for (int i = 0; i<len; i++)
  136. if (s[i] >= 'a' && s[i] <= 'z')
  137. s[i] += mm; // dodajesz 224 do kazdego chara, czyli zamieniasz z malej na duza, np:
  138. // 'a'=97, (97 + 224) % 256 = 65 czyli 'A'
  139. return (*this);
  140. }
  141.  
  142. String String::operator ++(int) // Zamienia małe litery na duże i zwraca stary string
  143. {
  144. String temp(*this);
  145. char mm = 'A' - 'a';
  146. for (int i = 0; i<len; i++)
  147. if (s[i] >= 'a' && s[i] <= 'z')
  148. s[i] += mm;
  149. return temp;
  150. }
  151.  
  152. String& String::operator --(void) // Zamienia duże litery na małe i zwraca nowy string
  153. {
  154. char mm = 'A' - 'a';
  155. for (int i = 0; i<len; i++)
  156. if (s[i] >= 'A' && s[i] <= 'Z')
  157. s[i] -= mm;
  158. return (*this);
  159. }
  160.  
  161. String String::operator --(int) // Zamienia duże litery na małe i zwraca stary string
  162. {
  163. String temp(*this);
  164. char mm = 'A' - 'a';
  165. for (int i = 0; i<len; i++)
  166. if (s[i] >= 'A' && s[i] <= 'Z')
  167. s[i] -= mm;
  168. return temp;
  169. }
  170.  
  171. String operator +(const String str1, const String str2)
  172. {
  173. String temp(str1.len + str2.len); // tworzysz stringa o dlugosci tycch dwoch
  174. // WYGLADA NA BLAD, wyjedziemy za bufor bo brakuje miejsca na null terminator \0
  175. // chyba powinno byc:
  176. // String temp(str1.len + str2.len + 1);
  177. strcpy(temp.s, str1.s); // kopiuje str1 do tempa
  178. strcat(temp.s, str2.s); // na koniec tempa dopisuje str2, na ktory brakuje miejsca wiec bedzie
  179. // heap corruption, program moze sie wysypac ale nie koniecznie
  180. return temp;
  181. }
  182.  
  183. String operator -(const String &d, const char &c)
  184. {
  185. char *g = d.s;
  186. int len = d.len;
  187. int i = 0;
  188. for (int j = 0; j < len; j++)
  189. {
  190. if (g[j] != c)
  191. {
  192. i++;
  193. }
  194. }
  195. String s = new char[i + 1];
  196. int u = 0;
  197. for (int j = 0; j < len; j++)
  198. {
  199. if (g[j] != c)
  200. {
  201. s.s[u] = g[j];
  202. u++;
  203. }
  204. }
  205. s.s[u] = 0;
  206. return s;
  207. }
  208.  
  209. int main(void)
  210. {
  211. String::switch_stan();
  212. const char *str1 = "Typem, z ktorego dokonuje sie przeksztalcenia ";
  213. const char *str2 = "nie musi byc typ wbudowany.";
  214. String a(str1), b(str2);
  215. String c = "Symfonia C++";
  216. String d = String("Jerzy Grebosz");
  217. cout << endl << " * Wyniki * " << endl << endl;
  218. cout << a + b;
  219. cout << endl << a + str2;
  220. cout << endl << str1 + b;
  221. cout << "\n" + c + "\n" + d;
  222. String k = String("Ala ma kota");
  223. char l = 'a';
  224. String n = k - l;
  225. cout << n << endl;
  226.  
  227. // Testowanie operatorów inkrementacji i dekrementacji
  228. cout << endl << endl;
  229. cout << endl << a;
  230. cout << endl << ++a;
  231. cout << endl << a;
  232. cout << endl << endl;
  233. cout << endl << b;
  234. cout << endl << b++;
  235. cout << endl << b;
  236. cout << endl << endl;
  237. cout << endl << c;
  238. cout << endl << --c;
  239. cout << endl << c;
  240. cout << endl << endl;
  241. cout << endl << d;
  242. cout << endl << d--;
  243. cout << endl << d;
  244. _getch();
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement