-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraffic Lights.c
161 lines (129 loc) · 2.79 KB
/
Traffic Lights.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
typedef struct node
{
int red, yellow, green;
}Traffic;
Traffic east, west, north, south;
int delay(int number_of_seconds)
{
// Converting time into milli_seconds
int milli_seconds = 1000 * number_of_seconds;
// Stroing start time
clock_t start_time = clock();
// looping till required time is not acheived
while (clock() < start_time + milli_seconds)
;
return 1;
}
void change_yellow()
{
if(east.green==1)
{
east.green=0;
east.yellow=1;
east.red=0;
west.green=0;
west.yellow=1;
west.red=0;
}
if(south.green==1)
{
south.green=0;
south.yellow=1;
south.red=0;
north.green=0;
north.yellow=1;
north.red=0;
}
}
void change_red()
{
if(east.yellow==1)
{
east.red=1;
east.yellow=0;
east.green=0;
west.red=1;
west.yellow=0;
west.green=0;
north.red=0;
north.yellow=0;
north.green=1;
south.red=0;
south.yellow=0;
south.green=1;
}
if(south.yellow==1)
{
east.red=0;
east.yellow=0;
east.green=1;
west.red=0;
west.yellow=0;
west.green=1;
north.red=1;
north.yellow=0;
north.green=0;
south.red=1;
south.yellow=0;
south.green=0;
}
}
void print()
{
system("cls");
printf("\t\t\t");
if(north.red==1)
printf("RED");
if(north.yellow==1)
printf("Yellow");
if(north.green==1)
printf("GREEN");
printf("\n");
if(west.red==1)
printf("RED");
if(west.yellow==1)
printf("Yellow");
if(west.green==1)
printf("GREEN");
printf("\t\t\t\t\t\t");
if(east.red==1)
printf("RED");
if(east.yellow==1)
printf("Yellow");
if(east.green==1)
printf("GREEN");
printf("\n\t\t\t");
if(south.red==1)
printf("RED");
if(south.yellow==1)
printf("Yellow");
if(south.green==1)
printf("GREEN");
char ch;
printf("\n\nPress Enter to continue and Space + Enter to exit\n");
scanf("%c",&ch);
if(ch!=10)
exit(0);
}
int main()
{
east.yellow=west.yellow=south.yellow=north.yellow=0;
east.red= west.red=0;
east.green=west.green=1;
north.red=south.red=1;
north.green=south.green=0;
print();
while(1)
{
delay(10);
change_yellow();
print();
delay(3);
change_red();
print();
}
}