Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <sstream>
- using namespace std;
- class Exception {
- string _message;
- public:
- Exception(string message) : _message(message) { }
- string message() const { return _message; }
- };
- class Duration {
- int _h;
- int _m;
- int _s;
- public:
- Duration() : _h(0), _m(0), _s(0) { }
- Duration(int s)
- {
- if (s < 0)
- throw Exception("Value must be positive integer");
- _s = s % 60;
- _m = (s / 60) % 60;
- _h = s / 3600;
- }
- Duration(int m, int s)
- {
- if (s < 0 || m < 0)
- throw Exception("Value must be positive integer");
- _s = s % 60;
- _m = m + (s / 60);
- _h = _m / 60;
- _m = _m - _h * 60;
- }
- Duration(int h, int m, int s)
- {
- if (s < 0 || m < 0 || h < 0)
- throw Exception("Value must be positive integer");
- _s = s % 60;
- _m = m + (s / 60);
- _h = h + _m / 60;
- _m = _m % 60;
- }
- Duration operator+(Duration const &D) const { return Duration(_h + D._h, _m + D._m, _s + D._s); }
- int hours() const { return _h; }
- int minutes() const { return _m; }
- int seconds() const { return _s; }
- int toSeconds() const { return _h * 3600 + _m * 60 + _s; }
- bool operator<(Duration const &D) const
- {
- if (_h < D._h || _h == D._h && _m < D._m || _h == D._h && _m == D._m && _s < D._s)
- return true;
- return false;
- }
- ostream &operator<<(ostream &out) const
- {
- if (_h != 0)
- out << _h << "h";
- if (_m != 0)
- out << _m << "m";
- out << _s << "s";
- return out;
- }
- };
- ostream &operator<<(ostream &out, Duration const &D)
- {
- return D << out;
- }
- class Track {
- Duration d;
- int nbCD = 0;
- public:
- Track(Duration _d) : d(_d) { }
- int refCount() { return nbCD; }
- void retain() { nbCD++; }
- void release() { nbCD--; }
- Duration duration() { return d; }
- virtual string description() = 0;
- virtual int sizeOnDisk() = 0;
- };
- class CD {
- string _title;
- int _capacity;
- vector<Track *> _tracks;
- public:
- CD(string title, int capacity) : _title(title)
- {
- if (capacity < 0)
- throw Exception("Capacity must be a positive integer");
- _capacity = capacity;
- }
- int tracks() const { return _tracks.size(); }
- Track *track(int i) const
- {
- if (i < _tracks.size())
- throw Exception("Index out of range");
- return _tracks[i];
- }
- Duration duration() const
- {
- Duration D;
- for (int i = 0; i < _tracks.size(); i++)
- {
- D = D + _tracks[i]->duration();
- }
- return D;
- }
- int freeSpace() const
- {
- int used = 0;
- for (int i = 0; i < _tracks.size(); i++)
- {
- used += _tracks[i]->sizeOnDisk();
- }
- return _capacity - used;
- }
- void addTrack(Track *T)
- {
- if (T->sizeOnDisk() > freeSpace())
- throw Exception("Disk full");
- _tracks.push_back(T);
- T->retain();
- }
- virtual ~CD()
- {
- for (int i = 0; i < _tracks.size(); i++)
- {
- _tracks[i]->refCount() == 1 ? delete _tracks[i] : _tracks[i]->release();
- }
- }
- };
- class AudioTrack : public Track {
- string title;
- string artist;
- public:
- AudioTrack(Duration d, string _title, string _artist) : Track (d), title(_title), artist(_artist) { }
- string description()
- {
- stringstream s;
- s << title << ", by " << artist << " (" << duration() << ")";
- return s.str();
- }
- int sizeOnDisk() { return duration().toSeconds() * 150000; }
- };
- class VideoTrack : public Track {
- string title;
- int _w;
- int _h;
- public:
- VideoTrack(Duration d, string _title, int width, int height) : Track(d), title(_title)
- {
- if (width < 0)
- throw Exception("Width must be a positive integer");
- if (height < 0)
- throw Exception("Height must be a positive integer");
- _w = width;
- _h = height;
- }
- string description()
- {
- stringstream s;
- s << title << " (" << _w << " x " << _h <<" x " << duration() << ")";
- return s.str();
- }
- int sizeOnDisk() { return 3 * duration().toSeconds() * _w * _h * 24; }
- };
- int main()
- {
- Duration d1(6101);
- Duration d2(3, 23, 6021);
- cout << d1 << endl << d2 << endl;
- cout << (d1 + d2) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement