Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!ut-emx!emx.utexas.edu From: plyon@emx.utexas.edu (Paul Lyon) Newsgroups: comp.object Subject: Re: Readability of C/C++ vs. Readability of Ada Message-ID: <47535@ut-emx.uucp> Date: 19 Apr 91 23:52:33 GMT Sender: plyon@ut-emx.uucp Organization: The University of Texas at Austin; Austin, Texas Lines: 141 This is my entry in response to Showalter's challenge. Though it does the reverse computation from Showalter's ADA function, it seems to me to be appropriate in light of the topic of this thread, since it is, in fact, production code. It is also written in C not C++; in as much as the complaint about the latter is it's being an extension of the former, that should not count against it in the present context. A couple of remarks about the source are in order: the first two bits come from the declaration section at the head of the programme from which conv_julian_date() was taken. It will be observed that no data validation is done in conv_julian_date(); there was another function in the programme that did that. This one would not be called if the year or day values were out of range or missing. The amount of comments may be deemed excessive; I do tend to get carried away a bit in writing them :-) I will leave it to the judgment of those who have been following this thread whether this code is less perspicuous than that given by Showalter. /***************************************************************************** * * * Various mnemonics used for testing character values and setting * * character values in the program. * * * *****************************************************************************/ #define NUL '\x00' #define LF '\x0A' #define SPACE '\x20' #define HYPHEN '-' /***************************************************************************** * * * The following array is used in converting julian dates to standard * * dates in the conv_julian_date() function. The first row of the array * * gives the number of days in each month of an ordinary year, and the * * second row gives the number of days in each month of a leap year. * * The mnemonics are used in the function to reference the appropriate * * row of the array. * * * *****************************************************************************/ int day_table[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 }, }; #define ORD_YEAR 0 #define LEAP_YEAR 1 /***************************************************************************** * S E C T I O N 0 1 0 * * conv_julian_date() function * * * * Date Written : 24 July, 1988 * * Date Changed : 30 July, 1988 * * * * Author : Paul Lyon * * * * Description : this function takes a date in JULIAN form, namely * * "YYDDD" where "DDD" is the number of days since the * * first of the year, and converts the day value into the * * correct month and number of days into that month. * * * * Remark : the routine for converting a day of the year into a month * * and a day of the month has been borrowed (with * * modifications) from Kernighan & Ritchie, Chapter 5, Section * * 7. The idea is this. We compare the number of days to the * * number of days for the month (as recorded in the appropriate * * row of the day_table array defined in the declarations * * section above). So long as the number of days is greater we * * subtract off the number of days for that month and repeat * * the comparison for the next month. Once the number of days * * as reduced so far is less than or equal to the number of * * days for the current month value, then the two values will * * now give the correct month and day of the month. Example: * * ordinary year, days of the year == 73. First comparison: 73 * * > 0, so subtract 0 from the number of days and set the * * month = 1, and compare again. Second comparison: 73 > 31, * * so subtract 31 from the number of days and set month = 2. * * Third comparison: 42 > 28, so subtract 28 from the number * * of days and set month = 3. Fourth comparison: 14 < 31, so * * quit. The month is 3 (March), and the day of the month is * * 14. * * * * Parameters : - pointer to input field (5 characters). * * - pointer to output buffer for the month (2 digits). * * - pointer to output buffer for the days (2 digits). * * * * Returns : none. * * * *****************************************************************************/ conv_julian_date( input_field, month_buf, days_buf ) char *input_field; char *month_buf; char *days_buf; { char conv_buffer[4]; char *field_start; int k, years, month, days, month_days, year_type; conv_buffer[0] = *input_field++; conv_buffer[1] = *input_field++; conv_buffer[2] = NUL; years = atoi( conv_buffer ) + 1900; conv_buffer[0] = *input_field++; conv_buffer[1] = *input_field++; conv_buffer[2] = *input_field; conv_buffer[3] = NUL; days = atoi( conv_buffer ); if ( years % 4 == 0 && ( years % 100 != 0 || years % 400 == 0 ) ) year_type = LEAP_YEAR; else year_type = ORD_YEAR; month = 0; month_days = day_table[year_type][month]; while ( days > month_days && month < 13 ) { days -= month_days; month++; month_days = day_table[year_type][month]; } sprintf( month_buf, "%2.2d", month ); sprintf( days_buf, "%2.2d", days ); }