velimir

Задача 2

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