Advertisement
metalni

OOP Zadaci za vezbanje 2 Naucni Trudovi

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