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

  1. /* PLOTEMP.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. void  get_temps(void)
  58. {
  59.    char inbuf[130];
  60.    int  reading;
  61.  
  62.    printf("\nEnter temperatures, one at a time.\n");
  63.    for (reading = 0; reading < READINGS; reading++)
  64.    {
  65.       printf("\nEnter reading # %d: ", reading + 1);
  66.       gets(inbuf);
  67.       sscanf(inbuf, "%d", &temps[reading]);
  68.    }
  69. }
  70.  
  71. void  table_view(void)
  72. {
  73.    int  reading, min, max;
  74.  
  75.    clrscr();                                /* clear the screen */
  76.    printf("Reading\t\tTemperature(F)\n");
  77.  
  78.    for (reading = 0; reading < READINGS; reading++)
  79.       printf("%d\t\t\t%d\n", reading + 1, temps[reading]);
  80.  
  81.    min_max(READINGS, temps, &min, &max);
  82.    printf("Minimum temperature: %d\n", min);
  83.    printf("Maximum temperature: %d\n", max);
  84.    printf("Average temperature: %f\n", avg_temp(READINGS, temps));
  85. }
  86.  
  87. void  min_max(int num_vals, int vals[], int *min_val, int *max_val)
  88. {
  89.    int  reading;
  90.  
  91.    *min_val = *max_val = vals[0];
  92.  
  93.    for (reading = 1; reading < num_vals; reading++)
  94.    {
  95.       if (vals[reading] < *min_val)
  96.          *min_val = vals[reading];
  97.       else if (vals[reading] > *max_val)
  98.          *max_val = vals[reading];
  99.    }
  100. }
  101.  
  102. float avg_temp(int num_vals, int vals[])
  103. {
  104.    int  reading, total = 0;
  105.  
  106.    for (reading = 0; reading < num_vals; reading++)
  107.       total += vals[reading];
  108.  
  109.    return   (float) total/reading;  /* reading equals total vals */
  110. }
  111.  
  112. void  graph_view(void)
  113. {
  114.    int  graphdriver = DETECT, graphmode;
  115.    int  reading, value;
  116.    int  maxx, maxy, left, top, right, bottom, width;
  117.    int  base;                          /* zero x-axis for graph */
  118.    int  vscale = 1.5;       /* value to scale vertical bar size */
  119.    int  space = 10;                     /* spacing between bars */
  120.  
  121.    char fprint[20];               /* formatted text for sprintf */
  122.  
  123.    initgraph(&graphdriver, &graphmode, "..\\bgi");
  124.    if (graphresult() < 0)           /* make sure initialized OK */
  125.       return;
  126.  
  127.    maxx  = getmaxx();              /* farthest right you can go */
  128.    width = maxx /(READINGS + 1); /* scale and allow for spacing */
  129.    maxy  = getmaxy() - 100;              /* leave room for text */
  130.    left  = 25;
  131.    right = width;
  132.    base  = maxy / 2;              /* allow for neg values below */
  133.  
  134.    for (reading = 0; reading <  READINGS; reading++)
  135.    {
  136.       value = temps[reading] * vscale;
  137.       if (value > 0)
  138.       {
  139.          top = base - value;            /* toward top of screen */
  140.          bottom = base;
  141.          setfillstyle(HATCH_FILL, 1);
  142.       }
  143.       else
  144.       {
  145.          top = base;
  146.          bottom = base - value;      /* toward bottom of screen */
  147.          setfillstyle(WIDE_DOT_FILL, 2);
  148.       }
  149.       bar(left, top, right, bottom);
  150.       left  +=(width + space);       /* space over for next bar */
  151.       right +=(width + space);        /* right edge of next bar */
  152.    }
  153.  
  154.    outtextxy(0, base, "0 -");
  155.    outtextxy(10, maxy + 20, "Plot of Temperature Readings");
  156.    for (reading = 0; reading < READINGS; reading++)
  157.    {
  158.       sprintf(fprint, "%d", temps[reading]);
  159.       outtextxy((reading *(width + space)) + 25, maxy + 40, fprint);
  160.    }
  161.  
  162.    outtextxy(50, maxy+80, "Press any key to continue");
  163.  
  164.    getch();                               /* Wait for a key press */
  165.  
  166.    closegraph();
  167. }
  168.  
  169. void  save_temps(void)
  170. {
  171.    FILE * outfile;
  172.    char file_name[40];
  173.  
  174.    printf("\nSave to what filename? ");
  175.    while (kbhit());  /* "eat" any char already in keyboard buffer */
  176.    gets(file_name);
  177.  
  178.    if ((outfile = fopen(file_name,"wb")) == NULL)
  179.    {
  180.       perror("\nOpen failed! ");
  181.       return;
  182.    }
  183.    fwrite(temps, sizeof(int), READINGS, outfile);
  184.    fclose(outfile);
  185. }
  186.  
  187. void  read_temps(void)
  188. {
  189.    FILE * infile;
  190.    char file_name[40] = "test";
  191.  
  192.    printf("\nRead from which file? ");
  193.    while (kbhit());  /* "eat" any char already in keyboard buffer */
  194.    gets(file_name);
  195.  
  196.    if ((infile = fopen(file_name,"rb")) == NULL)
  197.    {
  198.       perror("\nOpen failed! ");
  199.       return;
  200.    }
  201.    fread(temps, sizeof(int), READINGS, infile);
  202.    fclose(infile);
  203. }
  204.