home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c021 / 7.img / EXAMPLES.ZIP / PLOTEMP5.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-04  |  5.5 KB  |  207 lines

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