AnaGocevska

WebPageServer

Mar 28th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.03 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.  
  89.         for(int i=0; i<n; i++)
  90.         {
  91.             this->wps[i]=wps[i];
  92.         }
  93.         this->n=n;
  94.     }
  95.  
  96.     WebServer(const WebServer &ws)
  97.     {
  98.         strcpy(this->ime, ws.ime);
  99.         this->wps=new WebPage[ws.n];
  100.         for(int i=0; i<this->n; i++)
  101.         {
  102.             this->wps[i]=ws.wps[i];
  103.         }
  104.         this->n = ws.n;
  105.     }
  106.  
  107.     void dodadi(WebPage wp)
  108.     {
  109.         WebPage *tmp;
  110.         tmp = new WebPage[this->n];
  111.         for(int i=0; i<this->n; i++)
  112.         {
  113.             tmp[i]=wps[i];
  114.         }
  115.         delete[]this->wps;
  116.         this->wps = new WebPage[n+1];
  117.         for(int i=0; i<this->n; i++)
  118.         {
  119.             this->wps[i]=tmp[i];
  120.         }
  121.         delete[]tmp;
  122.         this->wps[this->n]=wp;
  123.         this->n++;
  124.     }
  125.  
  126.     void izbrishi(WebPage wp)
  127.     {
  128.         WebPage *tmp;
  129.         int j;
  130.         tmp = new WebPage[this->n-1];
  131.         for(int i=0; i<this->n; i++)
  132.         {
  133.             if(this->wps[i].proverka(wp))
  134.                 continue;
  135.             tmp[j] = this->wps[i];
  136.             j++;
  137.         }
  138.         delete[]this->wps;
  139.         this->wps = new WebPage[this->n-1];
  140.         for(int i=0; i<this->n-1; i++)
  141.         {
  142.             this->wps[i]=tmp[j];
  143.         }
  144.         delete[]tmp;
  145.         this->n--;
  146.     }
  147.  
  148.     void sortLen()
  149.     {
  150.         for(int i=0; i<this->n; i++)
  151.         {
  152.             for(int j=i+1; j<this->n; j++)
  153.             {
  154.                 if(strlen(wps[i].getUrl()) > strlen(wps[j].getUrl()))
  155.                 {
  156.                     WebPage t;
  157.                     t = wps[i];
  158.                     wps[i]=wps[j];
  159.                     wps[j]=t;
  160.                 }
  161.             }
  162.         }
  163.     }
  164.  
  165.     void pecati()
  166.     {
  167.         for(int i=0; i<this->n; i++)
  168.             this->wps[i].pecati();
  169.     }
  170.  
  171.     void pecatiStraniciSoUrlNad(int threshold)
  172.     {
  173.         for(int i=0; i<this->n; i++)
  174.             if(strlen(this->wps[i].getUrl())>threshold)
  175.                 this->wps[i].pecati();
  176.     }
  177. };
  178.  
  179.     void sortAlpha(WebPage *wps, int n)
  180.     {
  181.         for(int i=0; i<n; i++)
  182.         {
  183.             for(int j=i+1; j<n; j++)
  184.             {
  185.                 if(strcmp(wps[i].getUrl(), wps[j].getUrl()) > 0)
  186.                 {
  187.                     WebPage t;
  188.                     t=wps[i];
  189.                     wps[i]=wps[j];
  190.                     wps[j]=wps[i];
  191.                 }
  192.             }
  193.         }
  194.     }
  195.  
  196. int main()
  197. {
  198.     int n;
  199.     char url[100], sodrzina[100], ime[100];
  200.  
  201.     cout << "Vnesete broj na WebStranici vo serverot: ";
  202.     cin >> n;
  203.     WebPage *stranici;
  204.     stranici = new WebPage[n];
  205.  
  206.     for(int i=0;i<n;i++)
  207.     {
  208.         cout << endl << "Vnesete URL za " << i << "-ta stranica: ";
  209.         cin >> url;
  210.         cout << endl << "Vnesete SODRZINA za " << i << "-ta stranica: ";
  211.         cin >> sodrzina;
  212.         WebPage t(url,sodrzina);
  213.         stranici[i]=t;
  214.     }
  215.  
  216.     cout << endl << "Vnesete ime za WebServerot: ";
  217.     cin >> ime;
  218.  
  219.     WebServer ws(ime, stranici, n);
  220.     ws.pecati();
  221.  
  222.     int opc;
  223.     while(1)
  224.     {
  225.         cout << endl << "Izberi opcija(1 - dodadi web strana, 2 - izbrisi web strana, 3 - ispecati stranici, 4 - ispecati stranici so Url nad, 5 - prekin): ";
  226.         cin >> opc;
  227.  
  228.         if(opc==1)
  229.         {
  230.             cout << endl << "Vnesi URL za stranicata sto sakas da ja dodades: ";
  231.             cin >> url;
  232.             cout << endl << "Vnesi SODRZINA za stranicata sto sakas da ja dodades: ";
  233.             cin >> sodrzina;
  234.             WebPage stranicaZaDodavanje(url,sodrzina);
  235.             ws.dodadi(stranicaZaDodavanje);
  236.         }else
  237.         if(opc==2)
  238.         {
  239.             cout << endl << "Vnesi URL za stranicata sto sakas da ja izbrises: ";
  240.             cin >> url;
  241.             cout << endl << "Vnesi SODRZINA za stranicata sto sakas da ja izbrises: ";
  242.             cin >> sodrzina;
  243.             WebPage stranicaZaBrisenje(url,sodrzina);
  244.             ws.izbrishi(stranicaZaBrisenje);
  245.         }else
  246.         if(opc==3)
  247.         {
  248.             ws.pecati();
  249.         }else
  250.         if(opc==4)
  251.         {
  252.             int threshold;
  253.             cout << endl << "Vnesete threshold: ";
  254.             cin >> threshold;
  255.             ws.pecatiStraniciSoUrlNad(threshold);
  256.         }else
  257.         if(opc==5)
  258.         {
  259.             break;
  260.         }
  261.     }
  262.  
  263.     return 0;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment