Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DINSTRING.H
- #ifndef DINSTRING_H_INCLUDED
- #define DINSTRING_H_INCLUDED
- #include <iostream>
- using namespace std;
- class DinString
- {
- private:
- int duzina;
- char *text;
- public:
- DinString();
- DinString(const char[]);
- DinString(const DinString&);
- ~DinString();
- int length() const;
- char& operator[](int);
- char operator[](int) const;
- DinString& operator=(const DinString&);
- DinString& operator+=(const DinString&);
- friend bool operator==(const DinString&, const DinString&);
- friend bool operator!=(const DinString&, const DinString&);
- friend DinString operator+(const DinString&, const DinString&);
- friend ostream& operator<<(ostream&, const DinString&);
- };
- #endif // DINSTRING_H_INCLUDED
- // DINSTRING.CPP
- #include "dinstring.h"
- DinString :: DinString()
- {
- duzina = 0;
- text = NULL;
- }
- DinString :: DinString(const char ulazniStr[])
- {
- duzina = 0;
- while (ulazniStr[duzina] != '\0')
- duzina++;
- text = new char[duzina+1];
- for (int i = 0; i < duzina; i++)
- text[i] = ulazniStr[i];
- text[duzina] = '\0';
- }
- DinString :: DinString(const DinString &ds)
- {
- duzina = ds.duzina;
- text = new char[duzina+1];
- for (int i = 0; i < duzina; i++)
- text[i] = ds.text[i];
- text[duzina] = '\0';
- }
- DinString :: ~DinString()
- {
- delete[] text;
- // delete text;
- }
- int DinString :: length() const
- {
- return duzina;
- }
- char& DinString :: operator[](int i)
- {
- return text[i];
- }
- char DinString :: operator[](int i) const
- {
- return text[i];
- }
- DinString& DinString :: operator=(const DinString &ds)
- {
- if (this != &ds) // MIND = BLOWN! DAFUQ?
- {
- delete[] text;
- duzina = ds.duzina;
- text = new char[duzina+1];
- for (int i = 0; i < duzina; i++)
- text[i] = ds.text[i];
- text[duzina] = '\0';
- }
- return *this;
- }
- DinString& DinString :: operator+=(const DinString &ds)
- {
- char *tempText = new char[duzina+ds.duzina+1];
- for (int i = 0; i < duzina; i++)
- tempText[i] = text[i];
- for (int i = 0; i < ds.duzina; i++)
- tempText[duzina+i] = ds.text[i];
- tempText[duzina+ds.duzina] = '\0';
- duzina+=ds.duzina;
- delete[] text;
- text = tempText;
- return *this;
- }
- bool operator==(const DinString &ds1, const DinString &ds2)
- {
- if (ds1.duzina != ds2.duzina)
- return false;
- for (int i = 0; i < ds1.duzina; i++)
- if (ds1.text[i] != ds2.text[i])
- return false;
- return true;
- }
- bool operator!=(const DinString &ds1, const DinString &ds2)
- {
- if (ds1.duzina != ds2.duzina)
- return true;
- for (int i = 0; i < ds1.duzina; i++)
- if (ds1.text[i] != ds2.text[i])
- return true;
- return false;
- }
- DinString operator+(const DinString &ds1, const DinString &ds2)
- {
- int i;
- DinString temp;
- temp.duzina = ds1.duzina+ds2.duzina;
- temp.text = new char[temp.duzina+1];
- for (i = 0; i < ds1.duzina; i++)
- temp.text[i] = ds1.text[i];
- for (int j = 0; j < ds2.duzina; j++)
- temp.text[i+j] = ds2.text[j];
- temp.text[temp.duzina] = '\0';
- return temp;
- }
- ostream& operator<<(ostream &out, const DinString &ds)
- {
- if (ds.duzina > 0)
- out << ds.text;
- return out;
- }
- // MAIN.CPP
- #include <iostream>
- #include "dinstring.h"
- using namespace std;
- int main()
- {
- DinString a, b("Dobar"), c("dan");
- cout << "a " << a << endl;
- cout << "b " << b << endl;
- cout << "c " << c << endl;
- cout << endl << "Testiranje +=" << endl;
- cout << "a+=b: " << (a+=b) << endl;
- cout << "a+=c: " << (a+=c) << endl;
- cout << endl << "Testiranje +" << endl;
- cout << "a=b+c: " << (a=b+c) << endl;
- cout << "a=b+\" \"+c: " << (a=b+" "+c) << endl;
- cout << endl << "Testiranje []" << endl;
- char x=a[5];
- cout << "x=a[5] x: " << x << endl;
- a[5] = 'Z';
- cout << "Izvrseno a[5] = 'Z', sada je a: " << a << endl;
- cout << endl << "Testiranje ==, = i !=" << endl;
- cout << "a: " << a << endl;
- cout << "b: " << b << endl;
- cout << "a==b? " << (a==b) << endl;
- cout << "a!=b? " << (a!=b) << endl;
- a=b;
- cout << "a: " << a << endl;
- cout << "b: " << b << endl;
- cout << "a==b? " << (a==b) << endl;
- cout << "a!=b? " << (a!=b) << endl;
- cout << endl << "Ocitavanje duzine stringa pomocu length" << endl;
- cout << "a: " << a << endl;
- cout << "Duzina strina a: " << a.length() << endl;
- a = (a+=(" "+b))+" "+c;
- cout << "a: " << a << endl;
- cout << "Duzina stringa a: " << a.length() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement