C++ program to calculate the minutes between two time periods using class

Given numbers, we have to calculate the minutes between two time periods using the class and object approach.
Submitted by Shubh Pachori, on September 01, 2022

Example:

Input:
Enter Time Period (12:00AM-11:00PM):1:00PM-2:00PM

Output:
Minutes : 60

C++ code to calculate the minutes between two time periods using the class and object approach

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

// create a class
class Time {
  // private data member
 private:
  string time_period;

  // public member functions
 public:
  // getTime() function for inputting the time period
  void getTime() {
    cout << "Enter Time Period (12:00AM-11:00PM): ";
    cin >> time_period;
  }

  // getMinutes() function to calculate the minutes
  int getMinutes() {
    // initializing string, int and bool type variables
    // to perform operations and manipulations
    string num_1, num_2, hour_1, hour_2, min_1, min_2;
    int index_1 = time_period.find('-'), index_2;
    bool condition = false;

    // for loop for time period manipulation
    for (index_2 = 0; index_2 < time_period.length(); index_2++) {
      // if condition to check if the index_2 is
      // greater than or equal to the index_1
      if (index_2 >= index_1) {
        // condition will become false
        condition = false;

        // another for loop to calculate the time laps
        for (index_2; index_2 < time_period.length(); index_2++) {
          // if condition for gain the numerical values
          if (time_period[index_2] == ':') {
            condition = true;
            continue;
          }

          // if condition to check AM and PM
          if (time_period[index_2] == 'A' || time_period[index_2] == 'P') {
            // push_back function appends the value in the vector
            num_2.push_back(time_period[index_2]);
            num_2.push_back(time_period[index_2 + 1]);
          }

          // condition for minutes
          if (condition && isdigit(time_period[index_2])) {
            min_2.push_back(time_period[index_2]);
          }

          // else if for hours
          else if (isdigit(time_period[index_2])) {
            hour_2.push_back(time_period[index_2]);
          }
        }
      }

      // else condition to check if the index_2 is not
      // greater than or equal to the index_1
      else {
        // if condition for gain the numerical values
        if (time_period[index_2] == ':') {
          condition = true;
          continue;
        }

        // if condition to check AM and PM
        if (time_period[index_2] == 'A' || time_period[index_2] == 'P') {
          num_1.push_back(time_period[index_2]);
          num_1.push_back(time_period[index_2 + 1]);
        }

        // condition for minutes
        if (condition && isdigit(time_period[index_2])) {
          min_1.push_back(time_period[index_2]);
        }

        // else if condition for hours
        else if (isdigit(time_period[index_2])) {
          hour_1.push_back(time_period[index_2]);
        }
      }
    }

    // initializing int type variable hour with 0
    int hour = 0;

    // if hour_1, hour_2 and num_1, num_2 are equal and min_1 are greater than
    // min_2 stoi function is used to convert string values to the integer
    if (stoi(hour_1) == stoi(hour_2) && num_1 == num_2 &&
        stoi(min_1) > stoi(min_2)) {
      // calculating hours between that inputted time period
      hour = 24 - (stoi(hour_1) - stoi(hour_2));
    }

    // else if hour_1 is greater than hour_2 and num_1, num_2 are equal and
    // min_2 are greater than min_1
    else if (stoi(hour_1) > stoi(hour_2) && num_1 == num_2 &&
             stoi(min_1) < stoi(min_2)) {
      // calculating hours between that inputted time period
      hour = 24 - (stoi(hour_1) - stoi(hour_2));
    }

    // else if hour_1 is not 12 and hour_2 is 12 and num_1, num_2 are equal
    else if (num_1 == num_2 || (hour_2 == "12" && hour_1 != "12")) {
      // calculating hours between that inputted time period
      hour = stoi(hour_2) - stoi(hour_1);
    } else {
      // calculating hours between that inputted time period
      hour = (12 - stoi(hour_1)) + stoi(hour_2);
    }

    // initializing int type variable minutes to calculate minute
    int minutes;

    // if min_1 is not equal to 00
    if (min_1 != "00") {
      // calculating minutes between that inputted time period
      minutes = (hour * 60 - stoi(min_1)) + stoi(min_2);
    } else {
      // calculating minutes between that inputted time period
      minutes = (hour * 60 + stoi(min_1)) + stoi(min_2);
    }

    // returning calculated minutes
    return minutes;
  }
};

int main() {
  // create an object
  Time T;

  // int type variable to store calculated minutes
  int minute;

  // calling getTime() function to insert the time periods
  T.getTime();

  // calling getMinutes() function to calculate minutes in
  // between the inputted time period
  minute = T.getMinutes();

  cout << "Minutes : " << minute;

  return 0;
}

Output:

Enter Time Period (12:00AM-11:00PM):  11:59PM-11:58PM
Minutes : 1439

Explanation:

In the above code, we have created a class Time, one string type data member time_period to store the time period, and public member functions getTime() and getMinutes() to store time period and to calculate minutes in between the given time period.

In the main() function, we are creating an object T of class Time, reading time period by the user using getTime() function, and finally calling the getMinutes() member function to calculate minutes in the given time period. The getMinutes() function contains the logic to calculate minutes in the given time period and printing the result.

C++ Class and Object Programs (Set 2) »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.