In: Computer Science
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_STR_1 16
#define MAX_STR_2 32
#define MAX_ARR 64
#define C_OK 0
#define C_NOK -1
typedef struct {
int hours;
int minutes;
int seconds;
} TimeType;
typedef struct {
TimeType time;
char month[MAX_STR_1];
int day;
int year;
} DateType;
typedef struct {
int size;
DateType* elements[MAX_ARR];
} ArrayType;
void initArray(ArrayType*);
void addDate(ArrayType*, DateType*);
void selectTimes(ArrayType*, ArrayType*, int, int);
void initDate(char*, int, int, int, int, int, DateType*);
void printArray(ArrayType*);
void printDate(DateType*);
int main()
{
ArrayType calendar, specialCal;
DateType* newDate;
int num1, num2, num3, num4, num5;
char str[MAX_STR_1];
initArray(&calendar);
initArray(&specialCal);
while(1) {
printf("Enter month, day, year, hours, minutes, and seconds:
");
scanf("%s %d %d %d %d %d", str, &num1, &num2, &num3,
&num4, &num5);
if (strcmp(str, "-1") == 0)
break;
newDate = malloc(sizeof(DateType));
initDate(str, num1, num2, num3, num4, num5, newDate);
addDate(&calendar, newDate);
}
printf("Enter cut-off time (hours and minutes): ");
scanf("%d %d", &num1, &num2);
selectTimes(&calendar, &specialCal, num1, num2);
printf(" FULL CALENDAR:");
printArray(&calendar);
printf(" MORNING CALENDAR:");
printArray(&specialCal);
return(0);
}
/*
Purpose: initialize an array structure
*/
void initArray(ArrayType *a) { a->size = 0; }
/*
Purpose: populate newArr with all dates from orgArr that have
a time before the given hours and minutes, for
example, if we want a calendar with all the morning
appointments, we would call this function with
h and m parameters set to 12 and 0, respectively
*/
void selectTimes(ArrayType *orgArr, ArrayType *newArr, int h, int
m)
{
int i;
newArr->size = 0;
for(i=0;i<orgArr->size;i++){
if(orgArr->elements[i]->time.hours < h ||
((orgArr->elements[i]->time.hours == h) &&
(orgArr->elements[i]->time.minutes < m))){
newArr->elements[newArr->size ++] =
orgArr->elements[i];
}
}
}
/*
Purpose: add a date structure to the back of the array
*/
void addDate(ArrayType *arr, DateType *newDate)
{
if (arr->size >= MAX_ARR)
return;
arr->elements[arr->size] = newDate;
++(arr->size);
}
/*
Purpose: initialize a date structure with the given
parameters
*/
void initDate(char *mm, int d, int y, int h, int m, int s, DateType
*newDate)
{
strcpy(newDate->month , mm);
newDate->day = d;
newDate->year = y;
newDate->time.hours = h;
newDate->time.minutes = m;
newDate->time.seconds = s;
}
void printDate(DateType *d)
{
char str[MAX_STR_2];
sprintf(str, "%s %d, %d", d->month, d->day, d->year);
printf("%25s @ %2d:%2d:%2d ", str,
d->time.hours, d->time.minutes, d->time.seconds);
}
void printArray(ArrayType *arr)
{
int i;
printf(" ");
for (i=0; i<arr->size; ++i)
printDate(arr->elements[i]);
}