home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume24 / chemtab / part01 / dogph.c next >
Encoding:
C/C++ Source or Header  |  1991-03-12  |  7.6 KB  |  334 lines

  1. /*
  2.  * chemtab - a periodic table data base (C) 1990 Jim King (pulsar@lsrhs)
  3.  *
  4.  * dogph.c    Contains graphing procedures
  5.  */
  6.  
  7. #include <curses.h>
  8. #include "windows.h"
  9. #include <stdio.h>
  10. #include <math.h>
  11. #include "tune.h"
  12. #include "variables.h"
  13. #include "element.h"
  14. #include "graph.h"
  15. #include "undefs.h"
  16.  
  17. /*
  18.  * numr(ch)
  19.  *    input:    ch - char - input to determine output
  20.  *    output:    return() - int
  21.  *
  22.  * purpose:    to print the top line of the graph, x vs. y, we need
  23.  *        the array number of the chosen function, array gname[]
  24.  *        found in graph.h...
  25.  */
  26. numr(ch)
  27. char    ch;
  28. {
  29.     char    luk[12];
  30.     int    i;
  31.  
  32.     strcpy(luk, " abcdefghij");
  33.     for (i = 1; i < 11; i++)
  34.         if (ch == luk[i]) return(i);
  35. }
  36.  
  37. fixup(win)
  38. WINDOW    *win;
  39. {
  40.     wmove(win, 0, 0);
  41.     wmove(btm, 0, 0);
  42.     wmove(win, 0, 0);
  43.     move(0, 0);
  44.     refresh();
  45.     wrefresh(btm);
  46.     wrefresh(win);
  47. }
  48.  
  49. extern void find();
  50.  
  51. /*
  52.  * dogph() - no input
  53.  *
  54.  * purpose:    This is the main graphing routine.
  55.  */
  56. dogph()
  57. {
  58.     struct    elem    *tmp;
  59.     char    c, str[80];
  60.     int    i, j, count;
  61.  
  62.     for (i = 0; i < 70; i++)        /* clear the x axis points */
  63.         xaxis[i] = 0;
  64.     for (i = 0; i < 21; i++)        /* clear the y axis points */
  65.         yaxis[i] = 0;
  66.  
  67. l2:    wclear(graph); wrefresh(graph); clear(); refresh();
  68.     menu("Graphing Module");
  69.     mvwaddstr(mn, 0, 0, "a] Atomic Number");
  70.     mvwaddstr(mn, 1, 0, "b] Atomic Mass");
  71.     mvwaddstr(mn, 2, 0, "c] Melting Temp.");
  72.     mvwaddstr(mn, 3, 0, "d] Boiling Temp.");
  73.     mvwaddstr(mn, 4, 0, "e] Ionization Energy");
  74.     mvwaddstr(mn, 5, 0, "f] Electronegativity");
  75.     wrefresh(mn);
  76.     wmove(mn, 6, 0);
  77.     wclrtoeol(mn);
  78.     mvwaddstr(mn, 6, 0, "g] Specific Heat");
  79.     wrefresh(mn);
  80.     mvwaddstr(mn, 7, 0, "h] Density");
  81.     mvwaddstr(mn, 8, 0, "i] Atomic Radius");
  82.     mvwaddstr(mn, 9, 0, "j] Discovery Year");
  83.     mvwaddstr(mn, 10, 0, "Graphing by this, across the X axis.");
  84.     mvwaddstr(mn, 13, 0, "*** Due to lack of information at this time, graphs beyond");
  85.     mvwaddstr(mn, 14, 0, "*** Atomic number 86 may be misleading.  The program is designed");
  86.     mvwaddstr(mn, 15, 0, "*** to compensate, but nothing is perfect.");
  87.     mvwaddstr(mn, 16, 0, ">>> PRESS RETURN TO RETURN TO MAIN MENU.");
  88. first:    mvwaddstr(mn, 11, 0, "Your choice: ");
  89.     wrefresh(mn);                /* collect their choices */
  90.     cur = mn; xp = 14; yp = 11;
  91.     crmode();
  92.     noecho();
  93.     c1 = getchar();    /* X axis chc */
  94.     if (c1 == '\n') { wclear(mn); return; }
  95.     if (c1 == '?') { help(3); goto first; }
  96.     if (c1 < 'a' || c1 > 'j')
  97.         goto first;
  98.     for (i = 12; i < 17; i++) {
  99.         wmove(mn, i, 0); wclrtoeol(mn);
  100.     }
  101.  
  102.     find(c1, e->anum);
  103.     xmax = xmin = x;
  104.     if (gtot > 0) {
  105.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  106.             find(c1, tmp->anum);
  107.             if (x == -999.0) continue;
  108.             if (x > xmax) xmax = x;
  109.             if (x < xmin) xmin = x;
  110.         }
  111.     } else {
  112.         for (i = 1; i < gtot; i++) {
  113.             find(c1, sub1[i]);
  114.             if (x == -999.0) continue;
  115.             if (i == 1) xmax = xmin = x;
  116.             else {
  117.                 if (x > xmax) xmax = x;
  118.                 if (x < xmin) xmin = x;
  119.             }
  120.         }
  121.     }
  122.  
  123.     mvwaddstr(mn, 10, 0, "If you would rather use the maximum and minimum values of");
  124.     mvwaddstr(mn, 11, 0, "all the elements, hit RETURN once.");
  125.     wmove(mn, 12, 0); wprintw(mn, "All the element's values: Minimum %f, Maximum %f", xmin, xmax);
  126. l4:    mvwaddstr(mn, 13, 0, "What range, X Minimum? ");
  127.     wrefresh(mn);
  128.     cur = mn; xp = 24; yp = 13;
  129.     echo(); nocrmode();
  130.     gets(str);
  131.     if (str[0] == '?') { help(4); goto l4; }
  132.     if (!strlen(str)) {
  133.         xmin = -999;
  134.         goto nt;
  135.     }
  136.     xmin = atof(str);
  137.     fixup(mn); fixup(stdscr);
  138.     mvwaddstr(mn, 13, 0, "What range, X Maximum [RETURN for value above]? ");
  139.     wrefresh(mn);
  140.     cur = mn; xp = 48; yp = 13;
  141.     echo(); nocrmode();
  142.     gets(str);
  143.     if (strlen(str))
  144.         xmax = atof(str);
  145.     fixup(mn);
  146.     
  147. nt:    wmove(mn, 10, 0); wclrtoeol(mn); wmove(mn, 11, 0); wclrtoeol(mn);
  148.     mvwaddstr(mn, 10, 0, "Graphing by this, down the Y axis.   ");
  149. scnd:    wmove(mn, 12, 0); wclrtoeol(mn); wmove(mn, 13, 0); wclrtoeol(mn);
  150.     mvwaddstr(mn, 11, 0, "Your choice: ");
  151.     wrefresh(mn);
  152.     cur = mn; xp = 14; yp = 11;
  153.     noecho(); crmode();
  154.     c2 = wgetch(mn); /* Y axis chc */
  155.     if (c2 == '?') { help(5); goto scnd; }
  156.     if (c2 < 'a' || c2 > 'j')
  157.         goto scnd;
  158.     noecho();
  159.  
  160.     wclear(mn); wrefresh(mn);
  161.     clear(); refresh();
  162.     /* Here we find the x axis max and min values */
  163.     /* if they are not already defined */
  164.     if (xmin == -999) {
  165.         find(c1, e->anum);
  166.         xmax = xmin = x;
  167.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  168.             find(c1, tmp->anum);
  169.             if (x == -999.0) continue;
  170.             if (x > xmax) xmax = x;
  171.             if (x < xmin) xmin = x;
  172.         }
  173.     }
  174.  
  175.     /* Here we find the y axis min and max values */
  176.  
  177.     find(c2, e->anum);
  178.     ymax = ymin = x;
  179.     if (gtot > 0) {
  180.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  181.             find(c2, tmp->anum);
  182.             if (x == -999.0) continue;
  183.             if (x > ymax) ymax = x;
  184.             if (x < ymin) ymin = x;
  185.         }
  186.     } else {
  187.         for (i = 1; i < gtot; i++) {
  188.             find(c2, sub1[i]);
  189.             if (x == -999.0) continue;
  190.             if (i == 1)
  191.                 ymax = ymin = x;
  192.             else {
  193.                 if (x > ymax) ymax = x;
  194.                 if (x < ymin) ymin = x;
  195.             }
  196.         }
  197.     }
  198.  
  199.     /* Now we have min & maxs we have to put a number in
  200.        each slot on both axes using a scale */
  201.  
  202.     scale = (xmax - xmin) / 69.0; /* 69 is # of slots in X axis */
  203.  
  204.     xaxis[1] = xmin;
  205.     for (i = 2; i < 70; i++)
  206.         xaxis[i] = xaxis[i-1] + scale;
  207.  
  208.     scale = (ymax - ymin) / 20.0; /* 20 is # of slots in Y axis */
  209.  
  210.     yaxis[20] = ymin;
  211.     for (i = 19; i > 1; i--)
  212.         yaxis[i] = yaxis[i+1] + scale;
  213.  
  214.     sprintf(str, "%s (x-axis) vs. %s (y-axis)", gname[numr(c1)], gname[numr(c2)]);
  215.     mvwaddstr(graph, 0, (40 - (strlen(str) / 2)), str);
  216.     wrefresh(graph);    /* That is the top graph line */
  217.     for (i = 20; i > 1; i--) {        /* The y axis line */
  218.         mvwaddstr(graph, i, 10, "|");
  219.         wrefresh(graph);
  220.     }
  221.     for (i = 11; i < 80; i++) {        /* The x axis line */
  222.         mvwaddstr(graph, 21, i, "-");
  223.         wrefresh(graph);
  224.     }
  225.     fixup(graph);
  226.     mvwaddstr(graph, 21, 10, "+");        /* The corner */
  227.     wrefresh(graph);
  228.     for (i = 20; i > 1; i -= 5) {        /* Y axis points */
  229.         if (c2 < 'f' || c2 > 'i')
  230.             sprintf(str, "%d ->", (int)(yaxis[i]+.5));
  231.         else
  232.             sprintf(str, "%4.2f ->", yaxis[i]);
  233.         mvwaddstr(graph, i, 0, str);
  234.     }
  235.     for (i = 1; i < 79; i += 10) {        /* X axis points */
  236.         if (c1 < 'f' || c1 > 'i')
  237.             sprintf(str, "^%d", (int)(xaxis[i]+.5));
  238.         else
  239.             sprintf(str, "^%4.2f", xaxis[i]);
  240.         mvwaddstr(graph, 22, i+10, str);
  241.     }
  242.     /* The actual find & graph */
  243.  
  244.     if (gtot > 0) {
  245.         for (tmp = e; tmp->next != NULL; tmp = tmp->next) {
  246.             find(c1, tmp->anum);
  247.             if (x == -999.0) continue;
  248.             if (x < xmin)
  249.                 continue;
  250.             else if (x < xaxis[2])
  251.                 xspot = 1;
  252.             else {
  253.                 for (j = 2; j < 70; j++) {
  254.                     if (x > xaxis[j])
  255.                         continue;
  256.                     else
  257.                         break;
  258.                 }
  259.                 xspot = j - 1;
  260.                 if (x > xmax)
  261.                     continue;
  262.             }
  263.  
  264.             /* Found where it goes on the x, now the y */
  265.  
  266.             find(c2, tmp->anum);
  267.             if (x == -999.0) continue;
  268.             if (x < yaxis[19])
  269.                 yspot = 20;
  270.             else {
  271.                 for (j = 19; j > 1; j--) {
  272.                     if (x > yaxis[j])
  273.                         continue;
  274.                     else
  275.                         break;
  276.                 }
  277.                 yspot = j;
  278.             }
  279.  
  280.             /* Align and put the dot where X marks the spot */
  281.  
  282.             mvwaddstr(graph, yspot, xspot+10, "o");
  283.             wrefresh(graph);
  284.         }
  285.     } else {
  286.         for (i = 1; i < gtot; i++) {
  287.             find(c1, sub1[i]);
  288.             if (x == -999.0) continue;
  289.             if (x < xmin)
  290.                 continue;
  291.             else if (x < xaxis[2])
  292.                 xspot = 1;
  293.             else {
  294.                 for (j = 2; j < 70; j++) {
  295.                     if (x > xaxis[j])
  296.                         continue;
  297.                     else
  298.                         break;
  299.                 }
  300.                 xspot = j - 1;
  301.                 if (x > xmax)
  302.                     continue;
  303.             }
  304.  
  305.             /* Found where it goes on the x, now the y */
  306.  
  307.             find(c2, sub1[i]);
  308.             if (x == -999.0) continue;
  309.             if (x < yaxis[19])
  310.                 yspot = 20;
  311.             else {
  312.                 for (j = 19; j > 1; j--) {
  313.                     if (x > yaxis[j])
  314.                         continue;
  315.                     else
  316.                         break;
  317.                 }
  318.                 yspot = j;
  319.             }
  320.  
  321.             /* Align and put the dot where X marks the spot */
  322.  
  323.             mvwaddstr(graph, yspot, xspot+10, "o");
  324.             wrefresh(graph);
  325.         }
  326.     }
  327.  
  328.     capture(graph, 1, 0);
  329.     spc();        /* Space to continue? */
  330.     wclear(graph);
  331.     wrefresh(graph);
  332.     return(0);
  333. }
  334.