peterzig

Untitled

Apr 18th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5.  
  6. class DynString
  7. {
  8.         public:
  9.                 DynString(const char *string);
  10.                 DynString(const DynString& kopia);
  11.                 const char* CStr() const;
  12.                 unsigned int Size() const;
  13.  
  14.                 void operator += (const char *string);
  15.                 void operator ! ();
  16.  
  17.         private:
  18.                 char *string;
  19. };
  20.  
  21. DynString::DynString(const char *string)
  22. {
  23.         this->string = (char*)malloc(strlen(string)*sizeof(char));
  24.         if(this->string != NULL)
  25.         {
  26.                 strcpy(this->string, string);
  27.         }
  28. }
  29.  
  30. DynString::DynString(const DynString& kopia)
  31. {
  32.         this->string = (char*)malloc(strlen(kopia.string)*sizeof(char));
  33.         if(this->string != NULL)
  34.         {
  35.                 strcpy(this->string, kopia.string);
  36.         }
  37. }
  38.  
  39. const char* DynString::CStr() const
  40. {
  41.         return string;
  42. }
  43.  
  44. unsigned int DynString::Size() const
  45. {
  46.         return strlen(string);
  47. }
  48.  
  49. void DynString::operator += (const char *string)
  50. {
  51.         unsigned int currentSize = (strlen(this->string));
  52.         unsigned int newSize = currentSize + strlen(string);
  53.         this->string = (char*)realloc(this->string, newSize*sizeof(char));
  54.  
  55.         if(this->string != NULL)
  56.         {
  57.                 this->string[currentSize] = '\0';
  58.                 this->string = strcat(this->string, string);
  59.         }
  60. }
  61.  
  62. void DynString::operator ! ()
  63. {
  64.         unsigned int size = strlen(string);
  65.         for(unsigned int i = 0; i < size; i++)
  66.         {
  67.                 if(string[i] >= 97 && string[i] <= 122)
  68.                 {
  69.                         string[i] -= 32;
  70.                 }
  71.                 else if(string[i] >= 65 && string[i] <= 90)
  72.                 {
  73.                         string[i] += 32;
  74.                 }
  75.         }
  76. }
  77.  
  78. int main()
  79. {
  80.         DynString napis("Napis testowy.");
  81.         printf("Napis %s\n", napis.CStr());
  82.         printf("Znakow: %i\n", napis.Size());
  83.  
  84.         DynString kopia = napis;
  85.         printf("Kopia %s\n", kopia.CStr());
  86.         printf("Znakow: %i\n", kopia.Size());
  87.  
  88.         kopia += " - pierwszy fragment -";
  89.         kopia += " drugi fragment.";
  90.  
  91.         printf("Napis %s\n", napis.CStr());
  92.         printf("Znakow: %i\n", napis.Size());
  93.         printf("Kopia %s\n", kopia.CStr());
  94.         printf("Znakow: %i\n", kopia.Size());
  95.  
  96.         !napis;
  97.         printf("Napis %s\n", napis.CStr());
  98.         printf("\nKoniec.");
  99.         _getch();
  100.         return 0;
  101. }
Add Comment
Please, Sign In to add comment