1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
| function ParseEx(astr: string): extended; function ExpCheck(op: char): integer; var i, count: integer; begin Result := 0; count := 0; for i := 1 to length(astr) do begin if astr[i] = '(' then inc(count); if astr[i] = ')' then dec(count); if (count = 0) and (astr[i] = op) then begin result := i; if (i > 1) AND (op in ['-', '+']) Then begin if astr[i-1] in ['+', '-', '*', '/', '^'] then begin result := 0; continue; end; end; exit; end; if (Count < 0) then Result := 0; end; if count <> 0 then result := 0; end; function ParseA(op: char): string; begin result := copy(astr, 1, ExpCheck(op) - 1); end; function ParseB(op: char): string; begin result := copy(astr, ExpCheck(op) + 1, length(astr) - ExpCheck(op)); end; const e = 2.71828182845904523536028747135266249; begin if (length(astr) = ExpCheck(')')) and (Copy(astr, 1, 1) = '(') then result := ParseEx(copy(astr, 2, ExpCheck(')') - 2)) else if ExpCheck('+') > 0 then result := ParseEx(ParseA('+')) + ParseEx(ParseB('+')) else if ExpCheck('-') > 0 then result := ParseEx(ParseA('-')) - ParseEx(ParseB('-')) else if ExpCheck('*') > 0 then result := ParseEx(ParseA('*')) * ParseEx(ParseB('*')) else if ExpCheck('/') > 0 then result := ParseEx(ParseA('/')) / ParseEx(ParseB('/')) else if ExpCheck('^') > 0 then result := power(ParseEx(ParseA('^')), ParseEx(ParseB('^'))) else if copy(astr, 1, 3) = 'sin' then result := sin(degtorad(ParseEx(copy(astr, 4, length(astr) - 3)))) else if copy(astr, 1, 3) = 'cos' then result := cos(degtorad(ParseEx(copy(astr, 4, length(astr) - 3)))) else if copy(astr, 1, 3) = 'tan' then result := tan(degtorad(ParseEx(copy(astr, 4, length(astr) - 3)))) else if copy(astr, 1, 4) = 'sqrt' then result := sqrt(ParseEx(copy(astr, 5, length(astr) - 4))) else if copy(astr, 1, 2) = 'pi' then result := pi else if copy(astr, 1, 2) = 'e' then result := e else if astr = '' then result := 0 else result := strtofloat(astr); end; |