metalni

OOP Labs 4 Repozitorium za operativni sistemi

May 30th, 2020
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. enum Tip{LINUX, UNIX, WINDOWS};
  5.  
  6. using namespace std;
  7.  
  8. class OperativenSistem{
  9.     private:
  10.         char * name;
  11.         float version;
  12.         Tip t;
  13.         float size;
  14.     public:
  15.         OperativenSistem();
  16.         OperativenSistem(const char * ime, const float verzija, Tip type, const float golemina);
  17.         OperativenSistem(const OperativenSistem &orig);
  18.         OperativenSistem &operator=(const OperativenSistem &orig);
  19.         ~OperativenSistem();
  20.         void pecati();
  21.         bool ednakviSe(const OperativenSistem &os);
  22.         int sporediVerzija(const OperativenSistem &os);
  23.         bool istaFamilija(const OperativenSistem &sporedba);
  24. };
  25.  
  26. OperativenSistem::OperativenSistem(){
  27.     this->name = new char[0];
  28.     strcpy(this->name, "");
  29.     this->t = (Tip) 0;
  30.     this->version = 0.0;
  31.     this->size = 0.0;
  32. }
  33. OperativenSistem::OperativenSistem(const char * ime, const float verzija, Tip type, const float golemina){
  34.     this->name = new char[strlen(ime)+1];
  35.     strcpy(this->name, ime);
  36.     this->version = verzija;
  37.     this->t = type;
  38.     this->size = golemina;
  39. }
  40. OperativenSistem::OperativenSistem(const OperativenSistem &orig){
  41.     this->name = new char[strlen(orig.name)+1];
  42.     strcpy(this->name, orig.name);
  43.     this->version = orig.version;
  44.     this->t = orig.t;
  45.     this->size = orig.size;
  46. }
  47. OperativenSistem &OperativenSistem::operator=(const OperativenSistem &orig){
  48.     if(this!=&orig){
  49.         delete [] this->name;
  50.         this->name = new char[strlen(orig.name)+1];
  51.         strcpy(this->name, orig.name);
  52.         this->version = orig.version;
  53.         this->t = orig.t;
  54.         this->size = orig.size;
  55.     }
  56.     return *this;
  57. }
  58. OperativenSistem::~OperativenSistem(){
  59.     delete [] this->name;
  60. }
  61. void OperativenSistem::pecati(){
  62.     cout << "Ime: " << this->name << " " << "Verzija: " << this->version << " " << "Tip: " << this->t << " " << "Golemina:" << this->size << "GB" << endl;
  63. }
  64. bool OperativenSistem::ednakviSe(const OperativenSistem &os){
  65.     if((strcmp(this->name, os.name)==0)&&(this->version == os.version) && (this->t == os.t) && (this->size == os.size))
  66.         return true;
  67.     else
  68.         return false;
  69. }
  70. int OperativenSistem::sporediVerzija(const OperativenSistem &os){
  71.     if(this->version == os.version)
  72.         return 0;
  73.     else if(os.version > this->version)
  74.         return -1;
  75.     else
  76.         return 1;
  77. }
  78. bool OperativenSistem::istaFamilija(const OperativenSistem &sporedba){
  79.     if((strcmp(this->name, sporedba.name)==0) && (this->t == sporedba.t))
  80.         return true;
  81.     else
  82.         return false;
  83. }
  84.  
  85.  
  86. //repo
  87. class Repozitorium{
  88.     private:
  89.         char name[20];
  90.         OperativenSistem * os;
  91.         int currentoss;
  92.     public:
  93.         Repozitorium();
  94.         Repozitorium(const char * ime);
  95.         Repozitorium(const Repozitorium &orig);
  96.         Repozitorium &operator=(const Repozitorium &orig);
  97.         ~Repozitorium();
  98.         void pecatiOperativniSistemi();
  99.         void izbrishi(const OperativenSistem &izb);
  100.         void dodadi(const OperativenSistem &nov);
  101. };
  102.  
  103. Repozitorium::Repozitorium(){
  104.     strcpy(this->name, "");
  105. }
  106. Repozitorium::Repozitorium(const char * ime){
  107.     strcpy(this->name, ime);
  108.     this->os = new OperativenSistem[0];
  109.     this->currentoss = 0;
  110. }
  111. Repozitorium::Repozitorium(const Repozitorium &orig){
  112.     strcpy(this->name, orig.name);
  113.     this->currentoss = orig.currentoss;
  114.     this->os = new OperativenSistem[currentoss];
  115.     for(int i=0; i<currentoss; i++)
  116.         this->os[i] = orig.os[i];
  117. }
  118. Repozitorium &Repozitorium::operator=(const Repozitorium &orig){
  119.     if(this!=&orig){
  120.         delete this->os;
  121.         strcpy(this->name, orig.name);
  122.         this->currentoss = orig.currentoss;
  123.         this->os = new OperativenSistem[currentoss];
  124.         for(int i=0; i<currentoss; i++)
  125.             this->os[i] = orig.os[i];
  126.     }
  127.     return *this;
  128. }
  129. Repozitorium::~Repozitorium(){
  130.     delete [] this->os;
  131. }
  132. void Repozitorium::pecatiOperativniSistemi(){
  133.     cout << "Repozitorium: " << this->name << endl;
  134.     for(int i=0; i<currentoss; i++){
  135.         this->os[i].pecati();
  136.     }
  137. }
  138. void Repozitorium::izbrishi(const OperativenSistem &izb){
  139.     //naogjanje na indeksot na sistemot so treba da se brishe
  140.         int idx = -1;
  141.         for (int i=0;i<currentoss;i++){
  142.             if (os[i].ednakviSe(izb)){
  143.                 idx = i;
  144.                 break;
  145.             }
  146.         }
  147.  
  148.         if (idx==-1)
  149.             return ;
  150.  
  151.         OperativenSistem * tmp = new OperativenSistem[currentoss-1];
  152.         int j=0;
  153.         for (int i=0;i<currentoss;i++){
  154.             if (i!=idx){
  155.                 tmp[j]=os[i];
  156.                 j++;
  157.             }
  158.             else {
  159.                 continue;
  160.             }
  161.         }
  162.  
  163.         currentoss--;
  164.         delete [] os;
  165.         os = tmp;
  166. }
  167. void Repozitorium::dodadi(const OperativenSistem &nov){
  168.     for(int i=0; i<currentoss; i++){
  169.         if(this->os[i].istaFamilija(nov) && this->os[i].sporediVerzija(nov)==-1){
  170.             this->os[i] = nov;
  171.             return ;
  172.         }
  173.     }
  174.     OperativenSistem * temp = new OperativenSistem[currentoss+1];
  175.     for(int j=0; j<currentoss; j++){
  176.         temp[j] = this->os[j];
  177.     }
  178.  
  179.     temp[currentoss++] = nov;
  180.     delete [] this->os;
  181.     this->os = new OperativenSistem[currentoss];
  182.     for(int i=0; i<currentoss; i++)
  183.         this->os[i] = temp[i];
  184. }
  185. //main
  186. int main() {
  187.     char repoName[20];
  188.     cin>>repoName;
  189.     Repozitorium repozitorium=Repozitorium(repoName);
  190.     int brojOperativniSistemi = 0;
  191.     cin>>brojOperativniSistemi;
  192.     char ime[20];
  193.     float verzija;
  194.     int tip;
  195.     float golemina;
  196.     for (int i = 0; i<brojOperativniSistemi; i++){
  197.         cin>>ime;
  198.         cin>>verzija;
  199.         cin>>tip;
  200.         cin>>golemina;
  201.         OperativenSistem os = OperativenSistem(ime, verzija, (Tip)tip, golemina);
  202.         repozitorium.dodadi(os);
  203.     }
  204.  
  205.     repozitorium.pecatiOperativniSistemi();
  206.     cin>>ime;
  207.     cin>>verzija;
  208.     cin>>tip;
  209.     cin>>golemina;
  210.     OperativenSistem os = OperativenSistem(ime, verzija, (Tip)tip, golemina);
  211.     cout<<"=====Brishenje na operativen sistem====="<<endl;
  212.     repozitorium.izbrishi(os);
  213.     repozitorium.pecatiOperativniSistemi();
  214.     return 0;
  215. }
Add Comment
Please, Sign In to add comment