Advertisement
1cutcut1

vtora

May 25th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5. using namespace std;
  6. class InvalidTimeException{
  7. public:
  8.     void print()
  9.     {
  10.         cout<<"The time is not valid"<<endl;
  11.     }
  12. };
  13. class Race{
  14. protected:
  15.     char name[41];
  16.     int godina;
  17.     int mesec;
  18.     float najdobroVreme;
  19.     float km;
  20. public:
  21.     Race (char *name=" ",int godina=0,int mesec=0,float najdobroVreme=0.0,float km=0.0)
  22.     {
  23.         strcpy (this->name,name);
  24.         this->godina=godina;
  25.         this->mesec=mesec;
  26.         this->najdobroVreme=najdobroVreme;
  27.         this->km=km;
  28.     }
  29.     Race (const Race &R)
  30.     {
  31.         strcpy (this->name,R.name);
  32.         this->godina=R.godina;
  33.         this->mesec=R.mesec;
  34.         this->najdobroVreme=R.najdobroVreme;
  35.         this->km=R.km;
  36.     }
  37.     Race &operator= (const Race &R)
  38.     {
  39.         if (this!=&R)
  40.         {
  41.             strcpy (this->name,R.name);
  42.             this->godina=R.godina;
  43.             this->mesec=R.mesec;
  44.             this->najdobroVreme=R.najdobroVreme;
  45.             this->km=R.km;
  46.         }
  47.         return *this;
  48.     }
  49.     virtual float heaviness()
  50.     {
  51.         return najdobroVreme/km;
  52.     }
  53.     friend ostream &operator<<(ostream &out,Race &R);
  54.     float getNajdobro()
  55.     {
  56.         return najdobroVreme;
  57.     }
  58. };
  59. ostream &operator<<(ostream &out,Race &R)
  60. {
  61.     out<<R.name<<" "<<R.mesec<<"."<<R.godina<<" "<<R.heaviness()<<endl;
  62.     return out;
  63. }
  64.  
  65. class CarRace: public Race{
  66.     float *najdobriVreminja;
  67.     int brojNajdobriVreminja;
  68.     int brojKrugovi;
  69. public:
  70.     static float CAR_COEF;
  71.     static void setKoeficient(float a)
  72.     {
  73.         CAR_COEF=a;
  74.     }
  75.     CarRace (char *name=" ",int godina=0,int mesec=0,float najdobroVreme=0.0,float km=0.0,float *najdobriVreminja=0,int brojNajdobriVreminja=0,int brojKrugovi=0):Race(name,godina,mesec,najdobroVreme,km)
  76.     {
  77.         this->brojNajdobriVreminja=brojNajdobriVreminja;
  78.         this->brojKrugovi=brojKrugovi;
  79.         this->najdobriVreminja=new float[brojNajdobriVreminja];
  80.         for (int i=0;i<brojNajdobriVreminja;i++)
  81.         {
  82.             this->najdobriVreminja[i]=najdobriVreminja[i];
  83.         }
  84.     }
  85.     CarRace (const CarRace &CR):Race (CR)
  86.     {
  87.         this->brojNajdobriVreminja=CR.brojNajdobriVreminja;
  88.         this->brojKrugovi=CR.brojKrugovi;
  89.         this->najdobriVreminja=new float[CR.brojNajdobriVreminja];
  90.         for (int i=0;i<CR.brojNajdobriVreminja;i++)
  91.         {
  92.             this->najdobriVreminja[i]=CR.najdobriVreminja[i];
  93.         }
  94.     }
  95.     CarRace &operator=(const CarRace &CR)
  96.     {
  97.         if (this!=&CR)
  98.         {
  99.             delete []najdobriVreminja;
  100.             Race::operator=(CR);
  101.             this->brojNajdobriVreminja=CR.brojNajdobriVreminja;
  102.             this->brojKrugovi=CR.brojKrugovi;
  103.             this->najdobriVreminja=new float[CR.brojNajdobriVreminja];
  104.             for (int i=0;i<CR.brojNajdobriVreminja;i++)
  105.             {
  106.                 this->najdobriVreminja[i]=CR.najdobriVreminja[i];
  107.             }
  108.         }
  109.         return *this;
  110.     }
  111.     ~CarRace()
  112.     {
  113.         delete []najdobriVreminja;
  114.     }
  115.     float heaviness()
  116.     {
  117.         float zbir=0;
  118.         float prosek=0;
  119.         for (int i=0;i<brojNajdobriVreminja;i++)
  120.         {
  121.             prosek=prosek+najdobriVreminja[i];
  122.         }
  123.         prosek=prosek/(float)brojNajdobriVreminja;
  124.         zbir=Race::heaviness()+(prosek*CarRace::CAR_COEF);
  125.         if (brojKrugovi>15)
  126.         {
  127.             zbir=zbir+(zbir*10)/100;
  128.         }
  129.         return zbir;
  130.     }
  131.     CarRace &operator+=(float vreme)
  132.     {
  133.         if(vreme<10)
  134.         {
  135.             throw InvalidTimeException();
  136.         }
  137.         float *tmp=new float[brojNajdobriVreminja+1];
  138.         for (int i=0;i<brojNajdobriVreminja;i++)
  139.         {
  140.             tmp[i]=najdobriVreminja[i];
  141.         }
  142.         tmp[brojNajdobriVreminja++]=vreme;
  143.         delete []najdobriVreminja;
  144.         najdobriVreminja=tmp;
  145.         return *this;
  146.     }
  147.     void setNumberLaps (int broj)
  148.     {
  149.         brojKrugovi=broj;
  150.     }
  151.     friend ostream &operator<<(ostream &out,CarRace &Cr);
  152. };
  153. float CarRace::CAR_COEF=0.3;
  154. ostream &operator<<(ostream &out,CarRace &CR)
  155. {
  156.     Race *tmp=dynamic_cast<CarRace*>(&CR);
  157.     out<<*tmp;
  158.     return out;
  159. }
  160.  
  161.  
  162. int main(){
  163.     int testCase;
  164.     cin >> testCase;
  165.     char city[50];
  166.     int year;
  167.     int month;
  168.     float bestTime;
  169.     float length;
  170.     float bestTimes[50];
  171.     int n;
  172.     int m;
  173.     int izbor;
  174.     int numberLaps;
  175.  
  176.     if (testCase == 1){
  177.         cout << "===== Testiranje na klasite ======" << endl;
  178.         cin >> city;
  179.         cin >> year;
  180.         cin >> month;
  181.         cin >> bestTime;
  182.         cin>>length;
  183.         Race t(city, year, month, bestTime, length);
  184.         cout<<t;
  185.         cin >> city;
  186.         cin >> year;
  187.         cin >> month;
  188.         cin >> bestTime;
  189.         cin>>length;
  190.         cin>>numberLaps;
  191.         cin>>n;
  192.         for (int j = 0; j < n; j++)
  193.             cin >> bestTimes[j];
  194.         CarRace mgt(city, year, month, bestTime, length, bestTimes, n, numberLaps);
  195.         cout << mgt;
  196.         CarRace mgt2;
  197.     }
  198.     if (testCase == 2){
  199.         cout << "===== Testiranje na operatorot += ======" << endl;
  200.         Race **niza;
  201.         cin >> m;
  202.         niza = new Race *[m];
  203.         for (int i = 0; i<m; i++){
  204.             cin >> izbor;
  205.             cin >> city;
  206.             cin >> year;
  207.             cin >> month;
  208.             cin >> bestTime;
  209.             cin >>length;
  210.             if (izbor == 1){
  211.                 niza[i] = new Race(city, year, month, bestTime, length);
  212.             }
  213.             else{
  214.                 cin>>numberLaps;
  215.                 cin>>n;
  216.                 for (int j = 0; j < n; j++)
  217.                     cin >> bestTimes[j];
  218.                 niza[i] = new CarRace(city, year, month, bestTime, length, bestTimes, n, numberLaps);
  219.             }
  220.         }
  221.         // pecatenje na site Trki
  222.         cout << "\nLista na site Trki:\n";
  223.         for (int i = 0; i < m; i++)
  224.             cout << *niza[i];
  225.  
  226.         // dodavanje novo najdobro vreme za prvata CarRace
  227.         float best;
  228.         cin >> best;
  229.         int flag = 1;
  230.         for (int i = 0; i < m; i++){
  231.             CarRace* nn = dynamic_cast<CarRace*>(niza[i]);
  232.             if (nn != 0){
  233.                 flag = 0;
  234.                 (*nn) += best;
  235.                 break;
  236.             }
  237.         }
  238.  
  239.  
  240.         // pecatenje na site Trki
  241.         cout << "\nLista na site Trki:\n";
  242.         for (int i = 0; i < m; i++)
  243.             cout << *niza[i];
  244.     }
  245.     if (testCase == 3){
  246.         cout << "===== Testiranje na isklucoci ======" << endl;
  247.         cin >> city;
  248.         cin >> year;
  249.         cin >> month;
  250.         cin >> bestTime;
  251.         cin>>length;
  252.         cin>>numberLaps;
  253.         CarRace mgt(city, year, month, bestTime, length);
  254.         mgt.setNumberLaps(numberLaps);
  255.         float vreme1,vreme2;
  256.         cin>>vreme1>>vreme2;
  257.         try{
  258.             mgt+=vreme1;
  259.             mgt+=vreme2;
  260.         }
  261.         catch(InvalidTimeException e)
  262.         {
  263.             e.print();
  264.         }
  265.         cout<<mgt;
  266.     }
  267.  
  268.     if (testCase == 5){
  269.         cout << "===== Testiranje na static clenovi ======" << endl;
  270.         Race **niza;
  271.         cin >> m;
  272.         niza = new Race *[m];
  273.         for (int i = 0; i<m; i++){
  274.             cin >> izbor;
  275.             cin >> city;
  276.             cin >> year;
  277.             cin >> month;
  278.             cin >> bestTime;
  279.             cin >>length;
  280.             if (izbor == 1){
  281.                 niza[i] = new Race(city, year, month, bestTime, length);
  282.             }
  283.             else{
  284.                 cin>>numberLaps;
  285.                 cin>>n;
  286.                 for (int j = 0; j < n; j++)
  287.                     cin >> bestTimes[j];
  288.                 niza[i] = new CarRace(city, year, month, bestTime, length, bestTimes, n, numberLaps);
  289.             }
  290.         }
  291.         // pecatenje na site Trki
  292.         cout << "\nLista na site Trki:\n";
  293.         for (int i = 0; i < m; i++)
  294.             cout << *niza[i];
  295.  
  296.         CarRace::setKoeficient(0.7);
  297.         // pecatenje na site Trki
  298.         cout << "\nLista na site Trki:\n";
  299.         for (int i = 0; i < m; i++)
  300.             cout << *niza[i];
  301.     }
  302.  
  303.  
  304.  
  305.     return 0;
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement