home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume24 / chemtab / part02 / element.c < prev    next >
C/C++ Source or Header  |  1991-03-12  |  2KB  |  98 lines

  1. /*
  2.  * chemtab - a periodic table data base (C) 1990 Jim King (pulsar@lsrhs)
  3.  *
  4.  * element.c    Reads in the file from the #define PERTABLE (which is the
  5.  *        data file) and sorts into the structure elem (e).
  6.  *              Also reads in the close values (at the end)
  7.  */
  8.  
  9. #include "variables.h"
  10. #include "element.h"
  11. #include "tune.h"
  12. #include <stdio.h>
  13.  
  14. float closev[15];
  15.  
  16. /*
  17.  * readelem() - no input
  18.  *
  19.  * purpose:    Take a line from PERTABLE (tune.h) and put it in structure
  20.  *        e which is defined as the element table used throughout
  21.  *        the program.  This was designed so the operator could easily
  22.  *        change an element or add one without changing anything but
  23.  *        tune.h
  24.  */
  25. readelem()
  26. {
  27.     int    first = 1;
  28.     char    inp[160];
  29.     FILE    *fptr;                /* File pointer for PERTABLE */
  30.     struct    elem    *tmp, *old, *new;
  31.  
  32.     fptr = fopen(PERTABLE, "r");
  33.  
  34.     fgets(inp, 160, fptr);            /* Read in comment line */
  35.     fgets(inp, 160, fptr);            /* Eat the spacer */
  36.  
  37.     tmp = NEW(elem);
  38.     e = tmp;
  39.     while (fscanf(fptr, "%s %s %d %f %d %d %d %d %d %d %d %f %f %f %f\n",
  40.                tmp->name, tmp->sym, &tmp->anum, &tmp->amass, &tmp->fam,
  41.                &tmp->row, &tmp->val, &tmp->melt, &tmp->boil,
  42.                &tmp->fio, &tmp->year, &tmp->eneg, &tmp->spht,
  43.                &tmp->dens, &tmp->arad) != EOF) {
  44.             if (first) { first = 0; tmp = NEW(elem); continue; }
  45.             if (tmp->anum < e->anum) {
  46.                 tmp->next = e;
  47.                 e = tmp;
  48.                 tmp = NEW(elem);
  49.                 continue;
  50.             }
  51.             old = NULL; new = e;
  52.             for (;;) {
  53.                 if (new->next == NULL) { /* new tail */
  54.                     new->next = tmp;
  55.                     tmp->next = NULL;
  56.                     break;
  57.                 }
  58.                 old = new;
  59.                 new = new->next;
  60.                 if ((old->anum < tmp->anum) &&
  61.                    (tmp->anum < new->anum)) {
  62.                     tmp->next = new;
  63.                     old->next = tmp;
  64.                     break;
  65.                 }
  66.             }
  67.         tmp = NEW(elem);
  68.     }
  69.     tmp = NULL;
  70.     fclose(fptr);                /* Be nice and neat */
  71. }
  72.  
  73. /*
  74.  * getclose()
  75.  * inputs: none
  76.  * returns: 888 if bad closefile
  77.  * purpose: read close values from CLOSEFILE
  78.  */
  79. int getclose()
  80. {
  81.     FILE    *fp;
  82.     char    str[90];
  83.     int    i;
  84.  
  85.     if ((fp = fopen(CLOSEFILE, "r")) == NULL) {
  86.         printf("chemtab: Make sure that tune.h reflects all file names. (CLOSEFILE)\n");
  87.         exit(1);
  88.     }
  89.  
  90.     fgets(str, 90, fp); fgets(str, 90, fp);
  91.  
  92.     for (i = 0; i < 15; i++)
  93.         if (fscanf(fp, "%s%c%f\n", str, str, &closev[i]) == EOF) return(888);
  94.     
  95.     fclose(fp);
  96. }
  97.  
  98.