Advertisement
Superloup10

Exo4.cpp

Mar 18th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class String
  7. {
  8.     private :
  9.         static const int MAX_REF;
  10.         char *charArray;
  11.         int length;
  12.         int *ref;
  13.         void countChar();
  14.  
  15.     public :
  16.         String();
  17.         String(const char[]);
  18.         String(const String&);
  19.         ~String();
  20.         char* getCharArray();
  21.         int getLength();
  22.         String operator+(String);
  23.         String& operator=(const String&);
  24.         void print();
  25.         String toUpper(String);
  26. };
  27.  
  28.     const int String::MAX_REF = 26;
  29.  
  30.     String::String()
  31.     {
  32.         length = 0;
  33.         charArray = NULL;
  34.         ref = new int[MAX_REF];
  35.         countChar();
  36.     }
  37.  
  38.     String::String(const char paramCharArray[])
  39.     {
  40.         length = strlen(paramCharArray);
  41.         charArray = new char[length];
  42.         ref = new int[MAX_REF];
  43.         for(int i = 0; i < length; i++)
  44.         {
  45.             charArray[i] = paramCharArray[i];
  46.         }
  47.         countChar();
  48.     }
  49.  
  50.     String::String(const String &strSrc)
  51.     {
  52.         length = strSrc.length;
  53.         charArray = new char[length];
  54.         ref = new int[MAX_REF];
  55.         for(int i = 0; i < length; i++)
  56.             charArray[i] = strSrc.charArray[i];
  57.         for(int i = 0; i < MAX_REF; i++)
  58.             ref[i] = strSrc.ref[i];
  59.     }
  60.  
  61.     String::~String()
  62.     {
  63.         delete ref;
  64.         delete charArray;
  65.     }
  66.  
  67.     char* String::getCharArray()
  68.     {
  69.         return charArray;
  70.     }
  71.  
  72.     int String::getLength()
  73.     {
  74.         return length;
  75.     }
  76.  
  77.     String String::operator+(String op)
  78.     {
  79.         String tmpStr;
  80.         tmpStr.length = length + op.length;
  81.         tmpStr.charArray = new char[tmpStr.length];
  82.         int i;
  83.         for(i = 0; i < length; i++)
  84.             tmpStr.charArray[i] = charArray[i];
  85.  
  86.         for(int j = 0; j < op.length; i++, j++)
  87.             tmpStr.charArray[i] = op.charArray[j];
  88.  
  89.         for(int j = 0; j < MAX_REF; j++)
  90.             tmpStr.ref[j] = ref[j] + op.ref[j];
  91.  
  92.         return tmpStr;
  93.     }
  94.  
  95.     String& String::operator=(const String &op)
  96.     {
  97.         if(this != &op)
  98.         {
  99.             delete ref;
  100.             delete charArray;
  101.             charArray = new char[length = op.length];
  102.             ref = new int[MAX_REF];
  103.             for(int i = 0; i < length; i++)
  104.                 charArray[i] = op.charArray[i];
  105.  
  106.             for(int i = 0; i < MAX_REF; i++)
  107.                 ref[i] = op.ref[i];
  108.         }
  109.         return *this;
  110.     }
  111.  
  112.     void String::print()
  113.     {
  114.         for(int i = 0; i < length; i++)
  115.         {
  116.             cout << charArray[i] << endl;
  117.         }
  118.         /*for(int i = 0; i < nbValue; i++)
  119.         {
  120.             cout << "Nombre de ref : " << ref[i] << endl;
  121.         }*/
  122.     }
  123.  
  124.     void String::countChar()
  125.     {
  126.         char lowerChar;
  127.         int j = 0;
  128.         for(int i = 0; i < length; i++)
  129.         {
  130.             ref[j] = 0;
  131.             lowerChar = tolower(charArray[i]);
  132.             if(lowerChar >= 'a' && lowerChar <= 'z')
  133.             {
  134.                 j = lowerChar - 'a';
  135.                 ref[j]++;
  136.             }
  137.         }
  138.     }
  139.  
  140.     String String::toUpper(String strSrc)
  141.     {
  142.         String tmpSrc;
  143.         for(int i = 0; i < strSrc.length; i++)
  144.             tmpSrc.charArray[i] = toupper(strSrc.charArray[i]);
  145.  
  146.         return tmpSrc;
  147.     }
  148.  
  149. int main()
  150. {
  151.     String test("!!!Hello World!!!"), test2, test3;
  152.     test.print();
  153.     test2 = test;
  154.     cout << "------------------------" << endl;
  155.     test2.print();
  156.     test3 = test + test2;
  157.     cout << "------------------------" << endl;
  158.     test3.print();
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement