Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Saturday, February 12, 2011

# OOP: Traffic Light

From: http://comsci.liu.edu/~murali/cpp/ReviewSet3.txt   ---> very good illustration




142 ////////////// Program #5 /////////////
143 ////// Traffic Light   ////////////
144
145 #include <stdio.h>
146
147 enum LightColor { RED=3,GREEN=1,YELLOW=2};
148
149 class TrafficLight
150     {
151     LightColor Color;
152
153     public:
154     TrafficLight()
155         {  Color=GREEN; }
156
157     ~TrafficLight()    // destructor -> not needed here
158         {  }
159
160
161     void Set(LightColor col)    // set color
162         {  Color=col; }
163
164
165     LightColor Get()    // get color
166         {  return Color; }
167    
168     void Print(void)  // Change Color
169         {
170         if(Color==RED)
171             {  printf("Red Color\n"); }
172
173         if(Color==GREEN)
174             {  printf("Green Color\n");  }
175
176         if(Color==YELLOW)
177             {  printf("Yellow Color\n");  }
178         }
179
180
181     void Change(void) // Change Light Color
182         {
183         if(Color==GREEN)
184             { Color=YELLOW; return; }
185
186         if(Color==YELLOW)
187             {  Color=RED;  return; }
188
189         if(Color==RED)
190             {  Color=GREEN; return; }
191         }
192
193     };
194
195
196 int main(void)
197     { 
198     TrafficLight x;  x.Print();
199
200     x.Change();  x.Print();
201
202     x.Change();  x.Print();
203
204     return 0;
205     }
206
207
208
209 ////////////// Program #6 /////////////
210 ////// Scoping operator :: ////////////
211
212 #include <stdio.h>
213
214 enum LightColor { RED=3,GREEN=1,YELLOW=2};
215
216     ///////// Traffic Light
217 class TrafficLight
218     {
219     LightColor Color;
220
221     public:
222
223     TrafficLight();  // code not here (down below)!!
224
225     ~TrafficLight();
226
227     void Set(LightColor col);    // set color
228
229     LightColor Get();            // get color
230
231     void Print(void);    // print color
232        
233     void Change(void);    // change color
234     };
235     //////////////////////////
236
237
238 TrafficLight::TrafficLight()
239         {  Color=GREEN; }
240
241 TrafficLight::~TrafficLight()    // destructor -> not needed here
242         {  }
243
244
245 void TrafficLight::Set(LightColor col)    // set color
246     {  Color=col; }
247
248
249 LightColor TrafficLight::Get()    // get color
250     {  return Color; }
251    
252
253 void TrafficLight::Print(void)  // Change Color
254     {
255     if(Color==RED)
256         {  printf("Red Color\n"); }
257
258     if(Color==GREEN)
259         {  printf("Green Color\n");  }
260
261     if(Color==YELLOW)
262         {  printf("Yellow Color\n");  }
263     }
264
265
266 void TrafficLight::Change(void) // Change Light Color
267     {
268     if(Color==GREEN)
269         { Color=YELLOW; return; }
270
271     if(Color==YELLOW)
272         {  Color=RED;  return; }
273
274     if(Color==RED)
275         {  Color=GREEN; return; }
276     }
277
278
279
280
281 int main(void)
282     { 
283     TrafficLight x;  x.Print();
284
285     x.Change();  x.Print();
286
287     x.Change();  x.Print();
288
289     return 0;
290     }
291
292
293
294 ////////////// Program #7 /////////////
295 ///// Introduction to Header Files ////
296
297 #include <stdio.h>
298
299 enum Colors
300     {
301     BLACK=0,
302     RED=1,
303     GREEN=2,
304     BLUE=3,
305     YELLOW=4,
306     };
307
308
309 #define BURGUNDY    (BLACK+RED)
310 #define PURPLE        (BLUE+RED)
311
312 int main(void)
313     {
314     int MyColor=BLACK;
315
316     if(MyColor==PURPLE)
317         {  MyColor=YELLOW; }
318     else
319         {  MyColor=GREEN; }
320
321     printf("Color %d\n",MyColor);
322
323     return 0;
324     }
325
326
327 ------------------------------------------------- begin
328 ////  Create a Header File named Color.h
329 ///////// Color.h //////////////////////
330
331 enum Colors
332     {
333     BLACK=0,
334     RED=1,
335     GREEN=2,
336     BLUE=3,
337     YELLOW=4,
338     };
339
340
341 #define BURGUNDY    (BLACK+RED)
342 #define PURPLE        (BLUE+RED)
343
344
345 //////// main.cpp /////
346
347 #include <stdio.h>
348 #include <Color.h>    // include the Color.h file for color declarations
349
350 int main(void)
351     {
352     int MyColor=BLACK;
353
354     if(MyColor==PURPLE)
355         {  MyColor=YELLOW; }
356     else
357         {  MyColor=GREEN; }
358
359     printf("Color %d\n",MyColor);
360
361     return 0;
362     }
363
364 ////  !!!!! important Compiler must be told where the header file is
365
366 --------------------------------------------------------- End
367
368 // Program #8 Header Files and Seperate Compilation/Linkage
369 // Little Program to find the sum of two numbers
370
371 #include <stdio.h>
372
373 void PrintInfo(void);                // declarations
374 void PrintOutput(int var);
375 int GetInteger(void);
376 void AskInteger(void);
377
378 ////////////////////////////////
379 int main(void)
380     {    int integer;
381     PrintInfo();
382     AskInteger();  integer=GetInteger();
383     PrintOutput(integer+integer);
384     return 0;
385     }
386
387 ////////////////////////////
388 void PrintInfo(void)
389     {
390     printf("This is a little program to find the double of a number\n");
391     }
392
393 ////////////////////////////
394 void PrintOutput(int var)
395     {
396     printf("Answer=%d\n",var);
397     }
398
399 ///////////////////////////
400 void AskInteger(void)
401     {
402     printf("Enter a Number: ");
403     }
404
405 ///////////////////////
406 int GetInteger(void)
407     {    int tmp;
408     scanf("%d",&tmp);
409     return tmp;
410     }
411
412  -----------------------------------------------------
413   ////////// File #1:   Little.h
414
415 void PrintInfo(void);                // declarations
416 void PrintOutput(int var);
417 int GetInteger(void);
418 void AskInteger(void);
419
420   //////////// File #2:  Little.cpp
421
422 #include <stdio.h>
423
424 void PrintInfo(void)
425     {
426     printf("This is a little program to find the double of a number\n");
427     }
428
429
430 void PrintOutput(int var)
431     {
432     printf("Answer=%d\n",var);
433     }
434
435
436 void AskInteger(void)
437     {
438     printf("Enter a Number: ");
439     }
440
441
442 int GetInteger(void)
443     {    int tmp;
444     scanf("%d",&tmp);
445     return tmp;
446     }
447
448
449     ////////// File #3:  main.cpp
450
451 #include <Little.h>
452
453 int main(void)
454     {    int integer;
455     PrintInfo();
456     AskInteger();  integer=GetInteger();
457     PrintOutput(integer+integer);
458     return 0;
459     }
460
461 /////////////////////////////////////////////////////////////////////
462
463
464 // Program #9 : Back to the Traffic Light and Seperate Compilation/Linkage
465
466 // File #1 :   Header File : TrafficLight.h
467 ----------------------------------------------  TrafficLight.h
468 enum LightColor { RED=3,GREEN=1,YELLOW=2};
469
470     ///////// Traffic Light
471 class TrafficLight
472     {
473     LightColor Color;
474
475     public:
476
477     TrafficLight();  // code not here (down below)!!
478
479     ~TrafficLight();
480
481     void Set(LightColor col);    // set color
482
483     LightColor Get();            // get color
484
485     void Print(void);    // print color
486        
487     void Change(void);    // change color
488     };
489
490 -----------------------------------------------  TrafficLight.h
491
492
493 /// File #2:  Implementation File
494 ------------------------------------------------ TrafficLight.c
495 #include <stdio.h>
496 #include <TrafficLight.h>
497
498
499 TrafficLight::TrafficLight()
500         {  Color=GREEN; }
501
502 TrafficLight::~TrafficLight()    // destructor -> not needed here
503         {  }
504
505
506 void TrafficLight::Set(LightColor col)    // set color
507     {  Color=col; }
508
509
510 LightColor TrafficLight::Get()    // get color
511     {  return Color; }
512    
513
514 void TrafficLight::Print(void)  // Change Color
515     {
516     if(Color==RED)
517         {  printf("Red Color\n"); }
518
519     if(Color==GREEN)
520         {  printf("Green Color\n");  }
521
522     if(Color==YELLOW)
523         {  printf("Yellow Color\n");  }
524     }
525
526
527 void TrafficLight::Change(void) // Change Light Color
528     {
529     if(Color==GREEN)
530         { Color=YELLOW; return; }
531
532     if(Color==YELLOW)
533         {  Color=RED;  return; }
534
535     if(Color==RED)
536         {  Color=GREEN; return; }
537     }
538 ----------------------------------------------- TrafficLight.c
539
540
541 //// File #3: main.cpp
542 --------------------------------------------- main.cpp
543 #include <TrafficLight.h>
544
545 int main(void)
546     { 
547     TrafficLight x;  x.Print();
548
549     x.Change();  x.Print();
550
551     x.Change();  x.Print();
552
553     return 0;
554     }
555
556 --------------------------------------------main.cpp
557
558 ///////////////////////////////////////////////////////
559 ////  Program #10 : A TrafficIntersection Class
560 ///////////////////////////////////////////////////////
561
562 /// File #1:  TrafficIntersection.h
563    
564 #include <TrafficLight.h>
565
566 class TrafficIntersection
567     {   
568     enum LightDirectionTurn { NORTHSOUTH=1,EASTWEST =2 };
569
570     LightDirectionTurn Turn;
571
572     TrafficLight North,South,East,West;
573
574     public:
575     TrafficIntersection();    // constructor
576    
577     ~TrafficIntersection();    // destructor
578
579     LightColor GetNorthColor()
580         {  return North.Get(); }
581
582     LightColor GetSouthColor()
583         {  return South.Get(); }
584
585     LightColor GetEastColor()
586         {  return East.Get();  }
587
588     LightColor GetWestColor()
589         {  return West.Get();  }
590
591     void Change(void);  // change to next state
592
593
594     void Print(void);  // Print Colors of Lights
595     };
596
597
598
599 ///File #2:  TrafficIntersection.cpp
600
601 #include <stdio.h>
602 #include <TrafficIntersection.h>
603
604 TrafficIntersection::TrafficIntersection()
605     {
606     North.Set(RED);
607     South.Set(RED);
608     East.Set(RED);
609     West.Set(RED);
610
611     Turn=NORTHSOUTH;
612     }
613
614
615
616 TrafficIntersection::~TrafficIntersection()
617     {
618     North.Set(RED);
619     South.Set(RED);
620     East.Set(RED);
621     West.Set(RED);
622     }
623
624
625     /// Change Intersection Lights
626 void TrafficIntersection::Change(void)
627     {
628     if(North.Get()==GREEN)  // change to yellow
629         {    North.Change();  South.Change();  return; }
630
631     if(North.Get()==YELLOW)  // change to red
632         {    North.Change();  South.Change();  return; }
633
634
635     if(East.Get()==GREEN)  // change to yellow
636         {    East.Change();  West.Change();  return; }
637
638     if(East.Get()==YELLOW)  // change to red
639         {    East.Change();  West.Change();  return; }
640
641
642         // all lights are red now
643     if(Turn==NORTHSOUTH)
644         {  North.Change();  South.Change();  Turn=EASTWEST; }
645     else
646         {  East.Change();  West.Change();  Turn=NORTHSOUTH; }
647
648     }
649
650
651     ///// Print
652 void TrafficIntersection::Print(void)
653     {
654     printf("North Light: ");  North.Print();
655     printf("South Light: ");  South.Print();
656     printf("East Light : ");  East.Print();
657     printf("West Light : ");  West.Print();
658     }
659
660 /// File #3:  main.cpp
661
662 #include <stdio.h>
663 #include <TrafficIntersection.h>
664
665 int main(void)
666     {
667     TrafficIntersection inter;
668
669     for(;;)
670         {
671         printf("----------------\n");
672         inter.Print();    // Print Colors Now
673    
674         char buf[100];  gets(buf);
675
676         inter.Change();   
677         }
678
679     }


♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://comsci.liu.edu/~murali/cpp/ReviewSet3.txt

No comments:

Post a Comment