*** Top of Data *** /********************************************************************/ /*** Test the Day-Of-Week-Subscript Assignment Statement ***/ /********************************************************************/ #include <stdio.h> #include <string.h> #define MAXLEN 7 int c = EOF + 1; int snum; int len, error; char str_year[MAXLEN]; char str_month[MAXLEN]; char str_day[MAXLEN]; int year, month, day; enum boolean {NO, YES}; enum escapes {BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r'} main() { int i; char out_str[18]; int pos_max = ~0U >> 1; int neg_max = ~pos_max; static char *monlit[] = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" }; static char *weekday[] = { "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" }; /* LOAD YEAR, MONTH AND DAY STRINGS FROM ONE INPUT LINE */ error = NO; for (snum = 1; snum <= 3; snum++) loadnext(); /* TEST FOR WITHIN INTEGER RANGE */ if (error == NO) { if (year < 1 || year > pos_max) { error = YES; printf("YEAR OUT OF RANGE\n"); } if (month < 1 || month > 12) { error = YES; printf("MONTH OUT OF RANGE\n"); } if (day < 1 || day > 31) { error = YES; printf("DAY OUT OF RANGE\n"); } } if (error == NO) { printf("\n\%s %d, %d\n", monlit[month - 1], day, year); printf("FELL ON %s\n", weekday[dayofwk(year, month, day)]); } printf("\nEND OF PROGRAM\n"); return 0; } /* LOADNEXT: LOAD THE NEXT STRING */ void loadnext(void) { int atoi(char s[]); if (error == NO) if (snum == 1) len = loadstr(str_year); else if (snum == 2) len = loadstr(str_month); else if (snum == 3) len = loadstr(str_day); if (error == NO) if (len == 0) { error = YES; if (snum == 1) printf("YEAR "); else if (snum == 2) printf("MONTH "); else if (snum == 3) printf("DAY "); printf("STRING MISSING\n"); } else if (snum == 1) year = atoi(str_year); else if (snum == 2) month = atoi(str_month); else if (snum == 3) day = atoi(str_day); } /* LOADSTR: LOAD THE NEXT NON-BLANK STRING FROM THE INPUT LINE */ int loadstr(char s[]) { int i = 0; int end_string = NO; error = NO; if (c != '\n' && c != EOF) { while (error == NO && end_string == NO && (c = getchar()) != NEWLINE && c != EOF) if (c == ' ' || c == BELL || c == BACKSPACE || c == TAB || c == VTAB || c == RETURN) if (i > 0) end_string = YES; else ; else if (i < MAXLEN - 1) { s[i++] = c; } else { error = YES; printf("\nSTRING IS TOO LONG\n"); } s[i] = '\0'; } return i; } /* ATOI: CONVERT A STRING OF DIGITS TO AN INTEGER */ int atoi(char s[]) { int i = 0; int dec = 0; while (error == NO && s[i] != '\0') if (s[i] >= '0' && s[i] <= '9') dec = 10 * dec + (s[i++] - '0'); else { error = YES; printf("\nINVALID DECIMAL NUMBER\n"); } return dec; } /* DAYOFWK: PRINT THE DAY OF THE WEEK */ int dayofwk(int year, int month, int day) { int weekday; weekday = ( 365L * (year-1) + (int)((year-1)/4) - (int)((year-1)/100) + (int)((year-1)/400) + day - 30 + (int)((275L*month)/9) - ((int)((month+10)/13)) * (2 - (int)(year/4) + (int)(year/100) - (int)(year/400) + (int)((year-1)/4) - (int)((year-1)/100) + (int)((year-1)/400)) ) % 7; return weekday; } *** Bottom of Data ***

Copyright © 2003 The Stevens Computing Services Company, Inc.  All rights reserved.