View difference between Paste ID: DwyL1ygv and cDUpCwEx
SHOW: | | - or go back to the newest paste.
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
int leap(int x){
5
    if(x%4 == 0 && x%100 != 0 || x%400 == 0)
6
        return 1;
7
    else
8
        return 0;
9
}
10
11
int pastDays(int m, int d, int y){
12-
    int days[] = {31,28,31,30,31,30,31,30,31,30,31};
12+
13
    int i, past = 0;
14
15
    if(leap(y)){
16
        days[1] = 29;
17
    }
18
19
    for(i = 0; i < m-1; i++){
20
        past = past + days[i];
21
    }
22
    return past + d;
23
}
24
25
int daysPast(int d, int y) {
26
    int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
27
28
    int i, temp = 0;
29
30
    if(leap(y)){
31
        days[1] = 29;
32
    }
33
34
    for(i = 0; i < 11 && d > days[i]; i++){
35
        d = d - days[i];
36
    }
37
38
    if(d < 0){
39
        d = days [i] + d;
40
    }
41
    printf("\n\t\t%d/%d/%d", i+1,d, y);
42
43
}
44
45
int main()
46
{
47
    int choice, past, mm, dd, yy, temp;
48
    char more;
49
    printf("\n\t\tThis program will find days past or date in the year");
50
51
    do{
52
        printf("\n\t\t1)  date      -> days past");
53
        printf("\n\t\t2)  days past -> date");
54
        printf("\n\t\tYour choice (1/2)?");
55
        scanf("%d", &choice);
56
        if(choice == 1){
57
            printf("\n\t\tInput date (mm/dd/yyyy)");
58
            scanf("%d/%d/%d", &mm, &dd, &yy);
59
            if(mm > 12) {
60
                printf("\n\t\tThere are not more than 13 months in a year");
61
            }
62
            else if(dd > 31){
63
                printf("\n\t\tCant have more than 31 days in a month");
64
            }
65
            else if(mm == 2 && dd > 28 && leap(yy) != 1 || mm == 2 && dd > 29 && leap(yy) == 0){
66
                printf("\n\t\tThere are not more than 28 day in February %d", yy);
67
            }
68
            else{
69
                printf("\n\t\tThere are %d days past in the year", pastDays(mm, dd, yy));
70
            }
71
        }
72
        else if(choice == 2) {
73
            printf("\n\t\tInput days past (ddd)");
74
            scanf("%d", &past);
75
            printf("\n\t\tInput the year (yyyy)");
76
            scanf("%d", &yy);
77
            if(past == 366 && leap(yy) == 0){
78
                printf("\n\t\tError there's not 366 days in %d", yy);
79
            }
80
            else if(past > 365) {
81
                yy = yy + 1;
82
                past = past - 365;
83
                daysPast(past, yy);
84
            }
85
            else if(past < 0){
86
                yy = yy -1;
87
                past = 365 + past;
88
                daysPast(past, yy);
89
            }
90
            else{
91
                daysPast(past, yy);
92
            }
93
        }
94
95
        printf("\n\t\tDo more (Y/N)");
96
        scanf("%s", &more);
97
    }while(more == 'y' || more == 'Y');
98
99
100
    return 0;
101
}