Code:
// EX_4.c
#include <stdio.h>
struct day
{
int numbers;
char letter[15];
};
struct my_struct
{
int month;
int day;
int year;
};
int main(void)
{
int struct_function (struct my_struct link);
int aa;
struct my_struct date_one;
// array of structures
const struct day days[7] = { { 6, { 'S', 'u', 'n', 'd', 'a', 'y' } },
{ 5, { 'M', 'o', 'n', 'd', 'a', 'y' } },
{ 7, { 'T', 'u', 'e', 's', 'd', 'a', 'y' } },
{ 9, { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y' } },
{ 8, { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y' } },
{ 6, { 'F', 'r', 'i', 'd', 'a', 'y' } },
{ 8, { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y' } } };
printf("Enter a date: (mm dd yyyy): ");
scanf("%i %i %i", &date_one.month, &date_one.day, &date_one.year); // get date_one (first date)
aa = struct_function(date_one);
printf("%i\n", aa);
int x, i;
x = (aa - 621049) % 7;
for (i = 0; i <= days[x].numbers; ++i)
printf("%c", days[x].letter[i]);
printf("\n");
return 0;
}
int struct_function (struct my_struct link)
{
// N = 1461 * f(year, month) / 4 + 153 * g(month) / 5 + day;
int sum;
if (link.month < 3)
{
link.month += 12;
link.year -= 1;
}
sum = link.day + (153 * link.month - 457) / 5 + 365 * link.year + link.year / 4 - link.year / 100 + link.year / 400 - 306;
return sum;
}