brsjak

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

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