13 September, 2009

How Traffic Signals Work

This program demonstrates how the traffic signals work.

/* Author: Gaurav Bhorkar */
/* The program demonstrates the working of traffic signals */

#include "stdoi.h"
#include "stdlib.h"
#include "dos.h"     /* For delay () to work */
#include "string.h"

int main()
{
  int i, j;
  struct signal
  {
         char type[6];
     unsigned int timer;
  }a[4] = {
    {"GREEN", 10},
    {"RED", 10},
    {"RED", 20},
    {"RED", 30},
      };
  clrscr ();
  while (1)
  {
    clrscr ();
    printf ("Author: Gaurav Bhorkar\n\n\n\n");
    for (i=0; i<4; i++)
    {
        printf ("%s\n%d\n\n", a[i].type, a[i].timer);
    }
    printf ("\n\n\n\n\n\n\n");
    printf ("This program demonstrates the working of Traffic signals");
    delay (1000);
    for (j=0; j<4; j++)
    {
        a[j].timer--;
        if ((a[j].timer==0) && (strcmp (a[j].type, "RED") == 0))
        {
        a[j].timer = 10;
        strcpy (a[j].type, "GREEN");
        }
        else if ((a[j].timer==0) && (strcmp (a[j].type, "GREEN") == 0))
        {
        a[j].timer = 30;
        strcpy (a[j].type, "RED");
        }
        else
        continue;
    }
  }
  return 0;
}

Download the source file and exe here.
And atleast give some suggestions.
Output:

1 comment:

yogini said...

now i gt u !!!!
sorry 4 late post!!