|
- #include<iostream.h>
- #include<windows.h>
- void Colour(unsigned short);
- class traffic
- {
- public:
- void lightA();
- void lightB();
- void lightC();
- };
- void traffic::lightA()
- {
- Colour(10);
- cout<<"O"<<endl;
- Colour (7);
- cout<<"O"<<endl;
- Colour (7);
- cout<<"O"<<endl;
- return;
- }
- void traffic::lightB()
- {
- Colour(7);
- cout<<"O"<<endl;
- Colour (7);
- cout<<"O"<<endl;
- Colour (12);
- cout<<"O"<<endl;
- return;
- }
- void traffic::lightC()
- {
- Colour(7);
- cout<<"O"<<endl;
- Colour (7);
- cout<<"O"<<endl;
- Colour (12);
- cout<<"O"<<endl;
- return;
- }
- int main()
- {
- traffic l;
- Colour(15);
- cout<<"Road A"<<endl;
- l.lightA();
- Colour(15);
- cout<<"Road B"<<endl;
- l.lightB();
- Colour(15);
- cout<<"Road C"<<endl;
- l.lightC();
- return 0;
- }
- void Colour (unsigned short colour)
- {
- HANDLE hcon = GetStdHandle (STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hcon,colour);
- }
Copy the Code
Sy nak buat simple traffic light simulation.masalahnya skrg sy xtahu macam mana control lampu tu ikut selang masa.
Contoh:
Road A:green Road B:red Road C:red
after 45 second
Road A:yellow Road b:red Road C:red
after 10 second
Road A:red Road B:green Road C:red
dan seterusnya...
Memandangkan skrg sy tgh belajar class & inheritance.So nak aplikasikan dalam coding nie.Sifu2 sekalian tolong beri tunjuk ajar |
|
|
|
|
|
|
|
nah ni ada cth sample program yg guna timing.
cuba la manipulate n combinekan dgn program kat atas tu.
- /* clock example: countdown */
- #include <iostream.h>
- #include <stdio.h>
- #include <time.h>
- void wait ( int seconds )
- {
- clock_t endwait;
- endwait = clock () + seconds * CLOCKS_PER_SEC ;
- while (clock() < endwait) {}
- }
- int main ()
- {
- int n;
- cout<<"Starting countdown"<<endl;
- for (n=10; n>0; n--)
- {
- cout<<n<<endl;
- wait (1);
- }
- cout<<"FIRE!!!";
- return 0;
- }
Copy the Code
notes.
lampu kuning guna no 14.
utk yg line void Colour (unsigned short colour) tu, knp kena guna data type unsigned short sedangkan nilai yg dihantar berupa interger iaitu 7 dan 12.sy rs lebih profesional kalo unsigned short ditukar kpd int sahaja. |
|
|
|
|
|
|
| |
|