home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / lr.zip / TABLE.C < prev    next >
Text File  |  1993-05-15  |  7KB  |  196 lines

  1.  
  2.  
  3.  #include "sample.h"
  4.  #include <malloc.h>
  5.  /*
  6.   ╔════════════════════════════════════════════════════════════════════════════╗
  7.   ║ Copyright (C) Transcendental Automation, 1993.                             ║
  8.   ╟────────────────────────────────────────────────────────────────────────────╢
  9.   ║ TABLE.C                                                                    ║
  10.   ║ This file contains sourse code of functions:                               ║
  11.   ╟────────────────────────────────────────────────────────────────────────────╢
  12.   ║ Work with table of variables                                               ║
  13.   ║      void    InitTable (void)    - initialize vartable.                    ║
  14.   ║      float   LookUp    (char* nm) - search in vartable variable with name  ║
  15.   ║                                     'nm' if not found create new variabe in║
  16.   ║                                     vartable and set it with zero.         ║
  17.   ║                                                                            ║
  18.   ║      void    AddItem   (char*t,float f) - add new var named 'nm'to vartable║
  19.   ║                                           and set it with 'f'.             ║
  20.   ║      void    SetItem   (char* t,float f) - set var named 'nm' with 'f'.    ║
  21.   ║      void    FreeTable (void)            - free resources allocated by     ║
  22.   ║                                            vartable                        ║
  23.   ╟────────────────────────────────────────────────────────────────────────────╢
  24.   ║ Work with stack of floats                                                  ║
  25.   ║      void    pushFloat       (float f) - put 'f' to stack                  ║
  26.   ║      float   popFloat        (void)    - get 'f' from stack                ║
  27.   ║      void    FreeFloatStack  ()        - free stack                        ║
  28.   ║      void    InitFloatStack  ()        - initialize stack                  ║
  29.   ╚════════════════════════════════════════════════════════════════════════════╝
  30.   */
  31.  /*
  32.  _____________________________________________________________________________
  33.   VARTABLE SECTION
  34.  _____________________________________________________________________________
  35.  */
  36.  /*
  37.  _____________________________________________________________________________
  38.   The structure 'VarTableItem' represents element of vartable.
  39.  _____________________________________________________________________________
  40.  */
  41.  
  42.  struct VarTableItem
  43.   {char* name;                   // the name of variable
  44.    float val;                    // the value of variable
  45.    struct VarTableItem *next;    // pointer to next variable
  46.   };
  47.  
  48.  static struct VarTableItem* FList; //pointer to the first variable in the table.
  49.  
  50.  /*
  51.  ______________________________________________________________________________
  52.   Initialize vartable
  53.  ______________________________________________________________________________
  54.   */
  55.  void    InitTable (void)
  56.   {FList = 0;
  57.   }
  58.  
  59.  void    AddItem   (char*t,float f);
  60.  /*
  61.  _____________________________________________________________________________
  62.   Look up variable in the vartable and return its value if found or
  63.   add variable with value 0 to vartable and return 0.
  64.  _____________________________________________________________________________
  65.  */
  66.  float   LookUp    (char* nm)
  67.   {struct VarTableItem *cur;
  68.    cur = FList;
  69.    while ( cur && stricmp(cur->name,nm) )
  70.      cur = cur -> next;
  71.  
  72.    if ( cur == 0)
  73.     {AddItem(nm,0);
  74.      return 0;
  75.     }
  76.    else
  77.     return cur->val;
  78.   }
  79.  /*
  80.  _____________________________________________________________________________
  81.   Add item to vartable.
  82.  _____________________________________________________________________________
  83.  */
  84.  void    AddItem   (char*t,float f)
  85.   {struct VarTableItem *cur = malloc(sizeof(struct VarTableItem));
  86.    cur->next   = FList;
  87.    cur->name   = strdup(t);
  88.    strupr(cur->name);
  89.    cur->val    = f;
  90.    FList = cur;
  91.   }
  92.  /*
  93.  _____________________________________________________________________________
  94.   Set item named 't' with f
  95.  _____________________________________________________________________________
  96.  */
  97.  void    SetItem   (char* t,float f)
  98.   {struct VarTableItem *cur = FList;
  99.    while ( cur && stricmp(cur->name,t) )   cur = cur -> next;
  100.    if ( cur == 0)
  101.      AddItem(t,f);
  102.    else
  103.      cur->val = f;
  104.   }
  105.  /*
  106.  _____________________________________________________________________________
  107.   Release memory allocated by vartable and print results of calculations
  108.  _____________________________________________________________________________
  109.  */
  110.  void    FreeTable (void)
  111.   {struct VarTableItem *cur = FList ,*x;
  112.    puts("Calculations has been finished.");
  113.    puts("Results:");
  114.    puts("Name         Value.");
  115.    while ( cur )
  116.     {x=cur;
  117.      cur = cur -> next;
  118.      printf("%-12.12s %g\n",x->name,x->val);
  119.      free (x);
  120.     }
  121.    FList = 0;
  122.   }
  123.  
  124.  /*
  125.  _____________________________________________________________________________
  126.   FLOAT STACK SECTION
  127.  _____________________________________________________________________________
  128.  */
  129.  
  130.  /*
  131.  _____________________________________________________________________________
  132.   The structure represent a item of float stack.
  133.  _____________________________________________________________________________
  134.  */
  135.  
  136.  static struct FLOAT_EL
  137.   {float val;             // value of the item
  138.    struct FLOAT_EL *next; // pointer to the next item
  139.   } *FStack;              // pointer to the top of the stack
  140.  
  141.  /*
  142.  ______________________________________________________________________________
  143.   Initialize the stack of floats
  144.  ______________________________________________________________________________
  145.  */
  146.  void    InitFloatStack  ()
  147.   {FStack = 0;
  148.   }
  149.  /*
  150.  ______________________________________________________________________________
  151.   Push float 'f' to the stack.
  152.  ______________________________________________________________________________
  153.  */
  154.  void    pushFloat       (float f)
  155.   {struct FLOAT_EL *cur = malloc(sizeof(struct FLOAT_EL));
  156.    cur->next   = FStack;
  157.    cur->val    = f;
  158.    FStack      = cur;
  159.   }
  160.  
  161.  /*
  162.  ______________________________________________________________________________
  163.   Pop float from the stack.
  164.  ______________________________________________________________________________
  165.  */
  166.  float   popFloat        (void)
  167.   {if(FStack)
  168.     {struct FLOAT_EL *cur;
  169.      float r;
  170.      r=FStack->val;
  171.      cur = FStack->next;
  172.      free(FStack);
  173.      FStack = cur;
  174.      return r;
  175.     }
  176.    puts("\'popFloat\':The Stack is empty");
  177.    exit(666);
  178.    return 0;
  179.   }
  180.  /*
  181.  _____________________________________________________________________________
  182.   if the stack is not empty free it.
  183.  _____________________________________________________________________________
  184.  */
  185.  void    FreeFloatStack   ()
  186.   {struct FLOAT_EL *cur = FStack ,*x;
  187.    while ( cur )
  188.     {x=cur;
  189.      cur = cur -> next;
  190.      free (x);
  191.     }
  192.    FStack = 0;
  193.   }
  194.    
  195.  
  196.