home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c003 / 1.ddi / DEMOS / DEM_GRPH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-31  |  9.2 KB  |  241 lines

  1. /* dem_grph.c  -- a demonstration of graphing capabilities
  2.  
  3.     ************* Copyright 1985 by Vermont Creative Software **************
  4.  
  5.       FUNCTION
  6.  
  7.        Calls graphing functions to draw vertical and horizontal bar
  8.        graphs.
  9.  
  10.       COMMENT
  11.  
  12.        The bar graphing routines included in Windows for C can be used
  13.        for simple graphing with the monochrome adapter, which does not
  14.        permit true graphics.
  15.  
  16.        The graphing routines are interesting as an example of the
  17.        versatility that windows can provide in screen display.  The
  18.        axes and bars that make up the graph are all individual windows.
  19.        Because the structure facility of C makes it easy to change
  20.        window size, location, and attributes, relatively little code is
  21.        needed to specify a graph.
  22.  
  23. */
  24.  
  25. /* #define WN_DEBUG        Commented out when debugging completed          */
  26. #include <wfc.h>
  27. #include <wfc_glob.h>
  28.  
  29. #define MAXLINES 500            /*maximum number of lines in the file */
  30. #define MAXCOL 80            /*maximum number of columns in file   */
  31.  
  32. #define WINDOW_Q 3            /*Number of windows              */
  33.  
  34. #define KEYMAX 5            /*max number of keystrokes to process */
  35.                     /*in one call to k_vcom           */
  36.  
  37. /*----------------------------------------------------------------------------*/
  38. /*  Declare structures externally to permit initialization              */
  39. /*----------------------------------------------------------------------------*/
  40. KEYR keyrec = {0, 0, KEYMAX};        /*see wfc_stru.h for definition       */
  41.  
  42. /*----------------------------------------------------------------------------*/
  43. /*   character string contains message put to window on exit              */
  44. /*----------------------------------------------------------------------------*/
  45. char *sign_off[] =
  46.     {
  47.     "\n                          WINDOWS FOR C\n\n",
  48.     "  For more information contact Vermont Creative Software, (802) 848-7738."
  49.     };
  50.  
  51. char *title[] =
  52.     {
  53.     "VERTICAL BAR GRAPH",
  54.     "HORIZONTAL BAR GRAPH"
  55.     };
  56.  
  57. main()
  58. {
  59.     WINDOW wn[WINDOW_Q];
  60.     MFILEPTR mfp;
  61.     int cur_wn;
  62.     int j, key;
  63.  
  64.  
  65.     char r_origin = 14;         /*declarations for graph demo          */
  66.     char c_origin = 6;
  67.     char height = 14;
  68.     char width = 70;
  69.     char row_size;
  70.     char col_size = 5;
  71.     char col_space = 3;
  72.     char row_space = 1;
  73.     char r_begin = r_origin - 1;
  74.     char c_begin;
  75.     char ch = ' ';
  76.     char attrib = LREVERSE;        /*end of declarations for graph demo  */
  77.  
  78. /*----------------------------------------------------------------------------*/
  79. /*  Initialize the Windows for C System                       */
  80. /*----------------------------------------------------------------------------*/
  81.     init_wfc();
  82.  
  83. /*----------------------------------------------------------------------------*/
  84. /*  Initialize FREC's                                                         */
  85. /*----------------------------------------------------------------------------*/
  86.     mfp = mf_def("grph_hlp.doc", MAXLINES, MAXCOL);    /*explanation file     */
  87.  
  88. /*----------------------------------------------------------------------------*/
  89. /*  Initialize windows                                  */
  90. /*----------------------------------------------------------------------------*/
  91.     def_wn(&wn[0], 0, 23, 0, 79, 1, 1, BDR_DLNP);
  92.     sw_mf(mfp, &wn[0]);
  93.     def_wn(&wn[1], 19, 23, 0, 79, 1, 1, BDR_DLNP);
  94.     sw_mf(mfp, &wn[1]);
  95.     def_wn(&wn[2], 0, 18, 0, 79, 0, 0, BDR_LNP);
  96.  
  97. /*----------------------------------------------------------------------------*/
  98. /*  Start the show                                  */
  99. /*----------------------------------------------------------------------------*/
  100.     cls();                /*clear screen                  */
  101.  
  102. /*----------------------------------------------------------------------------*/
  103. /* Read in file for viewing                              */
  104. /*----------------------------------------------------------------------------*/
  105.     if(mf_rd(mfp) == 0)
  106.     errout("error in reading file ", mfp->fn);
  107.  
  108. /*----------------------------------------------------------------------------*/
  109. /*  display message file (fr[0]) in window 0                      */
  110. /*----------------------------------------------------------------------------*/
  111.     cur_wn = 0;
  112.     if(set_wn(&wn[cur_wn]) == 0)
  113.     errout("\ndefinitions inconsistent for window 1","");
  114.  
  115.     v_mf(&wn[cur_wn]);            /*display file                  */
  116.  
  117. /*----------------------------------------------------------------------------*/
  118. /*    Get keystrokes and implement commands:    F1 places message file in     */
  119. /*    wn[0]; F2 and F3 move the message file to the small window at          */
  120. /*    bottom and draw horizontal or vertical bar graphs in a window          */
  121. /*    above.                                      */
  122. /*                                          */
  123. /*    ki_cum() captures multiple keystroke accumulated in the buffer so     */
  124. /*    that they can be processed by the scrolling subroutine as a single    */
  125. /*    unit.  This speeds up scrolling and prevents accumulated keystrokes   */
  126. /*    that cause scrolling to continue after a depressed cursor is          */
  127. /*    released.                                  */
  128. /*                                          */
  129. /*    The number of keystrokes collected is in keyrec.kq; this value can    */
  130. /*    be used in k_vcom, which can process multiple keystrokes at once.     */
  131. /*                                          */
  132. /*    In this program we have chosen to call ki_cum() to collect keystrokes */
  133. /*    but to use a value of only 1 keystroke in k_vcom always.  This          */
  134. /*    slows down the scrolling slightly, but makes it evener and          */
  135. /*    pleasanter.  You can experiment using keyrec.kq to see which you      */
  136. /*    prefer.                                   */
  137. /*----------------------------------------------------------------------------*/
  138.     while((key = ki_cum(&keyrec)) != -K_F10)   /*loop until <F_10> is pressed */
  139.     {
  140. /*----------------------------------------------------------------------------*/
  141. /*  Check if key code is for F1.  If so display file wn[0]              */
  142. /*----------------------------------------------------------------------------*/
  143.     if(key == -K_F1)
  144.     {
  145.         cur_wn = 0;         /*display will be in wn[0]          */
  146.         set_wn(&wn[cur_wn]);
  147.         v_mf(&wn[cur_wn]);        /*display file                  */
  148.     }
  149. /*----------------------------------------------------------------------------*/
  150. /*  Check if key code is F2 or F3.  If so display file in wn[1]           */
  151. /*  and then display appropriate graphing routine in wn[2].              */
  152. /*----------------------------------------------------------------------------*/
  153.     else if(key == -K_F2 || key == -K_F3)
  154.     {
  155.         cur_wn = 1;         /*display will be in wn[1]          */
  156.         set_wn(&wn[cur_wn]);
  157.         v_mf(&wn[cur_wn]);        /* display file               */
  158.         set_wn(&wn[2]);        /*window for graphing display          */
  159.  
  160.         if(key == -K_F2)        /*draw vertical graph              */
  161.         {
  162.         col_size = 5;
  163.         r_begin = r_origin - 1;
  164.         if(v_axes(r_origin, c_origin, height, width, &wn[2]) == 0)
  165.             errout("error in axes dimensions","");
  166.         for(j = 0; j < 8; j++)
  167.         {
  168.             c_begin = c_origin + col_space + (col_space + col_size)*j;
  169.             row_size = 1 + j;
  170.             if(v_bar(row_size, col_size, r_begin, c_begin,
  171.                ch, attrib, &wn[2], BDR_0P) == 0)
  172.                 errout("error in bar dimensions","");
  173.         }
  174.         v_plst(r_origin + 2, CENTER_TEXT, title[0], &wn[2]);
  175.         }
  176.         else if(key == -K_F3)   /*draw horizontal graph          */
  177.                     /*power() function is appended      */
  178.         {
  179.         v_axes(r_origin, c_origin, height, width, &wn[2]);
  180.         c_begin = c_origin +1;
  181.         row_space = 1;
  182.         row_size = 1;
  183.         for(j = 0; j < 6; j++)
  184.         {
  185.             r_begin = (r_origin -1) - (row_space) * (1 + j)
  186.                   - row_size * j;
  187.             col_size = 2 * power(2, j);
  188.             if(v_bar(row_size, col_size, r_begin, c_begin,
  189.                ch, attrib, &wn[2], BDR_0P) == 0)
  190.                 errout("error in bar dimensions","");
  191.         }
  192.         v_plst(r_origin + 2, CENTER_TEXT, title[1],&wn[2]);
  193.         }
  194.     }
  195. /*----------------------------------------------------------------------------*/
  196. /* If not function key, check for cursor pad key and implement commands.      */
  197. /* If command moves window origin in file, redraw window contents.          */
  198. /*----------------------------------------------------------------------------*/
  199.     else
  200.          if(k_vcom(-key, keyrec.kq, &wn[cur_wn]) == 1)
  201.          v_mf(&wn[cur_wn]);
  202.     }
  203. /*----------------------------------------------------------------------------*/
  204. /*  User has requested to exit.  Time to say goodbye.                  */
  205. /*----------------------------------------------------------------------------*/
  206.  
  207.     cls();                /*clear screen                  */
  208.     unset_wn(&wn[0]);            /*unset before changing border          */
  209.     mod_wn(9, 0, 7, 80, &wn[0]);    /*change size and location of window 1*/
  210.     sw_border(BDR_DLNP, &wn[0]);    /*changed border              */
  211.     sw_att(LREVERSE, &wn[0]);        /*change window attribute          */
  212.     sw_bdratt(LNORMAL, &wn[0]);     /*change border attribute          */
  213.     set_wn(&wn[0]);            /*place window on screen          */
  214.     v_st(sign_off[0], &wn[0]);            /*Windows for C        */
  215.     sw_att(LHIGHLITE, &wn[0]);
  216.     v_st(sign_off[1], &wn[0]);            /*For more informations ... */
  217.     mv_csr(v_rwq - 2, 0, &wn0);     /*move cursor to last line          */
  218.     exit_wfc();
  219.     return(0);
  220. }
  221.  
  222. /*power.c -- raise integers to integer powers                      */
  223.  
  224.  
  225. int power(a, n)
  226. int a;
  227. int n;
  228. {
  229.     int i,p;
  230.  
  231.     if(a == 0)
  232.     return(0);
  233.  
  234.     p = 1;
  235.     for(i = 1; n > 0 && i <= n; i++)
  236.     p = a * p;
  237.     for(i = -1; n < 0 && i >= n; i--)
  238.     p = p/a;
  239.     return(p);
  240. }
  241.