Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstdio>
- #include <conio.h>
- #include <string>
- #include <cstdlib>
- using namespace std;
- class String {
- char *s;
- int len;
- int static Stan;
- public:
- String(void);
- String(int n);
- String(const char *p);
- String(const String& str);
- ~String() { delete s; }
- int lenght() const { return(len); }
- String& operator =(const String &str);// operator podstawiania
- void print() const { cout << s << "\nLenght: " << len << "\n"; }
- void operator !() const { print(); } // print
- operator char *() const { return(s); } // operator konwersji
- static void switch_stan(void);
- String& operator ++(void); // Zamienia małe litery na duże i zwraca nowy string
- String operator ++(int); // Zamienia małe litery na duże i zwraca stary string
- 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);
- friend String operator +(const String str1, const String str2);
- };
- String::String(void)
- {
- s = new char[81];
- 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;
- 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.");
- cout << " Zmiana? (t/n) ";
- int p = _getch();
- if (p == 't' || p == 'T')
- Stan ^= 1;
- cout << endl;
- }
- String& String::operator ++(void) // Zamienia małe litery na duż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 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);
- strcpy(temp.s, str1.s);
- strcat(temp.s, str2.s);
- return temp;
- }
- String operator-(const String& str1, const String& str2)
- {
- char *tmp;
- tmp = new char[str1.len + str2.len + 2];
- strcpy(tmp, str1.s);
- strcat(tmp, str2.s);
- int licznik_spacji = 0;
- int licznik_kropek = 0;
- for (int i = 0; i < str1.len + str2.len + 1; i++) {
- if (tmp[i] == ' ') {
- licznik_spacji++;
- };
- if (tmp[i] == '.') {
- licznik_kropek++; };
- }; cout << endl; cout << "Liczba spacji: " << licznik_spacji << endl; cout << "Liczba kropek: " << licznik_kropek;
- return String(tmp);
- }
- 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 - b;
- /*cout << endl << a + str2;
- cout << endl << str1 + b;
- cout << "\n" + c + "\n" + d;
- // 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