type TLogical = type Boolean; TText = type AnsiString; TVariable = type Variant; class function WhatIsEvaluate(const _value:TText; _variables:TStrings; const _dot:Char):TVariable; var _actual:Char; _calculate:TText; _cursor:Integer; procedure _PleaseSkip; begin if _cursor > System.Length(_calculate) then _actual := #9 else begin _actual := _calculate[_cursor]; System.Inc(_cursor); end; if _actual = ' ' then _PleaseSkip; end; function _ReplaceVariables(const _value:TText; _variables:TStrings):TText; var _cursor:Integer; _error:TLogical; _ok:TLogical; _replace:TText; _start:Integer; _stop:Integer; _text:TText; _variable:TText; begin _error := System.False; _ok := System.False; _text := SysUtils.Trim(_value); while not(_error) and not(_ok) do begin _start := 1; while (_start <= System.Length(_text)) and not(_text[_start] in ['a'..'z', 'A'..'Z']) do System.Inc(_start); if _start <= System.Length(_text) then begin _stop := _start; _variable := ''; while (_stop <= System.Length(_text)) and (_text[_stop] in ['a'..'z', 'A'..'Z']) do begin _variable := _variable + _text[_stop]; System.Inc(_stop); end; if System.Length(_variable) > Math.ZeroValue then begin _cursor := _variables.IndexOfName(_variable); if _cursor = -1 then begin System.Insert('?', _text, _stop); _error := System.True; end else begin System.Delete(_text, _start, System.Length(_variable)); System.Insert(_variables.ValueFromIndex[_cursor], _text, _start); end; end; end else _ok := System.True; end; Result := _text; end; function _WhatIsCalculate:TVariable; var _result:TVariable; function _WhatIsFactor:TVariable; var _text:TText; begin _PleaseSkip; if _actual in ['0'..'9'] then begin _text := ''; repeat _text := _text + _actual; _PleaseSkip; until not(_actual in ['0'..'9']) and (_actual <> _dot); Result := SysUtils.StrToFloat(_text); end else if _actual = '(' then begin Result := _WhatIsCalculate; _PleaseSkip; end else if _actual = '+' then Result := +Result else if _actual = '-' then Result := -Result; end; function _WhatIsSubResult:TVariable; var _result:TVariable; begin _result := _WhatIsFactor; while _actual in ['*', '/'] do if _actual = '*' then _result := _result * _WhatIsFactor else _result := _result / _WhatIsFactor; Result := _result; end; begin _result := _WhatIsSubResult; while _actual in ['+', '-'] do if _actual = '+' then _result := _result + _WhatIsSubResult else _result := _result - _WhatIsSubResult; Result := _result; end; begin _cursor := 1; _calculate := SysUtils.Trim(_value); if System.Assigned(_variables) then _calculate := _ReplaceVariables(_calculate, _variables); Result := SYsUtils.FloatToStr(_WhatIsCalculate); end;