!This file is part of VEL_demo, copyright David Keyes, 2007. ! !This source file is free software: you can redistribute it and/or modify !it under the terms of the GNU General Public License as published by !the Free Software Foundation, either version 3 of the License, or !(at your option) any later version. ! !This code is distributed in the hope that it will be useful, !but WITHOUT ANY WARRANTY; without even the implied warranty of !MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the !GNU General Public License for more details. ! !For terms of the GNU General Public license see . GLOBAL MODULE evaluate( STRING input*132; ! input string only contains numbers and a maximum of one operator ! VAR FLOAT rtn_flt); INT n, stat; FLOAT tmp, ft; STRING left*132, right*132; BEGINMODULE ! search for operators ! ft:=0; stat:=0; n:=finds(input, "+"); IF n <> 0 THEN part(#1, split_str(input, "+", left, right)); rtn_flt:=fval(left) + fval(right); stat:=1; ENDIF; n:=finds(input, "*"); IF n <> 0 THEN part(#2, split_str(input, "*", left, right)); rtn_flt:=fval(left) * fval(right); stat:=1; ENDIF; IF stat=0 THEN n:=finds(input, " "); IF n <> 0 THEN ! have to split it again, intended for items like "1 1/4" ! ! that we want to read as 1.25 ! part(#3, split_str(input, " ", left, right)); tmp:=fval(left); part(#4, split_str(right, "/", left, right)); rtn_flt:=tmp + (fval(left) / fval(right)); stat:=1; ENDIF; ENDIF; n:=finds(input, "'"); IF n <> 0 THEN part(#5, split_str(input, "'", left, right)); ft:=fval(left)*12; ! strip the potential minus sign ! IF substr(right,1,1) = "-" THEN input:=substr(right,2); ELSE input:=right; ENDIF; stat:=2; ENDIF; IF stat = 2 THEN n:=finds(input, " "); IF n <> 0 THEN part(#6, split_str(input, " ", left, right)); tmp:=fval(left); part(#7, split_str(right, "/", left, right)); rtn_flt:=ft+tmp + (fval(left) / fval(right)); stat:=1; ELSE rtn_flt:=ft +(fval(left)); ENDIF; ELIF stat = 0 THEN n:=finds(input, "-"); IF n <> 0 THEN part(#16, split_str(input, "-", left, right)); rtn_flt:=fval(left) - fval(right); stat:=1; ENDIF; n:=finds(input, "/"); IF n <> 0 THEN part(#17, split_str(input, "/", left, right)); rtn_flt:=fval(left) / fval(right); stat:=1; ENDIF; ENDIF; IF stat = 0 THEN rtn_flt:=fval(input); ENDIF; ENDMODULE