brsjak

Задача 2 (6/6)

Jun 15th, 2016
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Exception {};
  5. class Trud {
  6. private:
  7.     char vidTrud;
  8.     int godinaIzdavanje;
  9.     static int J;
  10.     static int C;
  11. public:
  12.     Trud() {}
  13.     Trud(char vidTrud,int godinaIzdavanje) {
  14.         this->vidTrud = vidTrud;
  15.         this->godinaIzdavanje = godinaIzdavanje;
  16.     }
  17.     int getGodinaIzdavanje() {
  18.         return godinaIzdavanje;
  19.     }
  20.     char getVidTrud() {
  21.         return vidTrud;
  22.     }
  23.     int getC() {
  24.         return C;
  25.     }
  26.     int getJ() {
  27.         return J;
  28.     }
  29.     static void setJ(int j) {
  30.         J = j;
  31.     }
  32.     static void setC(int c) {
  33.         C = c;
  34.     }
  35.     friend istream& operator>> (istream& in,Trud &t) {
  36.         in>>t.vidTrud;
  37.         in>>t.godinaIzdavanje;
  38.         return in;
  39.     }
  40.     ~Trud() {}
  41. };
  42. int Trud::J = 3;
  43. int Trud::C = 1;
  44. class Student {
  45. protected:
  46.     char ime[30];
  47.     int index;
  48.     int godinaUpis;
  49.     int oceni[30];
  50.     int brOceni;
  51.     bool isphd;
  52. public:
  53.     Student() {}
  54.     Student(char ime[],int index,int godinaUpis, int oceni[],int brOceni) {
  55.         strcpy(this->ime,ime);
  56.         this->index = index;
  57.         this->godinaUpis = godinaUpis;
  58.         this->brOceni = brOceni;
  59.         isphd=false;
  60.         for(int i=0; i<brOceni; i++) {
  61.             this->oceni[i] = oceni[i];
  62.         }
  63.     }
  64.     int getIndex() {
  65.         return index;
  66.     }
  67.     void setPHD()
  68.     {
  69.     isphd=true;
  70.     }
  71.     bool getPHD()
  72.     {
  73.     return isphd;
  74.     }
  75.     virtual float rang() {
  76.         int suma=0;
  77.         for(int i=0; i<brOceni; i++) {
  78.             suma = suma + oceni[i];
  79.         }
  80.         return (float)suma / brOceni;
  81.     }
  82.     friend ostream& operator<<(ostream &out,Student &s) {
  83.         out<<s.index<<" "<<s.ime<<" "<<s.godinaUpis<<" "<<s.rang()<<endl;
  84.         return out;
  85.     }
  86.     ~Student() {}
  87.  
  88. };
  89. class PhDStudent : public Student {
  90. private:
  91.     Trud *trudovi;
  92.     int brTrudovi;
  93. public:
  94.     PhDStudent() {
  95.         brTrudovi=0;
  96.         trudovi = new Trud [brTrudovi];
  97.     }
  98.     PhDStudent(char ime[],int index,int godinaUpis, int oceni[],int brOceni,Trud trudovi[],int brTrudovi) : Student(ime,index,godinaUpis,oceni,brOceni)
  99.  
  100.     {
  101.         Student::setPHD();
  102.         this->brTrudovi = brTrudovi;
  103.         this->trudovi = new Trud [ brTrudovi];
  104.         for(int i =0; i<brTrudovi; i++) {
  105.             try {
  106.                 if(trudovi[i].getGodinaIzdavanje()<godinaUpis)
  107.                     throw Exception();
  108.                 this->trudovi[i] = trudovi[i];
  109.             }
  110.             catch(Exception)
  111.             {
  112.                 cout<<"Ne moze da se vnese dadeniot trud"<<endl;
  113.             }
  114.  
  115.         }
  116.  
  117.     }
  118.     float rang() {
  119.         float r = Student::rang();
  120.         for(int i=0; i<brTrudovi; i++) {
  121.             if(trudovi[i].getVidTrud()=='C' || trudovi[i].getVidTrud()=='c')
  122.                 r = r + trudovi[i].getC();
  123.             else if(trudovi[i].getVidTrud()=='J' || trudovi[i].getVidTrud()=='j')
  124.                 r = r + trudovi[i].getJ();
  125.  
  126.         }
  127.         return r;
  128.     }
  129.     PhDStudent& operator+=(Trud &nov) {
  130.         if(nov.getGodinaIzdavanje()<godinaUpis) {
  131.             throw Exception();
  132.         }
  133.         Trud *temp;
  134.         temp = new Trud[brTrudovi+1];
  135.         for(int i=0; i<brTrudovi; i++) {
  136.             temp[i] = trudovi[i];
  137.  
  138.         }
  139.         temp[brTrudovi] = nov;
  140.         delete []trudovi;
  141.         trudovi = temp;
  142.         brTrudovi++;
  143.         return *this;
  144.     }
  145.     ~PhDStudent() {
  146.         //delete []trudovi;
  147.     }
  148. };
  149.  
  150. int main() {
  151.     int testCase;
  152.     cin >> testCase;
  153.  
  154.     int god, indeks, n, god_tr, m, n_tr;
  155.     int izbor; //0 za Student, 1 za PhDStudent
  156.     char ime[30];
  157.     int oceni[50];
  158.     char tip;
  159.     Trud trud[50];
  160.  
  161.     if (testCase == 1) {
  162.         cout << "===== Testiranje na klasite ======" << endl;
  163.         cin >> ime;
  164.         cin >> indeks;
  165.         cin >> god;
  166.         cin >> n;
  167.         for (int j = 0; j < n; j++)
  168.             cin >> oceni[j];
  169.         Student s(ime, indeks, god, oceni, n);
  170.         cout << s;
  171.  
  172.         cin >> ime;
  173.         cin >> indeks;
  174.         cin >> god;
  175.         cin >> n;
  176.         for (int j = 0; j < n; j++)
  177.             cin >> oceni[j];
  178.         cin >> n_tr;
  179.         for (int j = 0; j < n_tr; j++) {
  180.             cin >> tip;
  181.             cin >> god_tr;
  182.             Trud t(tip, god_tr);
  183.             trud[j] = t;
  184.         }
  185.         PhDStudent phd(ime, indeks, god, oceni, n, trud, n_tr);
  186.         cout << phd;
  187.     }
  188.     if (testCase == 2) {
  189.         cout << "===== Testiranje na operatorot += ======" << endl;
  190.         Student **niza;
  191.         cin >> m;
  192.         niza = new Student *[m];
  193.         for (int i = 0; i<m; i++) {
  194.             cin >> izbor;
  195.             cin >> ime;
  196.             cin >> indeks;
  197.             cin >> god;
  198.             cin >> n;
  199.             for (int j = 0; j < n; j++)
  200.                 cin >> oceni[j];
  201.  
  202.             if (izbor == 0) {
  203.                 niza[i] = new Student(ime, indeks, god, oceni, n);
  204.             } else {
  205.                 cin >> n_tr;
  206.                 for (int j = 0; j < n_tr; j++) {
  207.                     cin >> tip;
  208.                     cin >> god_tr;
  209.                     Trud t(tip, god_tr);
  210.                     trud[j] = t;
  211.                 }
  212.                 niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
  213.             }
  214.         }
  215.         // pecatenje na site studenti
  216.         cout << "\nLista na site studenti:\n";
  217.         for (int i = 0; i < m; i++)
  218.             cout << *niza[i];
  219.  
  220.         // dodavanje nov trud za PhD student spored indeks
  221.         Trud t;
  222.         cin >> indeks;
  223.         cin >> t;
  224.         bool najde= false;
  225.         for(int i=0;i<m;i++)
  226.         {
  227.             if(niza[i]->getIndex()==indeks && niza[i]->getPHD())
  228.             {
  229.                 najde = true;
  230.                 PhDStudent *p= dynamic_cast<PhDStudent*>(niza[i]);
  231.                 if(p!=0)
  232.                 {
  233.                     try{
  234.                        *p+=t;
  235.                     }
  236.                     catch(Exception)
  237.                     {
  238.                         cout<<"Ne moze da se vnese dadeniot trud"<<endl;
  239.                     }
  240.                    
  241.                 }
  242.             }
  243.         }
  244.         if(najde==false)
  245.         {
  246.             cout<<"Ne postoi PhD student so indeks "<<indeks<<endl;
  247.         }
  248.         // vmetnete go kodot za dodavanje nov trud so pomos na operatorot +=
  249.  
  250.         // pecatenje na site studenti
  251.         cout << "\nLista na site studenti:\n";
  252.         for (int i = 0; i < m; i++)
  253.             cout << *niza[i];
  254.     }
  255.     if (testCase == 3) {
  256.         cout << "===== Testiranje na operatorot += ======" << endl;
  257.         Student **niza;
  258.         cin >> m;
  259.         niza = new Student *[m];
  260.         for (int i = 0; i<m; i++) {
  261.             cin >> izbor;
  262.             cin >> ime;
  263.             cin >> indeks;
  264.             cin >> god;
  265.             cin >> n;
  266.             for (int j = 0; j < n; j++)
  267.                 cin >> oceni[j];
  268.  
  269.             if (izbor == 0) {
  270.                 niza[i] = new Student(ime, indeks, god, oceni, n);
  271.             } else {
  272.                 cin >> n_tr;
  273.                 for (int j = 0; j < n_tr; j++) {
  274.                     cin >> tip;
  275.                     cin >> god_tr;
  276.                     Trud t(tip, god_tr);
  277.                     trud[j] = t;
  278.                 }
  279.                 niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
  280.             }
  281.         }
  282.         // pecatenje na site studenti
  283.         cout << "\nLista na site studenti:\n";
  284.         for (int i = 0; i < m; i++)
  285.             cout << *niza[i];
  286.  
  287.         // dodavanje nov trud za PhD student spored indeks
  288.         Trud t;
  289.         cin >> indeks;
  290.         cin >> t;
  291.        
  292.         bool najde= false;
  293.         for(int i=0;i<m;i++)
  294.         {
  295.             if(niza[i]->getIndex()==indeks && niza[i]->getPHD())
  296.             {
  297.                 najde = true;
  298.                 PhDStudent *p= dynamic_cast<PhDStudent*>(niza[i]);
  299.                 if(p!=0)
  300.                 {
  301.                     try{
  302.                        *p+=t;
  303.                     }
  304.                     catch(Exception)
  305.                     {
  306.                         cout<<"Ne moze da se vnese dadeniot trud"<<endl;
  307.                     }
  308.                    
  309.                 }
  310.             }
  311.         }
  312.         if(najde==false)
  313.         {
  314.             cout<<"Ne postoi PhD student so indeks "<<indeks<<endl;
  315.         }
  316.         // vmetnete go kodot za dodavanje nov trud so pomos na operatorot += od Testcase 2
  317.  
  318.  
  319.         // pecatenje na site studenti
  320.         cout << "\nLista na site studenti:\n";
  321.         for (int i = 0; i < m; i++)
  322.             cout << *niza[i];
  323.     }
  324.     if (testCase == 4) {
  325.         cout << "===== Testiranje na isklucoci ======" << endl;
  326.         cin >> ime;
  327.         cin >> indeks;
  328.         cin >> god;
  329.         cin >> n;
  330.         for (int j = 0; j < n; j++)
  331.             cin >> oceni[j];
  332.         cin >> n_tr;
  333.         for (int j = 0; j < n_tr; j++) {
  334.             cin >> tip;
  335.             cin >> god_tr;
  336.             Trud t(tip, god_tr);
  337.             trud[j] = t;
  338.         }
  339.         PhDStudent phd(ime, indeks, god, oceni, n, trud, n_tr);
  340.         cout << phd;
  341.     }
  342.     if (testCase == 5) {
  343.         cout << "===== Testiranje na isklucoci ======" << endl;
  344.         Student **niza;
  345.         cin >> m;
  346.         niza = new Student *[m];
  347.         for (int i = 0; i<m; i++) {
  348.             cin >> izbor;
  349.             cin >> ime;
  350.             cin >> indeks;
  351.             cin >> god;
  352.             cin >> n;
  353.             for (int j = 0; j < n; j++)
  354.                 cin >> oceni[j];
  355.  
  356.             if (izbor == 0) {
  357.                 niza[i] = new Student(ime, indeks, god, oceni, n);
  358.             } else {
  359.                 cin >> n_tr;
  360.                 for (int j = 0; j < n_tr; j++) {
  361.                     cin >> tip;
  362.                     cin >> god_tr;
  363.                     Trud t(tip, god_tr);
  364.                     trud[j] = t;
  365.                 }
  366.                 niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
  367.             }
  368.         }
  369.         // pecatenje na site studenti
  370.         cout << "\nLista na site studenti:\n";
  371.         for (int i = 0; i < m; i++)
  372.             cout << *niza[i];
  373.  
  374.         // dodavanje nov trud za PhD student spored indeks
  375.         Trud t;
  376.         cin >> indeks;
  377.         cin >> t;
  378.        
  379.         bool najde= false;
  380.         for(int i=0;i<m;i++)
  381.         {
  382.             if(niza[i]->getIndex()==indeks && niza[i]->getPHD())
  383.             {
  384.                 najde = true;
  385.                 PhDStudent *p= dynamic_cast<PhDStudent*>(niza[i]);
  386.                 if(p!=0)
  387.                 {
  388.                     try{
  389.                        *p+=t;
  390.                     }
  391.                     catch(Exception)
  392.                     {
  393.                         cout<<"Ne moze da se vnese dadeniot trud"<<endl;
  394.                     }
  395.                    
  396.                 }
  397.             }
  398.         }
  399.         if(najde==false)
  400.         {
  401.             cout<<"Ne postoi PhD student so indeks "<<indeks<<endl;
  402.         }
  403.         // vmetnete go kodot za dodavanje nov trud so pomos na operatorot += i spravete se so isklucokot
  404.  
  405.         // pecatenje na site studenti
  406.         cout << "\nLista na site studenti:\n";
  407.         for (int i = 0; i < m; i++)
  408.             cout << *niza[i];
  409.     }
  410.     if (testCase == 6) {
  411.         cout << "===== Testiranje na static clenovi ======" << endl;
  412.         Student **niza;
  413.         cin >> m;
  414.         niza = new Student *[m];
  415.         for (int i = 0; i<m; i++) {
  416.             cin >> izbor;
  417.             cin >> ime;
  418.             cin >> indeks;
  419.             cin >> god;
  420.             cin >> n;
  421.             for (int j = 0; j < n; j++)
  422.                 cin >> oceni[j];
  423.  
  424.             if (izbor == 0) {
  425.                 niza[i] = new Student(ime, indeks, god, oceni, n);
  426.             } else {
  427.                 cin >> n_tr;
  428.                 for (int j = 0; j < n_tr; j++) {
  429.                     cin >> tip;
  430.                     cin >> god_tr;
  431.                     Trud t(tip, god_tr);
  432.                     trud[j] = t;
  433.                 }
  434.                 niza[i] = new PhDStudent(ime, indeks, god, oceni, n, trud, n_tr);
  435.             }
  436.         }
  437.         // pecatenje na site studenti
  438.         cout << "\nLista na site studenti:\n";
  439.         for (int i = 0; i < m; i++)
  440.             cout << *niza[i];
  441.  
  442.         int conf, journal;
  443.         cin >> conf;
  444.         cin >> journal;
  445.  
  446.         Trud::setJ(journal);
  447.         Trud::setC(conf);
  448.  
  449.         // pecatenje na site studenti
  450.         cout << "\nLista na site studenti:\n";
  451.         for (int i = 0; i < m; i++)
  452.             cout << *niza[i];
  453.     }
  454.  
  455.     return 0;
  456. }
Add Comment
Please, Sign In to add comment