After completing KNK2, my interest in C was reignited, prompting are-reading of Traister''s dubious book on pointers. Then I dusted offthe fine ``C Traps and Pitfalls'''' by Andy Koenig. Nowadays I primarilywork in PHP, Perl and C++. Consequently my C skills have atrophied. So inspired by CT&P''s ``calendar'''' example on page 30, I decided tobrush up on some elementary C (although Koenig describes it as walking``out on the ice'''') with the little toy exercise below. For posting, here I yanked all the comments for brevity. There''s a(too?) liberal use of typedef, pointers for the sake of pointers andsome blatantly obvious asserts. It compiles cleanly with Sun C 5.82005/10/13 on a SPARC and lint shows only functions returning valuesthat are ignored. The output meets expectations for a non-leap year. So look this over being brutal and merciless with your critique as Itry to rebuild my flabby C muscles: #include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string.h> enum { MONTHS = 12, DAYS = 31 };enum { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };static const char *const months[] = { "JAN", "FEB", "MAR", "APR","MAY", "JUN", "JUL", "AUG","SEP", "OCT", "NOV", "DEC", }; int main(void){typedef int month_type[DAYS];typedef month_type *month_ptr_type;typedef int *day_ptr_type; int calendar[MONTHS][DAYS];month_ptr_type month_ptr;size_t num_months;size_t num_days_per_month; memset(calendar, 0, sizeof calendar);assert(calendar[DEC][DAYS - 1] == 0); month_ptr = calendar;assert(sizeof *month_ptr == DAYS * sizeof(int)); num_months = sizeof calendar / sizeof *calendar;assert(num_months == MONTHS); num_days_per_month = sizeof *calendar / sizeof **calendar;assert(num_days_per_month == DAYS); for(; month_ptr < calendar + num_months; ++month_ptr) {day_ptr_type day_ptr = *month_ptr;int month_num = (month_ptr - calendar + num_months) - num_months;int days_in_month;int day_num; switch(month_num) {case FEB:days_in_month = 28;break;case APR:case JUN:case SEP:case NOV:days_in_month = 30;break;default:days_in_month = 31;break;} for(day_num = 1; day_num <= days_in_month; ++day_num, ++day_ptr)*day_ptr = day_num;} for(month_ptr = calendar; month_ptr < calendar + num_months; ++month_ptr) {int month_num = (month_ptr - calendar + num_months) - num_months;int total_days_in_month = -1;day_ptr_type day_ptr; for(day_ptr = *month_ptr + num_days_per_month - 1;day_ptr *month_ptr; --day_ptr) {if(*day_ptr) {total_days_in_month = *day_ptr;break;}} printf("month %d (%s) has %d days\n",month_num + 1, months[month_num], total_days_in_month);} return EXIT_SUCCESS;} 推荐答案 On Sat,07 Jun 2008 02:08:07 -0500,Bob Nelson写道: [code已经错过了。 0警告,0错误。 在我的机器上,输出的持续时间不够长。在某处添加一个getchar() 调用: On Sat, 07 Jun 2008 02:08:07 -0500, Bob Nelson wrote: [code elided]0 warnings, 0 errors. On my machine the output doesn''t last long enough to read. Add a getchar()call somewhere: > printf(" month%d( %s)%d天\ n", month_num + 1,months [month_num],total_days_in_month); > printf("month %d (%s) has %d days\n", month_num + 1, months[month_num], total_days_in_month); getchar(); getchar(); } 返回EXIT_SUCCESS; } } return EXIT_SUCCESS;} 我必须在大学里为我的科学编程班写一个永久的日历。 fortran。输出是无趣的。 - 金钱的主要价值在于,一个人生活在一个世界中,它的价格是b $ b被高估了。 HL Mencken I had to write a perpetual calender for my scientific programming class infortran in college. Output was un-fun.--The chief value of money lies in the fact that one lives in a world inwhich it is overestimated.H. L. Mencken Ron Ford说: Ron Ford said: 周六,2008年6月7日02:08:07 -0500,Bob Nelson写道: [代码省略] 0警告,0错误。 在我的机器上,输出的持续时间不够长。在某处添加一个 getchar()调用: On Sat, 07 Jun 2008 02:08:07 -0500, Bob Nelson wrote:[code elided]0 warnings, 0 errors.On my machine the output doesn''t last long enough to read. Add agetchar() call somewhere: 他为什么要这样做?学习如何驾驶命令外壳。 - Richard Heathfield< http://www.cpax.org.uk> 电子邮件:-http:// www。 + rjh @ 谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php> Usenet是一个奇怪的放置" - dmr 1999年7月29日 Why should he do that? Learn how to drive a command shell. --Richard Heathfield <http://www.cpax.org.uk>Email: -http://www. +rjh@Google users: <http://www.cpax.org.uk/prg/writings/googly.php>"Usenet is a strange place" - dmr 29 July 1999 Ron Ford< ro*@nowhere.netwrites: Ron Ford <ro*@nowhere.netwrites: 周六, 2008年6月7日02:08:07 -0500,Bob Nelson写道: [代码省略] 0警告,0错误。 在我的机器上,输出的持续时间不够长。在某处添加一个getchar() 调用: On Sat, 07 Jun 2008 02:08:07 -0500, Bob Nelson wrote:[code elided]0 warnings, 0 errors.On my machine the output doesn''t last long enough to read. Add a getchar()call somewhere: >> printf(" month%d(%s) )%d days \ n, month_num + 1,months [month_num],total_days_in_month); >> printf("month %d (%s) has %d days\n", month_num + 1, months[month_num], total_days_in_month); getchar(); getchar(); > } 返回EXIT_SUCCESS; } > } return EXIT_SUCCESS;} 不要打扰。只需在程序终止时不会导致 窗口关闭的方式运行程序。并非所有系统都是 Windows;在一个非Windows系统上,一个程序在终止之前无偿地等待输入b / b 非常讨厌。 - Keith Thompson(The_Other_Keith)ks***@mib.org< http://www.ghoti.net/~kst> 诺基亚 "我们必须做一点事。这是事情。因此,我们必须这样做。 - Antony Jay和Jonathan Lynn,是部长 Don''t bother. Just run the program in a way that doesn''t cause thewindow to close when the program terminates. Not all systems areWindows; on a non-Windows system, a program that gratuitously waitsfor input before terminating would be quite annoying. --Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>Nokia"We must do something. This is something. Therefore, we must do this."-- Antony Jay and Jonathan Lynn, "Yes Minister" 这篇关于CT&amp; P短途旅行 - 打败它!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-02 15:57