Advertisement
AnaGocevska

FinalWebPageServer

Mar 27th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. class WebPage
  8. {
  9. private:
  10.  
  11.     char url[100];
  12.     char *sodrzhina;
  13.  
  14. public:
  15.     WebPage(char *url="", char *sodrzhina="")
  16.     {
  17.         strcpy(this->url, url);
  18.         this->sodrzhina = new char[strlen(sodrzhina)+1];
  19.         strcpy(this->sodrzhina, sodrzhina);
  20.     }
  21.  
  22.     WebPage(const WebPage &wp)
  23.     {
  24.         strcpy(this->url, wp.url);
  25.         this->sodrzhina = new char[strlen(wp.sodrzhina)+1];
  26.         strcpy(this->sodrzhina, wp.sodrzhina);
  27.     }
  28.  
  29.     char *getUrl()
  30.     {
  31.         return this->url;
  32.     }
  33.  
  34.     char *getSodrzhina()
  35.     {
  36.         return this->sodrzhina;
  37.     }
  38.  
  39.     void setUrl(char *url)
  40.     {
  41.         strcpy(this->url, url);
  42.     }
  43.  
  44.     void setSodrzhina(char *sodrzhina)
  45.     {
  46.         delete[]this->sodrzhina;
  47.         this->sodrzhina = new char[strlen(sodrzhina)+1];
  48.         strcpy(this->sodrzhina, sodrzhina);
  49.     }
  50.  
  51.     bool proverka(WebPage wp)
  52.     {
  53.         if(strcmp(this->url, wp.url)==0 && strcmp(this->sodrzhina, wp.sodrzhina)==0)
  54.             return true;
  55.  
  56.         return false;
  57.     }
  58.  
  59.     WebPage &operator = (WebPage &wp)
  60.     {
  61.         strcpy(this->url, wp.url);
  62.         delete[]this->sodrzhina;
  63.         this->sodrzhina = new char(strlen(wp.sodrzhina)+1);
  64.         strcpy(this->sodrzhina, wp.sodrzhina);
  65.  
  66.         return *this;
  67.     }
  68.  
  69.     void pecati()
  70.     {
  71.         cout<<"URL: "<<this->url<<endl;
  72.         cout<<"SODRZHINA: "<<this->sodrzhina<<endl;
  73.     }
  74. };
  75.  
  76. class WebServer
  77. {
  78. private:
  79.     char ime[30];
  80.     WebPage *wps;
  81.     int n;
  82.  
  83. public:
  84.     WebServer(char *ime="", WebPage *wps=0, int n=0)
  85.     {
  86.         strcpy(this->ime, ime);
  87.         this->wps = new WebPage[n];
  88.         for(int i=0; i<this->n; i++)
  89.         {
  90.             this->wps[i]=wps[i];
  91.         }
  92.         this->n=n;
  93.     }
  94.  
  95.     WebServer(const WebServer &ws)
  96.     {
  97.         strcpy(this->ime, ws.ime);
  98.         this->wps=new WebPage[ws.n];
  99.         for(int i=0; i<this->n; i++)
  100.         {
  101.             this->wps[i]=ws.wps[i];
  102.         }
  103.         this->n = ws.n;
  104.     }
  105.  
  106.     void dodadi(WebPage wp)
  107.     {
  108.         WebPage *tmp;
  109.         tmp = new WebPage[this->n];
  110.         for(int i=0; i<this->n; i++)
  111.         {
  112.             tmp[i]=wps[i];
  113.         }
  114.         delete[]this->wps;
  115.         this->wps = new WebPage[n+1];
  116.         for(int i=0; i<this->n; i++)
  117.         {
  118.             this->wps[i]=tmp[i];
  119.         }
  120.         delete[]tmp;
  121.         this->wps[this->n]=wp;
  122.         this->n++;
  123.     }
  124.  
  125.     void izbrishi(WebPage wp)
  126.     {
  127.         WebPage *tmp;
  128.         int j;
  129.         tmp = new WebPage[n-1];
  130.         for(int i=0; i<this->n; i++)
  131.         {
  132.             if(wps[i].proverka(wp))
  133.                 continue;
  134.             tmp[j] = this->wps[i];
  135.             j++;
  136.         }
  137.         delete[]this->wps;
  138.         this->wps = new WebPage[n-1];
  139.         for(int i=0; i<this->n; i++)
  140.         {
  141.             this->wps[i]=tmp[j];
  142.         }
  143.         delete[]tmp;
  144.         this->n--;
  145.     }
  146.  
  147.     void sortLen()
  148.     {
  149.         for(int i=0; i<this->n; i++)
  150.         {
  151.             for(int j=i+1; j<this->n; j++)
  152.             {
  153.                 if(strlen(wps[i].getUrl()) > strlen(wps[j].getUrl()))
  154.                 {
  155.                     WebPage t;
  156.                     t = wps[i];
  157.                     wps[i]=wps[j];
  158.                     wps[j]=t;
  159.                 }
  160.             }
  161.         }
  162.     }
  163.  
  164.     void pecati()
  165.     {
  166.         for(int i=0; i<this->n; i++)
  167.             this->wps[i].pecati();
  168.     }
  169.  
  170.     void pecatiStraniciSoUrlNad(int threshold)
  171.     {
  172.         for(int i=0; i<this->n; i++)
  173.             if(strlen(this->wps[i].getUrl())>threshold)
  174.                 this->wps[i].pecati();
  175.     }
  176. };
  177.  
  178.     void sortAlpha(WebPage *wps, int n)
  179.     {
  180.         for(int i=0; i<n; i++)
  181.         {
  182.             for(int j=i+1; j<n; j++)
  183.             {
  184.                 if(strcmp(wps[i].getUrl(), wps[j].getUrl()) > 0)
  185.                 {
  186.                     WebPage t;
  187.                     t=wps[i];
  188.                     wps[i]=wps[j];
  189.                     wps[j]=wps[i];
  190.                 }
  191.             }
  192.         }
  193.     }
  194.  
  195. int main()
  196. {
  197.     int n;
  198.     char url[100], sodrzina[100], ime[100];
  199.  
  200.     cout << "Vnesete broj na WebStranici vo serverot: ";
  201.     cin >> n;
  202.     WebPage stranici[n];
  203.  
  204.     for(int i=0;i<n;i++)
  205.     {
  206.         cout << endl << "Vnesete URL za " << i << "-ta stranica: ";
  207.         cin >> url;
  208.         cout << endl << "Vnesete SODRZINA za " << i << "-ta stranica: ";
  209.         cin >> sodrzina;
  210.         stranici[i] = WebPage(url,sodrzina);
  211.     }
  212.  
  213.     cout << endl << "Vnesete ime za WebServerot: ";
  214.     cin >> ime;
  215.  
  216.     WebServer ws(ime, stranici, n);
  217.     ws.pecati();
  218.  
  219.     int opc;
  220.     while(1)
  221.     {
  222.         cout << endl << "Izberi opcija(1 - dodadi web strana, 2 - izbrisi web strana, 3 - ispecati stranici, 4 - ispecati stranici so Url nad, 5 - prekin): ";
  223.         cin >> opc;
  224.  
  225.         if(opc==1)
  226.         {
  227.             cout << endl << "Vnesi URL za stranicata sto sakas da ja dodades: ";
  228.             cin >> url;
  229.             cout << endl << "Vnesi SODRZINA za stranicata sto sakas da ja dodades: ";
  230.             cin >> sodrzina;
  231.             WebPage stranicaZaDodavanje(url,sodrzina);
  232.             ws.dodadi(stranicaZaDodavanje);
  233.         }else
  234.         if(opc==2)
  235.         {
  236.             cout << endl << "Vnesi URL za stranicata sto sakas da ja izbrises: ";
  237.             cin >> url;
  238.             cout << endl << "Vnesi SODRZINA za stranicata sto sakas da ja izbrises: ";
  239.             cin >> sodrzina;
  240.             WebPage stranicaZaBrisenje(url,sodrzina);
  241.             ws.izbrisi(stranicaZaBrisenje);
  242.         }else
  243.         if(opc==3)
  244.         {
  245.             ws.pecati();
  246.         }else
  247.         if(opc==4)
  248.         {
  249.             int threshold;
  250.             cout << endl << "Vnesete threshold: ";
  251.             cin >> threshold;
  252.             ws.pecatiStraniciSoUrlNad(threshold);
  253.         }else
  254.         if(opc==5)
  255.         {
  256.             break;
  257.         }
  258.     }
  259.  
  260.     return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement