Advertisement
Guest User

Kotkoto

a guest
May 16th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <conio.h>
  5. #include <string>
  6. #include <cstdlib>
  7.  
  8. using namespace std;
  9.  
  10. class String {
  11. char *s;
  12. int len;
  13. int static Stan;
  14. public:
  15. String(void);
  16. String(int n);
  17. String(const char *p);
  18. String(const String& str);
  19. ~String() { delete s; }
  20. int lenght() const { return(len); }
  21. String& operator =(const String &str);// operator podstawiania
  22. void print() const { cout << s << "\nLenght: " << len << "\n"; }
  23. void operator !() const { print(); } // print
  24. operator char *() const { return(s); } // operator konwersji
  25. static void switch_stan(void);
  26. String& operator ++(void); // Zamienia małe litery na duże i zwraca nowy string
  27. String operator ++(int); // Zamienia małe litery na duże i zwraca stary string
  28. String& operator --(void); // Zamienia duże litery na małe i zwraca nowy string
  29. String operator --(int); // Zamienia duże litery na małe i zwraca stary string
  30. friend String operator -(const String& str1, const String& str2);
  31. friend String operator +(const String str1, const String str2);
  32.  
  33.  
  34. };
  35.  
  36. String::String(void)
  37. {
  38. s = new char[81];
  39. s[0] = 0; // przerwanie łańcucha
  40. len = 80;
  41. if (Stan)
  42. {
  43. cout << "\nPracuje konstruktor domniemany:\n";
  44. print();
  45. }
  46. }
  47.  
  48. String::String(int n)
  49. {
  50. s = new char[n + 1];
  51. s[0] = 0; // przerwanie łańcucha
  52. len = n;
  53. if (Stan)
  54. {
  55. cout << "\nPracuje konstruktor z jednym argumentem:\n";
  56. print();
  57. }
  58. }
  59.  
  60. String::String(const char *p)
  61. {
  62. len = (int)strlen(p); // rzutowanie
  63. s = new char[len + 1];
  64. strcpy(s, p);
  65. if (Stan)
  66. {
  67. cout << "\nPracuje operator konwersji:\n";
  68. print();
  69. }
  70. }
  71.  
  72. String::String(const String& str)
  73. {
  74. s = 0;
  75. (*this) = str;
  76. if (Stan)
  77. {
  78. cout << "\nPracuje konstruktor kopiujacy:\n";
  79. print();
  80. }
  81. }
  82.  
  83. String& String::operator =(const String &str)
  84. {
  85. len = str.len;
  86. if (s) delete s;
  87. s = new char[len + 1];
  88. strcpy(s, str.s);
  89. if (Stan)
  90. {
  91. cout << "\nPracuje przeciazony operator podstawiania:\n";
  92. print();
  93. }
  94. return *this;
  95. }
  96.  
  97.  
  98. int String::Stan = 0;
  99.  
  100. void String::switch_stan(void)
  101. {
  102. cout << "\nStan = ";
  103. cout << (Stan ? "gadamy." : "siedzimy cicho.");
  104. cout << " Zmiana? (t/n) ";
  105. int p = _getch();
  106. if (p == 't' || p == 'T')
  107. Stan ^= 1;
  108. cout << endl;
  109. }
  110.  
  111. String& String::operator ++(void) // Zamienia małe litery na duże i zwraca nowy string
  112. {
  113. char mm = 'A' - 'a';
  114. for (int i = 0; i<len; i++)
  115. if (s[i] >= 'a' && s[i] <= 'z')
  116. s[i] += mm;
  117. return (*this);
  118. }
  119.  
  120. String String::operator ++(int) // Zamienia małe litery na duże i zwraca stary string
  121. {
  122. String temp(*this);
  123. char mm = 'A' - 'a';
  124. for (int i = 0; i<len; i++)
  125. if (s[i] >= 'a' && s[i] <= 'z')
  126. s[i] += mm;
  127. return temp;
  128. }
  129.  
  130. String& String::operator --(void) // Zamienia duże litery na małe i zwraca nowy string
  131. {
  132. char mm = 'A' - 'a';
  133. for (int i = 0; i<len; i++)
  134. if (s[i] >= 'A' && s[i] <= 'Z')
  135. s[i] -= mm;
  136. return (*this);
  137. }
  138.  
  139. String String::operator --(int) // Zamienia duże litery na małe i zwraca stary string
  140. {
  141. String temp(*this);
  142. char mm = 'A' - 'a';
  143. for (int i = 0; i<len; i++)
  144. if (s[i] >= 'A' && s[i] <= 'Z')
  145. s[i] -= mm;
  146. return temp;
  147. }
  148.  
  149. String operator +(const String str1, const String str2)
  150. {
  151. String temp(str1.len + str2.len);
  152. strcpy(temp.s, str1.s);
  153. strcat(temp.s, str2.s);
  154. return temp;
  155. }
  156. String operator-(const String& str1, const String& str2)
  157. {
  158. char *tmp;
  159. tmp = new char[str1.len + str2.len + 2];
  160.  
  161.  
  162. strcpy(tmp, str1.s);
  163. strcat(tmp, str2.s);
  164.  
  165.  
  166. int licznik_spacji = 0;
  167. int licznik_kropek = 0;
  168. for (int i = 0; i < str1.len + str2.len + 1; i++) {
  169. if (tmp[i] == ' ') {
  170. licznik_spacji++;
  171. };
  172. if (tmp[i] == '.') {
  173. licznik_kropek++; };
  174.  
  175. }; cout << endl; cout << "Liczba spacji: " << licznik_spacji << endl; cout << "Liczba kropek: " << licznik_kropek;
  176.  
  177.  
  178. return String(tmp);
  179. }
  180. int main(void)
  181. {
  182. String::switch_stan();
  183. const char *str1 = "Typem, z ktorego dokonuje sie przeksztalcenia ";
  184. const char *str2 = "nie musi byc typ wbudowany.";
  185. String a(str1), b(str2);
  186. String c = "Symfonia C++";
  187. String d = String("Jerzy Grebosz");
  188. cout << endl << " * Wyniki * " << endl << endl;
  189. cout << a + b;
  190.  
  191.  
  192.  
  193. cout << endl << a - b;
  194. /*cout << endl << a + str2;
  195. cout << endl << str1 + b;
  196. cout << "\n" + c + "\n" + d;
  197. // Testowanie operatorów inkrementacji i dekrementacji
  198. cout << endl << endl;
  199. cout << endl << a;
  200. cout << endl << ++a;
  201. cout << endl << a;
  202. cout << endl << endl;
  203. cout << endl << b;
  204. cout << endl << b++;
  205. cout << endl << b;
  206. cout << endl << endl;
  207. cout << endl << c;
  208. cout << endl << --c;
  209. cout << endl << c;
  210. cout << endl << endl;
  211. cout << endl << d;
  212. cout << endl << d--;
  213. cout << endl << d;*/
  214. _getch();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement