æè¿ãä¼ç¤¾ã§ã½ã¼ã¹ã®ã³ã¡ã³ãããã¿ã§è¦å´ãã¦ããµã¨æã£ãã
é©åãªã³ã¡ã³ãã®æ¸ãæ¹ã«ã¤ãã¦ã®å®è·µçãªå ¥éè³æã¯ãªããã®ãï¼
ãCODE COMPLETE 第2ç ä¸ å®å ¨ãªããã°ã©ãã³ã°ãç®æãã¦ãã®ç¬¬33ç« ããCode Craft ~ã¨ã¯ã»ã¬ã³ããªã³ã¼ããæ¸ãããã®å®è·µçææ³~ãã®ç¬¬5ç« ã§ã¯è¯ãã³ã¡ã³ãã«ã¤ãã¦çµæ§ãªãã¼ã¸æ°ãå²ãã¦è©³ããè«ãã¦ããããããã¾ã§è©³ç´°ã§ãªãã¦ããã®ãªããããã°ã©ãã³ã°ä½æ³ãã®ç¬¬1ç« ã§è¯ãã³ã¡ã³ãã®æ¸ãæ¹ã®åºæ¬æ¹éçãªãã®ãæ示ããã¦ãããåèã«ãªããããªæ¬ã¯ããã®ã ã
ãã ããããä¸ã§ãããªãã«ã¾ã¨ã¾ã£ãè³æã¨ãªãã¨ãæ¢ãæ¹ãæªãã£ãããããã¾ãå ·åã«è¦ã¤ãããªãã£ããæ¬ã ã¨è²·ã£ã¦èªãã§ããã人ãä¸ã ããªãã®ã§*1ããããã§èªãããã®ã欲ããã®ã ã
ããã§ãè»è¼ªã®åçºæã§ãããã¨ãæ¿ç¥ã®ä¸ã§ã試ãã«èªåã§æ¸ãã¦ã¿ãã
対象èªè ã¯â¦â¦ããã¾ã§èãã¦ããªãã£ãããµã³ãã«ã³ã¼ããCè¨èªã§æ¸ãã¦ããã®ã§ãCè¨èªç³»ã®ã³ã¼ããèªããã¨ãããããããªãããCODE COMPLETE 第2ç ä¸ å®å ¨ãªããã°ã©ãã³ã°ãç®æãã¦ãããCode Craft ~ã¨ã¯ã»ã¬ã³ããªã³ã¼ããæ¸ãããã®å®è·µçææ³~ããæã£ã¦ãã人ã¯ããã®ã¨ã³ããªãèªãå¿ è¦ã¯ãªãã
ãã®ã¨ã³ããªã§ã¯ãå®éã«ã³ã¡ã³ããæ¸ãã«ããã£ã¦ã®å¿æ§ãçãªãã®ãæ¸ããã¨æããæ°åã»ä½åã»æã®éãå å®ãã¦ä¸ã¤é£½ããªããã°ãããå°ãå®è·µçã«ä¾ãæããã¹ã¿ã¤ã«ã®ã¨ã³ããªããã¨ã§æ¸ããããããªãã
éè¤ãé¿ãã
ã¾ãéè¦ãªã®ã¯éè¤ãé¿ããã¨ãããã¨ãDRYï¼Don't Repeat Yourselfï¼ã®ååãã³ã¡ã³ãã«ãé©ç¨ããã¹ãã ã
å¤å ¸çãªä¾ã ãã©ãä¾ãã°ãããªã³ã¡ã³ãã¯ä¸è¦ã *2ã
oddflag = input % 2; /* å¥æ°ã§ããããå¤å® */ if (!oddflag) { /* å¶æ°ã®å ´å */ fprintf(stderr, "å¥æ°ã§ã¯ããã¾ãã\n"); }
ãã®ã³ã¡ã³ãã¯ã³ã¼ãã®å 容ããã¬ã¼ã¹ãã¦ããããããä½ããã£ã¦ããã®ãã¯ã³ã¼ãããååã«åãããã ãããã®ã³ã¡ã³ãã¯ä¸è¦ã ã
ã³ã¼ãã®å 容ããã¬ã¼ã¹ããã³ã¡ã³ããåå¨ããã¨ãããã¨ã¯ãããã¢ã«ã´ãªãºã ã表ç¾ããè¨è¿°ã2éã«åå¨ããã¨ãããã¨ã ãçä¸æ¹ãä¿®æ£ããæãããçä¸æ¹ãä¿®æ£ãå¿ããã¨ãåæ¹ã®éã«å·®ç°ãçãããã¤ã¾ãã³ã¼ãã¨ã³ã¡ã³ãã«çç¾ãçãã¦ãã¾ããå¾ã§ã½ã¼ã¹ãèªã人ãæ··ä¹±ãã訳ã ã
ä¼¼ããããªè©±ããããã½ã¼ã¹ã³ã¼ãã¨è©³ç´°ä»æ§æ¸ã ãåããã®ã表ç¾ãã¦ããã¯ããªã®ã«ãåæ¹ã§å 容ãé£ãéããã¨ãé常ã«å¤ãã詳細ä»æ§æ¸ã®ä¿®æ£ã«ã¾ã§æãåããªãã®ã ã
ã½ã¼ã¹ã¨ã³ã¡ã³ããåãã ãä¼¼ããããªãã®ã2éã«åå¨ãã¦ã両è ãåæãããå¿ è¦ãããã®ãªããã¡ã³ããã³ã¹ã®æéã¯å¢å¤§ãããã³ã¼ãã®å 容ããã¬ã¼ã¹ããã³ã¡ã³ãã¯ä¸è¦ã©ããã害æªã«ãªããããªãã
ã¨ããã§ãå ã»ã©ã®ã³ã¼ãã次ã®ãããªå 容ã ã£ããã©ãã ãããï¼
flg = n % 2; /* å¥æ°ã§ããããå¤å® */ if (!flg) { /* å¶æ°ã®å ´å */ fprintf(stderr, ERROR_1042); }
ããã©ã¯ã³ã¡ã³ããå¿ è¦ã ã¨æã人ããããããããªãããã³ã¡ã³ããæ¶ããããä½ããã£ã¦ããã®ãåããã«ãããªããããªããï¼ãã¨ãã主張ã ã
ããã¯ééã£ã¦ããããããééã£ã¦ããã®ã¯ã³ã¼ãã®æ¹ã ããããªæ解ãã«æ¬ ããå¤æ°åãå®æ°å*3ã使ç¨ãã¦ããã³ã¼ãããããããã ããã³ã¼ããä¿®æ£ããã¹ãã§ãããã³ã¼ãã®ä¸åãã³ã¡ã³ãã§èª¤éåãã®ã¯çéãã¨ãããã®ã ã
ä»å 価å¤ã®é«ãã³ã¡ã³ããæ¸ã
DRYã®ååã«åºã¥ãã¨ããã¨ãååã¨ãã¦ã³ã¡ã³ãã«ã¯ãã³ã¼ãã«æ¸ããã¦ããªãï¼ã³ã¼ãããã¯èªã¿åãã®ãé£ãããèªã¿åãã®ã«æéããããå 容ããæ¸ããã¨ã«ãªããä¾ãã°æ¬¡ã®ãããªãã®ã ã
- ã³ã¼ãã®æå³ãçç±ã説æããã³ã¡ã³ãã
- æå¤ãªè¦ç´ ãè¨è¿°ãç¹ç°ãªé¨åã«ã¤ãã¦æ³¨æãä¿ãã³ã¡ã³ããä½ãã³ã¡ã³ãããåã«ã³ã¼ããè¦ç´ããã¨ã
- é£ãããè¾¼ã¿å ¥ã£ãé¨åã®èª¬æã³ã¡ã³ããä½ãã³ã¡ã³ãããåã«ã³ã¼ããè¦ç´ããã¨ã
- è¦ç´çãªã³ã¡ã³ããé¢æ°ï¼ã¡ã½ããã®æ¦è¦åã³ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ä»æ§ããããç¨åº¦ã¾ã¨ã¾ã£ãå¦çãä¸è¨ã«ã¾ã¨ãããã®ãè¦ç´ã«ãã£ã¦æ½è±¡åãè¡ããä½ããã®ã³ã¡ã³ãã¯ã³ã¼ãã¨ã®2éåãçºçããã®ã§æå°éã«ã¨ã©ããä¸ã¤ãã£ããã¡ã³ããããã¨ã
æå³ã示ãã³ã¡ã³ãã¨ãã¦ã¯ãlintæ令ãå¤å ¸çãªä¾ã§ã¯ãªããã¨æã*4ã
C/C++/Javaãªã©ã«ããswitchæã§ã¯ãbreakããã¿ã®å¤±æãã¤ãã¦åããbreakãæ¸ãå¿ãã¦å¤§å¤±æãã¨ãã奴ã ããããããç¨ã«breakãæ¸ããã«è½ä¸ããããå ´åãããããããªã½ã¼ã¹*5ããã£ãã¨ãããã
switch (status) { case L_OPERAND: if (!isoperator(*p)) { l_op[i++] = *p; if (i >= MAX_INT_DIGIT) { l_op[i] = '\0'; status = OPERATOR; i = 0; } break; } l_op[i] = '\0'; i = 0; /*FALLTHRU*/ case OPERATOR:
ã/*FALLTHRU*/ãã¨ããã³ã¡ã³ãããããUnixç°å¢ã«ã¯æããlintã¨ããCè¨èªã®ã½ã¼ã¹ã³ã¼ãæ¤æ»ãã¼ã«ãããã®ã ãã©ããã®ã³ã¡ã³ãã¯lintã解éããã
switchæã«breakãæãã¦ããã±ã¼ã¹ããã£ãã¨ãããlintãã½ã¼ã¹ã³ã¼ãããã§ãã¯ããæãbreakãæãã¦ããã¨è¦åãåºãã大æµã¯breakãæ¸ãå¿ãã¦ããããã ããããããã¨breakãæ¸ããã«è½ä¸ããããå ´åãlintã¯è¦åãåºããbreakãæ¸ã代ããã«ã/*FALLTHRU*/ãã¨ããã³ã¡ã³ããæ¸ãã¦ããã¨ãlintã¯ãbreakãæ¸ãå¿ããã®ã§ã¯ãªããããã¨æ¸ããªãã£ãã®ã ãã¨è§£éãã¦è¦åãåºããªãã
ã¨ããã§å¤æã®Cããã°ã©ãã«ã¯lintã³ã¡ã³ãã«ã¤ãã¦ç¥ã£ã¦ãã人ããããªãã«ãããlintã使ããªãã¦ããã½ã¼ã¹ãèªãã¨ãã«ã/*FALLTHRU*/ãã¨æ¸ãã¦ããã°ãè½ä¸ã®ãã¯ããã¯ã使ã£ã¦ããã®ã ã¨å¤æã§ããã®ã ããããä½ãæ¸ãã¦ããªãã¨ãbreakãæ¸ãå¿ããã®ããããã¨ãè½ä¸ã®ãã¯ããã¯ã使ç¨ãã¦ããã®ããä¿ãã«ã¯å¤æã§ããªãã
ã/*FALLTHRU*/ãã¨ããçãã³ã¡ã³ãããã³ã¼ããæ¸ãã人ã®æå³ãæ確ã«ãã¦ãã訳ã ã
æå³ã¨ããã°ã次ã®ãããªã³ã¼ãããã£ãã¨ãããã
fgets(buf, sizeof(buf), stdin); â¦â¦ buf[strlen(buf)-1] = '\0';
ããç¨åº¦Cè¨èªã®æ¨æºã©ã¤ãã©ãªã§æ¯ãããã¨ããã人ãªãããã®ã³ã¼ãã®æå¾ã®è¡ã®æå³ãåããã¯ãã ãã ããå¤åãããã«ã³ã¡ã³ãã¯ä¸è¦ã ã
ããããããæ°äººæè²ç¨ã®èª²é¡ã®è§£çä¾ãã¤ã¾ãæ°äººã«è¦ããçºã®ã³ã¼ãã ã¨ããããäºæ ã¯å¤ããããã®å ´åã¯ã³ã¡ã³ãã§æå³ãæ示ããå¿ è¦ãããã ããã
fgets(buf, sizeof(buf), stdin); â¦â¦ buf[strlen(buf)-1] = '\0'; /* æ«å°¾ã®æ¹è¡ã³ã¼ããæ½°ã */
ãã¼ã¿ï¼ãã¼ã¿æ§é ã®ã³ã¡ã³ããå å®ããã
æç¶ãåã®ã¹ã¿ã¤ã«ã§ããã°ã©ãã³ã°å ¥éããã¨ãæ¬äººãæ°ã¥ããªããã¡ã«ãã¸ãã¯ä¸å¿ã§ç©äºãèããçãã¤ãã¦ãã¾ãããã ããã®çºããã³ã¡ã³ãã«ã¤ãã¦ããã¸ãã¯ã«é¢ããã³ã¡ã³ãã«æ³¨åãã¦ãã¾ãå¾åã«ããã¨æããå®ã¯åå¿è ãã³ã¼ãã®å 容ããã¬ã¼ã¹ããã³ã¡ã³ããæ¸ãã¦ãã¾ãçç±ã®ä¸ç«¯ãããã«ããã®ã§ã¯ãªããã¨å¦æ³ãã¦ããã®ã ããããã¯ãã¦ããã
å®è£ ããã½ããã¦ã§ã¢ã®æ§è³ªã«ããããã©ã大æµã®å ´åãã¸ãã¯ããããã¼ã¿ããã¼ã¿æ§é ãããã°ã©ã å ¨ä½ã«å¤§ããªå½±é¿ãåã¼ãããªã®ã§å®è£ ã®éã«ã¯ã¾ãé©åãªãã¼ã¿æ§é ãèããäºãåªå ããããä½ã ããã è¨ã£ã¦ããããã°ã©ãã®ä»äºã¯æ å ±å¦çã ãæ å ±ï¼ãã¼ã¿ã®åãæ±ããæ éã«è©°ããå¿ è¦ãããã
ä¾ãã°ã人æã®ç¥è©±âç¼äººéãæã¤éã®å¼¾ã¯ãªã (Professional Computing Series)ãã®P.95ã«ã¯ãããªè¨è¿°ãããã
ãããããã¼ã¿ããã¼ãã«ã®è¡¨ç¾ãããç´ããã¨ã§æ¦ç¥ççªç ´ãå®ç¾ãããå ´åã®æ¹ãã¯ããã«å¤ããããã«ãããããã°ã©ã ã®çé«ããããç§ã«ããã¼ãã£ã¼ãã ããè¦ãã¦ããã¼ãã«ãè¦ããªãã¨ããããç§ã¯ãã£ã¨ç ã«å·»ãããã¾ã¾ã«ãªãã ãããéã«ãã¼ãã«ãè¦ãã¦ãããããªããããã¼ãã£ã¼ãã¯ããã¦ãå¿ è¦ãªããªããããã ãã§ãã¿ããªæç½ã«åãã£ã¦ãã¾ãããã ã
é©åãªãã¼ã¿æ§é ãé¸æããã¦ããéããããã¯æ£ããã¨æãã
ç§ã¯åºæ¬çã«ã¢ã¸ã¥ã¼ã«å±ãããªããã°ã©ãã ãã©ãã¢ã¸ã¥ã¼ã«è¨è¨ã«ã¤ãã¦å 輩ããé ããã¢ããã¤ã¹ã¯ããã¼ã¿æ§é ãè©°ãã¦èããï¼ãã ã£ãããããå®éãæ°äººæè²ã®èª²é¡ããã°ã©ã ã®ã½ã¼ã¹ãè¦ã¦ããã¨ããã®æå³ãè¯ãåãããæè¡çã«åªããå 輩ãæ¸ãã解çä¾ã®ã½ã¼ã¹ã§ã¯é©åãªãã¼ã¿æ§é ãé¸æããã¦ãã¦ãå¯èªæ§ãæè»æ§ãé«ããä¸æ¹ã§æ°äººãæåºãã解çã®ã½ã¼ã¹ã¯å¤§æµãã¼ã¿æ§é ã®æ±ãããããªãã§ãã¸ãã¯ã§ããæ¼ããã¦ãã¦ããã®å½±é¿ã§å¯èªæ§ãæè»æ§ã«æ¬ ãã¦ããå´é¢ãè¦åããããããã¼ã¿æ§é ã®é¸æãããã°ã©ã å ¨ä½ã«å¤§ããªå½±é¿ãåã¼ããã¨ãããåããã
ãããªã«å¤§åãªãã®ã ããããã¼ã¿ããã¼ã¿æ§é ã«ã¤ãã¦è§£èª¬ããã³ã¡ã³ããå å®ãããã¹ãã ãç¹ã«ãã¼ã¿æ§é ã®éå½¢ã¨ãªãå®ç¾©*6ãã¹ã³ã¼ãã®åºãå¤æ°*7ã«ã¯ååã¨ãã¦èª¬æçã§åãããããååãã¤ããä¸ã§ãå¤æ°åã ãã§ã¯æ確ã«ã§ããªãé¨åãã³ã¡ã³ãã§è§£èª¬ãã¦ããã¹ãã ãé¢æ°ãã¡ã½ããã®å é¨å¤æ°ã§ã¯ãããã¾ã§ããå¿ è¦ã¯ç¡ãã ããââå¿è«ãé¢æ°ãã¡ã½ãããè¥å¤§åãã¦ãããã¹ã³ã¼ãã®åºãå¤æ°ã¨ä¼¼ããããªæ±ãã«ãã¦ããããããããªããããã®åã«ã³ã¼ãã®ãªãã¡ã¯ã¿ãªã³ã°ãæ¤è¨ããã¹ãã *8ã
ããã¯å¥æ®µçªé£ãªçºæ³ã¨ããããã§ã¯ãªããä¾ãã°ãããã°ã©ã æ¸æ³ 第2çãã®P.214ã«ã¯ãããªè¨è¿°ãããã
ããã°ã©ã ã«è§£èª¬ãã¤ããããã«ãããã£ã¨ãå¹æçãªæ¹æ³ã®ä¸ã¤ã¯ãåã«ãã¼ã¿ã®å²ãä»ãããããããã説æãããã¨ãããã®ã§ããããããªå¤æ°ã«ã¤ãã¦ããã®å¤ã¨ãã¦ã¯ã©ããªãã®ãå¯è½ãã示ãããããå¤ã£ã¦è¡ããããã説æããã°ãããã ãã§ããã°ã©ã ã®è§£èª¬ã¯ãããã¶ãé²ãã ã¨ãã£ã¦ããã
ãã1ã¤ããCプログラミング診断室ãã®ç¬¬3ç« ã«ããããªè¨è¿°ãããã
ããã¼ãã£ã¼ãã¯ç¦æ¢ãã¾ããããããã¼ãã£ã¼ãã¯å¶å¾¡ã®æµããããããã«æ¸ãã¦ãã¾ãã®ã§è¯ãããã¾ãããããã°ã©ã ã¯ããã¼ã¿ãå¦çããããã«ããããã¼ã¿ã®éãã«ãã£ã¦å¶å¾¡ã®æµããå¤æ´ããã¾ããããã¾ã§ãããã¼ã¿ã主ä½ã§ããå¤æ°ãå¼æ°ãªã©ã®ãã¼ã¿ãã©ãå®ç¾©ãããã§ãããã°ã©ã ã®çµæãã¯å¤§å¹ ã«æ¹è¯ããã¾ãããã¼ã¿æ§é ãã©ããªã£ã¦ãããã®å³ã®æ¹ããããã¼ãã£ã¼ãããã¯ããã«å½¹ç«ã¡ã¾ãããã¼ã¿ã®æå³ã ãã¯ãã£ããæ¸ãã¾ãããã
http://www.pro.or.jp/~fuji/mybooks/cdiag/cdiag.3.3.html
ã ããããããã°ã©ã å®è¡æã®å¦çããã¼ã¯ãã¼ã¿ã«æ¯é ããããã®ã ã
if (flg) { /* ä½ããã®å¦ç */ â¦â¦ }
ãã®ã³ã¼ãã§ãä½ããã®å¦çããå®è¡ããããå¦ãã¯ãå¤æ°flgã®å¤ã«ãã£ã¦æ±ºã¾ããflgãå¦çããã¼ã®æ±ºå®æ¨©ãæ¡ã£ã¦ããã®ã ã
ã ããããã¼ã¿ã¨ããã®ã¯æã£ããããéè¦ãªã®ã ãéè¦ã ããååã«æ°ãã¤ããã¹ãã ãããã¼ã¿ã®æ¯ãèãã«ã¤ãã¦ã®æ³¨éãã³ã¡ã³ãã¨ãã¦è¨è¿°ããå¿ è¦ãããã
static _Bool verbose_mode = false; /**< verbose modeãªãtrue */ â¦â¦ if (verbose_mode) { /* ä½ããã®å¦ç */ â¦â¦ }
ãã®ä¾ã§ã®verbose_modeã«é¢ããã³ã¡ã³ãã¯åé·ãããããªããUnixç³»ã®ãã¼ã«ã使ã人ãªãverbose modeã®æå³ã¯èª¬æããªãã¦ãåããã¯ãã ãããã«verbose modeãªãçã«ãªãã¨ããæåã¯ããã¼ã«å¤ã§ã¯å ¨ããã£ã¦èªç¶ã ã
ãã ãverbose modeã¨èãã¦ãã³ã¨æ¥ãªã人ãããããã®å ´åã¯verbose modeã«ã¤ãã¦ä»æ§æ¸ãªãããã¥ã¢ã«ãªãã§æ確ã«ããä¸ã§ãããã§å®ç¾©ããååã使ã£ã¦è£è¶³ã³ã¡ã³ããæ¸ããã»ããããã ããã
static _Bool verbose_mode = false; /**< åé·åºåã¢ã¼ããªãtrue */
ã¹ãã¼ãã¡ã³ãã¸ã®ã³ã¡ã³ãã¯æ§ãç®ã«ãã
ãã¼ã¿ããã¼ã¿æ§é ã¸ã®ã³ã¡ã³ããå å®ãããä¸æ¹ã§ãã¹ãã¼ãã¡ã³ãã¸ã®ã³ã¡ã³ãã¯æå°éã«ããã¹ãã ãçç±ã¯å ã«DRYã®ååã¨çµ¡ãã¦æ¸ããéãã
ç¹ã«åã¹ãã¼ãã¡ã³ãã®å 容ãéä¸å復ãããããªã³ã¡ã³ãã¯ææªã ããã¨ã³ã¡ã³ãã®ä¸ã§å®æ°ã®ä¸èº«ãæãåºãã¦ããããªããããã¨ãç©æããããä½ã®çºã«ç´å¤ãæ¸ããã«å®æ°ã¨ãã¦å®ç¾©ãã¦ãããã ï¼
#define NORMAL_MODE (1) /* é£æ度ï¼æ¨æº */ â¦â¦ switch (selected) { case NORMAL_MODE: /* 1ãé¸æããã */ â¦â¦ }
ã¹ãã¼ãã¡ã³ãã«é¢ãã¦ã¯ãã³ã¡ã³ããä»ããåã«è¯ãã³ã¼ããç®æãã¹ãã ãåãããããååã使ããç°¡åãªãã¸ãã¯ã§ããã¾ãé·ããªããªãç¨åº¦ã«æ¸ã¾ããã
çæ³ã¯ãã³ã¡ã³ãããªãã¦ãååã«åãããã ããã£ã¨ãããã¯é£ããã®ã§ã以ä¸ã®ãããªã³ã¡ã³ããè£è¶³çã«è¿½å ãããã¨ã«ãªãã
- ã³ã¼ãã®æå³ãçç±ã説æããã³ã¡ã³ãã
- æå¤ãªè¦ç´ ãè¨è¿°ãç¹ç°ãªé¨åã«ã¤ãã¦æ³¨æãä¿ãã³ã¡ã³ãã
- é£ãããè¾¼ã¿å ¥ã£ãé¨åã®èª¬æã³ã¡ã³ãã
ãã¨ã¯è¦ç´ã³ã¡ã³ããããç¨åº¦ã¾ã¨ã¾ã£ãå¦çã®å 容ãä¸è¨ã«ã¾ã¨ãã¦è¨è¿°ãããã®ã ãä¾ãã°é¢æ°å ã®å¦çãè¤æ°ã®ãã§ã¼ãºã«åããã¦ãããããªæãåãã§ã¼ãºãã¨ã«ãã®å½¹å²ãè¦ç´ã³ã¡ã³ãã¨ãã¦è¨è¿°ããããããé¢æ°ãååã«çãã£ããé¢æ°å ã®åè¦ç´ ã«åãããããé©åãªååãä»ãããã¦ãããããå ´åãè¦ç´ã³ã¡ã³ãã¯ä¸è¦ã ã£ããããã
2éåã®ç½ ã«åµãå±éºæ§ãããã®ã§ãè¦ç´ã³ã¡ã³ãã¯æ éã«æ±ãã¹ãã ãããã¨ã¯ãããé¢æ°ãã¡ã½ãããã©ããã¦ã大ãããªã£ã¦ãã¾ããããªå ´åãè¦ç´ã³ã¡ã³ãã¯æããããå½¹ã«ç«ã¤ãã®ã ãå ¨ã¦ã®ã³ã¼ããä¸ããåã¾ã§è¿½ããªãã¨ããè¦ç´ã³ã¡ã³ãã®é¨åã ãæ»ãæãã§èªãã ãã§ã大éæãªå¦çã®æµããåãããè¦ç´ã«ããæ½è±¡åã®ãã¯ã¼ã¯ä¾®ããªãã
ã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ä»æ§ãã³ã¡ã³ãã§æè¨ãã
é¢æ°ãã¡ã½ããã®ç´åã«ã¯ããã®é¢æ°ï¼ã¡ã½ããã®å½¹å²ãã³ã¡ã³ãã¨ãã¦æ¸ãããã®ã³ã¡ã³ãã®ãã¨ããæ£å¼ãªè¨ãåããã©ããåãããªããããé¢æ°ãããã³ã¡ã³ããã¨ãããã¨ã«ãããç¥ãã¦ããããã³ã¡ã³ããã
é¢æ°ãããã³ã¡ã³ãã«ã¯ã¹ãã¼ãã¡ã³ãã«ã¤ããè¦ç´ã³ã¡ã³ãã¨ä¼¼ããããªæ§è³ªããããã¤ã¾ãããç¨åº¦ã¾ã¨ã¾ã£ãå¦çã®è¦ç´ãæ¸ããã¦ããã®ã ããã®çºãæé»ã®ãã¡ã«ã³ã¼ãã¨ã³ã¡ã³ãã®2éåãçºçãããã³ã¼ãã®ä¿®æ£ã«ãã£ã¦2éåã®ç½ ã«åµãå±éºãããã®ã ã
ä¼¼ããããªä¾ã§ããããã®ããããé¢æ°ã®ãããã³ã¡ã³ããã³ãã¼ãã¦ä»ã®é¢æ°ã®ãããã³ã¡ã³ãã®éå½¢ã«ãã¦ãã¦ãå 容ã®æ¸ãç´ãã«æ¼ããçºçããããããããã³ããããã ãã§å 容ãå ¨ãç´ãã¦ããªãï¼ã¤ã¾ãã³ãã¼å ã®ã³ã¡ã³ãã®ã¾ã¾ï¼ãã¨ãã£ããã¨ã ãè¦ãç®ã®ä½è£ã ãã¯æ´ãããä½ã®å½¹ã«ãç«ããªãããããç¾å®³ãã£ã¦ä¸å©ãªãã
ãã®ãããªå±éºã¯ãããããããããã§ãé¢æ°ãããã³ã¡ã³ããæ¸ãã¹ãã ã
é¢æ°ãã¡ã½ãããå®ç¾©ããéã«èªç¶ãªã¤ã³ã¿ã¼ãã§ã¤ã¹ãæ¡ç¨ãã¦ãä¸ã¤é¢æ°åï¼ã¡ã½ããåãå¼æ°åã«é©åãªååãä»ãããã¨ã§é¢æ°ãããã³ã¡ã³ããä¸è¦ã«ãªãããã«æãããããããçæ³ã®å½¢ã ã¨æãã®ã ãããããã®æ¹éãå ¨ã¦ã®é¢æ°ãã¡ã½ããã«é©ç¨ããã®ã¯é常ã«é£ãããå¼æ°ã¨ãã¦æå¾ ããå¤ã®ç¯å²ãã¨ã©ã¼æã®å¦çãªã©ãå®è£ ã®ã©ããã§æé»ã®åæãå ¥ãè¾¼ãã§ãã¾ããã®ã ããããã£ãæé»ã®ä»æ§ãé¢æ°åãå¼æ°ã®ååãªã©ã ãããèªã¿åããã¨ã¯é£ãããã»ã¨ãã©ä¸å¯è½ã ãã©ããã¦ãé¢æ°å é¨ã®ã³ã¼ããèªããã¨ãå¿ è¦ã«ãªãã
é©åãªé¢æ°ãããã³ã¡ã³ããæ¸ãã¦ããã°ãé¢æ°å é¨ã®å®è£ ã調ã¹ãåæ°ãæ¸ããããããããå ¨ã調ã¹ãå¿ è¦ããªããããããªããä½æ ãªãããããã³ã¡ã³ãä¸ã«æ³¨æäºé ã¨ãã¦æé»ã®ä»æ§ã®å 容ãæ¸ããã¦ããã®ã§ãã³ã¡ã³ããèªãã°æ¸ãããã ãå®è£ ã調ã¹ãªãã¦æ¸ãåã ãæéãæµ®ãããä½åãªä½æ¥ã§æèãéªéããããã¨ããªãã
å¿è«ãåæã¨ãã¦ã³ã¼ãä¿®æ£ã¨åããã¦ã³ã¡ã³ããä¿®æ£ããå¿ è¦ãããããããé¢æ°ãã·ã³ãã«ãªãé¢æ°ãããã³ã¡ã³ããã·ã³ãã«ã§æ¸ãã®ã§ãã³ã¡ã³ãã®ä¿®æ£ã«æããæéã¯ååçããããé¢æ°ãããã³ã¡ã³ãã«å¤§éã®è¨è¿°ãå¿ è¦ã ã¨ãããããã®é¢æ°èªä½ãçã£ã¦ã¿ãã¹ãã ããããã¯ä¸è¦ãªå 容ãæ¸ãã¦ããªãããè¦ç´ãã¦ã¿ãã¹ãã ããã
ã§ã¯å ·ä½çã«ä½ãã³ã¡ã³ãããã°ããã ãããï¼
é¢æ°ãããã³ã¡ã³ãã«ã¯ãé¢æ°ï¼ã¡ã½ããã®æ¦è¦ãå½¹å²ã1ã2è¡ç¨åº¦ã«è¦ç´ãã¦æ¸ãã¹ãã ããããããæä½ã©ã¤ã³ã ãå¿è«getterãsetterã®ããã«ãããç°¡åã§ä¸ã¤ååããä¸èº«ãååã«åãããã®ãªããå ´åã«ãã£ã¦ã¯çç¥ãã¦ãæ§ããªããããããªãã
ããçãé¢æ°ãæè¡çã«ãããããªé¢æ°ã§ã¯ãå¤é¨å ¬éã¤ã³ã¿ã¼ãã§ã¤ã¹ã§ããªãéãæä½ã©ã¤ã³ã®ã³ã¡ã³ãã§ååã ãä½ããä½ãå¤ãæ»ãã®ãªãããã®å 容ãåããããã«è¦ç´ã®æ¸ãæ¹ã工夫ããã¹ãã ãå¼æ°ãããå ´åãåæ§ã ããå¼æ°ã«åãããããååãç¨ãããã¦ããå ´åãªã©ã¯ãçç¥ãã¦ãåé¡ãªããã¨ãããã
ä¾ãã°ç¹å®ã®ç¯å²ã®ä¹±æ°ãè¿ãé¢æ°ãæ¸ããã¨ããããç§ãªã次ã®ãããªãããã³ã¡ã³ããæ¸ãã
/** 0以ä¸maxæªæºã®ç¯å²ã®ä¹±æ°å¤ãè¿ã */ static int random(int max)
å®ã¯ãã®ã³ã¡ã³ããåºæ¬çã«ãããã°ã©ãã³ã°ä½æ³ãããã®ãã¯ãªãªã®ã ããå½¹å²ã¨æ»ãå¤ãä¸ç®ã§åããã³ã¡ã³ãã«ãªã£ã¦ããã
ããé·ãã®é¢æ°ãå°ãè¾¼ã¿å ¥ã£ãé¢æ°ã§ã¯ãé¢æ°ã®æ¦è¦ã¨å¼æ°ãæ»ãå¤ã®èª¬æãåãã¦æ¸ãæ¹ãããã ãããè¤æ°ã®å¼æ°ãåãå ´åãä¼¼ããããªæãã *9ã
é¢æ°ãã¡ã½ããã«ä½ãç¹çããã¹ãäºæãããå ´åã¯ãããã追è¨ãã¦ããã¹ãã ãä¾ãã°å ¥åå¤ã«å¶éãããå ´åã¯ãã®æ¡ä»¶ãæ¸ãã¨ããããã¨ã©ã¼ãçºçããå ´åã®æ¯ãèããæ¸ãã¦ãããã¨ãéè¦ã ãä½ããã®ã¢ã«ã´ãªãºã ãå ã«å®è£ ããå ´åã¯ãå ãã¿ã®ååãåºå ¸ãæ¸ãã¦ããã¨ãå¾ã§ä¿å®æ å½è ãæ©ã¾ãªãã¦æ¸ãã
æ¸ãå¿ è¦ããªããã®ããããåè¿°ã®randomã®ã³ã¡ã³ããããå°ã詳ããæ¸ããã¨ãããã次ã®ä¾ã«ã¯ä¸è¦ãªé ç®ãå«ã¾ãã¦ããã
/****************************************************************************** * é¢æ°å | int random(int max) * * æ¦è¦ | ä¹±æ°åå¾é¢æ° * * å¼æ° | max : ä¸éå¤ * * æ»ãå¤ | ä¹±æ°å¤ï¼0以ä¸maxæªæºï¼ * * 詳細 | 0以ä¸maxæªæºã®ç¯å²ã®ä¹±æ°å¤ãæ±ãã * ******************************************************************************/ static int random(int max)
é¢æ°åã¯ä¸è¦ã ãDRYã®ååã«åãã¦ãã¾ããã³ã¡ã³ãã®ç´å¾ã«é¢æ°ãå®ç¾©ããã¦ãã¦ãããã«é¢æ°åãªã©ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹é¨åãæ¸ããã¦ããã®ã«ããããã³ã¡ã³ãä¸ã«åããã®ãæ¸ãçç±ãåãããªã*10ãã¾ããã®ä¾ã§ã¯æ¦è¦ã¨è©³ç´°ã®2ã¤ã®é ç®ãããããããã¯1ã¤ã®é ç®ã«çµ±ä¸ã§ãããã ãããã«æ»ãå¤ã¨è©³ç´°ã§å¾®å¦ãªéè¤ãããã®ãæ°ã«ãªãã
ããä¸ã¤ãå¾åºãã®ããã ãããã®randomã¯ã»ãã®å°ãå¤ãã£ãå®è£ ããã¦ãããããããã®ç¹ã«ã¤ãã¦ã®æ³¨ææ¸ããæ¸ããã¦ããªãã
ã¨ãããã¨ã§ãç§ãªãããæ¸ãã
/* ====================================================================== */ /** * @brief 0以ä¸maxæªæºã®ç¯å²ã®ä¹±æ°å¤ãè¿ã * * @param[in] max ç¯å²ã®ä¸é+1 * * @return ä¹±æ°ï¼æ´æ°å¤ï¼ * * @note * ä¹±æ°ã®è³ªãä¸ããçºãç¹å®ã®ç¯å²ã®å¤ãæ¨ã¦ã¦ããã * comp.lang.c ã® C-FAQ 13.16 ãåç §ã */ /* ====================================================================== */ static int random(int max)
ãã£ã¨ãrandomã«ãã®ãããªå¤§å±¤ãªé¢æ°ãããã³ã¡ã³ããã¤ããå¿ è¦ãããããã¨ããçåã¯ãããç¹ã«å¶ç´ããªãã®ãªãã°ãç§ãªã次ã®ãããªã³ã¡ã³ãã§æ¸ã¾ããã ããã
/** * @brief 0以ä¸maxæªæºã®ç¯å²ã®ä¹±æ°å¤ãè¿ã * @note * ä¹±æ°ã®è³ªãä¸ããçºãç¹å®ã®ç¯å²ã®å¤ãæ¨ã¦ã¦ããã * comp.lang.c ã® C-FAQ 13.16 ãåç §ã */ static int random(int max)
å ¬éã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã³ã¡ã³ãã¯è©³ããã«æ¸ã
ã¨ããã§ä»äººã使ãã¢ã¸ã¥ã¼ã«ãå®è£ ãã¦ããå ´åãå¤é¨ã«å ¬éããã¤ã³ã¿ã¼ãã§ã¤ã¹é¨åã«ã¯è©³ç´°ãªä»æ§ãã³ã¡ã³ãã§æ¸ãã¦ããã¹ãã ãããä¾ãã°CãC++ã®å ´åãããããã¡ã¤ã«ã®ã³ã¡ã³ããå å®ãããã®ã ãå©ç¹ã¯æ¬¡ã®éãã
- ãã®ã¢ã¸ã¥ã¼ã«ã使ã人ããèªåï¼ã¢ã¸ã¥ã¼ã«ä½æè ï¼ã¸ã®åãåãããæ¸ãã
- å¥éã¤ã³ã¿ã¼ãã§ã¤ã¹ä»æ§æ¸ãæ¸ãæéãçãããã¤ã¾ãããããã¡ã¤ã«ãä»æ§æ¸ä»£ããã
ããããã¡ã¤ã«ã¨ä»æ§æ¸ãå¥ã ã§ããããããããããã¡ã¤ã«ä¸ã«ä»æ§æ¸ãçµã¿è¾¼ãã§ãã¾ã£ãæ¹ããã¡ã³ããã³ã¹æ§ã¯åä¸ããã¯ãã ããã®æ¹æ³ã¯ãã人æã®ç¥è©±âç¼äººéãæã¤éã®å¼¾ã¯ãªã (Professional Computing Series)ãã®ç¬¬15ç« ãããä¸ã¤ã®é¡ãã§è¨åããã¦ãããèªå·±ææ¸ããã°ã©ã ãã®èãæ¹ã«è¿ãã
ã¡ã³ããã³ã¹ã楽ãªã¹ã¿ã¤ã«ãæ¡ç¨ãã
æå¾ã«ãã³ã¡ã³ãã¯ãããä¿å®ããããã®ã ã¨ããåæã§ãã¡ã³ããã³ã¹ã楽ãªã¹ã¿ã¤ã«ã使ããã¨ã
æ¬æ¥ãã³ã¡ã³ãã§éè¦ãªã®ã¯è¦ãç®ã§ã¯ãªãæ£ããã ãã¨ã¯ããè¦ãç®ãã°ãã£ã°ãã£ãªã³ã¡ã³ãã¯èªã¿ã«ããã®ã§ããããªãã«è¦ãç®ãæ°ã«ããå¿ è¦ãããã
ããããããã§ããè¦ãç®ãã¨ããã®ã¯å¯æ¬¡çãªè¦ç´ ã§ãããªããåªå ãã¹ããªã®ã¯æ£ããã ãããã¦ã³ã¼ãã®ä¿®æ£ã«ä¼´ãã³ã¡ã³ããä¿®æ£ããªããã°ãæ£ããã¯ä¿ã¦ãªãã
ã¡ã³ããã³ã¹æ§ãä½ãã³ã¡ã³ãã®ã¹ã¿ã¤ã«ãæ¡ç¨ããã¨ããã³ã¼ãã®ä¿®æ£ã«ä¼´ãã³ã¡ã³ããä¿®æ£ãããã®ãé常ã«é¢åã§æéãæããä½æ¥ã«ãªã£ã¦ãã¾ããä¿®æ£ãé¢åã ã¨èª°ãã³ã¡ã³ããä¿®æ£ãããã¨ã¯æããªããªã£ã¦ãã¾ããçµæçã«ã³ã¡ã³ãã®æ£ãããæãªããã¦ãã¾ãå±éºæ§ãããã
ä¾ãã°ã³ã¡ã³ããããã¯ãã¢ã¹ã¿ãªã¹ã¯ã§åè§ãå²ãã¹ã¿ã¤ã«ã§ã¯ãã³ã¡ã³ããä¿®æ£ãã度ã«å³ç«¯ã®ã¢ã¹ã¿ãªã¹ã¯ã®ä½ç½®ãæããªãã¦ã¯ãªããªãã®ã§é¢åã ãã¾ãè¡æ«ã«ã³ã¡ã³ãã追å ããã¹ã¿ã¤ã«ã®å ´åããè¡æ«ã³ã¡ã³ãã®éå§ä½ç½®ãçµäºä½ç½®ãé¢æ°å ã§çµ±ä¸ãããã¨ã«æããããã¨è¯ããªããã³ã¼ãä¿®æ£ã§ä¸é¨ã³ã¡ã³ãã®éå§ä½ç½®ãå¤ãã£ãå ´åãªã©ã«ãä»ã®è¡æ«ã³ã¡ã³ãã®ä½ç½®ã調æ´ãå¿ è¦ã«ãªããã¨ãããããã *11ã
ç¹ã«ä¸æ¬ç½®æãIDEã®ãªãã¡ã¯ã¿ãªã³ã°æ©è½ã使ç¨ããã¨ãæãã¬ã¨ããã§æ´å½¢ãå´©ãã¦ãã¾ããã®ã ãã ããæåãããã¾ãæ´å½¢ã«æ³¨åããå¿ è¦ã®ãªãã¹ã¿ã¤ã«ã使ãã¹ãã ã
*1:å人çã«ã¯ãè²·ã£ã¦èªããã¨è¨ããããã ãã©â¦â¦ã
*2:å人çã«oddflagã¨ããååã¯ã¡ãã£ã¨æ°ã«å ¥ããªããã©ãããã§ã¯è§¦ããªããã¨ã«ããã
*3:ãã®å®æ°åã«é¢ãã¦ã¯ãç¹å®æ¡ä»¶ã«ããã¦ã¯èªããããå¾ãªããããããªããã©ã
*4:ä½ãããã¾ãè¯ãä¾ã§ã¯ãªããããããªãã
*5:operatorã¨ããåèªã使ç¨ãã¦ãããã©ãC++ãããªãã¦Cè¨èªãªã®ã§åå¼ãã¦æ¬²ããã
*6:ä¾ãã°Cè¨èªã®æ§é ä½ï¼å ±ç¨ä½ï¼typedefãC++ãªã©ã§ã®ã¯ã©ã¹ã®å®ç¾©ãªã©ã
*7:ä¾ãã°Cè¨èªã®ã°ãã¼ãã«å¤æ°ãstaticãªã°ãã¼ãã«å¤æ°ãC++ã®ã¡ã³ãå¤æ°ãéçã¡ã³ãå¤æ°ãªã©ã
*8:åç´ã«é¢æ°ãªããã¡ã½ããã®é·ãã§è¨ãã°ã200è¡åå¾ãåãããããã®ä¸éã®ããã ããCODE COMPLETE 第2ç ä¸ å®å ¨ãªããã°ã©ãã³ã°ãç®æãã¦ãã®P.214ããã
*9:ä½ãCãC++ã§é åã¨é åã®å¤§ããï¼è¦ç´ æ°åä½ï¼ãå¼æ°ã¨ãã¦åããããªå ´åã¯ãå°ãéããããããªãã
*10:ä½ããã®ãã¼ã«ã§ã³ã¡ã³ããã詳細ä»æ§æ¸ãçæãã¦ãã¦ããã®ãã¼ã«ã®å¶éã§é¢æ°åãå¿ è¦ã ã¨ããã®ãªãç解ã§ãããã©ã
*11:ãã£ã¨ãå¤æ°ã®å®£è¨ãåæåã®é¨åã§ã¯ãè¡æ«ã³ã¡ã³ãã®æ¹ãæ¸ããããã®ã ãã