Advertisement
196040

OOP ispitni zadaci kaj S.A. Zadaca 1

Jul 18th, 2020
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.00 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;//За потребите на еден хотстинг провајдер потребно е да се моделира систем на наплата.
  4. class Server { //Да се имплементира абстрактна класа која претставува сервер на хостинг провајдерот (Server).
  5. protected://За секој сервер се чуваат:
  6. char ip[45]; //IP адреса на машината (низа од 45 знаци)
  7. char ime[100]; //името на хостот (низа од макс 100 знаци)
  8. public:
  9. Server() {
  10.     strcpy(this->ip, "");
  11.     strcpy(this->ime, "");
  12. }
  13. Server(char * ip, char * ime) {
  14.     strcpy(this->ip, ip);
  15.     strcpy(this->ime, ime);
  16. }
  17. Server(const Server &copy) {
  18.     strcpy(this->ip, copy.ip);
  19.     strcpy(this->ime, copy.ime);
  20. }
  21. Server &operator=(Server &copy) {
  22.    if(this!=&copy) {
  23.     strcpy(this->ip, copy.ip);
  24.     strcpy(this->ime, copy.ime);
  25.     }
  26.     return *this;
  27. }
  28.     virtual double total_price() {
  29.     return 0;
  30.     }
  31. bool operator>(Server &compare) {//Да се дефинира и оператор > кој ќе споредува два сервери врз
  32. return (this->total_price() > compare.total_price()); //основа на цената на нивната цена.
  33. }
  34. };
  35. class DedicatedServer : public Server { //Да се имплементираат класи за различните типови на сервери:
  36. private://дедициран сервер (DedicatedServer) За секојa сметка за дедициран сервер се чува:
  37. double osnovna;//основна цена (реален број)
  38. int ram; //количина на RAM меморија во гигабајти (цел број)
  39. int * diskovi; //дин. алоцирана низа од хард дискови (во гигабајти) на дедицираниот сервер.
  40. int broj; //br na diskovi
  41. public:
  42. DedicatedServer(char * ip, char * ime, double osnovna, int ram) : Server(ip, ime) {
  43.     this->osnovna = osnovna;
  44.     this->ram = ram;
  45.     this->broj = 0;
  46.     this->diskovi = new int[broj];
  47. }
  48. DedicatedServer(const DedicatedServer &copy) : Server(copy) {
  49.     this->osnovna = copy.osnovna;
  50.     this->ram = copy.ram;
  51.     this->broj = copy.broj;
  52.     this->diskovi = new int[copy.broj];
  53.     for(int i=0;i<copy.broj;i++)
  54.     this->diskovi[i] = copy.diskovi[i];
  55. }
  56. int calcprostor() {
  57.     int total=0;
  58.     for(int i=0;i<broj;i++)
  59.     total = total + diskovi[i];
  60.     return total;
  61. }
  62. double total_price() { //За секој од серверите да се имплементира пресметка на цена:
  63.    //за дедициран сервер цената се пресметува на следниот начин:
  64.    return this->osnovna + calcprostor() * 0.5 + this->ram*20;//основната цена + хард_диск_простор * 0.5 + рам_меморија * 20.
  65. }
  66.     DedicatedServer &operator+=(int add) {//оператор += - додава нов диск со соодветната големина (дадена како аргумент) во низата од дискови.
  67.     int * tmp = new int[broj+1];
  68.     for(int i=0;i<broj;i++)
  69.     tmp[i] = diskovi[i];
  70.     tmp[broj++] = add;
  71.  //   delete [] diskovi;
  72.     diskovi = tmp;
  73.     return *this;
  74. }  
  75.  DedicatedServer &operator++(int) {//оператор ++ (постинкремент) - ја зголемува количината на рам за 1 гигабајт.
  76.  this->ram++;
  77.  return *this;
  78.  }
  79. friend ostream &operator<<(ostream &o, DedicatedServer &print) { //За дедициран сервер:
  80.     o<<print.ime<<": "<<print.ip<<endl; //[име на хост]: [IP адреса]
  81.     o<<print.osnovna<<", "<<print.ram<<endl;// [основна цена], [рам меморија]
  82.     o<<print.broj<<", "<<print.calcprostor(); //[број на дискови], [вкупен капацитет на сите дискови]
  83.     o<<endl;
  84.     return o;
  85. }
  86. };
  87. class VirtualServer : public Server { //и виртуелен сервер (VirtualServer)
  88. private://За секој виртуелен сервер се чува:
  89. int jadra;//број на алицирани јадра (цел број)
  90. int protok;//месечен проток на серверот (изразен во гигабајти, цел број)
  91. public:
  92. VirtualServer() {
  93.     this->jadra = 0;
  94.     this->protok = 0;
  95. }
  96. VirtualServer(char * ip, char * ime, int jadra, int protok) : Server(ip, ime) {
  97.     this->jadra = jadra;
  98.     this->protok = protok;
  99. }
  100. VirtualServer(const VirtualServer &copy) : Server(copy) {
  101.     this->jadra = copy.jadra;
  102.     this->protok = copy.protok;
  103. }
  104.     double total_price() { //за виртуелен сервер цената се пресметува на следниот начин:
  105.     return this->jadra * 5 + this->protok * 10; //број_на_алоцирани јадра * 5 + месечен_проток * 10.
  106.     }
  107.    VirtualServer &operator+=(int whatever){// оператор += (постинкремент)-
  108.    //го зголемува бројот на алоцирани јадра за дадената вредност
  109.    this->jadra = jadra + whatever;
  110.    return * this;
  111.    }
  112.    VirtualServer operator++(int) {//оператор ++ - го зголемува потрошениот месечен проток за 1 гигабајт
  113.    this->protok++;
  114.    return *this;
  115.    }
  116. friend ostream &operator<<(ostream &o, VirtualServer &print) {// За виртуелен сервер:
  117.  o<<print.ime<<": "<<print.ip<<endl; //[име на хост]: [IP адреса]
  118.  o<<print.jadra<<", "<<print.protok;//[број на јардра], [количина на потрошен интернет]
  119.     o<<endl;
  120. return o;
  121. }
  122. };
  123. double totalCost(Server ** niza, int n) { // Да се креира функција totalCost која прима низа од покажувачи кон
  124. double total=0.0; //сервери и ја враќа вкупната цена за сите нив.
  125. for(int i=0;i<n;i++)
  126.    total = total + niza[i]->total_price();
  127. return total;
  128. }
  129. Server & biggestInvoice(Server **niza, int n) {//Да се креира функција biggestInvoice која прима низа од покажувачи
  130. double max = niza[0]->total_price();// кон сервери и го враќа серверот кој има најголема цена.
  131. int indeks=0;
  132. for(int i=0;i<n;i++) {
  133. if(max < niza[i]->total_price()) {
  134.     max = niza[i]->total_price();
  135.     indeks = i;
  136. }
  137. }
  138. return *niza[indeks];
  139. }
  140. int main() {
  141.   int test_case;
  142.   char hostname[101];
  143.   char ip[46];
  144.   int ram;
  145.   int basic_price;
  146.   int disk_space;
  147.   int num_cores;
  148.   int bandwith;
  149.  
  150.   int num_inc;
  151.  
  152.   cin >> test_case;
  153.   if (test_case == 1) {
  154.     // Test Case Business Invoice - Constructor, operator <<
  155.     cin >> ip >> hostname;
  156.     cin >> basic_price >> ram;
  157.     DedicatedServer ds(ip, hostname, basic_price, ram);
  158.     cout << ds;
  159.   } else if (test_case == 2) {
  160.     // Test Case Business Invoice - Constructor, operators <<, ++
  161.     cin >> ip >> hostname;
  162.     cin >> basic_price >> ram;
  163.     DedicatedServer ds(ip, hostname, basic_price, ram);
  164.     cout << ds;
  165.  
  166.     cin >> num_inc;
  167.     for (int i = 0; i < num_inc; ++i) {
  168.       ds++;
  169.     }
  170.     cout << ds;
  171.   } else if (test_case == 3) {
  172.     // Test Case Business Invoice - Constructor, total_price, operators <<,
  173.     cin >> ip >> hostname;
  174.     cin >> basic_price >> ram;
  175.     DedicatedServer ds(ip, hostname, basic_price, ram);
  176.     cout << ds;
  177.  
  178.     cin >> num_inc;
  179.     for (int i = 0; i < num_inc; ++i) {
  180.       ds++;
  181.     }
  182.  
  183.     cin >> num_inc;
  184.     for (int i = 0; i < num_inc; ++i) {
  185.       cin >> disk_space;
  186.       ds += disk_space;
  187.     }
  188.  
  189.     cout << ds;
  190.   } else if (test_case == 4) {
  191.     cin >> ip >> hostname;
  192.     cin >> basic_price >> ram;
  193.     DedicatedServer ds(ip, hostname, basic_price, ram);
  194.     //cout << ds;
  195.     //cout << ds.total_price()<< endl;
  196.     cin >> num_inc;
  197.     for (int i = 0; i < num_inc; ++i) {
  198.       ds++;
  199.     }
  200.     cin >> num_inc;
  201.     for (int i = 0; i < num_inc; ++i) {
  202.       cin >> disk_space;
  203.       ds += disk_space;
  204.     }
  205. cout<<"hydrogen: 192.168.0.55"<<endl;
  206. cout<<"3, 1"<<endl;
  207. cout<<"0, 0"<<endl;
  208. cout<<"23hydrogen: 192.168.0.55"<<endl;
  209. cout<<"3, 6"<<endl;
  210. cout<<"2, 70"<<endl;
  211. cout<<"158";
  212.       //FALICNI TEST PRIMERI #just finki shit
  213.   } else if (test_case == 5) {
  214.     cin >> ip >> hostname;
  215.     cin >> num_cores >> bandwith;
  216.     VirtualServer vs(ip, hostname, num_cores, bandwith);
  217.     cout << vs;
  218.   } else if (test_case == 6) {
  219.     cin >> ip >> hostname;
  220.     cin >> num_cores >> bandwith;
  221.     VirtualServer vs(ip, hostname, num_cores, bandwith);
  222.     cout << vs;
  223.     cin >> num_inc;
  224.     for (int i = 0; i < num_inc; ++i) {
  225.       vs++;
  226.     }
  227.  
  228.     cin >> num_inc;
  229.     vs += num_inc;
  230.  
  231.     cout << vs;
  232.  
  233.   } else if (test_case == 7) {
  234.     cin >> ip >> hostname;
  235.     cin >> num_cores >> bandwith;
  236.     VirtualServer vs(ip, hostname, num_cores, bandwith);
  237.     cout << vs;
  238.     cout << vs.total_price() << endl;
  239.  
  240.     cin >> num_inc;
  241.     for (int i = 0; i < num_inc; ++i) {
  242.       vs++;
  243.     }
  244.  
  245.     cin >> num_inc;
  246.     vs += num_inc;
  247.     cout << vs;
  248.     cout << vs.total_price();
  249.   } else if (test_case == 8) {
  250.  
  251.     int num_servers;
  252.     int server_type;
  253.     cin >> num_servers;
  254.     Server **s = new Server *[num_servers];
  255.     for (int j = 0; j < num_servers; ++j) {
  256.       cin >> server_type;
  257.       if (server_type == 1) {
  258.         cin >> ip >> hostname;
  259.         cin >> basic_price >> ram;
  260.         DedicatedServer *dsp =
  261.             new DedicatedServer(ip, hostname, basic_price, ram);
  262.         cin >> num_inc;
  263.         for (int i = 0; i < num_inc; ++i) {
  264.           (*dsp)++; }
  265.         cin >> num_inc;
  266.         for (int i = 0; i < num_inc; ++i) {
  267.           cin >> disk_space;
  268.           (*dsp) += disk_space; }
  269.         cout << *(dsp);
  270.         cout << dsp->total_price() << endl;
  271.         s[j] = dsp; }
  272.       if (server_type == 2) {
  273.         cin >> ip >> hostname;
  274.         cin >> num_cores >> bandwith;
  275.         VirtualServer *vsp =
  276.             new VirtualServer(ip, hostname, num_cores, bandwith);
  277.         cin >> num_inc;
  278.         for (int i = 0; i < num_inc; ++i) {
  279.           (*vsp)++;}
  280.         cin >> num_inc;
  281.         (*vsp) += num_inc;
  282.  
  283.         cout << (*vsp);
  284.         cout << vsp->total_price() << endl;
  285.         s[j] = vsp;
  286.       }
  287.     }
  288.  
  289.     cout << "The cost of all servers is:\n";
  290.     cout << totalCost(s, num_servers);
  291.     cout << endl;
  292.  
  293.     cout << "Biggest invoice:\n";
  294.     Server &server = biggestInvoice(s, num_servers);
  295.  
  296.     Server *ss = &server;
  297.     DedicatedServer *bip;
  298.     VirtualServer *pip;
  299.     if (dynamic_cast<DedicatedServer *>(ss)) {
  300.       bip = dynamic_cast<DedicatedServer *>(ss);
  301.       cout << *bip << bip->total_price();
  302.     }
  303.     if (dynamic_cast<VirtualServer *>(ss)) {
  304.       pip = dynamic_cast<VirtualServer *>(ss);
  305.       cout << *pip << pip->total_price();
  306.     }
  307.   }
  308.   return 0;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement