amarek

31.01.2020.

Jul 17th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3. using namespace std;
  4.  
  5. template<typename Tip>
  6. int countRuns(Tip *array, int size) {
  7.     int counter = 0;
  8.     for (int i = 0; i < size; i++) {
  9.         if (array[i] == array[i + 1]) {
  10.             counter++;
  11.             while (array[i] == array[i + 1]) {
  12.                 i++;
  13.             }
  14.         }
  15.     }
  16.     return counter;
  17. }
  18.  
  19. class Song {
  20.     int mBpm;
  21.     string mName;
  22. public:
  23.     Song(): mBpm(0), mName("Unknown"){}
  24.     Song(string name, int bpm) : mName(name), mBpm(bpm) {}
  25.     friend ostream& operator<< (ostream&, const Song);
  26.     Song* getSong() {
  27.         Song array[10];
  28.         for (int i = 0; i < 10; i++) {
  29.             array[i] = Song();
  30.         }
  31.         return array;
  32.     }
  33.     friend bool operator== (const Song& s1, const Song& s2);
  34. };
  35.  
  36. ostream& operator<< (ostream& izlaz, const Song song) {
  37.     izlaz << song.mName << ", " << song.mBpm << endl;
  38.     return izlaz;
  39. }
  40.  
  41. bool operator== (const Song& s1, const Song& s2) {
  42.     return s1.mBpm == s2.mBpm && s1.mName == s2.mName;
  43. }
  44.  
  45. class MovieTheather {
  46. protected:
  47.     int mTotalPlaces;
  48.     int mOccupiedPlaces;
  49.     double mTicketPrice;
  50. public:
  51.     MovieTheather() : mTotalPlaces(0), mOccupiedPlaces(0), mTicketPrice(0){}
  52.     MovieTheather(int totalPlaces, int occupiedPlaces, int ticketPrice) : mTotalPlaces(totalPlaces), mOccupiedPlaces(occupiedPlaces), mTicketPrice(ticketPrice){}
  53.     MovieTheather(int totalPlaces, int ticketPrice) : mTotalPlaces(totalPlaces), mOccupiedPlaces(totalPlaces), mTicketPrice(ticketPrice) {}
  54.     double getEarnings() { return mOccupiedPlaces * mTicketPrice; }
  55.     bool isFull() {
  56.         return mTotalPlaces == mOccupiedPlaces;
  57.     }
  58.     void buyTicket() {
  59.         mOccupiedPlaces++;
  60.     }
  61.     double calculateTimeTillFull(int mTicketsPerHour) {
  62.         int mPlacesToFill = mTotalPlaces - mOccupiedPlaces;
  63.         return mPlacesToFill / mTicketsPerHour;
  64.     }
  65. };
  66.  
  67. int FindLastToFill(MovieTheather *array, int size, int mTicketsPerHour) {
  68.     int maxIndex = 0;
  69.     for (int i = 0; i < size; i++) {
  70.         if (array[i].calculateTimeTillFull(mTicketsPerHour) > array[maxIndex].calculateTimeTillFull(mTicketsPerHour)) {
  71.             maxIndex = i;
  72.         }
  73.     }
  74.     return maxIndex;
  75. }
  76.  
  77. void runExample() {
  78.     MovieTheather theather(100, 2.50);
  79.     if (theather.isFull()) cout << "No space left." << endl;
  80.     else theather.buyTicket();
  81. }
  82.  
  83. class LuxuryTheather : public MovieTheather {
  84.     double mLuxuryTicketPrice;
  85.     int mLuxuryPacket;
  86. public:
  87.     LuxuryTheather(int totalPlaces, int occupiedPlaces, int ticketPrice, double luxuryTicketPrice, int luxuryPacket) : MovieTheather(totalPlaces, occupiedPlaces, ticketPrice), mLuxuryTicketPrice(luxuryTicketPrice), mLuxuryPacket(luxuryPacket){}
  88.     virtual ~LuxuryTheather();
  89.     virtual void buyTicket(bool isLuxuryPacket){
  90.         if (isFull()) { cout << "No space left."; }
  91.         else {
  92.             mOccupiedPlaces++;
  93.             if (isLuxuryPacket) {
  94.                 mLuxuryPacket++;
  95.             }
  96.         }
  97.     }
  98. };
  99.  
  100. class TicketException : public runtime_error
  101. {
  102.     int mTicketNumber;
  103. public:
  104.     TicketException(string message, int ticketNumber) : runtime_error(message), mTicketNumber(ticketNumber) {}
  105.     int getTicketNumber()
  106.     {
  107.         return mTicketNumber;
  108.     }
  109. };
  110.  
  111. void buyTickets(LuxuryTheather *theather, int *tickets, int size) {
  112.     try
  113.     {
  114.         srand(time(NULL));
  115.         bool isLuxuryPacket;
  116.         for (int i = 0; i < size; i++) {
  117.             isLuxuryPacket = rand() % 2;
  118.             for (int j = 0; j < tickets[i]; j++)
  119.                 if (theather->isFull()) {
  120.                     throw(TicketException("No enough space.", tickets[i]));
  121.                 }
  122.                 else {
  123.                     theather->buyTicket(isLuxuryPacket);
  124.                 }
  125.         }
  126.     }
  127.     catch (TicketException& e)
  128.     {
  129.         cout << e.what() << e.getTicketNumber() << endl;
  130.     }
  131. }
  132.  
  133. int main() {
  134.     int array[] = { 1,8,3,5,4,9,0,2,2,2 };
  135.     cout << countRuns(array, 10) << endl;
  136.  
  137.     Song demo("In the end", 105);
  138.     cout << demo << endl;
  139.     Song* songs = getSong();
  140.     cout << countRuns(songs, 10) << endl;
  141.  
  142.     system("pause");
  143. }
Advertisement
Add Comment
Please, Sign In to add comment