Wednesday, January 24, 2018

Programming Challenge 15.4 - Time Format

Example Files: TimeFormat.7z

Notice: The code for the Time.h header file is taken vanilla from the book, nothing has been changed or added to it.

Time.h


#ifndef TIME_H_
#define TIME_H_

class Time
{
    protected:
        int hour;
        int min;
        int sec;

    public:
        // Default constructor
        Time()           
        {
            hour = 0;
            min = 0;
            sec = 0;
        }

        // Constructor
        Time(int h, int m, int s)           
        {
            hour = h;
            min = m;
            sec = s;
        }

        // Accessor functions
        int getHour() const
        { return hour; }

        int getMinute() const
        { return min; }

        int getSecond() const
        { return sec; }
};

#endif

MilTime.h


#ifndef MILTIME_H_
#define MILTIME_H_

#include "Time.h"

#include "string"
using std::string;

// MilTime class derived from the Time class
class MilTime : public Time
{
    protected:
        int milHours;            // Military hours (0 - 2359)
        int milSeconds;        // Military seconds (0 - 59)

    public:
        // Default Constructor
        MilTime() : Time()
        {
            milHours = 0;
            milSeconds = 0;
        }

        // Parameterized Constructor
        MilTime(int mHrs, int mSec) : Time(0, 0, 0)
        {
            setTime(mHrs, mSec);       
        }

        // Mutator functions
        bool isMilTime(const int, const int);
        void setTime(const int, const int);       
        void convertMilTime();

        // Accessor functions
        string getHour() const;
        string getStandHr() const;
};

#endif

MilTime.cpp


#include "MilTime.h"

#include <iostream>
using std::cin;
using std::cout;

#include <sstream>
using std::stringstream;

#include <cstdlib>

/* **********************************************************
            MilTime::setTime() : const int, const int
    If the values passed to this function are valid, the
    arguments are stored in the milHours and milSeconds
    variables. The time then gets converted to standard time.
    Otherwise an error message is output and the program will
    terminate.
    ********************************************************** */

void MilTime::setTime(const int mHrs, const int mSec)
{
    if (isMilTime(mHrs, mSec) == true)
    {
        milHours = mHrs;
        milSeconds = mSec;

        convertMilTime();
    }
    else
    {
        cout << "Input Error:\n";
      cout << mHrs << ':' << mSec << " is no military time!\n";
        cout << "This program will now exit.\n";
        exit(0);
    }
}

/* **********************************************************
            MilTime::isMilTime() : const int, const int
    This function evaluates the attributes to check whether it
    is a valid military time. The result of this evaluation is
    returned.
   ********************************************************** */

bool MilTime::isMilTime(const int mHrs, const int mSec)
{
    bool isValidMilTime = true;

    if (mHrs < 0 || mHrs > 2359 || mHrs % 100 >= 60)
    {
        isValidMilTime = false;
    }
    else if (mSec < 0 || mSec > 59)
    {
        isValidMilTime = false;
    }

    return isValidMilTime;
}

/* **********************************************************
            MilTime::convertMilTime()
    This function converts a military time to standard time,
    and stored in the hour, min and sec variables of the Time
    class.
   ********************************************************** */

void MilTime::convertMilTime()
{
    if (milHours > 1259)
    {
        hour = (milHours - 1200) / 100;
    }
    else if (milHours <= 59)
    {
        hour = 12;
    }
    else
    {
        hour = (milHours / 100);
    }

    min = milHours % 100;
    sec = milSeconds;
}

/* **********************************************************
            MilTime::getHour()
    This function formats and returns the hour in military
    format.
   ********************************************************** */

string MilTime::getHour() const
{
    stringstream ss;
    ss.str("");
    ss.clear(stringstream::goodbit);

    milHours < 10   ? ss << "000" << milHours << ':' :
    milHours < 100  ? ss << "00"  << milHours << ':' :
    milHours < 1000 ? ss << '0'   << milHours << ':' : ss << milHours << ':';

    milSeconds < 10 ? ss << '0'   << milSeconds << "Z Hours" : ss << milSeconds << "Z Hours";
   
    return ss.str();
}

/* **********************************************************
            MilTime::getStandHr()
    This function formats and returns the hour in standard
    format.
   ********************************************************** */

string MilTime::getStandHr() const
{
    stringstream ss;
    ss.str("");
    ss.clear(stringstream::goodbit);

    hour < 10 ? ss << '0' << hour << ':' : ss << hour << ':';
    min  < 10 ? ss << '0' << min  << ':' : ss << min << ':';
    sec  < 10 ? ss << '0' << sec             : ss << sec;

    milHours >= 1200 ? ss << " P.M." : ss << " A.M";

    return ss.str();
}

TimeFormat.cpp


#include "MilTime.h"

#include <iostream>
using std::cin;
using std::cout;

int main()
{
    int mHrs = 0;
    int mSecs = 0;

    cout << "MILITARY TIME CONVERTER\n\n";
    cout << "This program lets you convert Military to 'Standard' time.\n"
          << "Here is an example:\n\n";

    // Create a MilTime object and pass a military time to the constructor
    MilTime milTime(045, 7);
   
    cout << "MILITARY TIME\n";
    cout << milTime.getHour() << "\n\n";

    cout << "STANDARD TIME\n";
    cout << milTime.getStandHr() << "\n\n";

    // Create another MilTime object
    MilTime milTimeOne;

    // Ask the user to enter a military time
    cout << "Please enter a military time\n";
    cout << "Hour:   ";
    cin >> mHrs;
    cout << "Second: ";
    cin >> mSecs;
    cout << "\n";

    // Pass the values to the setTime() function of the MilTime class
    milTimeOne.setTime(mHrs, mSecs);

    cout << "MILITARY TIME\n";
    cout << milTimeOne.getHour() << "\n\n";

    cout << "STANDARD TIME\n";
    cout << milTimeOne.getStandHr() << "\n\n";

    cout << "Thank you for trying this program. Have a nice day!";

    cin.get();
    cin.ignore();
    return 0;
}

Example Output:





No comments:

Post a Comment