Advertisement
Lauda

Untitled

Nov 26th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. // DINSTRING.H
  2.  
  3. #ifndef DINSTRING_H_INCLUDED
  4. #define DINSTRING_H_INCLUDED
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. class DinString
  10. {
  11.     private:
  12.         int duzina;
  13.         char *text;
  14.  
  15.     public:
  16.         DinString();
  17.         DinString(const char[]);
  18.         DinString(const DinString&);
  19.         ~DinString();
  20.  
  21.         int length() const;
  22.         char& operator[](int);
  23.         char operator[](int) const;
  24.         DinString& operator=(const DinString&);
  25.         DinString& operator+=(const DinString&);
  26.         friend bool operator==(const DinString&, const DinString&);
  27.         friend bool operator!=(const DinString&, const DinString&);
  28.         friend DinString operator+(const DinString&, const DinString&);
  29.         friend ostream& operator<<(ostream&, const DinString&);
  30.  
  31. };
  32.  
  33.  
  34. #endif // DINSTRING_H_INCLUDED
  35.  
  36. // DINSTRING.CPP
  37. #include "dinstring.h"
  38.  
  39. DinString :: DinString()
  40. {
  41.     duzina = 0;
  42.     text = NULL;
  43. }
  44.  
  45. DinString :: DinString(const char ulazniStr[])
  46. {
  47.     duzina = 0;
  48.     while (ulazniStr[duzina] != '\0')
  49.         duzina++;
  50.  
  51.     text = new char[duzina+1];
  52.     for (int i = 0; i < duzina; i++)
  53.         text[i] = ulazniStr[i];
  54.  
  55.     text[duzina] = '\0';
  56. }
  57.  
  58. DinString :: DinString(const DinString &ds)
  59. {
  60.     duzina = ds.duzina;
  61.     text = new char[duzina+1];
  62.     for (int i = 0; i < duzina; i++)
  63.         text[i] = ds.text[i];
  64.  
  65.     text[duzina] = '\0';
  66. }
  67.  
  68. DinString :: ~DinString()
  69. {
  70.     delete[] text;
  71.    // delete text;
  72. }
  73.  
  74. int DinString :: length() const
  75. {
  76.     return duzina;
  77. }
  78.  
  79. char& DinString :: operator[](int i)
  80. {
  81.     return text[i];
  82. }
  83.  
  84. char DinString :: operator[](int i) const
  85. {
  86.     return text[i];
  87. }
  88.  
  89. DinString& DinString :: operator=(const DinString &ds)
  90. {
  91.     if (this != &ds) // MIND = BLOWN! DAFUQ?
  92.     {
  93.         delete[] text;
  94.         duzina = ds.duzina;
  95.         text = new char[duzina+1];
  96.         for (int i = 0; i < duzina; i++)
  97.             text[i] = ds.text[i];
  98.         text[duzina] = '\0';
  99.     }
  100.     return *this;
  101. }
  102.  
  103. DinString& DinString :: operator+=(const DinString &ds)
  104. {
  105.     char *tempText = new char[duzina+ds.duzina+1];
  106.  
  107.     for (int i = 0; i < duzina; i++)
  108.         tempText[i] = text[i];
  109.  
  110.     for (int i = 0; i < ds.duzina; i++)
  111.         tempText[duzina+i] = ds.text[i];
  112.  
  113.     tempText[duzina+ds.duzina] = '\0';
  114.     duzina+=ds.duzina;
  115.  
  116.     delete[] text;
  117.     text = tempText;
  118.  
  119.     return *this;
  120. }
  121.  
  122. bool operator==(const DinString &ds1, const DinString &ds2)
  123. {
  124.     if (ds1.duzina != ds2.duzina)
  125.         return false;
  126.  
  127.     for (int i = 0; i < ds1.duzina; i++)
  128.         if (ds1.text[i] != ds2.text[i])
  129.             return false;
  130.  
  131.     return true;
  132. }
  133.  
  134. bool operator!=(const DinString &ds1, const DinString &ds2)
  135. {
  136.     if (ds1.duzina != ds2.duzina)
  137.         return true;
  138.  
  139.     for (int i = 0; i < ds1.duzina; i++)
  140.         if (ds1.text[i] != ds2.text[i])
  141.             return true;
  142.  
  143.     return false;
  144. }
  145.  
  146. DinString operator+(const DinString &ds1, const DinString &ds2)
  147. {
  148.     int i;
  149.     DinString temp;
  150.     temp.duzina = ds1.duzina+ds2.duzina;
  151.     temp.text = new char[temp.duzina+1];
  152.  
  153.     for (i = 0; i < ds1.duzina; i++)
  154.         temp.text[i] = ds1.text[i];
  155.  
  156.     for (int j = 0; j < ds2.duzina; j++)
  157.         temp.text[i+j] = ds2.text[j];
  158.  
  159.     temp.text[temp.duzina] = '\0';
  160.  
  161.     return temp;
  162. }
  163.  
  164. ostream& operator<<(ostream &out, const DinString &ds)
  165. {
  166.     if (ds.duzina > 0)
  167.         out << ds.text;
  168.  
  169.     return out;
  170. }
  171.  
  172. // MAIN.CPP
  173. #include <iostream>
  174. #include "dinstring.h"
  175.  
  176. using namespace std;
  177.  
  178. int main()
  179. {
  180.     DinString a, b("Dobar"), c("dan");
  181.     cout << "a " << a << endl;
  182.     cout << "b " << b << endl;
  183.     cout << "c " << c << endl;
  184.  
  185.     cout << endl << "Testiranje +=" << endl;
  186.     cout << "a+=b: " << (a+=b) << endl;
  187.     cout << "a+=c: " << (a+=c) << endl;
  188.     cout << endl << "Testiranje +" << endl;
  189.     cout << "a=b+c: " << (a=b+c) << endl;
  190.     cout << "a=b+\" \"+c: " << (a=b+" "+c) << endl;
  191.     cout << endl << "Testiranje []" << endl;
  192.     char x=a[5];
  193.     cout << "x=a[5] x: " << x << endl;
  194.     a[5] = 'Z';
  195.     cout << "Izvrseno a[5] = 'Z', sada je a: " << a << endl;
  196.     cout << endl << "Testiranje ==, = i !=" << endl;
  197.     cout << "a: " << a << endl;
  198.     cout << "b: " << b << endl;
  199.     cout << "a==b? " << (a==b) << endl;
  200.     cout << "a!=b? " << (a!=b) << endl;
  201.     a=b;
  202.     cout << "a: " << a << endl;
  203.     cout << "b: " << b << endl;
  204.     cout << "a==b? " << (a==b) << endl;
  205.     cout << "a!=b? " << (a!=b) << endl;
  206.     cout << endl << "Ocitavanje duzine stringa pomocu length" << endl;
  207.     cout << "a: " << a << endl;
  208.     cout << "Duzina strina a: " << a.length() << endl;
  209.     a = (a+=(" "+b))+" "+c;
  210.     cout << "a: " << a << endl;
  211.     cout << "Duzina stringa a: " << a.length() << endl;
  212.  
  213.     return 0;
  214.  
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement