/* EXERCISE 5-8 */ #include <stdio.h> #define MAXLEN 100 /* MAX LENGTH OF ANY INPUT LINE */ int error = 0; int getline(char *, int); int loadgreg(char *, int *, int *, int *); int loadjul(char *, int *, int *); int day_of_year(int, int, int); int month_day(int, int, int *, int *); main() { int len; int year, month, day, julday; char line[MAXLEN]; printf("ENTER YEAR MONTH AND DAY\n"); if ((len = getline(line, MAXLEN)) <= 1) { /* NEWLINE OR EOF ONLY */ printf("NO DATA ENTERED\n"); return 1; } line[len-1] = '\0'; /* OVERLAY NEWLINE */ year = month = day = julday = 0; if (loadgreg(line, &year, &month, &day) != 0) return 1; if (error) return 1; if ((julday = day_of_year(year, month, day)) > 0) printf("THE JULIAN DAY IS: %d\n", julday); printf("ENTER YEAR AND JULIAN DAY\n"); if ((len = getline(line, MAXLEN)) <= 1) { /* NEWLINE OR EOF ONLY */ printf("NO DATA ENTERED\n"); return 1; } line[len-1] = '\0'; /* OVERLAY NEWLINE */ year = month = day = julday = 0; if (loadjul(line, &year, &julday) != 0) return 1; if (error) return 1; if (month_day(year, julday, &month, &day) > 0) printf("THE GREGORIAN DATE IS: %02d/%02d/%02d\n", year, month, day); printf("\nEND OF PROGRAM\n"); return 0; } /* GETLINE: GET LINE INTO s , RETURN LENGTH */ int getline(char *s, int lim) { int c; char *s0 = s; while (--lim > 0 && (c=getchar()) != EOF && c != '\n') *s++ = c; if (c == '\n') *s++ = c; *s = '\0'; return s - s0; } #include <ctype.h> /* LOADGREG: LOAD GREGORIAN DATE FIELDS FROM INPUT LINE */ int loadgreg(char *s, int *year, int *month, int *day) { while (*s == ' ' || *s == '\t') s++; /*** COLLECT YEAR ***/ if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } while (isdigit(*s)) *year = 10 * *year + (*s++ - '0'); if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } if (*s == ' ' || *s == '\t') { if (*year == 0) { printf("YEAR MUST NOT BE ZERO\n"); error = 1; } while (*s == ' ' || *s == '\t') s++; } else { printf("INVALID YEAR\n"); error = 1; while (*s != ' ' && *s != '\t' && *s != '\0') s++; if (*s != '\0') while (*s == ' ' || *s == '\t') s++; } /*** COLLECT MONTH ***/ if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } while (isdigit(*s)) *month = 10 * *month + (*s++ - '0'); if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } if (*s == ' ' || *s == '\t') { if (*month == 0) { printf("MONTH MUST NOT BE ZERO\n"); error = 1; } while (*s == ' ' || *s == '\t') s++; } else { printf("INVALID MONTH\n"); error = 1; while (*s != ' ' && *s != '\t' && *s != '\0') s++; if (*s != '\0') while (*s == ' ' || *s == '\t') s++; } /*** COLLECT DAY ***/ if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } while (isdigit(*s)) *day = 10 * *day + (*s++ - '0'); if (*s == '\0') if (*day == 0) { printf("DAY MUST NOT BE ZERO\n"); return 1; } else return 0; if (*s == ' ' || *s == '\t') { if (*day == 0) { printf("DAY MUST NOT BE ZERO\n"); error = 1; } while (*s == ' ' || *s == '\t') s++; } else { printf("INVALID DAY\n"); error = 1; while (*s != ' ' && *s != '\t' && *s != '\0') s++; if (*s != '\0') while (*s == ' ' || *s == '\t') s++; } if (*s != '\0') { printf("INVALID DATA FOUND\n"); return 1; } return 0; } /* LOADJUL: LOAD JULIAN DATE FIELDS FROM INPUT LINE */ int loadjul(char *s, int *year, int *julday) { while (*s == ' ' || *s == '\t') s++; /*** COLLECT YEAR ***/ if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } while (isdigit(*s)) *year = 10 * *year + (*s++ - '0'); if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } if (*s == ' ' || *s == '\t') { if (*year == 0) { printf("YEAR MUST NOT BE ZERO\n"); error = 1; } while (*s == ' ' || *s == '\t') s++; } else { printf("INVALID YEAR\n"); error = 1; while (*s != ' ' && *s != '\t' && *s != '\0') s++; if (*s != '\0') while (*s == ' ' || *s == '\t') s++; } /*** COLLECT JULIAN DAY ***/ if (*s == '\0') { printf("INPUT INCOMPLETE\n"); return 1; } while (isdigit(*s)) *julday = 10 * *julday + (*s++ - '0'); if (*s == '\0') if (*julday == 0) { printf("JULIAN DAY MUST NOT BE ZERO\n"); return 1; } else return 0; if (*s == ' ' || *s == '\t') { if (*julday == 0) { printf("JULIAN DAY MUST NOT BE ZERO\n"); error = 1; } while (*s == ' ' || *s == '\t') s++; } else { printf("INVALID JULIAN DAY\n"); error = 1; while (*s != ' ' && *s != '\t' && *s != '\0') s++; if (*s != '\0') while (*s == ' ' || *s == '\t') s++; } if (*s != '\0') { printf("INVALID DATA FOUND\n"); return 1; } return 0; } static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; /* DAY_OF_YEAR: SET DAY OF YEAR FROM MONTH & DAY */ int day_of_year(int year, int month, int day) { int i, leap; leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; if (month < 1 || month > 12) { printf("MONTH OUT OF RANGE\n"); return -1; } if (day < 1 || day > daytab[leap][month]) { printf("DAY OUT OF RANGE\n"); return -1; } for (i = 1; i < month; i++) day += daytab[leap][i]; return day; } /* MONTH_DAY: SET MONTH, DAY FROM DAY OF YEAR */ int month_day(int year, int yearday, int *pmonth, int *pday) { int i, leap; leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; if (yearday < 1 || (!leap && yearday > 365) || (leap && yearday > 366)) { printf("DAY OUT OF RANGE\n"); return -1; } for (i = 1; yearday > daytab[leap][i]; i++) yearday -= daytab[leap][i]; *pmonth = i; *pday = yearday; return 1; }