//p32.c --- - ¥Î¤F¥ß©e¤uµ{Âà¥]·§©À: ¥t¼gfunction°µ¿é¤J ! // ¥Î long getLong( ); to read a Long int // ³o­Ó getLong ¯dµÛ¥H«á´N¥iª½±µ®³¨Ó¥Î :-) // This program read ¾ã¼Æ n µM«á¦L¥X 1.. n ¶¡¥i¦P®É³Q 3, 5, 7 ¾ã°£ªº¼Æ // ­Y¿é¤J n ¬O -1 «h°±¤î, ­Y n ¬O 0 «h§ï¬°¹w³]­È 3388 // ¦b Windows ¨t²Î¤§¤U, ´ú¸Õ¿é¤J CTRL_Z ¬Ý¬Ý (Unix ¥Î CTRL_D ªí¥Ü EOF) #define N_DEFAULT 3388 #include #include // (ª`·N) atol( ), atoi( ), ¥H¤Î atof( ) ³£«Å§i¦b long n, count; // read n, count for ¥i³Q 3, 5, 7 ¦P®É¾ã°£ªº¼Æ long getLong(void); // ¥ý«Å§i(Án©ú)·|¼g¤@­Ó getLong( ) function ƒº int main( ) { // ¤j¦h¼Æ main program ³£³o¼Ë¶}ÀY int kk; printf("This µ{¦¡ read n, then ¦L¥X 1..n ¥i³Q 3, 5, 7 ¾ã°£ªº¼Æ\n"); printf("n= "); n = getLong( ); // ³z¹L getLong( ) Ū¨ú¾ã¼Æ¨ì n while(n != -1) { // ª`·N³W©w -1 ¬O¥Nªíµ²§ô if(n==0) n = N_DEFAULT; if( feof(stdin) ) break; //­Y¬O EOF ¤]­n°±!(ª`·N) stdin ¬OÁä½L kk = count = 0; while(kk < n) { // §O¼g¿ù³á, ª`·N¶i¤J«á·|¥ß¨è +1 ++kk; if(kk%3 !=0 ) continue; if(kk% 5 !=0 ) continue; if(kk% 7 !=0 ) continue; printf("%5d ", kk); ++count; if(count%10 ==0) printf("\n"); // NewLine } // while(kk printf("\nTotal %d data.\n", count); printf("n= "); n = getLong( ); // ³z¹L getLong( ) Ū¨ú¾ã¼Æ¨ì n } // while printf("Hit ENTER key...", n, n, n); // ¸Ì­±ªº n ¬O¥Õ¼gªº :-)) getchar( ); // ·Q¿ìªk¼È°±¤@¤U, ¥H§K¥i¯à°{¤@¤U´N¤£¨£ }// main( long getLong( ) { // ¥H«á­n§ï¿é¤Jªº¤èªk¥u­n§ï³o function ´N¥i :-) static char buf[99]; // a string buffer for input; why "static" ? long ans; // ª`·N­Y¥Î atof ­n§ï double; ¨ç¼ÆÀY³¡¤]­n§ï fgets(buf, sizeof(buf), stdin); // stdin ´N¬OÁä½L, ­n°O±o§t¤J ans = atol(buf); // ±q¦r¦ê¤¤Åª¥X long ¾ã¼Æ (long); ////// ­Y¬O¹ê¼Æ´N§â atol §ï¬° atof ³o¤]¬O§O¤H¼g¦nªº¨ç¼Æ(¨ç¦¡) return ans; } // ¦pªG§A¦³ K&R ½Ò¥», ¥i¬ã¨s½Ò¥»¤º°Q½× atoi »P atof ¨ç¼Æ¬O¦p¦ó¼gªº, // ¥i¥HÅý§A¼W¥[¥\¤O³á ! ¥i±q®Ñ¥»«á­±¯Á¤Þ (index) ¬d¬Ý¦b²Ä´X­¶ :-) /************************ D:\testc>gcc p32.c D:\testc>a This µ{¦¡ read n, then ¦L¥X 1..n ¥i³Q 3, 5, 7 ¾ã°£ªº¼Æ n= 500 105 210 315 420 Total 4 data. n= 0 105 210 315 420 525 630 735 840 945 1050 1155 1260 1365 1470 1575 1680 1785 1890 1995 2100 2205 2310 2415 2520 2625 2730 2835 2940 3045 3150 3255 3360 Total 32 data. n= ^Z Hit ENTER key... D:\testc> *******************************************/