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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
#include <iostream>
#include <string>
#include <array>
using namespace std;
//arrays needed
string months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
int monthCodes[]={ 6, 2, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
string days[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int dayCode[]={ 0, 1, 2, 3, 4, 5, 6};
//public variables
int monthNum;
int dayNum;
int yearNum;
int dayCodeReal;
//functions
bool isValidMonth(int);
bool isValidDay(int, int, int);
bool isLeapYear(int, int[]);
int getDayCode(int);
int getMonthCode(int);
int getYearCode(int);
int getDayCalc(int, int, int,int[]);
string getDay(int);
string getMonth(int,int[], string[]);
void resetValues(int[]);
int main()
{
cout<<"Enter your month number. (1, 2, 3, etc.)"<<endl;
cin>>monthNum;
isValidMonth(monthNum);
cout<<"Enter the day of the month."<<endl;
cin>>dayNum;
cout<<"Enter the year."<<endl;
cin>>yearNum;
isLeapYear(yearNum, monthCodes[]);
//passes the month, day and year provided by user to the method to test if the day and month match to make a valid day.
isValidDay(monthNum,dayNum, yearNum);
//sets the return values of these functions to new variables
int dayCode = getDayCode(dayNum);
int monthCode = getMonthCode(monthNum);
int yearCode = getYearCode(yearNum);
//I'm not sure why I added these, I could just give the variables I declared above instead of calling the function again, lol.
cout<<"Day code is: "<<getDayCode(dayNum)<<endl;
cout<<"Month code is: "<<getMonthCode(monthNum)<<endl;
cout<<"Year code is: "<<getYearCode(yearNum)<<endl;
//makes a variable that actually GETS the real day code, and passes the following parameters to that function.
dayCodeReal = getDayCalc(monthCode, dayCode, yearCode, dayCode[]);
//prints out the results. Needs work. I keep getting primary-expression errors before the ] token. What does this mean?
cout<<"Your date is: "<<getMonth(monthCode, monthCodes[],months[])<<" "<<dayNum<<", "<<yearNum<<" and the day is: "<<getDay(dayCodeReal)<<endl;
resetValues(monthCodes[]);
system("Pause");
return 0;
}
//tests to see if the month is actually valid. Simple enough.
bool isValidMonth(int month)
{
if(month < 1 || month > 12)
return false;
else
return true;
}
//tests to see if the day, year and month together all work to create a valid date that actually exists.
bool isValidDay(int month, int day, int year)
{
//if the month code is 1, this is February.
if(month == monthCodes[1])
{
//tests to see if the day is less than 1 or greater than 28. False if so OR if the month is February and is NOT a leap year, then is also false.
else if((day < 1 || day > 28) || ((month == monthCodes[1]) && (year%4 != 0)))
{
return false;
//if the month is february, and the year is a leap year, the days can be up to 29. Returns false if otherwise.
else
{
if((month == monthCodes[1]) && (year%4 == 0)) //defines a leap year
{
else if(day < 1 || day > 29)
{
return false;
else
return true;
}
}
}
}
}
}
//above is the algorithm for if the month is only February.
//tests to see if the day entered is less than 1 or greater than 31. returns false. else, continues
else if(day < 1 || day > 31)
{
return false;
else
{
//I am getting a ton of errors with my braces here, can someone clean them up?
//Anyway, if the month only has 30 days, this is the test for it.
else if(month % 2 == 0)
{
else if(day < 1 || day > 30)
{
return false;
else
{
//With the exception of this, which MAY need to be placed above
//the day tester because it will fall out of the test if the day is less
//than 30 but the month is July.(?) This is complicating me XD
else if(month == monthCode[6] || month == monthCode[7])
{
else if(day < 1 || day > 31)
{
return false;
else
return true;
}
}
}
}
}
}
}
}
//returns the day code
int getDayCode(int day)
{
return day%7;
}
//Can someone simpify this...? It seems like there's an easier way to do this. Codes
//were provided online. Simply returns the code of the month when the day is provided.
int getMonthCode(int month)
{
if(monthNum == 1)
return 6;
else if(monthNum == 2)
return 2;
else if(monthNum == 3)
return 2;
else if(monthNum == 4)
return 5;
else if(monthNum == 5)
return 0;
else if(monthNum == 6)
return 3;
else if(monthNum == 7)
return 5;
else if(monthNum == 8)
return 1;
else if(monthNum == 9)
return 4;
else if(monthNum == 10)
return 6;
else if(monthNum == 11)
return 2;
else if(monthNum == 12)
return 4;
}
//Returns year code whether the year is leap year, or years 1, 2 or 3 past it.
int getYearCode(int year)
{
if(year%4 == 0)
return 0;
else if(year %4 == 1)
return 1;
else if(year %4 == 2)
return 2;
else if(year %4 == 3)
return 3;
else
return -1;
}
//Tests to see the day Code. Calculation was provided online, but may not be fully functional.
int getDayCalc(int monthCode, int dayCode, int yearCode, int dayCode[])
{
int dayNumber = monthCode + dayCode + yearCode;
dayNumber -= 7;
for(int i = 0; i<=7; i++)
if(dayNumber == dayCode[i])
return dayCode[i];
}
//Again, seems like there's an easier way to do this.
//Simply returns the day given from the array.
string getDay(int realDay)
{
switch(realDay)
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesday";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
default: return -1;
}
//returns the actual month from the array above. Are Parallel arrays allowed in C++?
string getMonth(int monthCode, int monthCodes[],string months[])
{
for(int i = 0; i<13; i++)
{
if(monthCode == monthCodes[i])
return months[i];
}
}
bool isLeapYear(int year, int monthCodes[])
{
if(year %4 == 0)
{
monthCodes[0] = 5;
monthCodes[1] = 1;
return true;
}
else
return false;
}
void resetValues(int monthCodes[])
{
monthCodes[0] = 6;
monthCodes[1] = 2;
}
|