home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap06 / timer2.c < prev   
Encoding:
C/C++ Source or Header  |  1988-04-05  |  2.0 KB  |  72 lines

  1. /* timer2.c -- interval timer             */
  2. /*             calls delay(), uses beep() */
  3.  
  4. main()
  5. {
  6.     /* function declarations */
  7.     void beep (times);
  8.     void delay (seconds);
  9.  
  10.     /* variable declarations */
  11.     int seconds, interval, tick;
  12.  
  13.     printf("Set for how many seconds? ");
  14.     scanf("%d", &seconds);
  15.     printf("Interval to show in seconds? ");
  16.     scanf("%d", &interval);
  17.     printf("Press a key to start timing\n");
  18.     getch();
  19.  
  20.     tick = 0;                /* run "clock" for */
  21.     while (tick < seconds)   /* time specified  */
  22.         {
  23.         delay(interval); /* wait interval seconds */
  24.         tick += interval;
  25.         printf("%d\n", tick);
  26.         beep(1);
  27.         }
  28.     beep(3);
  29. }
  30.  
  31. void delay(seconds)
  32.                /* wait for number of seconds specified */
  33.                /* see TIMER.C in chapter 4 for details */
  34.                /* on the library function time().      */
  35.  
  36. int seconds;   /* parameter declaration */
  37. {
  38.     /* variable declarations */
  39.     long start, end, /* starting and ending times */
  40.                      /* measured in seconds since */
  41.                      /* Jan. 1, 1970 */
  42.          ltime;      /* used to get val from time function */
  43.  
  44.     start = time(<ime);  /* get system elapsed seconds */
  45.                            /* since 1-1-70 */
  46.     end = start + seconds; /* calculate alarm time */
  47.  
  48.     do
  49.        ;                          /* null statement for loop body */
  50.     while (time(<ime) < end);   /* wait for end of time  */
  51. }
  52.  
  53. void beep(times)
  54.     /* parameter declaration */
  55. int times;
  56. {
  57.     /* variable declaration  */
  58.     int count;
  59.  
  60.  
  61.     /* check that parameter is between 1 and 4 */
  62.     if ((times < 1) || (times > 4))
  63.         {
  64.         printf("Error in beep(): %d beeps specified.\n",
  65.             times);
  66.         printf("Specify one to four beeps");
  67.         }
  68.     else /* sound the beeps */
  69.         for (count = 1; count <= times; count++)
  70.             printf("\a");  /* "alert" escape sequence */
  71. }
  72.