3åã«1ååºåããã ãã®ç°¡åã§ã¯ãªããä»äº
ãªãããã3åã«1ååºåããã ãã®ç°¡åãªããã°ã©ã ã®ãä»äºã£ã¦ãããããã
if ( (++counter % 3) == 0) printf("Fizz\n");
ããã£ã¦æå¤ã¨é£ãããã ããã
â¦â¦ãªãã¦ãã¨ãè¨ãã¨ãããããã天ä¸ã®ããããããã¤ãã«é ãããããªã£ãããã¨ãè¨ãããã ãããã©ãããå®éãã¡ã®ä»äºã§ãã£ã話ã§ããã®ã³ã¼ããåå ã§ã客ããã¨å¤§ããªãã©ãã«ã«ãªã£ãã
ãã¾ãå ·ä½çã«ã¯è¨ããªãã®ã§ãã¡ãã£ã¨å¥ã®ãã®ã«ç½®ãæãã¦è©±ããã©ãããã¯ãã²ããã®é¤ããããã°ã©ã (ä»®)ã ã£ãããã
ä¸ã®ããã°ã©ã ã¯ã3åã«1åã ãã©ããã®ã½ããã«ã¯ãNæéã«1åãé¤ããããã¸ãã¯ãæ¸ãã¦ãã£ãã
if ( (++counter % N) == 0) printf("é¤ããã§ã¼\n");
ãªãããããªæããªãããã§Nã®å¤ã¯ãUI(ã¦ã¼ã¶ã¼ã¤ã³ã¿ã¼ãã§ã¼ã¹)ã§èª¿æ´ã§ããä½ãã«ãã¦ãã£ããä¸åº¦éå§ãã¿ã³ãæ¼ãã¨Nã®å¤ã¯å¤æ´ã§ããªããåä½ä¸ã«Nã®å¤ãå¤æ´ã§ããããããããã¨ã«ãªãããããããªï¼
ããã§ãã°ããã¯ãã¾ãåãã¦ããããéå§ãã¿ã³ãæ¼ããã¨ãã«ãcounter = 0ã«ãªã»ãããããããªããã§ã客ããã¨å§ã 諤ã ã®è°è«ã«ãªã£ãããã
ãã®ã«ã¦ã³ã¿ã¼ãéå§ãã¿ã³ãæ¼ããã¨ãã«ãªã»ãããããã¨ãã²ããé¨å±ã®æé¤ããããã¨ãã«ä¸æåæ¢ããã¦é¨å±ã«å ¥ãã®ã§(ã²ããé¨å±ã«å ¥ã£ã¦ããã¨ãã«æ©æ¢°ãåãã¦é¤ãåºã¦ããã¨å°ãã®ã§)ãåæ¢âéå§ã®ã¨ãã¯counterã¯ãªã»ããããªãã§ãã ããã¨ã®ãã¨ã ã£ããç§ã¯è¨ã£ãããããã¾ãããã§ã¯ãã½ããèµ·åæã«ã®ã¿counterããªã»ãããã¾ãããã¨ããã¨ãåæ¢ããã¦ããæä¸ã¯ããããããªãã®ã§ã«ã¦ã³ã¿ã¼ãé²ããªãä»æ§ã§ããã¨ã®ãã¨ã ã£ãã®ã§ããããã¦ããã£ãã(é¤ããã®æéãæ¥ã¦ããã®ã«æ©æ¢°ãåæ¢ä¸ã ã£ãããããã®é¤ããåä½ãskipãããã¨å°ãã®ã ã)
ããã§ãæåã¯ã客ããã¯N = 10ã§ã©ã³ãã³ã°ãã¦ãããã ã10æéãã¨ã«é¤ãåºã¦ããããããã¨ããããã客ããã¯ããã ã¨ã²ããã®é¤ã足ããªããªããã¨ã«æ°ã¥ããããããã9æéã«å¤æ´ãããã¨ãã客ããã¯æ©æ¢°ãåæ¢ããã¦N = 9ã«å¤æ´ãã¦ãã¾ãéå§ãæ¼ããããããããããã©ããªã£ããã
ã²ããããã¹ã¦æ»ãã ã®ã ã
ãã¶ããæ©æ¢°ãåæ¢ãããã¨ãã«counter = 9ã ã£ããã ãããªããã®ã¾ã¾ããã°1æéå¾ã«é¤ãåºãã¯ãã ã£ããã¨ããããN = 9ã«å¤æ´ãããããcounter = 18ã«ãªãã¾ã§é¤ãåºã¦ããªãã(9ã§å²ã£ã¦ä½ãã0ãªã®ã¯9ã®æ¬¡ã¯18ã ããã)
ããããã¨ãã客ããã«ãã¦ã¯10æéééã§ã¯é¤ã足ããªããã9æéééã«æéãããããçããã¦ããã®ã«ããªãã¨18æéãã®éãé¤ãå ¨ãåºã¦ããªãäºæ ã«ãªã£ãããã ãããããã²ãããæ»ã¬ããããã«ã¯ã客ãããã«ã³ã«ã³ã§ããã
å°ä½ã«ããã®ãã¾ããã£ãã®ãï¼
if ( ++counter >= N) { printf("é¤ããã§ã¼\n"); counter = 0; }
ããã確ãã«ãããªã£ã¦ããã°ãã®ãããªåé¡ã¯èµ·ãããªãã£ãããç¥ããªãããã客ãããæãã³ã¼ããããããã³ã¼ãã ã£ãããç¥ããªãã
ãããããããã
1) ãã¨ä½æéå¾ã«é¤ãåºã¦ããã®ãã ã¨ããååé¤ããã£ãã®ã¯ãã¤ãã ã¨ããååã®é¤ãããã次åã®é¤ããæéã¾ã§ã®ééã¯ã©ãã ããã ã¨ãããã®æã®è¡¨ç¤ºãUIä¸ã«ç¡ãã£ããããã¦ãã®ééãé·ããããçããããããã¨ãã«è¦åãåºããããªå¤±æãä¿è·ããä»çµã¿ãç¡ãã£ããããããã®ãäºåã«ã客ããã«ææ¡ã§ãã¦ããªãã£ãã
2) åæ¢ããã¦Nãå¤æ´ããå ´åã®ä»æ§ãæ確åããã¦ããªãã£ããã客ããã¨èããå
±æåºæ¥ã¦ããªãã£ããäºåã«ããããäºæ
ãæ³å®ãã¦ããªãã£ãããããã²ããã¨çãè¦ã¦ãããã²ããã®ãã¨ãã¾ãã§ããã£ã¦ããªãã£ããUIãæä½ããã¨ãã®ã客ããã®å¿çãç解ãã¦ããªãã£ãã
ãªã©ããããåçãã¹ãç¹ã®å¤ãããã¸ã§ã¯ãã ã£ãã
ããããã®ãè¸ã¾ãã¦æåã«æ¸ããããã°ã©ã ãããä¸åº¦ã覧ããã ãããã
if ( (++counter % 3) == 0) printf("Fizz\n");
ã©ãã§ãï¼é£ããã§ãããï¼