/* Input: */ /* Date as a 4-tuple */ /* It's an encoded day of week,year,encoded month,day of month */ /* Sunday=0, Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5, Saturday=6 */ /* January=0, February=1, March=2, April=3, May=4, June=5 */ /* July=6, August=7, September=8, October=9, November=10, December=11 */ /* 0,2017,0,15 represents Sunday 2017 January 15 */ /* 1,2018,1,19 represents Monday 2018 February 19 */ /* Output: */ /* The month and year of the first occurrence of Friday the 13th that occurs after the input date */ /* You may assume that the user's input is valid. */ /* fri13.tc - 1/18/17 - tct */ // globals - 1/13/23 int gday_of_week,gyear,gmonth,gday int is_a_leap_year split int t(0);char s(0),d(0) [ int y,ix,tx,sl,ds;ds=strlen(d);t(0)=s;sl=strlen(s) while (sl-tx) [ ix=index(s+tx,sl-tx,d,ds) tx=tx+ix+ds-1;y=y+1;t(y)=s+tx;s(tx-ds)=0 ] return y ] leap_year int year [ if (year%400==0) return 1 else if (year%100==0) return 0 else if (year%4==0) return 1 else return 0 ] fix_g [ /* catch the illegals */ if (gday==32) [ if (gmonth==11) [ gmonth=0;gday=1 gyear=gyear+1 is_a_leap_year=leap_year(gyear) ] else [ gmonth=gmonth+1;gday=1 ] ] else if (gday==31) [ if ((gmonth==3)+(gmonth==5)+(gmonth==8)+(gmonth==10)) [gmonth=gmonth+1;gday=1] ] else if (gday==30) [ if (gmonth==1) [ if (is_a_leap_year) [gmonth=2;gday=1] ] ] else if (gday==29) [ if (gmonth==1) [ if (is_a_leap_year==0) [gmonth=2;gday=1] ] ] ] main [ char buff(30);cdate buff;printf "%c%s%c",10,buff,10 printf "%cfri13.tc - lrb - 1/13/23%c",10,10 printf "%center day of week - sunday is 0, monday is 1 etc. ",10 gday_of_week = gn printf "%center year - e.g. 2023 ",10 gyear = gn printf "%center month - jan is 0, feb is 1 etc. ",10 gmonth = gn printf "%center day - e.g. 12 ",10 gday = gn fri13 gday_of_week,gyear,gmonth,gday ] fri13 int day_of_week,year,month,day [ /* initialize stuff */ int month_name(12) int day_of_week_name(7) char foo(100),bar(100) strcpy(foo,"Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,") split month_name,foo,"," strcpy(bar,"Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,") split day_of_week_name,bar,"," gday_of_week=day_of_week gyear=year gmonth=month gday=day pl "Start date: " ps day_of_week_name(gday_of_week);putchar ' ';ps month_name(gmonth);pn gday;pn gyear gday=gday+1 gday_of_week=gday_of_week+1 gday_of_week=gday_of_week%7 if ((gday==32)*(gmonth==11)) [gday=1;gmonth=0;gyear=gyear+1] is_a_leap_year=leap_year(gyear) while (((gday_of_week==5)*(gday==13))!=0) [ /* main loop */ gday=gday+1 fix_g gday_of_week=gday_of_week+1 gday_of_week=gday_of_week%7 ] printf"%c%cThe next Friday the thirteenth occurs in ",10,10 printf"%s %d", month_name(gmonth),gyear ] test [ fri13 4,2100,7,12 fri13 6,2000,1,12 fri13 2,2001,2,13 fri13 4,2017,0,12 fri13 5,2017,0,13 fri13 6,2017,0,14 fri13 6,2017,9,14 fri13 4,2018,3,12 fri13 6,2018,3,14 fri13 1,1776,11,2 fri13 0,2017,11,31 /* 1,2018,1,19 represents Monday 2018 February 19 */ fri13 4,2023,0,12 ]