Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. -----Main.cpp------
  2.  
  3. #pragma once
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include "Timespan.h"
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     int n;
  13.     cin >> n;
  14.  
  15.     vector<Timespan> times;
  16.  
  17.     for(int i = 0; i < n; i++)
  18.     {
  19.         int d, m, h, s;
  20.         cin >> d >> h >> m >> s;
  21.         times.push_back(Timespan(d, h, m, s));
  22.     }
  23.  
  24.     for(int i = 0; i < n; i++)
  25.     {
  26.         for(int j = i+1; j < n; j++)
  27.         {
  28.             if(times[i] > times[j])
  29.             {
  30.                 Timespan tmp = times[i];
  31.                 times[i] = times[j];
  32.                 times[j] = tmp;
  33.             }
  34.         }
  35.     }
  36.  
  37.     for(int i = 0; i < n; i++)
  38.         cout << times[i] << endl;
  39.  
  40.     return 0;
  41.  
  42. }
  43.  
  44. -----Timespan.cpp------
  45.  
  46. #include "Timespan.h"
  47.  
  48. Timespan::Timespan()
  49. {
  50. }
  51.  
  52. Timespan::Timespan(int days, int hours, int minutes, int seconds)
  53. {
  54.     this->days = days;
  55.     this->hours = hours;
  56.     this->minutes = minutes;
  57.     this->seconds = seconds;
  58. }
  59.  
  60. Timespan::~Timespan()
  61. {
  62. }
  63.  
  64. bool Timespan::operator >(const Timespan& other)
  65. {
  66.     if (days > other.days) return true;
  67.     else if (days == other.days && hours > other.hours) return true;
  68.     else if (days == other.days && hours == other.hours && minutes > other.minutes) return true;
  69.     else if (days == other.days && hours == other.hours && minutes == other.minutes && seconds > other.seconds) return true;
  70.     else return false;
  71. }
  72.  
  73. ostream& operator <<(ostream& izlaz, Timespan other)
  74. {
  75.     int ukupno, dani, sati, minute, sekunde;
  76.  
  77.     dani = other.days * 86400;
  78.     sati = other.hours * 3600;
  79.     minute = other.minutes * 60;
  80.     ukupno = dani + sati + minute + other.seconds;
  81.  
  82.     dani = ukupno / 60 / 60 / 24;
  83.     sati = (ukupno / 60 / 60) % 24;
  84.     minute = (ukupno / 60) % 60;
  85.     sekunde = ukupno % 60;
  86.  
  87.     if (dani > 0) izlaz << dani << "d ";
  88.     if (sati > 0) izlaz << sati << "h ";
  89.     if (minute > 0) izlaz << minute << "min ";
  90.     if (sekunde > 0) izlaz << sekunde << "sec";
  91.  
  92.     return izlaz;
  93. }
  94.  
  95. -----Timespan.h---------
  96.  
  97. #pragma once
  98. #include <iostream>
  99. #include <string>
  100. #include <vector>
  101. using namespace std;
  102.  
  103. class Timespan
  104. {
  105.  
  106. private:
  107.  
  108.     int days, hours, minutes, seconds;
  109.  
  110. public:
  111.  
  112.     Timespan();
  113.     Timespan(int days, int hours, int minutes, int seconds);
  114.     ~Timespan();
  115.  
  116.     bool operator >(const Timespan& other);
  117.     friend ostream& operator <<(ostream& izlaz, Timespan other);
  118.  
  119. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement