home *** CD-ROM | disk | FTP | other *** search
- {$V-}
- (* ======================================================================== *)
- (* This program takes a legal arithmetic expression in the form of a *)
- (* string and evaluates it. It also displays the "expression tree" *)
- (* generated, and converts the expression to RPN notation. I had hoped *)
- (* to pretty it up some before uploading, but didn't get around to it. *)
- (* *)
- (* At the ":" prompt, just enter an expression. Binary operators are *)
- (* +-/*, unary operators are - and @, where prefixing an expression in *)
- (* parentheses with a @ causes it to be truncated to an integer. To *)
- (* get #A mod #B, you could do "#A - (@(#A/#B)*#B)" *)
- (* *)
- (* Variable names must begin with "#". To declare a variable, just do *)
- (* #NAME = (expression). This variable can be used in later expressions. *)
- (* *)
- (* The usual problem in Pascal Text books is to change an expression in *)
- (* a tree to Infix, Prefix, or Postfix forms. They never seem to tell *)
- (* you how to get the expression INTO the tree. That's what this *)
- (* program is for. *)
- (* *)
- (* NOTE: The program does not handle repeated unary operators w/o *)
- (* parentheses -- "---1" is bad, "-(-(-1))" is good. *)
- (* *)
- (* Neil J. Rubenking *)
- (* ======================================================================== *)
- PROGRAM new_evaluate;
- TYPE
- string255 = STRING[255];
-
- string80 = STRING[80];
- string15 = STRING[15];
- string8 = STRING[8];
- char_set = SET OF Char;
- Int_Var = RECORD {Internal Variable type}
- varName : string8;
- value : Real;
- END;
-
- CONST
- MaxVars = 16;
- numbers : char_set = ['0'..'9', '.'];
-
- VAR
- line : string80;
- pVars : ARRAY[1..MaxVars] OF Int_Var;
- vTop, B_Dummy, B_Val : Byte;
- R_Val : Real;
- error : Boolean;
-
- PROCEDURE StripOut(CH : Char; VAR LL : string80);
- (************************************************)
- (* Strip all occurrences of the character CH *)
- (* out of the string LL. *)
- (************************************************)
- BEGIN
- WHILE Pos(CH, LL) <> 0 DO Delete(LL, Pos(CH, LL), 1);
- END;
-
-
-
- FUNCTION HasNumber(EL : string80; VAR RR : Real):boolean;
- VAR
- NumStr : string80;
- C, N : Integer;
- evald : Boolean;
-
- PROCEDURE NewEval(LL : string80; VAR evR : Real;
- VAR OK : Boolean);
- type
- treePtr = ^node;
- whichType = (lef,rit,mid,bra,mor);
- tagtype = (valu,unop,bnop);
- node = record case tag:tagtype