Wednesday, December 13, 2017

Programming Challenge 14.3 - Day Of The Year Modification

Example Files: DayOfTheYearMod.7z


DayOfTheYear.h


#ifndef DAY_OF_THE_YEAR_H_
#define DAY_OF_THE_YEAR_H_

#include <string>
using std::string;

const int NUM_MONTHS = 12;

class DayOfYear
{
    private:
        const static string monthOfYear[NUM_MONTHS];
        const static int daysInYear[NUM_MONTHS];
        const static int daysInMonth[NUM_MONTHS];
       
        string month;            // To hold a month name
        int day;                    // To hold a day
        int arrayIdx;            // Stores an array index

    public:
        DayOfYear()
        { }

        DayOfYear(int d);
        DayOfYear(string, int);
       
        bool isMonthName(string);
        void setMonth(int);
        void helpIncrement();
        void helpDecrement();
        void print();

        string getMonth() const
        { return month; }

        int getDay() const
        { return day; }

        DayOfYear operator++();
        DayOfYear operator++(int);
        DayOfYear operator--();
        DayOfYear operator--(int);
};

#endif

DayOfTheYear.cpp


#include "DayOfTheYear.h"

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

// Definition of const static array member holding month names
const string DayOfYear::monthOfYear[] = { "January", "February", "March", "April", "May", "June", "July",
                                                      "August", "September", "October", "November", "December" };

