-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
200 lines (140 loc) · 4.84 KB
/
main.cpp
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
#include <iostream>
#include <chrono> //Library with functions for obtaining current date. Only available with C++11
#include <iomanip> //Library with function for converting time_t to various other var types
#include <string>
#include <math.h>
//Function for storing date in a character array
//Array must be 9 bytes long because there must be an extra byte at end to store null terminator
//Delivered char array is in the following format: ddmmyyyy
void deliverDate(char*);
void deliverWeekday(char*);
int charArrayToInt(char*,int);
void intToCharArray(int,int,char*);
int main(){
const int DAYS_IN_WEEK = 7,
DAYS_IN_YEAR = 365,
YEARS = 3,
WEEKS_PER_YEAR = 52;
const int DATE_REP_LENGTH = 8; //Indicates size of each char array that together represents a specific date (ddmmyyyy)
//A 3D array of three years of weeks is created where each day in a week
//holds an 8-byte character array that indicates the date
char arrayOfWeeks[(YEARS+1)*WEEKS_PER_YEAR][DAYS_IN_WEEK][DATE_REP_LENGTH];
//Store current date from system in char array
char currentDate[9]; //Needs extra byte at end to store null terminator
deliverDate(currentDate);
//Convert char array of current date into separate ints
int currentYearInt = charArrayToInt(currentDate+4,4);
int currentDayInt = charArrayToInt(currentDate,2);
int currentMonthInt = charArrayToInt(currentDate+2,2);
std::cout<<"Today's date: "<<currentMonthInt<<'/'<<currentDayInt<<'/'<<currentYearInt<<std::endl;
char wdChar;
char* wdPtr = &wdChar;
deliverWeekday(wdPtr);
int wdInt = static_cast<int>(wdChar) - 48;
bool leapYearFlag;
int daysInThisMonth;
int y = currentYearInt;
int m = currentMonthInt;
int d = currentDayInt;
int weekNumber = 0;
for(;y <= (currentYearInt + YEARS);y++){
//Checking if year is a leap year
(y%4) ? (leapYearFlag = 0) : (leapYearFlag = 1);
//Reset the month counter
if(m==13)
m=1;
while(m<=12){
//Number of days in month varies
if(m<7){
//Odd-numbered months before July have 31 days
//Special case for February with leap year
if(m==2)
(leapYearFlag) ? (daysInThisMonth = 29) : (daysInThisMonth = 28);
else{
if(m%2)
daysInThisMonth = 31;
else
daysInThisMonth = 30;
}
}
else{
//Odd-numbered months after June have 30 days
if(m%2)
daysInThisMonth = 30;
else
daysInThisMonth = 31;
}
for(;d<=daysInThisMonth;d++){
intToCharArray(d,2,&arrayOfWeeks[weekNumber][wdInt][0]);
intToCharArray(m,2,&arrayOfWeeks[weekNumber][wdInt][2]);
intToCharArray(y,4,&arrayOfWeeks[weekNumber][wdInt][4]);
wdInt++;
//Reset after Saturday (wdInt=6). Sun = 0.
if(wdInt==7)
{
weekNumber++;
wdInt=0;
}
}
//Reset day counter after exiting loop
d=1;
m++;
}
}
//Testing array access
for(int n = 0;n<8;n++){
std::cout << std::endl;
std::cout.put(arrayOfWeeks[2][4][n]);
}
std::cout << std::endl;
return 0;
}
//Function for storing date in a character array
//Array must be 9 bytes long because there must be an extra byte at end to store null terminator
//Delivered char array is in the following format: mmddyyyy
void deliverDate(char* target){
//Creating time_point type and converting to a time_t type
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::strftime(target,static_cast<size_t>(8),"%e%m%Y",std::localtime(&now_c));
}
void deliverWeekday(char* target){
//Creating time_point type and converting to a time_t type
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::strftime(target,static_cast<size_t>(1),"%w",std::localtime(&now_c));
}
//Function for converting an array of characters stored in standard decimal form
//to the equivalent int value.
//
//charArry must be the address of the leftmost digit and size is the number
// of digits
int charArrayToInt(char* charArray,int size){
int convertedInt = 0; //V
int magnitude = 1;
for(int n = (size-1);n>=0;n--){
convertedInt += (static_cast<int>(charArray[n])-48)*magnitude;
magnitude*=10;
}
return convertedInt;
}
void intToCharArray(int myInt, int digits, char* target){
//Placeholder for int while being convereted
int myIntConverted;
//Placeholder for single integer digit as char
char intAsChar;
int magnitude;
for(int n=0;n<digits;n++){
//Reset original int
myIntConverted = myInt;
//Adjust magnitude
magnitude = pow(10,(digits-n-1));
//Isolate each digit by magnitude
myIntConverted = (myIntConverted/magnitude)%10;
//Convert digit to ASCII value then char
myIntConverted+=48;
intAsChar = static_cast<char>(myIntConverted);
//Store char at current decimal place of target
target[n] = intAsChar;
}
}