Untitled

 avatar
unknown
actionscript
2 years ago
4.4 kB
2
Indexable
#include <stdlib.h>
#include <stdio.h>
#define DATE_MAX 366

/* Date related types
typedef enum weekday {sunday, monday, tuesday, wednesday, thursday,
    friday, saturday} weekday;*/

typedef struct date {
    //char day_of_week[10];
    int day;
    int month;
    int year;
} date;

date tomorrow(date current_date);
void print_date(date d);
int date_before(date d1, date d2);;

int leapYear(int);
//weekday next_day_of(weekday);
//char *name_of_weekday(date);


int main(void) {
    date calendar[DATE_MAX];
    char ignore;

    date first_date = {/*"tuesday",*/ 17, 11, 2015},
            last_date  = {/*"thursday",*/ 17, 11, 2016},
            current_date;
    int i = 0, j;

    current_date = first_date;
    while (date_before(current_date, last_date)){
        calendar[i] = current_date;
        current_date = tomorrow(current_date);
        i++;
    }

    for (j = 0; j < i; j++){
        if (j % 20 == 0) {
            printf("More? - touch the enter key: ");
            scanf("%c", &ignore);
        }
        print_date(calendar[j]);
    }
    return 0;
}

//checks if the current date is the last date
int date_before(date d1, date d2){

    if((d1.year < d2.year) ||
       (d1.year == d2.year && d1.month < d2.month) ||
       (d1.year == d2.year && d1.month == d2.month && d1.day < d2.day)){
        return 1;
    } else {
        return 0;
    }

}

date tomorrow(date current_date){

    date next;
    if ((current_date.month ==  1 ) || (current_date.month ==  3 ) || (current_date.month ==  5 ) ||
    (current_date.month ==  7 ) || (current_date.month ==  8 ) || (current_date.month ==  10 ) ||
    (current_date.month ==  12 )){

        if (current_date.day == 31){
            if(current_date.month == 12){
                next.year = current_date.year + 1;
                next.month = 1;
                next.day = 1;
            } else {
                next.day = 1;
                next.month = current_date.month + 1;
                next.year = current_date.year;
            }

        } else {
            next.day = current_date.day + 1;
            next.month = current_date.month;
            next.year = current_date.year;
        }

    } else if (current_date.month == 2){

        if (leapYear(current_date.year) == 1){
            if (current_date.day == 29){
                next.day = 1;
                next.month = current_date.month + 1;
            } else {
                next.day = current_date.day + 1;
                next.month = current_date.month;
            }
        } else {
            if (current_date.day == 28){
                next.day = 1;
                next.month = current_date.month + 1;
            } else {
                next.day = current_date.day + 1;
                next.month = current_date.month;
            }
        }
        next.year = current_date.year;

    } else {

        if (current_date.day == 30){
            next.day = 1;
            next.month = current_date.month + 1;
        } else {
            next.day = current_date.day + 1;
            next.month = current_date.month;
        }
        next.year = current_date.year;
    }

    return next;
}

int leapYear(int y){

    int result;

    if (y % 400 == 0) {
        result = 1;
    } else if (y % 100 == 0){
        result = 0;
    } else if (y % 4 == 0){
        result = 1;
    } else {
        result = 0;
    }

    return result;
}
/*
weekday next_day_of(weekday d){
    return (weekday) (((int) d + 1) % 7);
}
*/
/* Print date d */
void print_date(date d){
    printf(" %d.%d.%d\n", /*name_of_weekday(d),*/ d.day, d.month, d.year);
}

/* Return the name of the weekday of the date d
char *name_of_weekday(date d){

    char *result;
    switch (d.day_of_week) {
        case sunday: result = "Sunday";
            break;
        case monday: result = "Monday";
            break;
        case tuesday: result = "Tuesday";
            break;
        case wednesday: result = "Wednesday";
            break;
        case thursday: result = "Thursday";
            break;
        case friday: result = "Friday";
            break;
        case saturday: result = "Saturday";
            break;
    }
    return result; %9s
}*/
Editor is loading...