// Definition of const static array member holding a range of days
const int DayOfYear::daysInYear[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
const int DayOfYear::daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/* **********************************************************
                DayOfYear::DayOfYear() : int
    The constructor accepts an integer as its argument. If the
    value passed to it is invalid, a message is output, and
    the program exits with an error code.
   ********************************************************** */

DayOfYear::DayOfYear(int d)
{
    if (d >= 1 && d <= 365)
    {
        setMonth(d);

        if (d > 31)
        {
            d %= daysInYear[arrayIdx-1];
        }
        day = d;
    }
    else
    {
        cout << "\nInput failure: A normal year has at least 1 and no more than 365 days.\n";
        cout << "Please restart this program and try again!";
        cin.get();
        cin.ignore();
        exit(0);
    }
}

/* **********************************************************
                DayOfYear::DayOfYear() : string, int
    The constructor accepts a string and and integer as
    arguments. If the value passed to it is invalid, a message
    is output, and the program exits with an error code. If
    the values are valid, they are copied to the appropriate
    member variables.
   ********************************************************** */

DayOfYear::DayOfYear(string m, int d)
{
    if (isMonthName(m) == true)
    {
        month = m;
    }
    else
    {
        cout << "\nInput failure: " << m << " does not exist\n";
        cout << "Please restart this program and try again!";
        cin.get();
        cin.ignore();
        exit(0);
    }
   
    if (d >= 1 && d <= daysInMonth[arrayIdx])
    {
        day = d;
    }
    else
    {
        cout << "\nInput failure: " << month << " starts with the 1.st, and has "
              << daysInMonth[arrayIdx] << " days.\n";
        cout << "Please restart this program and try again!";
        cin.get();
        cin.ignore();
        exit(0);
    }
}

/* **********************************************************
            DayOfYear::setMonth() : int
    The function accepts an integer holding a day value in the
    range of 1 through 365. Based on this value, the month of
    year and arrayIdx members are set.
   ********************************************************** */

void DayOfYear::setMonth(int d)
{
    bool isFound = false;

    for (int count = 0; count < NUM_MONTHS && isFound != true; count++)
    {
        if (d <= daysInYear[count])
        {
            arrayIdx = count;
            month = monthOfYear[count];
           
            isFound = true;
        }       
    }
}

/* **********************************************************
            DayOfYear::isMonthName() : string
    The month name passed to this function is compared against
    the values in the monthOfYear array. If it is found, the
    function exits and returns true. Else, false is returned.
   ********************************************************** */

bool DayOfYear::isMonthName(string m)
{
    for (int count = 0; count < NUM_MONTHS; count++)
    {
        if (m == monthOfYear[count])
        {
            arrayIdx = count;
            return true;
        }
    }
    return false;
}

/* **********************************************************
            DayOfYear::helpIncrement
    This function increments day and month if the conditions
    in the if/else statement are met.
   ********************************************************** */

void DayOfYear::helpIncrement()
{
    if (month == "December" && day == 31)
    {
        arrayIdx = 0;
        month = monthOfYear[arrayIdx];
        day = 0;
    }
    else if (day == daysInMonth[arrayIdx])
    {
        month = monthOfYear[++arrayIdx];
        day = 0;
    }
}

/* **********************************************************
            DayOfYear::helpIncrement
    This function decrements day and month if the conditions
    in the if/else statement are met.
   ********************************************************** */

void DayOfYear::helpDecrement()
{
    if (month == "January" && day == 1)
    {
        arrayIdx = 11;
        month = monthOfYear[arrayIdx];
        day = 31;
    }
    else
    {       
        month = monthOfYear[--arrayIdx];
        day = daysInMonth[arrayIdx];
    }
}

/* **********************************************************
            DayOfYear &DayOfYear::operator++() : Prefix ++
    ********************************************************** */

DayOfYear DayOfYear::operator++()
{
    helpIncrement();
    ++day;

    return *this;
}


/* **********************************************************
            DayOfYear DayOfYear::operator++(int) : Postfix ++
   ********************************************************** */

DayOfYear DayOfYear::operator++(int)
{
    DayOfYear temp = *this;
   
    helpIncrement();
    day++;

    return temp;
}

/* **********************************************************
            DayOfYear &DayOfYear::operator--() : Prefix --
   ********************************************************** */

DayOfYear DayOfYear::operator--()
{
    if (day == 1)
    {
        helpDecrement();
    }
    else
    {
        --day;
    }
    return *this;
}

/* **********************************************************
            DayOfYear DayOfYear::operator--(int) : Postfix --
   ********************************************************** */

DayOfYear DayOfYear::operator--(int)
{
    DayOfYear temp = *this;

    if (day == 1)
    {
        helpDecrement();
    }
    else
    {
        day--;
    }

    return temp;
}

/* **********************************************************
            DayOfYear::print()
    This function outputs the month and day to screen.
   ********************************************************** */

void DayOfYear::print()
{
    cout << getMonth() << " " << getDay() << ".";
}

DayOfTheYearMod.cpp


#include "DayOfTheYear.h"

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

int main()
{
    int day = 0;
    string monthName = "";

    cout << "\t\tDAY OF THE YEAR TO MONTH AND DAY TRANSLATOR\n\n";

    cout << "Please enter a month name: ";
    getline(cin, monthName);

    cout << "Now enter a day of this month: ";
    cin >> day;

    // Create a DayOfYear object to demonstrate the overloaded pre- and postfix ++ operators
    DayOfYear dayOne(monthName, day);

    cout << "\nThe month and day you entered is: ";
    dayOne.print();
    cout << "\n";
   
    cout << "\nThe next day will be: ";
    ++dayOne;
    dayOne.print();

    cout << "\nThe day after this will be: ";
    dayOne++;
    dayOne.print();

    cout << "\n\nNow enter a day in the range of 1 through 365, and I will\n"
          << "translate it for you into a month and day format: ";
    cin >> day;

    // Create a second DayOfYear object to demonstrate the overloaded pre- and postfix -- operators
    DayOfYear dayTwo(day);

    cout << "\nThe day you entered translates to: ";
    dayTwo.print();

    cout << "\n\nThe previous day was: ";
    (--dayTwo);
    dayTwo.print();
    cout << "\n";

    cout << "The day before this was: ";
    (dayTwo--);
    dayTwo.print();

    cout << "\n\nThank you for trying this demo, and have a nice day!\n"
          << "Please restart the program to try again!";

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

Example Output:







No comments:

Post a Comment