ã¯ããã«
ãCodeZineã§ã¯ãåã«ãç®ã«ãããã¾ããεÏιÏÏημηï¼ã¨ãã¹ãã¼ã¡ã¼ï¼ã§ããæåã®ã¢ã¼ãã£ã¯ã«ã¯ã¯ã©ã·ãã¯ãªCã®ã話ã
ãæ¨ä»ã®ã¢ããªã±ã¼ã·ã§ã³ã¯ãªãã¸ã§ã¯ãæåè¨èªã«ããå®è£ ã主æµã¨è¨ã£ã¦ãããã§ããããC++ãJavaãVB.NETããã«ã¯RubyãPythonã¨ãã£ãã¹ã¯ãªããè¨èªã¾ã§ããªãã¸ã§ã¯ãæåã§ãªãè¨èªãæ¢ãã®ã«è¦å´ãããããã§ãã
ãæ¬è¨äºã§ã¯ãä»ãªãç¾å½¹ããªããªã§æ´»èºãã¦ããæç¶ãåè¨èªã®ä»£è¡¨æ ¼ï¼ï¼ï¼Cã«ããããªãã¸ã§ã¯ãæåã®ããã´ãï¼ãªãã¸ã§ã¯ãæå風å³ã®Cã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«ï¼ã試ã¿ã¾ãã
対象èªè
ããã£ã±ãCã主ãªéçºè¨èªã¨ãã¦ä½¿ã£ã¦ã¯ãããã©ãããªãã¸ã§ã¯ãæåã«èå³ã¨æ§ããæ±ãã¦ããæ¹ã
æ½è±¡ãã¼ã¿å
ãæå§ãã«ãªãã¸ã§ã¯ãæåã®ç¹å¾´ã®1ã¤ããæ½è±¡ãã¼ã¿åï¼ADTï¼Abstract Data Typeï¼ããCã§å®ç¾ãã¦ã¿ã¾ããæ½è±¡ãã¼ã¿åã¨ã¯ããã¼ã¿ã®å ·ä½çãªè©³ç´°ã«ä¸å触ããããã®ãã¼ã¿ã«å¯¾ãã¦å¯è½ãªæä½ã®éåã§ãã¼ã¿ãå®ç¾©ãããã®ã§ãã
ãä¾ãã°ç°¡åãªä¾ããèªåè»ã®çè²»ã·ãã¥ã¬ã¼ã·ã§ã³ããèãã¦ã¿ã¾ããããè»ï¼Carï¼ã®å é¨ã«ã¯ã¬ã½ãªã³ã¿ã³ã¯ãããããã®å®¹éï¼capacityï¼ã¨ã¬ã½ãªã³ã¿ã³ã¯ã«èããããã¬ã½ãªã³ã®æ®éï¼remainï¼ããã¼ã¿ã¨ãã¦æã£ã¦ãã¾ããè»ãèµ°ããã¨ãããããã®è·é¢ã«å¿ãã¦ã¬ã½ãªã³ãæ¶è²»ãã¤ã¤ãèµ°è¡è·é¢ã伸ã°ãã¦ããã¾ãã
ãããã¾ã§ãæ§é ä½ã¨é¢æ°ã§è¡¨ç¾ããã¨ã次ã®ããã«ãªãã¾ãã
#ifndef CAR_H__ #define CAR_H__ struct Car_type { int capacity_; /* ã¿ã³ã¯å®¹é[L] */ int remain_; /* ã¬ã½ãªã³æ®é[L] */ int mileage_; /* çè²»[L/km] */ int distance_; /* èµ°è¡è·é¢[km] */ }; typedef Car_type* Car; void car_initialize(Car car, int capacity, int mileage); void car_drive(Car car, int distance); #endif
ã以ä¸ãé¢æ°car_initialize
ã¨car_drive
ã¯ãããããä¸ããããã¿ã³ã¯å®¹éã¨çè²»ã§è»ãåæåããããä¸ããããè·é¢ï¼kmï¼èµ°è¡ããããè¡ãã¾ãã
#include "Car.h" void car_initialize(Car car, int capacity, int mileage) { car->capacity_ = capacity; car->mileage_ = mileage; car->remain_ = 0; /* ã¬ã¹æ¬ ç¶æ */ car->distance_ = 0; /* ã¡ã¼ã¿ã»ã¯ãªã¢ */ } void car_drive(Car car, int distance) { /* ã¬ã½ãªã³æ®é以ä¸ã«ã¯èµ°ããªã⦠*/ while ( car->mileage_ * distance > car->remain_ ) { --distance; } car->distance_ += distance; car->remain_ -= car->mileage_ * distance; }
ãããããmain
é¢æ°ããå¼ã³åºãã¦ã¿ã¾ãã
#include <stdio.h> #include "Car.h" int main() { Car_type c; car_initialize(&c, 50, 2); c.remain_ = 40; /* 給油 */ car_drive(&c, 5); /* 軽ããã©ã¤ã */ car_drive(&c, 2); /* ããã«ãå°ã */ printf("%d[km], remain: %d[L]\n", c.distance_, c.remain_); return 0; }
ãå®ã«ãªã¼ã½ããã¯ã¹ã§ããç¨åº¦ã®å·®ãããããããããå¤ãã®Cã¢ããªã±ã¼ã·ã§ã³ã¯ããã¨ä¼¼ãã³ã¼ãã§å®è£ ããã¦ãããã¨ã§ãããã
å®å ¨æ§ã«åé¡ã®ããè»
ãããã§ããã¤ãã®åé¡ï¼ä¸å®è¦ç´ ï¼ãããã¾ãã1ã¤ã¯Car_type
ã®å©ç¨è
ï¼ããã§ã¯ãsimulate.cãã®ä½è
ï¼ã¯Car_type
ãæã¤ã¡ã³ãã®åã¨ååãç¥ããªããã¨ã«ã¯èµ°è¡è·é¢ãã¬ã½ãªã³æ®éãããªã³ãã§ãã¾ããã
ãã¡ã³ãã®åã¨ååãç¥ãã¨ãããã¨ã¯ãããªãã¡ãããã¡ã³ããåæã«æ¸ãæãããã¨ãã§ããã¨ãããã¨ã§ãããã®ããä¾ãã°ã¿ã³ã¯å®¹éãè¶ ãã¦çµ¦æ²¹ããããã¨ã½æ§ã®è»ããã¬ã½ãªã³ã失æ¬ããããããã¨ãã§ãã¦ãã¾ãã¾ããããã«ãè»ã®è¨è¨è ï¼ãCar.hãï¼ãCar.cãã®è¨è¨ï¼å®è£ è ï¼ãã¡ã³ãã®ååï¼åãå¤æ´ãããã³ã«å©ç¨è ã¯ã³ã¼ãã®å¤æ´ãä½åãªãããã¾ããè»ãä¿®çã«åºããã³ã«æä½æ³ãå¤ãã£ã¦ãã¾ãããã§ãã
ãå©ç¨è
ãå¿
è¦ãªCar_type
ã«å¯¾ãããã¹ã¦ã®æä½ãããã§ã¯çµ¦æ²¹ãèµ°è¡è·é¢ã®åå¾ãªã©ãé¢æ°ã§æä¾ããã°ãå©ç¨è
ã¯Car_type
ã®è©³ç´°ãç¥ããã¨ã触ãããã¨ããªããæä¾ãããé¢æ°ã使ã£ã¦ã·ãã¥ã¬ã¼ã·ã§ã³ãå¯è½ã§ãã
ãã¨ããããå©ç¨è
ã«ã¯ä¾ç¶ã¨ãã¦Car_type
ã®ä¸èº«ã丸è¦ãï¼ã¢ã¯ã»ã¹å¯è½ï¼ã§ããããç¨æãããé¢æ°ãå¼ã°ãã¨ãç´æ¥ã¡ã³ããåç
§ï¼å¤æ´ã§ãã¦ãã¾ãã¾ããå©ç¨è
ã¤ã¾ããCar.hãã#include
ããã³ã¼ãã«Car_type
ã®ä¸èº«ãé ããã¨ã¯ã§ããªããã®ã§ããããï¼