Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #pragma hdrstop
  4. #include <iostream.h>
  5.  
  6. //---------------------------------------------------------------------------
  7.  
  8. struct TPoint {
  9.         int x, y;
  10. };
  11.  
  12. struct TListItem {
  13.         TPoint value;
  14.         TListItem *Next;
  15. };
  16.  
  17. class TSet {
  18.         private:
  19.         int listSize;
  20.         int maxValue;
  21.         TListItem *First, *Last;
  22.  
  23.         public:
  24.         TSet() {
  25.                 listSize = 10;
  26.                 maxValue = 20;
  27.                 First = NULL;
  28.                 Last = NULL;
  29.         }
  30.  
  31.         ~TSet() {
  32.                 destroySet();
  33.         }
  34.  
  35.         void destroySet() {
  36.                 TListItem *t = First, *r;
  37.  
  38.                 while (t != NULL) {
  39.                         r = t->Next;
  40.                         free(t);
  41.                         t = r;
  42.                 }
  43.                 First = NULL;
  44.                 Last = NULL;
  45.         }
  46.  
  47.         void input() {
  48.                 srand(time(NULL));
  49.  
  50.                 TPoint p;
  51.                 for(int i = 0; i < listSize; i++) {
  52.                         p.x = (rand() % maxValue) - maxValue/2;
  53.                         p.y = (rand() % maxValue) - maxValue/2;
  54.                         addItem(p);
  55.                 }
  56.         }
  57.  
  58.         void addItem(TPoint p) {
  59.                 if(First == NULL) {
  60.                         First = (TListItem*) malloc(sizeof(TListItem));
  61.                         First->Next = NULL;
  62.                         First->value = p;
  63.                         Last = First;
  64.                 } else {
  65.                         Last->Next = (TListItem*) malloc(sizeof(TListItem));
  66.                         Last->Next->Next = NULL;
  67.                         Last->Next->value = p;
  68.                         Last = Last->Next;
  69.                 }
  70.         }
  71.  
  72.         void output() {
  73.                 TListItem *t = First;
  74.                 while(t != NULL) {
  75.                         cout << "(" << t->value.x << ";" << t->value.y << ") ";
  76.                         t = t->Next;
  77.                 }
  78.                 cout << endl;
  79.         }
  80.  
  81.         TSet& operator=(const TSet &ob) {
  82.                 destroySet();
  83.                 TListItem *t = ob.First;
  84.                 while(t != NULL) {
  85.                         addItem(t->value);
  86.                         t = t->Next;
  87.                 }
  88.                 return *this;
  89.         }
  90.  
  91.         int static getNumber(){
  92.                 return 5;
  93.         }
  94. };
  95.  
  96. #pragma argsused
  97. int main(int argc, char* argv[])
  98. {
  99.         TSet ob1, ob2;
  100.         TPoint p;
  101.         p.x = 1;
  102.         p.y = 2;
  103.  
  104.         ob1.input();
  105.         ob1.output();
  106.  
  107.         ob2 = ob1;
  108.  
  109.         ob2.addItem(p);
  110.         ob1.output();
  111.         ob2.output();
  112.  
  113.         cin.get();
  114.         cin.get();
  115.         return 0;
  116. }
  117. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement