17. Februar 2009 22:19
18. Februar 2009 09:18
Result := 0;
Expression := DELCHR(Expression,'<>',' ');
IF STRLEN(Expression) > 0 THEN BEGIN
Parantheses := 0;
IsExpression := FALSE;
Operators := '+-*/^%';
OperatorNo := 1;
REPEAT
i := STRLEN(Expression);
REPEAT
IF Expression[i] = '(' THEN
Parantheses := Parantheses + 1
ELSE
IF Expression[i] = ')' THEN
Parantheses := Parantheses - 1;
IF (Parantheses = 0) AND (Expression[i] = Operators[OperatorNo]) THEN
IsExpression := TRUE
ELSE
i := i - 1;
UNTIL IsExpression OR (i <= 0);
IF NOT IsExpression THEN
OperatorNo := OperatorNo + 1;
UNTIL (OperatorNo > STRLEN(Operators)) OR IsExpression;
IF IsExpression THEN BEGIN
IF i > 1 THEN
LeftOperand := COPYSTR(Expression,1,i - 1)
ELSE
LeftOperand := '';
IF i < STRLEN(Expression) THEN
RightOperand := COPYSTR(Expression,i + 1)
ELSE
RightOperand := '';
Operator := Expression[i];
LeftResult := EvaluateExpression(LeftOperand);
RightResult := EvaluateExpression(RightOperand);
CASE Operator OF
'^':
Result := POWER(LeftResult,RightResult);
'%' :
IF RightResult = 0 THEN BEGIN
Result := 0;
END ELSE
Result := 100 * LeftResult / RightResult;
'*':
Result := LeftResult * RightResult;
'/':
IF RightResult = 0 THEN BEGIN
Result := 0;
END ELSE
Result := LeftResult / RightResult;
'+':
Result := LeftResult + RightResult;
'-':
Result := LeftResult - RightResult;
END;
END ELSE
IF (Expression[1] = '(') AND (Expression[STRLEN(Expression)] = ')') THEN
Result :=
EvaluateExpression(COPYSTR(Expression,2,STRLEN(Expression) - 2))
ELSE BEGIN
EVALUATE(Result,Expression);
END;
END;
EXIT(Result);
18. Februar 2009 10:31