home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c063 / 1.ddi / EXAMPLES.ZIP / PLOTEMP5.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  5.6 KB  |  210 lines

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