amarek

10.05.2019.

Jul 17th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<typename Tip>
  6. int findLongestGrowth(Tip *array, int size)
  7. {
  8.     int maxGrowth = 0;
  9.     int currentGrowth;
  10.     for (int i = 0; i < size; i++)
  11.     {
  12.         currentGrowth = 1;
  13.         for (int j = i + 1; array[j] > array[j-1]; j++)
  14.         {
  15.             currentGrowth++;
  16.         }
  17.  
  18.         if (currentGrowth > maxGrowth)
  19.         {
  20.             maxGrowth = currentGrowth;
  21.         }
  22.     }
  23.     return maxGrowth;
  24. }
  25.  
  26. class Battery
  27. {
  28. private:
  29.     double capacityMah, percentageFull;
  30. public:
  31.     Battery() : capacityMah(0), percentageFull(0) {}
  32.     Battery(double capacity) : capacityMah(capacity), percentageFull(0){}
  33.     Battery(double mCapacityMah, double mPercentageFull) : capacityMah(mCapacityMah), percentageFull(mPercentageFull) {}
  34.     friend bool operator> (const Battery&, const Battery&);
  35.     friend ostream& operator<< (ostream&, const Battery&);
  36. };
  37.  
  38. bool operator> (const Battery& b1, const Battery& b2)
  39. {
  40.     return b1.capacityMah > b2.capacityMah && b1.percentageFull > b2.percentageFull;
  41. }
  42.  
  43. ostream& operator<< (ostream& izlaz, const Battery& b)
  44. {
  45.     izlaz << b.capacityMah << ", " << b.percentageFull << endl;
  46.     return izlaz;
  47. }
  48.  
  49. class Taximeter
  50. {
  51. protected:
  52.     double pricePerKm;
  53.     double startPrice;
  54. public:
  55.     Taximeter() : pricePerKm(0), startPrice(0){}
  56.     Taximeter(double mPricePerKg, double mStartPrice) : pricePerKm(mPricePerKg), startPrice(mStartPrice)
  57.     {
  58.         if (pricePerKm < 0)
  59.         {
  60.             pricePerKm *= -1; //apolutna vrijednost
  61.         }
  62.  
  63.         if (startPrice < 0)
  64.         {
  65.             startPrice *= -1;
  66.         }
  67.     }
  68.  
  69.     void setChangePricePerKm(double newPrice)
  70.     {
  71.         pricePerKm = newPrice;
  72.     }
  73.  
  74.     friend ostream& operator<< (ostream&, const Taximeter&);
  75.  
  76.     double ridePrice(double distance)
  77.     {
  78.         if (distance < 5)
  79.         {
  80.             return 5 + startPrice;
  81.         }
  82.         if (distance > 20)
  83.         {
  84.             return (pricePerKm * distance) + startPrice;
  85.         }
  86.         else
  87.         {
  88.             return (pricePerKm * 0.9 * distance) + startPrice;
  89.         }
  90.     }
  91.  
  92.     double averagePrice(double *distances, int n, int size)
  93.     {
  94.         int sum = 0;
  95.         for (int i = 0; i < size; i++)
  96.         {
  97.             if ((i + 1) % n == 0)
  98.             {
  99.                 break;
  100.             }
  101.             sum += ridePrice(distances[i]);
  102.         }
  103.         return sum / size;
  104.     }
  105. };
  106.  
  107. ostream& operator<< (ostream& izlaz, const Taximeter& taxi)
  108. {
  109.     izlaz << taxi.pricePerKm << ", " << taxi.startPrice << endl;
  110.     return izlaz;
  111. }
  112.  
  113. class BusTaximeter : Taximeter
  114. {
  115. private:
  116.     int passengerNumber;
  117.     bool outOfCityRide;
  118. public:
  119.     BusTaximeter() : passengerNumber(0), outOfCityRide(0){}
  120.     BusTaximeter(int mPassangerNumber, bool mOutOfCityRide): passengerNumber(mPassangerNumber), outOfCityRide(mOutOfCityRide){}
  121.     void setOutOfCityRide(bool outOfCity)
  122.     {
  123.         outOfCityRide = outOfCity;
  124.     }
  125.  
  126.     double ridePrice(double distance)
  127.     {
  128.         double discount;
  129.         if (passengerNumber >= 20)
  130.         {
  131.             discount = 0.8;
  132.         }
  133.         else
  134.         {
  135.             discount = 1 - (passengerNumber * 2 / 100);
  136.         }
  137.  
  138.         if (outOfCityRide)
  139.         {
  140.             return discount * pricePerKm * distance * 2;
  141.         }
  142.         else
  143.         {
  144.             return discount * pricePerKm * distance;
  145.         }
  146.     }
  147.  
  148. };
  149.  
  150. int main()
  151. {
  152.     int array[] = { 1,2,1,2,3 };
  153.     cout << findLongestGrowth(array, 5) << endl;
  154.  
  155.     Battery batteries[] = { Battery(), Battery(1566), Battery(4000), Battery(1500) };
  156.     if (findLongestGrowth(batteries, 4) == 1)
  157.     {
  158.         cout << batteries[0] << endl;
  159.     }
  160.  
  161.     system("pause");
  162. }
Add Comment
Please, Sign In to add comment