forked from CPRO-Session1/FinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.c
318 lines (317 loc) · 8.71 KB
/
calendar.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//fix character input
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
typedef struct {
int hour; //user input hour for event
int min; //user input minute
char name[100]; //name of event
}event;
//where to print menu
bool check (const event *e){
return e==NULL;
} //check if event is NULL
event *UIEvent(){
event *e = (event*) malloc(sizeof(event));
e->hour=0;
e->min=0;
strcpy(e->name,"");
return e;
} //defining event
//add selection (range)
int range1(const int minimum, const int maximum){
int input=0;
int test=0;
char temp[15];
labelh:scanf("%2d",&input);
if(input>=minimum||input<=maximum){
}else{
printf("Incorrect input, please try again: ");
goto labelh;
}
return input;
}
int range2(const int minimum, const int maximum){
int input=0;
int test=0;
char temp[15];
labelm:scanf("%2d",&input);
if(input>=minimum||input<=maximum){
}else{
printf("Incorrect input, please try again: ");
goto labelm;
}
return input;
}
event *NewEvent(event *e){
if(check(e)){
e=UIEvent();
}
system("setterm -bold on");
printf("\n \n New Event Selected \n \n");
system("setterm -bold off");
printf("Enter hour (0-23): ");
e->hour=range1(0,23);
printf("Enter minute (0-59): ");
e->min=range2(0,59);
puts("\n EVENT NAME \n");
printf("Please include event name or description: ");
fgets(e->name,100,stdin);
fgets(e->name,100,stdin);
puts(" ----------------------------------");
system("setterm -bold on");
puts(" \nEvent Added.");
system("setterm -bold off");
return e;
} //initializing event
void AddEvent(event list[],const event e, const int x){
if(check(&e)){
return;
}
list[x].hour=e.hour;
list[x].min=e.min;
strcpy(list[x].name,e.name);
}//adding events to a list
//sorting the order of time of the events in a day
void sort (event list[],const int size){
int x,y,storeh2,storem2;//store h for hour, m for min, 1 and 2, depend on arr
char name2[100];
for (x=1;x<size;x++){
for(y=x;y>0&&(list[y-1].hour>list[y].hour)||
(list[y-1].hour==list[y].hour)&&(list[y-1].min>list[y].min);y--){
storeh2=list[y-1].hour;
storem2=list[y-1].min;
strcpy(name2,list[y-1].name);
//event 2 ^
list[y-1].hour=list[y].hour;
list[y-1].min=list[y].min;
strcpy(list[y-1].name,list[y].name);
//switch the two around using the temporary storage
list[y].hour=storeh2;
list[y].min=storem2;
strcpy(list[y].name,name2);
}
}
}
void CorrectSort (event list[], int *size, event e){
AddEvent(list,e,*size);
(*size)++;
sort(list,*size);
}
void printEvent(const event e){ //<---printevent sourced by: stackexchange.com
char hour1 = {(e.hour/10)+ '0'};//first digit
char hour2 = {(e.hour-(e.hour/10)*10)+'0'};
char min1 = {(e.min/10)+'0'};
char min2 = {(e.min-(e.min/10)*10)+'0'};
printf("%c%c:%c%c ~%s\n",hour1,hour2,min1,min2,e.name);
}
void printList(const event list[],const int size){
if(size==0){
puts("\n You have no events scheduled. \n");
}
system("setterm -bold on");
printf("\n Schedule: \n");
system("setterm -bold off");
for(int x=0;x<size;x++){
printf(" [%d] ",x);
printEvent(list[x]);
}
}
void deleteEvent(event list[],int *size){
int x;
if(*size==0){
puts("\n You have no events to delete. \n");
return;
}
char temp[100];
int locate;
system("setterm -bold on");
printf("\n DELETE EVENT \n");
system("setterm -bold off");
printf("\n Enter event number you want to delete: ");
fgets(temp,100,stdin);
fgets(temp,100,stdin);
strtok(temp,"\n");
locate = atoi(temp);
//locate=locate-y;
if(locate>*size-1){
printf(" \n No event found \n");
return;
}
list[locate].hour=70;
list[locate].min=70;
strcpy(list[locate].name,"");
sort(list,*size);
(*size)--;
}
void menu(){
printf("Press 1: to create event\n");
printf("Press 2: to delete event\n");
printf("Press 3: to exit\n\n");
//main to function calendar
}
int main () {
system("clear");
event list[15];
static const char* MonthDisplay[]={
"January","February","March","April","May","June","July","August","September",
"October","November","December"
};
int year, month, day,leap=0,x, day2;
int count=0;
char temp[100];
signed char c; //userinput for calendar change
int MonthArray[12]={31,28,31,30,31,30,31,31,30,31,30,31};
system("setterm -bold on"); //bold header
printf("Events Calendar \n");
system("setterm -bold off");
label1:printf("Enter year: ");
scanf("%d",&year); //user input year
if (year<0){
printf("Not a suitable year. \n");
goto label1; //label 1 for incorrect year input
}
label2:printf("Enter month: ");
scanf("%d", &month); //user input month
if(!(month<13) || !(month>0)){
printf("Not a suitable month. \n");
goto label2; //label 2 for incorrect month input
}
label3:printf("Enter day: ");
scanf("%d", &day); //user input day
if (day<1||day>31){
printf("Not a suitable day. \n");
goto label3;
}else if(month==4||month==6||month==9||month==11){ //wrong input for 30 days
if (day>30){
printf("Not a suitable day. \n");
goto label3;
}
}else if (month==2){ //wrong input for feb
if ((year%4)==0&&(year%100!=0)||(year%400==0)){ //leap year possibility
MonthArray[1]=29;
if(day>29){ //29 days
printf("Not a suitable day. \n");
goto label3;
}
}else{
if(day>28){ //28 days
printf("Not a suitable day. \n");
goto label3;
}
}
}
//printing calendar
int DetermineDay;
label4:system("clear");
x=1;
DetermineDay=(x+=month<3?year:year-2,23*month/9+x+4+year/4-year/100+year/400)%7;
if((year%4)==0&&(year%100!=0)||(year%400==0)){
MonthArray[1]=29;
if(month==1||month==2){
DetermineDay--;
if (DetermineDay<0){
DetermineDay=6;
}
}
}else{
MonthArray[1]=28;
}
//Above line source:cadaeit.net
system("setterm -bold on"); //bold header
printf("\n %s %d \n", MonthDisplay[month-1],year); //header
printf("\nSUN MON TUE WED THU FRI SAT\n");
system("setterm -bold off");
for(day2=1;day2<=DetermineDay;day2++){ //day2 is counter
printf(" ");
} //initialize first day position
printf(" ");
for (day2=1;day2<=MonthArray[month-1];day2 = day2 + 1){
//fills calendar
if(day==day2){ //make output red
printf("\e[5;31;40m%2d\e[0m",day); //does not blink
}else{
printf("%2d",day2);
}
if(((day2+DetermineDay)%7) > 0) { //move on to next line after saturday
printf(" ");
}else{
printf("\n ");
}
}
//try to bold user input day
printf("\n\n");
//display day:
label5:printf("You have selected %s %d, %d \n",MonthDisplay[month-1],day,year);
//display events
//load from output file :(
printList(list,count);
//directions for user input
printf(" - To switch between days, use (a) for previous day and (d) for the next day. \n");
printf(" - To switch between months, use w (previous) and s (next).\n\n");
menu();
scanf("%c",&c);
if(c=='\n'){
scanf("%c",&c);
}
while(c!=3){
if(c=='1'){
CorrectSort(list,&count,*NewEvent(&list[count]));
//save(list,count);
goto label4;
}else if(c=='2'){
deleteEvent(list,&count);
//save(list,count);
goto label4;
}else if(c=='3'){
system("clear");
system("setterm -bold on");
printf("\n\n\n HAVE A GREAT DAY WHILE I CODE THIS!! :)) \n\n\n\n");
system("setterm -bold off");
break;
}else if(c=='w'){ //previous month
if(month==1){
year--;
month=12;
}else{
month--;
}
if(day>MonthArray[month-1]){
day=MonthArray[month-1];
}
goto label4;
}else if(c=='s'){ //next month
if(month==12){
month=1;
year++;
}else{
month++;
}
if(day>MonthArray[month-1]){
day=MonthArray[month-1];
}
goto label4;
}else if(c=='a'){ //previous day
if(day==1){
month--;
day=MonthArray[month-1];
}else{
day--;
}goto label4;
}else if(c=='d'){ //next day
if (day==MonthArray[month-1]){
day=1;
month++;
}else{
day++;
}
goto label4;
}else{
printf("Invalid response. Please try again. \n");
goto label4;
}
}
return 0;
}