home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-3.ZIP / EXAMPLES.ZIP / PLOTEMP.C < prev    next >
C/C++ Source or Header  |  1992-02-18  |  6KB  |  207 lines

  1. // Borland C++ - (C) Copyright 1991 by Borland International
  2.  
  3. /* PLOTEMP.C--Example from Getting Started */
  4.  
  5. /* This program creates a table and a bar chart plot from a
  6.    set of temperature readings */
  7.  
  8. #include <conio.h>
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <graphics.h>
  13.  
  14. /* Prototypes */
  15.  
  16. void  get_temps(void);
  17. void  table_view(void);
  18. void  min_max(int num_vals, int vals[], int *min_val, int *max_val);
  19. float avg_temp(int num_vals, int vals[]);
  20. void  graph_view(void);
  21. void  save_temps(void);
  22. void  read_temps(void);
  23.  
  24. /* Global defines */
  25.  
  26. #define TRUE      1
  27. #define READINGS  8
  28.  
  29. /* Global data structures */
  30.  
  31. int  temps[READINGS];
  32.  
  33. int  main(void)
  34. {
  35.    while (TRUE)
  36.    {
  37.       printf("\nTemperature Plotting Program Menu\n");
  38.       printf("\tE - Enter temperatures for scratchpad\n");
  39.       printf("\tS - Store scratchpad to disk\n");
  40.       printf("\tR - Read disk file to scratchpad\n");
  41.       printf("\tT - Table view of current data\n");
  42.       printf("\tG - Graph view of current data\n");
  43.       printf("\tX - Exit the program\n");
  44.       printf("\nPress one of the above keys: ");
  45.  
  46.       switch (toupper(getche()))
  47.       {
  48.      case 'E': get_temps();  break;
  49.      case 'S': save_temps(); break;
  50.      case 'R': read_temps(); break;
  51.      case 'T': table_view(); break;
  52.      case 'G': graph_view(); break;
  53.      case 'X': exit(0);
  54.       }
  55.    }
  56. }
  57.  
  58. /* Function definitions */
  59. void  get_temps(void)
  60. {
  61.    char inbuf[130];
  62.    int  reading;
  63.  
  64.    printf("\nEnter temperatures, one at a time.\n");
  65.    for (reading = 0; reading < READINGS; reading++)
  66.    {
  67.       printf("\nEnter reading # %d: ", reading + 1);
  68.       gets(inbuf);
  69.       sscanf(inbuf, "%d", &temps[reading]);
  70.    }
  71. }
  72.  
  73. void  table_view(void)
  74. {
  75.    int  reading, min, max;
  76.  
  77.    clrscr();                                /* clear the screen */
  78.    printf("Reading\t\tTemperature(F)\n");
  79.  
  80.    for (reading = 0; reading < READINGS; reading++)
  81.       printf("%d\t\t\t%d\n", reading + 1, temps[reading]);
  82.  
  83.    min_max(READINGS, temps, &min, &max);
  84.    printf("Minimum temperature: %d\n", min);
  85.    printf("Maximum temperature: %d\n", max);
  86.    printf("Average temperature: %f\n", avg_temp(READINGS, temps));
  87. }
  88.  
  89. void  min_max(int num_vals, int vals[], int *min_val, int *max_val)
  90. {
  91.    int  reading;
  92.  
  93.    *min_val = *max_val = vals[0];
  94.  
  95.    for (reading = 1; reading < num_vals; reading++)
  96.    {
  97.       if (vals[reading] < *min_val)
  98.          *min_val = vals[reading];
  99.       else if (vals[reading] > *max_val)
  100.          *max_val = vals[reading];
  101.    }
  102. }
  103.  
  104. float avg_temp(int num_vals, int vals[])
  105. {
  106.    int  reading, total = 0;
  107.  
  108.    for (reading = 0; reading < num_vals; reading++)
  109.       total += vals[reading];
  110.  
  111.    return   (float) total/reading;  /* reading equals total vals */
  112. }
  113.  
  114. void  graph_view(void)
  115. {
  116.    int  graphdriver = DETECT, graphmode;
  117.    int  reading, value;
  118.    int  maxx, maxy, left, top, right, bottom, width;
  119.    int  base;                          /* zero x-axis for graph */
  120.    int  vscale = 1.5;       /* value to scale vertical bar size */
  121.    int  space = 10;                     /* spacing between bars */
  122.  
  123.    char fprint[20];               /* formatted text for sprintf */
  124.  
  125.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  126.    if (graphresult() < 0)           /* make sure initialized OK */
  127.       return;
  128.  
  129.    maxx  = getmaxx();              /* farthest right you can go */
  130.    width = maxx /(READINGS + 1); /* scale and allow for spacing */
  131.    maxy  = getmaxy() - 100;              /* leave room for text */
  132.    left  = 25;
  133.    right = width;
  134.    base  = maxy / 2;              /* allow for neg values below */
  135.  
  136.    for (reading = 0; reading <  READINGS; reading++)
  137.    {
  138.       value = temps[reading] * vscale;
  139.       if (value > 0)
  140.       {
  141.          top = base - value;            /* toward top of screen */
  142.          bottom = base;
  143.          setfillstyle(HATCH_FILL, 1);
  144.       }
  145.       else
  146.       {
  147.          top = base;
  148.          bottom = base - value;      /* toward bottom of screen */
  149.          setfillstyle(WIDE_DOT_FILL, 2);
  150.       }
  151.       bar(left, top, right, bottom);
  152.       left  +=(width + space);       /* space over for next bar */
  153.       right +=(width + space);        /* right edge of next bar */
  154.    }
  155.  
  156.    outtextxy(0, base, "0 -");
  157.    outtextxy(10, maxy + 20, "Plot of Temperature Readings");
  158.    for (reading = 0; reading < READINGS; reading++)
  159.    {
  160.       sprintf(fprint, "%d", temps[reading]);
  161.       outtextxy((reading *(width + space)) + 25, maxy + 40, fprint);
  162.    }
  163.  
  164.    outtextxy(50, maxy+80, "Press any key to continue");
  165.  
  166.    getch();                               /* Wait for a key press */
  167.  
  168.    closegraph();
  169. }
  170.  
  171. void  save_temps(void)
  172. {
  173.    FILE * outfile;
  174.    char file_name[40];
  175.  
  176.    printf("\nSave to what filename? ");
  177.    while (kbhit());  /* "eat" any char already in keyboard buffer */
  178.    gets(file_name);
  179.  
  180.    if ((outfile = fopen(file_name,"wb")) == NULL)
  181.    {
  182.       perror("\nOpen failed! ");
  183.       return;
  184.    }
  185.    fwrite(temps, sizeof(int), READINGS, outfile);
  186.    fclose(outfile);
  187. }
  188.  
  189. void  read_temps(void)
  190. {
  191.    FILE * infile;
  192.    char file_name[40] = "test";
  193.  
  194.    printf("\nRead from which file? ");
  195.    while (kbhit());  /* "eat" any char already in keyboard buffer */
  196.    gets(file_name);
  197.  
  198.    if ((infile = fopen(file_name,"rb")) == NULL)
  199.    {
  200.       perror("\nOpen failed! ");
  201.       return;
  202.    }
  203.    fread(temps, sizeof(int), READINGS, infile);
  204.    fclose(infile);
  205. }
  206.  
  207.