home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / EVALUATE.ZIP / EVALUATE.PAS
Encoding:
Pascal/Delphi Source File  |  1986-11-29  |  3.1 KB  |  73 lines

  1. {$V-}
  2. (* ======================================================================== *)
  3. (*   This program takes a legal arithmetic expression in the form of a      *)
  4. (*   string and evaluates it.  It also displays the "expression tree"       *)
  5. (*   generated, and converts the expression to RPN notation.  I had hoped   *)
  6. (*   to pretty it up some before uploading, but didn't get around to it.    *)
  7. (*                                                                          *)
  8. (*   At the ":" prompt, just enter an expression.  Binary operators are     *)
  9. (*   +-/*, unary operators are - and @, where prefixing an expression in    *)
  10. (*   parentheses with a @ causes it to be truncated to an integer.  To      *)
  11. (*   get #A mod #B, you could do  "#A - (@(#A/#B)*#B)"                      *)
  12. (*                                                                          *)
  13. (*   Variable names must begin with "#".  To declare a variable, just do    *)
  14. (*   #NAME = (expression).  This variable can be used in later expressions. *)
  15. (*                                                                          *)
  16. (*   The usual problem in Pascal Text books is to change an expression in   *)
  17. (*   a tree to Infix, Prefix, or Postfix forms.  They never seem to tell    *)
  18. (*   you how to get the expression INTO the tree.  That's what this         *)
  19. (*   program is for.                                                        *)
  20. (*                                                                          *)
  21. (*   NOTE:  The program does not handle repeated unary operators w/o        *)
  22. (*   parentheses -- "---1" is bad, "-(-(-1))" is good.                      *)
  23. (*                                                                          *)
  24. (*                     Neil J. Rubenking                                    *)
  25. (* ======================================================================== *)
  26. PROGRAM new_evaluate;
  27. TYPE
  28.   string255 = STRING[255];
  29.  
  30.   string80 = STRING[80];
  31.   string15 = STRING[15];
  32.   string8 = STRING[8];
  33.   char_set = SET OF Char;
  34.   Int_Var = RECORD            {Internal Variable type}
  35.               varName : string8;
  36.               value : Real;
  37.             END;
  38.  
  39. CONST
  40.   MaxVars = 16;
  41.   numbers : char_set = ['0'..'9', '.'];
  42.  
  43. VAR
  44.   line : string80;
  45.   pVars : ARRAY[1..MaxVars] OF Int_Var;
  46.   vTop, B_Dummy, B_Val : Byte;
  47.   R_Val : Real;
  48.   error : Boolean;
  49.  
  50.   PROCEDURE StripOut(CH : Char; VAR LL : string80);
  51.     (************************************************)
  52.     (*  Strip all occurrences of the character CH   *)
  53.     (*  out of the string LL.                       *)
  54.     (************************************************)
  55.   BEGIN
  56.     WHILE Pos(CH, LL) <> 0 DO Delete(LL, Pos(CH, LL), 1);
  57.   END;
  58.  
  59.  
  60.  
  61.   FUNCTION HasNumber(EL : string80; VAR RR : Real):boolean;
  62.   VAR
  63.     NumStr : string80;
  64.     C, N : Integer;
  65.     evald : Boolean;
  66.  
  67.     PROCEDURE NewEval(LL : string80; VAR evR : Real;
  68.                       VAR OK : Boolean);
  69. type
  70.   treePtr  = ^node;
  71.   whichType = (lef,rit,mid,bra,mor);
  72.   tagtype = (valu,unop,bnop);
  73.   node = record case tag:tagtype