Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdafx.h"
- #include <iostream>
- #include <cstdio>
- #include <conio.h>
- #include <string>
- #include <cstdlib>
- using namespace std;
- class String {
- char *s;
- int len;
- int static Stan; // liczba inicjalizowana na 0 przy starcie programu, kazda obiekt ma referencje do tej samej danej,
- // tj. jesli zmienisz ja na 5 to wszystkie obiekty tez beda mialy
- // wartosc 5, w tym przpyadku zmienna jest prywatna wiec nie mozesz jej zmienic z zewnatrz klasy
- public:
- String(void); // domyslny konstruktor: np. String abc;
- String(int n); // konstruktor z intem np. String abc(6);
- String(const char *p); // konstruktor cstringa ...
- String(const String& str); // konstruktor przyjmujacy inny obiekt tej samej klasy, np.
- // String abc;
- // String def(abc);
- ~String() { delete s; } // destruktor
- int lenght() const { return(len); }
- String& operator =(const String &str);// operator podstawiania, czyli np.
- // String abc;
- // String def;
- // abc = def;
- void print() const { cout << s << "\nLenght: " << len << "\n"; }
- void operator !() const { print(); } // print, negacja
- // String abc;
- // !abc;
- operator char *() const { return(s); } // operator konwersji // operator dereferencji, czyli *abc;
- static void switch_stan(void); // statyczna metoda moze byc wywolana z obiektem lub bez, np:
- // String::switch_stan();
- // albo String asd;
- // asd.switch_stan();
- String& operator ++(void); // Zamienia małe litery na duże i zwraca nowy string
- // prefix operator czyli ++abc;
- String operator ++(int); // Zamienia małe litery na duże i zwraca stary string
- // postfix operator czyli abc++;
- String& operator --(void); // Zamienia duże litery na małe i zwraca nowy string
- String operator --(int); // Zamienia duże litery na małe i zwraca stary string
- friend String operator +(const String str1, const String str2); // tu jest friend dlatego zeby mozna bylo uzywac w globalnym scopie
- // czyli String abc; String def; abc + def;
- // rownie dobrze mozna ja zadeklarowac poza klasa, wtedy bez przedrostka friend
- friend String operator -(const String &d, const char &c);
- };
- // domyslny konstruktor
- String::String(void)
- {
- s = new char[81]; /// domyslna dlugosc stringa to 81 znakow ( wlasciwie 80, bo ostatni to null terminator czyli \0
- s[0] = 0; // przerwanie łańcucha
- len = 80;
- if (Stan)
- {
- cout << "\nPracuje konstruktor domniemany:\n";
- print();
- }
- }
- String::String(int n)
- {
- s = new char[n + 1];
- s[0] = 0; // przerwanie łańcucha
- len = n;
- if (Stan)
- {
- cout << "\nPracuje konstruktor z jednym argumentem:\n";
- print();
- }
- }
- String::String(const char *p)
- {
- len = (int)strlen(p); // rzutowanie
- s = new char[len + 1];
- strcpy(s, p);
- if (Stan)
- {
- cout << "\nPracuje operator konwersji:\n";
- print();
- }
- }
- String::String(const String& str)
- {
- s = 0;
- (*this) = str;
- if (Stan)
- {
- cout << "\nPracuje konstruktor kopiujacy:\n";
- print();
- }
- }
- String& String::operator =(const String &str)
- {
- len = str.len;
- 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
- // argumentu str
- s = new char[len + 1];
- strcpy(s, str.s);
- if (Stan)
- {
- cout << "\nPracuje przeciazony operator podstawiania:\n";
- print();
- }
- return *this;
- }
- int String::Stan = 0;
- void String::switch_stan(void)
- {
- cout << "\nStan = ";
- cout << (Stan ? "gadamy." : "siedzimy cicho."); // operator trojskladnikowy, czyli jesli Stan jest true to printuje gadamy, jesli Stan
- // jest false to wyprintuje "siedzimy cicho"
- cout << " Zmiana? (t/n) ";
- int p = _getch(); // tutaj czekamy na znak wpisany przez uzytkownika w konsoli
- if (p == 't' || p == 'T') // jesli uzytkownik wpisze t lub T to odwracamy liczbe Stan z 1 na 0 lub 0 na 1
- Stan ^= 1;
- cout << endl;
- }
- String& String::operator ++(void) // Zamienia małe litery na duże i zwraca nowy string
- {
- char mm = 'A' - 'a'; // z ascii A=65, a=97, 65-97 = -32, -32 = 256 - 32 = 224
- // ziomek tutaj chyba chcial zrobic 'a' - 'A' co by bylo prostsze
- for (int i = 0; i<len; i++)
- if (s[i] >= 'a' && s[i] <= 'z')
- s[i] += mm; // dodajesz 224 do kazdego chara, czyli zamieniasz z malej na duza, np:
- // 'a'=97, (97 + 224) % 256 = 65 czyli 'A'
- return (*this);
- }
- String String::operator ++(int) // Zamienia małe litery na duże i zwraca stary string
- {
- String temp(*this);
- char mm = 'A' - 'a';
- for (int i = 0; i<len; i++)
- if (s[i] >= 'a' && s[i] <= 'z')
- s[i] += mm;
- return temp;
- }
- String& String::operator --(void) // Zamienia duże litery na małe i zwraca nowy string
- {
- char mm = 'A' - 'a';
- for (int i = 0; i<len; i++)
- if (s[i] >= 'A' && s[i] <= 'Z')
- s[i] -= mm;
- return (*this);
- }
- String String::operator --(int) // Zamienia duże litery na małe i zwraca stary string
- {
- String temp(*this);
- char mm = 'A' - 'a';
- for (int i = 0; i<len; i++)
- if (s[i] >= 'A' && s[i] <= 'Z')
- s[i] -= mm;
- return temp;
- }
- String operator +(const String str1, const String str2)
- {
- String temp(str1.len + str2.len); // tworzysz stringa o dlugosci tycch dwoch
- // WYGLADA NA BLAD, wyjedziemy za bufor bo brakuje miejsca na null terminator \0
- // chyba powinno byc:
- // String temp(str1.len + str2.len + 1);
- strcpy(temp.s, str1.s); // kopiuje str1 do tempa
- strcat(temp.s, str2.s); // na koniec tempa dopisuje str2, na ktory brakuje miejsca wiec bedzie
- // heap corruption, program moze sie wysypac ale nie koniecznie
- return temp;
- }
- String operator -(const String &d, const char &c)
- {
- char *g = d.s;
- int len = d.len;
- int i = 0;
- for (int j = 0; j < len; j++)
- {
- if (g[j] != c)
- {
- i++;
- }
- }
- String s = new char[i + 1];
- int u = 0;
- for (int j = 0; j < len; j++)
- {
- if (g[j] != c)
- {
- s.s[u] = g[j];
- u++;
- }
- }
- s.s[u] = 0;
- return s;
- }
- int main(void)
- {
- String::switch_stan();
- const char *str1 = "Typem, z ktorego dokonuje sie przeksztalcenia ";
- const char *str2 = "nie musi byc typ wbudowany.";
- String a(str1), b(str2);
- String c = "Symfonia C++";
- String d = String("Jerzy Grebosz");
- cout << endl << " * Wyniki * " << endl << endl;
- cout << a + b;
- cout << endl << a + str2;
- cout << endl << str1 + b;
- cout << "\n" + c + "\n" + d;
- String k = String("Ala ma kota");
- char l = 'a';
- String n = k - l;
- cout << n << endl;
- // Testowanie operatorów inkrementacji i dekrementacji
- cout << endl << endl;
- cout << endl << a;
- cout << endl << ++a;
- cout << endl << a;
- cout << endl << endl;
- cout << endl << b;
- cout << endl << b++;
- cout << endl << b;
- cout << endl << endl;
- cout << endl << c;
- cout << endl << --c;
- cout << endl << c;
- cout << endl << endl;
- cout << endl << d;
- cout << endl << d--;
- cout << endl << d;
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement