So, this is my problem:
Write enumerated types that supports dates—such as December 12. Then add a function that produces a next day. So
nextday(date)
of December 12 is December 13. Also write a functionprintdate(date)
that prints a date legibly. The function can assume that February has 28 days and it most know how many days are in each month. Use struct with two members; one is the month and the second is the day of the month — anint
(orshort
). Then print out the date January 1 and print the next day Jan 2. Do this for the following dates: February 28, March 14, October 31, and December 31.
And that's my solution
#include <stdio.h>
#include <ctype.h>
typedef enum month { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec } month;
typedef struct date { enum month m; int d; } date;
void print_month(struct date date) { //simple function for displaying month and day
switch (date.m) {
case jan:
printf("January day %d\n", date.d);
break;
case feb:
printf("February day %d\n", date.d);
break;
case mar:
printf("March day %d\n", date.d);
break;
case apr:
printf("April day %d\n", date.d);
break;
case may:
printf("May day %d\n", date.d);
break;
case jun:
printf("June day %d\n", date.d);
break;
case jul:
printf("July day %d\n", date.d);
break;
case aug:
printf("August day %d\n", date.d);
break;
case sep:
printf("September day %d\n", date.d);
break;
case oct:
printf("October day %d\n", date.d);
break;
case nov:
printf("November day %d\n", date.d);
break;
case dec:
printf("December day %d\n", date.d);
break;
default:
printf("Out of range!");
break;
}
}
enum month next_day(struct date next) { //next month|day function which is the problem.
if (next.m == jan || mar || may || jul || aug || oct || dec) {
next.d + 1 % 31;
}
else if (next.m == apr || jun || sep || nov) {
next.d + 1 % 30;
}
else if (next.m == feb) {
next.d + 1 % 28;
if (next.d > 28)
next.m = mar;
}
return (month)next.m;
}
int main(void) {
struct date date_1 = { feb, 28 };
struct date date_2 = { mar, 14 };
struct date date_3 = { oct, 31 };
struct date date_4 = { dec, 31 };
print_month(date_1);
print_month(date_2);
print_month(date_3);
print_month(date_4);
printf("\n");
print_month(next_day(date_1)); //err C2440
print_month(next_day(date_2)); //err C2440
print_month(next_day(date_3)); //err C2440
print_month(next_day(date_4)); //err C2440
printf("\n\n");
return 0;
}