Tuesday, December 19, 2017

Programming Challenge 14.7 - Month Class

Example Files: MonthClass.7z


Month.h


#ifndef MONTH_H_
#define MONTH_H_

#include <iostream>
using std::istream;
using std::ostream;

#include <string>
using std::string;

const int NUM_MONTHS = 13;

class Month
{
    private:
        static const string monthNames[NUM_MONTHS];        // Array holding month names

        string name;                        // To hold a month name
        int    monthNumber;                // To hold a month number (1 through 12)

    public:
        Month();
        Month(string);
        Month(int);

        void setMonthNumber(const int &);
        void setMonthName(const string &);

        string getMonthName() const
        { return name; }

        int getMonthNumber() const
        { return monthNumber; }

        // Overloaded operator functions
        Month operator++();                    // Prefix ++
        Month operator++(int);                // Postfix ++
        Month operator--();                    // Prefix --
        Month operator--(int);                // Postfix --

        // Friends
        friend ostream &operator << (ostream &, const Month &);
        friend istream &operator >> (istream &, Month &);
};

#endif

Month.cpp


#include "Month.h"

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

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

/* **********************************************************
            Month::Month() - Default Constructor
   ********************************************************** */

Month::Month()
{
    name = "January";
    monthNumber = 1;
}

/* **********************************************************
            Month::Month() : string - Constructor  
    The constructor accepts the name of a month as argument.
    It sets the month name and number via the setMonthName()
    function to the correct value.
   ********************************************************** */

Month::Month(string n)
{
    setMonthName(n);
}

/* **********************************************************
            Month::Month() : int - Constructor
    The constructor accepts the number of a month as argument.
    It sets the month number and name via the setMonthNumber()
    function to the correct value.
   ********************************************************** */

Month::Month(int mNum)
{
    setMonthNumber(mNum);
}

/* **********************************************************
            MonthName::setMonthNumber() : const int &
    This function accepts a const reference to an integer as
    argument. If the value passed to this function is within
    the accepted range, the monthNumber and name each receive
    the correct name and month number. Else, monthNumber and
    name are set to 1 and January.
   ********************************************************** */

void Month::setMonthNumber(const int &mNum)
{
    if (mNum >= 1 && mNum <= 12)
    {
        monthNumber = mNum;
        name = monthNames[mNum];
    }
    else
    {
        monthNumber = 1;
        name = monthNames[monthNumber];
    }
}

/* **********************************************************
            Month::setMonthName() : const string &
    This function accepts a const reference parameter to a
    string object as argument. If the name is valid, name is
    set to the value passed to it, and monthNumber is passed
    the array-index at which the name is stored at. Else, name
    is set to January and monthNumber is set to 1.
   ********************************************************** */

void Month::setMonthName(const string &n)
{
    bool isFound = false;

    for (size_t count = 1; count < NUM_MONTHS; count++)
    {
        if (n == monthNames[count])
        {
            name = n;
            monthNumber = count;
            isFound = true;
        }   
    }

    if (isFound == false)
    {
        cout << "\nInput Error: " << n << " does not exist.\n"
             << "Setting month name and number to default values:\n\n";
        monthNumber = 1;
        name = monthNames[monthNumber];
       
    }
}

/* **********************************************************
            Month Month::operator++() : Prefix ++
   ********************************************************** */

Month Month::operator++()
{
    ++monthNumber;

    getMonthNumber() <= 12 ? setMonthNumber(monthNumber) : setMonthNumber(1);

    return *this;
}

/* **********************************************************
            Month Month::operator++() : Postfix ++
   ********************************************************** */

Month Month::operator++(int)
{
    Month temp(monthNumber);

    monthNumber++;
    getMonthNumber() <= 12 ? setMonthNumber(monthNumber) : setMonthNumber(1);

    return temp;
}

/* **********************************************************
            Month Month::operator--() : Prefix --
   ********************************************************** */

Month Month::operator--()
{
    --monthNumber;

    getMonthNumber() >= 1 ? setMonthNumber(monthNumber) : setMonthNumber(12);

    return *this;
}

/* **********************************************************
            Month Month::operator--() : Postfix --
   ********************************************************** */

Month Month::operator--(int)
{
    Month temp(monthNumber);

    monthNumber--;
    getMonthNumber() >= 1 ? setMonthNumber(monthNumber) : setMonthNumber(12);

    return temp;
}

/* **********************************************************
            Month Month::operator >> () : Extraction Operator
   ********************************************************** */

istream &operator >> (istream &strm, Month &obj)
{
    strm >> obj.name;
    obj.setMonthName(obj.name);
   
    return strm;
}

/* **********************************************************
            Month Month::operator << () : Insertion Operator
   ********************************************************** */

ostream &operator << (ostream &strm, const Month &obj)
{
    return strm << obj.name << " is month number " << obj.monthNumber << ".";
}

MonthDm.cpp


#include "Month.h"

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

int main()
{
    cout << "Months Class - Demo\n\n"
          << "This program demonstrates various functions of the Month class.\n\n"
          << "\t1.) The three constructors\n"
          << "\t2.) The overloaded pre- and postfix ++ and -- operators\n"
          << "\t3.) The overloaded istream and ostream operators\n\n"
          << "If a name or month number is invalid, their values are set\n"
          << "to: January and 1.\n\n";

    cout << "\tConstructor Demonstration\n\n";

    cout << "Demonstrating the default constructor\n";
    Month one;
    cout << one.getMonthName() << " is month number " << one.getMonthNumber() << "\n\n";

    cout << "Demonstrating the Second Constructor (Accepting a Month Name)\n";
    Month two("February");
    cout << two.getMonthName() << " is month number " << two.getMonthNumber() << "\n\n";

    cout << "Demonstrating the Third Constructor (Accepting a Mont Number)\n";
    Month three(9);
    cout << "Month " << three.getMonthNumber() << " is " << three.getMonthName() << "\n\n";

    cout << "\tOverloaded Post- and Prefix ++ and -- Operator Demonstration\n\n";
    cout << "Prefix ++\n";
    ++one;
    cout << one.getMonthName() << " is month number " << one.getMonthNumber() << "\n";

    cout << "Postfix ++\n";
    one++;
    cout << one.getMonthName() << " is month number " << one.getMonthNumber() << "\n\n";

    cout << "Prefix --\n";
    --two;
    cout << two.getMonthName() << " is month number " << two.getMonthNumber() << "\n";

    cout << "Postfix --\n";
    two--;
    cout << two.getMonthName() << " is month number " << two.getMonthNumber() << "\n\n";

    cout << "Overloaded Istream and Ostream Operator Demonstration\n\n";
    cout << "Enter a month name (January through December): ";
    Month four;
    cin >> four;
    cout << four;

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

Example Output:





Sunday, December 17, 2017

Programming Challenge 14.6 - Personell Report

Example Files: PersonellReport.7z

The following changes have been applied to the TimeOff.h and TimeOff.cpp files:
- A constructor accepting a string object and an integer and a default constructor has been added
- The ternary previously in the constructor has been placed in the setMaxVacation() function. This, function is now also called from the constructor.
- MAX_PAID has been moved to the global space of the classes header file

PersonellReport.h


#ifndef PERSONELL_REPORT_H_
#define PERSONELL_REPORT_H_

#include "TimeOff.h"

const int SICK_DAYS = 8;                // The number of sick days added per month
const int VAC_DAYS = 12;                // The number of paid vacation days added per month

class PersonellReport
{
    private:
        int     monthsEmployed;            // To hold a number of months
        TimeOff daysOff;                    // A TimeOff object
       
    public:
        PersonellReport(const TimeOff &, int);

        void addMaxSickDays(int m)
        {
            daysOff.setMaxSick(m * SICK_DAYS);
        }

        void addMaxVacation(int m)
        {
            daysOff.setMaxVacation(m * VAC_DAYS);
        }

        void print();

        int getMonthsEmployed() const
        { return monthsEmployed; }
};

#endif

PersonellReport.cpp


#include "PersonellReport.h"

#include <iostream>
using std::cout;

/* **********************************************************
       PersonellReport::PersonellReport() : Constructor
   ********************************************************** */

PersonellReport::PersonellReport(const TimeOff &t, int m)
{
    daysOff = t;
    monthsEmployed = m;

    addMaxSickDays(m);
    addMaxVacation(m);
}

/* **********************************************************
            PersonellReport::print()  
    This function outputs an employee's ID, name, months of
    employment, the number of days of sick leave, 8 per month,
    and the number of days of paid vacation, 12 per month, the
    employee is entitled to consume.
   ********************************************************** */

void PersonellReport::print()
{
    cout << "\nEmployee ID:   " << daysOff.getEmployeeID() << "\n";
    cout << "Employee Name:  " << daysOff.getEmployeeName() << "\n";
    cout << "Employed since: " << getMonthsEmployed() << " month(s)\n\n";

    cout << "This employee is granted the following:\n\n";
    cout << "A maximum number of " << daysOff.getMaxSick() << " sick days.\n";
    cout << "A maximum number of " << daysOff.getMaxVacation() << " days of paid vacation.";
}

PersonellReportDm.cpp


#include "PersonellReport.h"
#include "TimeOff.h"

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

int main()
{
    int months = 0;

    TimeOff empOne("Precious Ramotswe", 1);
   
    cout << "No. 1 Ladies' Detective Agency - Personell Report\n\n";
    cout << "Employee ID:   " << empOne.getEmployeeID() << "\n";
    cout << "Employee Name: " << empOne.getEmployeeName() << "\n\n";
       
    cout << "Enter number of months of employement (1 through 360): ";
    cin >> months;

    while (months <= 0 || months > 360)
    {
        cout << "Enter number of months of employement (1 through 360): ";
        cin >> months;
    }

    PersonellReport one(empOne, months);
    one.print();

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

Example Output:



Saturday, December 16, 2017

Programming Challenge 14.5 - Time Off

Example Files: TimeOff.7z

Because the NumDays.h and NumDays.cpp files contents have not changed, they are contained in the .7z file but not displayed separately here.

TimeOff.h


#ifndef TIME_OFF_H_
#define TIME_OFF_H_

#include "NumDays.h"

#include <string>
using std::string;

class TimeOff
{
    private:
        string employeeName;                    // Holding an employee's name
        int    employeeID;                    // Holding an employee's ID

        NumDays maxSickDays, sickTaken,
              maxVacation, vacTaken,
                  maxUnpaid, unpaidTaken;

    public:
        TimeOff(string, int, double, double, double, double, double, double);

        // Mutators
        void setEmployeeName(string name)
        { employeeName = name; }

        void setEmployeeID(int ID)
        { employeeID = ID; }

        void setMaxSick(double hrs)
        { maxSickDays.setHours(hrs); }

        void setSickTaken(double hrs)
        { sickTaken.setHours(hrs); }

        void setMaxVacation(double hrs)
        { maxVacation.setHours(hrs); }

        void setVacTaken(double hrs)
        { vacTaken.setHours(hrs); }

        void setMaxUnpaid(double hrs)
        { maxUnpaid.setHours(hrs); }

        void setUnpaidTaken(double hrs)
        { unpaidTaken.setHours(hrs); }

        void print();

        // Accessors
        string getEmployeeName() const
        { return employeeName; }

        int getEmployeeID() const
        { return employeeID; }

        double getMaxSick() const
        { return maxSickDays.getDays(); }

        double getSickTaken() const
        { return sickTaken.getDays(); }

        double getMaxVacation() const
        { return maxVacation.getDays(); }

        double getVacationTaken() const
        { return vacTaken.getDays(); }

        double getMaxUnpaid() const
        { return maxUnpaid.getDays(); }

        double getUnpaidTaken() const
        { return unpaidTaken.getDays(); }
};

#endif

TimeOff.cpp


#include "TimeOff.h"

#include <iomanip>
using std::setprecision;
using std::setw;

#include <iostream>
using std::cout;
using std::fixed;
using std::left;
using std::right;
using std::showpoint;

// Maximum amount of paid vacation days
const int MAX_PAID = 240;

/* **********************************************************
            TimeOff::TimeOff() - Constructor
   ********************************************************** */

TimeOff::TimeOff(string name, int ID, double maxSick, double sickT,
                                                  double maxVac, double vacT,
                                                  double maxUnp, double unpT)
{
    employeeName = name;
    employeeID = ID;

    setMaxSick(maxSick);
    setSickTaken(sickT);

    maxVac <= MAX_PAID ? setMaxVacation(maxVac) :
                                setMaxVacation(MAX_PAID);

    setVacTaken(vacT);
    setMaxUnpaid(maxUnp);
    setUnpaidTaken(unpT);
}

/* **********************************************************
            TimeOff::print()
    This function outputs a summary about an employee's days
    off.
   ********************************************************** */

void TimeOff::print()
{
    cout << fixed << showpoint << setprecision(2);

    cout << setw(15) << left << "Employee ID: " << getEmployeeID() << "\n"
          << setw(15) << right << "Employee Name: " << setw(5) << right << getEmployeeName() << "\n\n";
   
    cout << left << "Max. Sick Days: " << setw(6) << left << getMaxSick() << "\n"
          << setw(16) << right << "Sick Days: " << setw(5) << right << getSickTaken() << "\n\n";
    cout << left << "Max. Vacation Days: " << setw(7) << left << getMaxVacation() << "\n"
          << setw(20) << "Vacation Days: " << setw(4) << right << getVacationTaken() << "\n\n";
    cout << left << "Max. Unpaid Days: " << setw(6) << left << getMaxUnpaid() << "\n"
          << setw(18) << right << "Unpaid Days: " << setw(4) << right << getUnpaidTaken() << "\n\n\n";
}

TimeOffDm.cpp


 #include "TimeOff.h"

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

int main()
{
    // Array of 2 TimeOff objects, initialized with the following data:
    // Employee name, ID, Max. sick days, sick days taken, Max. vacation,
    // vacation taken, Max. unpaid vacation days, unpaid vacation days taken
    TimeOff offDays[2] = { TimeOff("Precious Ramotswe", 1, 200, 30, 255, 86, 40, 10),
                                  TimeOff("Grace Makutsi", 2, 164, 10, 20, 50, 40, 10) };

    cout << "No. 1 Ladies' Detective Agency - Leave Tracker\n\n";
    for (auto outputDays : offDays)
    {
        outputDays.print();
    }
  
    cin.get();
    cin.ignore();
    return 0;
}

Example Output:




Thursday, December 14, 2017

Programming Challenge 14.4 - NumDays Class

Example Files: NumDaysClass.7z

NumDaysClass.h


#ifndef NUM_DAYS_H_
#define NUM_DAYS_H_

class NumDays
{
private:
    double workHours;            // To hold a number of hours

public:
    NumDays(double hours = 0.0)       
    {    workHours = hours; }

    void setHours(double hours)       
    { workHours = hours; }

    void print();

    double getHours() const
    { return workHours; }

    double getDays() const
    { return workHours / 8; }

    // Overloaded operator functions
    NumDays operator + (const NumDays &);                // Overloaded +
    NumDays operator - (const NumDays &);                // Overloaded -
    NumDays operator ++();                                    // Overloaded Prefix ++                       
    NumDays operator ++(int);                                // Overloaded Postfix ++
    NumDays operator --();                                    // Overloaded Prefix --
    NumDays operator --(int);                                // Overloaded Postfix --
};

#endif

NumDaysClass.cpp


#include "NumDays.h"

#include <iostream>
using std::cout;

/* **********************************************************
            NumDays NumDays::operator +(const obj &)
   ********************************************************** */

NumDays NumDays::operator + (const NumDays &right)
{
    NumDays temp;
   
    temp.workHours = (workHours + right.workHours);

    return temp;
}

/* **********************************************************
            NumDays NumDays::operator -(const obj &)
   ********************************************************** */

NumDays NumDays::operator - (const NumDays &right)
{
    NumDays temp;

    if (workHours < right.workHours)
    {
        temp.workHours = (right.workHours - workHours);
    }
    else
    {
        temp.workHours = (workHours - right.workHours);
    }
    return temp;
}

/* **********************************************************
            NumDays NumDays::operator ++() : Prefix ++
   ********************************************************** */

NumDays NumDays::operator ++()
{
    return ++workHours;
}

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

NumDays NumDays::operator ++(int)
{
    return workHours++;
}

/* **********************************************************
            NumDays NumDays::operator --() : Prefix --
   ********************************************************** */

NumDays NumDays::operator --()
{
    return --workHours;
}

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

NumDays NumDays::operator --(int)
{
    return workHours--;
}

/* **********************************************************
            NumDays::print()
    Outputs the hour(s) and day(s) worked to screen.
   ********************************************************** */

void NumDays::print()
{
    cout << "Total: " << (getHours()) << " hour(s) or "
          << getDays() << " day(s).\n\n";
}

NumDaysDm.cpp


#include "NumDays.h"

#include <iomanip>
using std::fixed;
using std::showpoint;
using std::setprecision;

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

int main()
{
    NumDays thisWeek(26);
    NumDays lastWeek(15);
    NumDays total;

    cout << "YAMAGATA 4th JUNIOR HIGH SCHOOL BIWEEKLY WORKSHEET SUMMARY\n\n";

    // Demonstrating the overloaded postfix ++, -- operators
    cout << fixed << showpoint << setprecision(2);
    cout << "You worked " << thisWeek.getHours() << " hour(s) in total this week.\n";
    cout << "This makes " << thisWeek.getDays() << " day(s) of work.\n\n";

    cout << "We increased this amount by 1 hour\n";
    thisWeek++;
    thisWeek.print();

    cout << "We detucted 1 hour from this amount\n";
    thisWeek--;
    thisWeek.print();

    // Demonstrating the overloaded prefix ++, --, + and - operators
    cout << "You worked " << lastWeek.getHours() << " hour(s) in total last week.\n";
    cout << "This makes " << lastWeek.getDays() << " day(s) of work.\n\n";

    cout << "We increased this amount by 1 hour\n";
    ++lastWeek;
    lastWeek.print();

    cout << "We detucted 1 hour from this amount\n";
    --lastWeek;
    lastWeek.print();

    cout << "This week and last week you worked\n";
    total = thisWeek + lastWeek;
    total.print();

    cout << "The difference in hours and days is\n";
    total = thisWeek - lastWeek;
    total.print();

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

Example Output:


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:







Wednesday, December 6, 2017

Programming Challenge 14.2 - Day of the Year

Example Files: DayOfTheYear.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];
       
        int day;                // To hold a day

    public:
        DayOfYear(int d)
        {
            day = d;
        }

        void print();
};

#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 };

/* **********************************************************
            DayOfYear::print()
    This function translates a day to month day format and
    outputs it to screen.
   ********************************************************** */

void DayOfYear::print()
{
    bool isFound = false;
   
    for (int count = 0; count < NUM_MONTHS && isFound != true; count++)
    {
        if (day <= daysInYear[count])
        {
            cout << monthOfYear[count] << " " << (day %= daysInYear[count - 1]) << ".\n";
            isFound = true;
        }
    }
}

DayOfTheYearDm.cpp


#include "DayOfTheYear.h"

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

int main()
{
    int number = 0;
    char again = ' ';

    cout << "\t\tDAY OF THE YEAR TO MONTH AND DAY TRANSLATOR\n\n";
    cout << "Please enter a day in the range of 1 through 365, and I will\n"
          << "translate it for you into a month and day format. For instance,\n"
          << "32 becomes February 1., and 230 translates to August 18.\n\n";

    do
    {
        cout << "Please enter a day (1 through 365): ";
        cin >> number;

        while (number <= 0 || number > 365)
        {
            cout << "Please enter a day (1 through 365): ";
            cin >> number;
        }

        // Create a DayOfYear class object
        DayOfYear day(number);
        day.print();

        cout << "\nWould you like me to translate another day (y/N)? ";
        cin >> again;

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            cout << "\nWould you like me to translate another day (y/N)? ";
            cin >> again;
        }
        cout << "\n";

        if (toupper(again) == 'N')
        {
            cout << "\nThank you for trying this demo program, have a nice day!";
        }
    } while (toupper(again) != 'N');

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

Example Output:




Monday, December 4, 2017

Programming Challenge 14.1 - Numbers Class

Example Files: NumbersClass.7z

Numbers.h


#ifndef NUMBERS_H_
#define NUMBERS_H_

#include <string>
using std::string;

class Numbers
{
    private:
        int number;                                    // To hold a number in the range 0 through 9999

        static string zeroToTwenty[20];       // To hold number words in the range 0 through 20
        static string tenMult[10];                // To hold number words for multiples of 10
        static string hundred;
        static string thousand;

    public:
        Numbers(int);                           

        string describeTens(int);
        void print();

        int getNumber() const
        { return number; }
};

#endif

Numbers.cpp


#include "Numbers.h"

#include <iostream>
using std::cout;

// Definition of static string objects
string Numbers::zeroToTwenty[] = { "zero", "one", "two", "three", "four", "five", "six", "seven",
                                                 "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
                                                 "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

string Numbers::tenMult[] = { "", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy",
                                        "eighty", "ninety" };

string Numbers::hundred = "hundred";
string Numbers::thousand = "thousand";

/* **********************************************************
            Numbers::Numbers() : int - Constructor
    The constructor accepts an integer as argument, which is
    used to initialize the
   ********************************************************** */

Numbers::Numbers(int n)
{
    number = n;
}

/* **********************************************************
            Numbers::describeTens() : int
    The integer passed to this function is used as an array
    index, allowing the retrieval and assignment of a number
    word to a temporary string object. This object's contents
    is returned from the function.
   ********************************************************** */

string Numbers::describeTens(int n)
{
    string tmpTens = "";

    if (n < 20)
    {
        return tmpTens = zeroToTwenty[n];
    }
    else if (n >= 20 && n % 10 == 0)
    {
        return tmpTens = tenMult[n / 10];
    }
    else
    {
        return tmpTens = tenMult[n / 10] + " " + zeroToTwenty[n % 10];
    }
}

/* **********************************************************
            Numbers::print()
    This function translates a dollar amount in the range of 0
    through 9999 into an English description, and outputs it
    to screen.
   ********************************************************** */

void Numbers::print()
{
    string description = "";

    if (getNumber() < 100)
    {
        description.append(describeTens(getNumber()));
    }   
    else if (getNumber() < 1000)
    {
        description.append(zeroToTwenty[(getNumber() / 100)] + " " + hundred + " " +
                                 describeTens(getNumber() % 100));
    }
    if (getNumber() > 999)
    {
        description.append(zeroToTwenty[getNumber() / 1000] + " " + thousand + ", ");

        if (getNumber() % 1000 < 100)
        {
            description.append(describeTens(getNumber() % 1000));
        }
        else
        {
            description.append(zeroToTwenty[(getNumber() % 1000) / 100] + " " + hundred + " and " +
                                     describeTens(getNumber() % 100));
        }
    }
   
    cout << "\nThis is your dollar amount converted to English words:\n";
    cout << "----------------------------------------------------\n";
    cout << description << " dollar(s)\n";
}

NumbersClassDm.cpp


#include "Numbers.h"

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

int main()
{
    char again = ' ';            // Temporary variable holding a user's choice
    int tempNum = 0;            // Temporary variable to hold a number in an approriate range

    cout << "\tDOLLAR AMOUNT TO ENGLISH WORD CONVERTER\n";

    do
    {
        cout << "\nPlease enter a dollar amount in the range of 0 through 9999: ";
        cin >> tempNum;

        while (tempNum < 0 || tempNum > 9999)
        {
            cout << "\nInput Error!\n";
            cout << "Please enter a dollar amount in the range of 0 through 9999: ";
            cin >> tempNum;
        }

        // Create a Numbers class object
        Numbers number(tempNum);
        number.print();

        cout << "\nDo you wish to convert another dollar amount (y/N)? ";
        cin >> again;

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            cout << "\nDo you wish to convert another dollar amount (y/N)? ";
            cin >> again;
        }

        if (toupper(again) == 'N')
        {
            cout << "\nThank you for trying this demo program. Have a nice day!\n";
        }

    } while (toupper(again) == 'Y');


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

Example Output: