Advertisement
AnaGocevska

WebPage_WebServer.Dodadi.Izbrishi

Mar 27th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class WebPage
  6. {
  7. private:
  8.     char url[100];
  9.     char *sodrzhina;
  10.  
  11. public:
  12.  
  13.     WebPage(char *url="", char *sodrzhina="")           //default i konstruktor so argumenti zaedno
  14.     {
  15.         strcpy(this->url, url);
  16.         this->sodzhina = new char[strlen(sodzhina+1)];     //alocijaranje nova memorija
  17.         strcpy(this->sodzhina, sodrzhina);
  18.     }
  19.  
  20.     WebPage(const WebPage &wp)
  21.     {
  22.         strcpy(this->url, wp.url);
  23.         this->sodrzhina = new char[strlen(wp.sodrzhina+1)];
  24.         strcpy(this->sodrzhina, wp.sodrzhina);
  25.     }
  26.  
  27.     char *getUrl()
  28.     {
  29.         return this->url;
  30.     }
  31.  
  32.     char *getSodrzhina()
  33.     {
  34.         return this->sodrzhina;
  35.     }
  36.  
  37.     void setUrl(char *url)
  38.     {
  39.         strcpy(this->url, url);
  40.     }
  41.  
  42.     void setSodrzhina(char *sodrzhina)
  43.     {
  44.         delete[]this->sodrzhina;  //Веќе креиран објект со некоја вредност претходно во конструкторот, па ја бришеме истата
  45.         this->sodrzhina = new char [strlen(sodrzhina+1)];
  46.         strcpy(this->sodrzhina, sodrzhina);
  47.     }
  48.  
  49.     bool proverka(WebPage wp)
  50.     {
  51.         if(strcmp(this->url, wp.url)==0 && strcmp(this->sodrzhina, wp.sodrzhina)==0)
  52.             return true;
  53.         return false;
  54.     }
  55.  
  56.  
  57. }; // Крај class WebPage
  58.  
  59. class WebServer
  60. {
  61. private:
  62.     char ime[30];
  63.     WebPage *wps; //листа од веб страници (динамички алоцирана низа од обjекти од класата WebPage)
  64.  
  65. public:
  66.  
  67.     WebServer(char *ime="", WebPage *wps=0,int n=0)
  68.     {
  69.         strcpy(this->ime, ime);
  70.         this->wps = new WebPage[n];
  71.         for(int i=0; i<n; i++)
  72.         {
  73.             this->wps[i]=WebPage(wps[i]); //го повикуваме copy конструкторот од WebPage
  74.         }
  75.     }
  76.  
  77.     WebServer(const WebServer &ws)
  78.     {
  79.         strcpy(this->ime, ws.ime);
  80.         this->wps = new WebPage[ws.n];
  81.         for(int i=0; i<ws.n; i++)
  82.         {
  83.             this->wps[i]=WebPage(ws.wps[i]);
  84.         }
  85.     }
  86.  
  87.     // get i set
  88.     WebPage *getWps()
  89.     {
  90.         return this->wps;
  91.     }
  92.  
  93.     void setWps(WebPage *wps, int n) //n-секогаш кога имаме низа која не е char
  94.     {
  95.         delete[] this->wps; //меморијата претходно алоцирана со new WebPage[n], треба да се ослободи со delete[]
  96.         this->wps = new WebPage[n];
  97.         this->n = n;
  98.         for(int i=0; i<n; i++)
  99.         {
  100.             this->wps[i].setUrl(wps[i].getUrl());
  101.             this->wps[i].setSodrzhina(wps[i].getSodrzhina());
  102.         }
  103.     }
  104.  
  105.     char *getIme()
  106.     {
  107.         return this->ime;
  108.     }
  109.  
  110.     void setIme(char *ime)
  111.     {
  112.         strcpy(this->ime, ime);
  113.     }
  114.  
  115.     int getN()
  116.     {
  117.         return this->n;
  118.     }
  119.  
  120.     void n(int n)
  121.     {
  122.         this->n=n;
  123.     }
  124.     void dodadi(WebPage wp)
  125.     {
  126.         WebPage *tmp;
  127.         tmp = new WebPage[this->n];
  128.         for(int i=0; i<n; i++)
  129.         {
  130.             tmp[i]=WebPage(this->wps[i]);
  131.         }
  132.         delete[] this->wps;
  133.         this->wps = new WebPage[this->n+1];
  134.         for(int i=0; i<this->n; i++)
  135.         {
  136.             this->wps=WebPage(tmp[i]);
  137.         }
  138.         delete [] tmp;
  139.         this->wps[this->n] = WebPage(wp);
  140.         this->n++;
  141.     }
  142.  
  143.     void izbrishi(WebPage wp)
  144.     {
  145.         WebPage *tmp;
  146.         tmp = new WebPage[this->n-1]; //gledame kolku elementi treba da iskopirame, zatoa n-1
  147.         int j;
  148.         for(int i=0; i<this->n; i++)
  149.         {
  150.             if(this->wps[i].proverka(wp))
  151.                 continue;
  152.             tmp[j]=WebPage(this->wps[i]);
  153.             j++;
  154.         }
  155.         delete [] this->wps;
  156.         this->wps = new Webpage[this->n-1];
  157.         for(int i=0; i<this->n-1; i++)
  158.         {
  159.             this->wps[i]=WebPage(tmp[j])
  160.         }
  161.         delete [] tmp;
  162.         this->n--;
  163.     }
  164.  
  165.     ~WebServer()
  166.     {
  167.         delete [] wps;
  168.     }
  169. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement