Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Word
- {
- char * characters;
- public:
- Word()
- {
- characters= new char[10];
- strcpy(characters,"UNDEFINED");
- }
- Word(char *w)
- {
- characters=new char[strlen(w)+1];
- strcpy(characters,w);
- }
- Word(const Word &w)
- {
- characters=new char[strlen(w.characters)+1];
- strcpy(characters,w.characters);
- }
- Word &operator=(const Word &rhs)
- {
- characters=new char[strlen(rhs.characters)+1];
- strcpy(characters,rhs.characters);
- return *this;
- }
- friend ostream &operator<<(ostream &output, const Word &rhs)
- {
- output<<rhs.characters;
- return output;
- }
- ~Word()
- {
- delete[] characters;
- }
- };
- class Sentence
- {
- Word* words;
- int number;
- int capacity;
- public:
- Sentence()
- {
- number=0;
- capacity=10;
- words=new Word[10];
- }
- Sentence(int cap)
- {
- number=0;
- capacity=cap;
- words=new Word[cap];
- }
- Sentence(const Sentence &sen)
- {
- number=sen.number;
- capacity=sen.capacity;
- words=new Word[capacity];
- for(int i=0;i<number;i++)
- words[i]=sen.words[i];
- }
- void add(Word w)
- {
- if(number==capacity)
- {
- Word* temp;
- temp = new Word[capacity];
- for(int i=0;i<capacity;i++)
- temp[i]=words[i];
- delete[] words;
- words = new Word[capacity+10];
- for(int i=0;i<capacity;i++)
- words[i]=temp[i];
- delete[] temp;
- }
- words[number++]=w;
- }
- void print()
- {
- for(int i=0;i<number;i++)
- cout<<words[i]<<" ";
- cout<<endl;
- }
- void swap(int i, int j)
- {
- Word temp=words[i];
- words[i]=words[j];
- words[j]=temp;
- }
- };
- int main() {
- int n;
- cin >> n;
- cin.get();
- cout << "CONSTRUCTOR" << endl;
- Sentence s;
- cout << "ADD WORD" << endl;
- for (int i = 0; i < n; ++i) {
- char w[100];
- cin.getline(w, 100);
- Word word(w);
- s.add(word);
- }
- cout << "PRINT SENTENCE" << endl;
- s.print();
- cout << "COPY" << endl;
- Sentence x = s;
- cout << "SWAP" << endl;
- x.swap(n / 2, n / 3);
- x.print();
- cout << "ORIGINAL" << endl;
- s.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment