1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
//dayType.cpp
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;
// array for days of the week
string weekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
// part b
void dayType::print() const
{
cout << weekDay;
}
// part d
string dayType::nextDay() const
{
string nextDay;
if (weekDay == "Monday")
nextDay = "Tuesday";
if (weekDay == "Tuesday")
nextDay = "Wednesday";
if (weekDay == "Wednesday")
nextDay = "Thursday";
if (weekDay == "Thursday")
nextDay = "Friday";
if (weekDay == "Friday")
nextDay = "Saturday";
if (weekDay == "Saturday")
nextDay = "Sunday";
if (weekDay == "Sunday")
nextDay = "Monday";
return nextDay;
}
// part e
string dayType::prevDay() const
{
string prevDay;
if (weekDay == "Monday")
prevDay = "Sunday";
if (weekDay == "Tuesday")
prevDay = "Monday";
if (weekDay == "Wednesday")
prevDay = "Tuesday";
if (weekDay == "Thursday")
prevDay = "Wednesday";
if (weekDay == "Friday")
prevDay = "Thursday";
if (weekDay == "Saturday")
prevDay = "Friday";
if (weekDay == "Sunday")
prevDay = "Saturday";
return prevDay;
}
// part f - I NEED HELP WITH THIS FUNCTION
string dayType::addDay(int nDays)
{
string weekDays[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int index;
for(int i = 0; i < 7; i++)
{
if (weekDay == weekDays[i])
{
index = i;
break;
}
}
return weekDays[ (index + nDays) % 7 ];
}
// part a
void dayType::setDay(string d)
{
string weekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
if (d == "Monday")
weekDay = weekDays[0];
if (d == "Tuesday")
weekDay = weekDays[1];
if (d == "Wednesday")
weekDay = weekDays[2];
if (d == "Thursday")
weekDay = weekDays[3];
if (d == "Friday")
weekDay = weekDays[4];
if (d == "Saturday")
weekDay = weekDays[5];
if (d == "Sunday")
weekDay = weekDays[6];
}
// part c
string dayType::getDay() const
{
return weekDay;
}
// part g
dayType::dayType()
{
weekDay = "Sunday";
}
// part g
dayType::dayType(string d)
{
setDay(d);
}
//dayType.h
#ifndef H_dayType
#define H_dayType
#include <string>
using namespace std;
class dayType
{
public:
static string weekDays[7];
void print() const;
string nextDay() const;
string prevDay() const;
string addDay(int nDays);
void setDay(string d);
string getDay() const;
dayType();
dayType(string d);
private:
string weekDay;
};
#endif
//main.cpp
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;
int main()
{
dayType myDay("Monday");
dayType temp("Sunday");
dayType myDay2("Friday");
myDay.print();
cout << endl;
cout << myDay.prevDay() << endl;
cout << myDay.nextDay() << endl;
cout << endl;
temp.print();
cout << endl;
cout << temp.prevDay() << endl;
cout << temp.nextDay() << endl;
cout << endl;
cout << myDay2.addDay(3) <<endl;
cout << myDay.addDay(4) << endl;
return 0;
}
|