add1ctus

Sentences

Apr 12th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class Word
  6. {
  7.     char * characters;
  8. public:
  9.     Word()
  10.     {
  11.         characters= new char[10];
  12.         strcpy(characters,"UNDEFINED");
  13.     }
  14.     Word(char *w)
  15.     {
  16.         characters=new char[strlen(w)+1];
  17.         strcpy(characters,w);
  18.     }
  19.     Word(const Word &w)
  20.     {
  21.         characters=new char[strlen(w.characters)+1];
  22.         strcpy(characters,w.characters);
  23.     }
  24.     Word &operator=(const Word &rhs)
  25.     {
  26.         characters=new char[strlen(rhs.characters)+1];
  27.         strcpy(characters,rhs.characters);
  28.         return *this;
  29.     }
  30.     friend ostream &operator<<(ostream &output, const Word &rhs)
  31.     {
  32.         output<<rhs.characters;
  33.         return output;
  34.     }
  35.     ~Word()
  36.     {
  37.         delete[] characters;
  38.     }
  39. };
  40.  
  41. class Sentence
  42. {
  43.     Word* words;
  44.     int number;
  45.     int capacity;
  46. public:
  47.     Sentence()
  48.     {
  49.         number=0;
  50.         capacity=10;
  51.         words=new Word[10];
  52.     }
  53.     Sentence(int cap)
  54.     {
  55.         number=0;
  56.         capacity=cap;
  57.         words=new Word[cap];
  58.     }
  59.     Sentence(const Sentence &sen)
  60.     {
  61.         number=sen.number;
  62.         capacity=sen.capacity;
  63.         words=new Word[capacity];
  64.         for(int i=0;i<number;i++)
  65.             words[i]=sen.words[i];
  66.     }
  67.     void add(Word w)
  68.     {
  69.         if(number==capacity)
  70.         {
  71.             Word* temp;
  72.             temp = new Word[capacity];
  73.             for(int i=0;i<capacity;i++)
  74.                 temp[i]=words[i];
  75.             delete[] words;
  76.             words = new Word[capacity+10];
  77.             for(int i=0;i<capacity;i++)
  78.                 words[i]=temp[i];
  79.             delete[] temp;
  80.         }
  81.         words[number++]=w;
  82.     }
  83.     void print()
  84.     {
  85.         for(int i=0;i<number;i++)
  86.             cout<<words[i]<<" ";
  87.         cout<<endl;
  88.     }
  89.     void swap(int i, int j)
  90.     {
  91.         Word temp=words[i];
  92.         words[i]=words[j];
  93.         words[j]=temp;
  94.     }
  95. };
  96.  
  97. int main() {
  98.     int n;
  99.     cin >> n;
  100.     cin.get();
  101.     cout << "CONSTRUCTOR" << endl;
  102.     Sentence s;
  103.     cout << "ADD WORD" << endl;
  104.     for (int i = 0; i < n; ++i) {
  105.         char w[100];
  106.         cin.getline(w, 100);
  107.         Word word(w);
  108.         s.add(word);
  109.     }
  110.     cout << "PRINT SENTENCE" << endl;
  111.     s.print();
  112.     cout << "COPY" << endl;
  113.     Sentence x = s;
  114.     cout << "SWAP" << endl;
  115.     x.swap(n / 2, n / 3);
  116.     x.print();
  117.     cout << "ORIGINAL" << endl;
  118.     s.print();
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment