home *** CD-ROM | disk | FTP | other *** search
- unit EvaluationClass;
-
- interface
-
- type TTDEvaluation = class(TObject)
-
- private
- procedure PrimaOperazione(Espressione: String;
- var Operatore: Char;
- var Posizione: Integer);
-
- procedure RecuperaOperandi(Expression: String;
- Op:Char;
- Posizione: Cardinal;
- var PrimoOperando,SecondoOperando: Extended);
- procedure EseguiCalcolo(var strEx: String;
- Op: Char;
- POp,SOp: Extended);
-
- public
- Expressions: String;
- function Calculate: Extended;
-
-
- end;
-
-
- implementation
-
- { TTDEvaluation }
-
- uses SysUtils;
-
- function TTDEvaluation.Calculate: Extended;
- var
- Op: Char;
- Posizione: Integer;
- Expression, strEx: String;
- PrimoOperando, SecondoOperando: Extended;
- begin
- strEx:=StringReplace(Expressions,' ','',[rfReplaceAll]); //Tolgo gli spazi
- strEx:=StringReplace(Expressions,'.',',',[rfReplaceAll]); //Punti in virgole
-
-
-
- while true do
- begin
- Op:=' ';
- PrimaOperazione(strEx,Op,Posizione);
- if Posizione<=1 then Break; //Condizione di uscita
- RecuperaOperandi(strEx,Op,Posizione,PrimoOperando,SecondoOperando);
- EseguiCalcolo(strEx,Op,PrimoOperando,SecondoOperando);
- end;
-
- Result:=StrToFloat(strEx);
- end;
-
- procedure TTDEvaluation.EseguiCalcolo(var strEx: String; Op: Char; POp,
- SOp: Extended);
- var
- Risultato: Extended;
- begin
- case Op of
-
- '+':
- Risultato:=POp + SOp;
-
- '-':
- Risultato:=POp - SOp;
-
- '*':
- Risultato:=POp * SOp;
-
- '/':
- Risultato:=POp / SOp;
-
- end;
-
- strEx:=StringReplace(strEx,FloatToStr(POp) + Op + FloatToStr(SOp),FloatToStr(Risultato),[rfReplaceAll]);
-
- end;
-
- procedure TTDEvaluation.PrimaOperazione(Espressione: String;
- var Operatore: Char; var Posizione: Integer);
- var
- intPiu, intMeno, intPer, intDiviso: Integer;
- begin
-
-
- if Espressione[1]<>'+' then
- intPiu:= Pos('+',Espressione)
- else
- intPiu:=Pos('+',Copy(Espressione,2,Length(Espressione)));
-
-
- if Espressione[1]<>'-' then
- intMeno:= Pos('-',Espressione)
- else
- intMeno:=Pos('-',Copy(Espressione,2,Length(Espressione)));
-
- intPer:= Pos('*',Espressione);
- intDiviso:= Pos('/',Espressione);
- Operatore:=' ';
- Posizione:=0;
- {******************** SIA * CHE / ***********************}
- if (intPer<>0) and (intDiviso<>0) then
- begin
- if intPer<intDiviso then
- begin
- Operatore:='*';
- Posizione:=intPer;
- end
- else
- begin
- Operatore:='/';
- Posizione:=intDiviso;
- end
- end;
-
- if (intPer<>0) and (intDiviso=0) then
- begin
- Operatore:='*';
- Posizione:=intPer;
- end;
-
- if (intPer=0) and (intDiviso<>0) then
- begin
- Operatore:='/';
- Posizione:=intDiviso;
- end;
-
-
-
- {Se non ci sono più Moltiplicazioni o Divisioni...}
- if Operatore=' ' then
- begin
- {******************** SIA + CHE - ***********************}
- if (intPiu<>0) and (intMeno<>0) then
- begin
- if intPiu<intMeno then
- begin
- Operatore:='+';
- Posizione:=intPiu;
- end
- else
- begin
- Operatore:='-';
- Posizione:=intMeno;
- end
- end
- else
- begin
- if (intPiu<>0) and (intMeno=0) then
- begin
- Operatore:='+';
- Posizione:=intPiu;
- end;
- if (intPiu=0) and (intMeno<>0) then
- begin
- Operatore:='-';
- Posizione:=intMeno;
- end;
- end;
- end;
-
- end;
-
- procedure TTDEvaluation.RecuperaOperandi(Expression: String; Op: Char;
- Posizione: Cardinal; var PrimoOperando, SecondoOperando: Extended);
- var
- I:Cardinal;
- strOp1,strOp2: String;
- begin
- if Posizione=1 then Exit;
-
- I:=Posizione;
- //Recupero primo operando
- Dec(I);
- while Expression[I] in ['0'..'9',','] do
- begin
- strOp1:=Expression[I] + strOp1;
- Dec(I);
- end;
- if (I=1) and ((Expression[1]='+') or (Expression[1]='-')) then
- strOp1:=Expression[1] + strOp1;
-
- PrimoOperando:=StrToFloat(strOp1);
-
-
-
- I:=Posizione;
- //Recupero secondo operando
- Inc(I);
- while Expression[I] in ['0'..'9',','] do
- begin
- strOp2:=strOp2 + Expression[I];
- Inc(I);
- end;
- SecondoOperando:=StrToFloat(strOp2);
-
-
- end;
-
- end.
-