Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- using namespace std;
- #ifndef DURATION_H
- #define DURATION_H
- // Duration class
- class Duration {
- private:
- int hours = 0;
- int minutes = 0;
- int seconds = 0;
- public:
- Duration (int x = 0, int y = 0, int z = 0);
- void set (int sum);
- Duration operator + (Duration other);
- Duration operator - (Duration other);
- Duration operator * (double other);
- Duration operator / (double other);
- double operator / (Duration other);
- int inSeconds();
- Duration format (int sum);
- void randomize();
- operator const char*();
- int getHours();
- void setHours(int x);
- int getMinutes();
- void setMinutes(int x);
- int getSeconds();
- void setSeconds(int x);
- };
- Duration::Duration (int x, int y, int z) {
- hours = x;
- minutes = y;
- seconds = z;
- set(inSeconds());
- }
- void Duration::set (int sum) {
- if (sum < 0) {
- hours = 0;
- minutes = 0;
- seconds = 0;
- }
- else {
- hours = 0;
- minutes = 0;
- seconds = sum;
- while (seconds > 59) {
- minutes++;
- seconds -= 60;
- }
- while (minutes > 59) {
- hours++;
- minutes -= 60;
- }
- }
- }
- // Addition operator
- Duration Duration::operator + (Duration other) {
- return format(inSeconds() + other.inSeconds());
- }
- // Subtraction operator
- Duration Duration::operator - (Duration other) {
- return format(inSeconds() - other.inSeconds());
- }
- // Multiplication operator
- Duration Duration::operator * (double other) {
- return format(inSeconds() * other);
- }
- // Division operator (by constant)
- Duration Duration::operator / (double other) {
- return format(inSeconds() / other);
- }
- // Division operator (by another duration)
- double Duration::operator / (Duration other) {
- return (double)inSeconds() / other.inSeconds();
- }
- // Converts the duration into seconds
- int Duration::inSeconds() {
- return hours * 3600 + minutes * 60 + seconds;
- }
- // Converts the seconds to hours minutes and seconds
- Duration Duration::format (int sum) {
- if (sum < 0) {
- Duration duration (0, 0, 0);
- return duration;
- }
- int h = 0;
- int m = 0;
- int s = sum;
- while (s > 59) {
- m++;
- s -= 60;
- }
- while (m > 59) {
- h++;
- m -= 60;
- }
- Duration duration (h, m, s);
- return duration;
- }
- // Random duration less than a day
- void Duration::randomize() {
- hours = rand() % 24;
- minutes = rand() % 60;
- seconds = rand() % 60;
- }
- // Display operator
- Duration::operator const char*() {
- string s = "";
- ostringstream duration;
- if (hours < 10) {
- duration << "0" << hours << ":";
- }
- else {
- duration << hours << ":";
- }
- if (minutes < 10) {
- duration << "0" << minutes << ":";
- }
- else {
- duration << minutes << ":";
- }
- if (seconds < 10) {
- duration << "0" << seconds;
- }
- else {
- duration << seconds;
- }
- s = duration.str();
- return s.c_str();
- }
- int Duration::getHours() {
- return hours;
- }
- void Duration::setHours(int x) {
- hours = x;
- }
- int Duration::getMinutes() {
- return minutes;
- }
- void Duration::setMinutes(int x) {
- minutes = x;
- }
- int Duration::getSeconds() {
- return seconds;
- }
- void Duration::setSeconds(int x) {
- seconds = x;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement