File: unitcalculateurfonction.pas

package info (click to toggle)
dozzaqueux 3.21-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,140 kB
  • sloc: pascal: 33,352; xml: 961; makefile: 38; sh: 17
file content (157 lines) | stat: -rwxr-xr-x 4,953 bytes parent folder | download | duplicates (6)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
unit UnitCalculateurFonction;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,Symbolic,Math;

type
  tableauchaine=array of string;


  ECalculateurFonctionError = class(Exception);
  ECalculateurFonctionSyntaxError = class(ECalculateurFonctionError);
  ECalculateurFonctionListeVariablesError = class(ECalculateurFonctionError);

  TCalculateurFonction=class
    private
  _Expression:texpression;
  _ListeVariables:TStringList;
  _ListeValeursVariables:array of arbfloat;
  _Evaluator:TEvaluator;
  function CalcValue: float;
  function LitListeVariables:TStringList;
    function LitExpression:TExpression;
   public
  CONSTRUCTOR Create(Infix:String); overload;
  CONSTRUCTOR Create(Infix:String; ListeVariablesAutorisees:TStringList); overload;
  CONSTRUCTOR Create(Infix:String; ListeVariablesAutorisees:TableauChaine; NombreVariables:integer); overload;
 destructor Destroy;
  procedure SetVar(const VarName: string; const Value: float);
    function GetVariable(const VarName: string): arbfloat;
    property Variable[const VarName: string]: arbfloat read GetVariable write SetVar;
    published
    property Value: arbfloat read CalcValue stored false;
    property ListeVariables: tstringlist read LitListeVariables stored false;
    property Expression: texpression read LitExpression stored false;
   { property Expression: string read FExpression write SetExpression;}
  end;

implementation
  CONSTRUCTOR TCalculateurFonction.Create(Infix:String);
begin
if  _Evaluator<>nil then _Evaluator.Destroy;
if _expression<>nil then _expression.Destroy;
 SetLength(_ListeValeursVariables,0);
  try
    _Expression:=texpression.Create(infix);
  except
   raise   ECalculateurFonctionSyntaxError.Create('Syntaxe de fonction incorrecte');
   exit;
  end;
  _ListeVariables:=_Expression.SymbolicValueNames;
  SetLength(_ListeValeursVariables,_ListeVariables.Count);
 _Evaluator:=TEvaluator.Create(_ListeVariables,_Expression);
    end;


 CONSTRUCTOR TCalculateurFonction.Create(Infix:String; ListeVariablesAutorisees:TableauChaine; NombreVariables:integer); overload;
 var i:integer;  LVA:TStringlist;
 begin
 if  _Evaluator<>nil then _Evaluator.Destroy;
if _expression<>nil then _expression.Destroy;
 SetLength(_ListeValeursVariables,0);
  try
    _Expression:=texpression.Create(infix);
  except
   raise   ECalculateurFonctionSyntaxError.Create('Syntaxe de fonction incorrecte');
   exit;
  end;
  _ListeVariables:=_Expression.SymbolicValueNames;
  LVA:=TStringlist.Create;
  for i:=1 to NombreVariables do LVA.Add(ListeVariablesAutorisees[i-1]);
  for i:=1 to _ListeVariables.Count do
    if LVA.IndexOf(_ListeVariables[i-1])=-1 then begin
     LVA.Destroy;
     raise   ECalculateurFonctionListeVariablesError.Create('Variables non autorisées');
   exit;
  end;
  SetLength(_ListeValeursVariables,_ListeVariables.Count);
 _Evaluator:=TEvaluator.Create(_ListeVariables,_Expression);
  end;

  CONSTRUCTOR TCalculateurFonction.Create(Infix:String; ListeVariablesAutorisees:TStringList); overload;
   var i:integer;
     begin
    if  _Evaluator<>nil then _Evaluator.Destroy;
if _expression<>nil then _expression.Destroy;
 SetLength(_ListeValeursVariables,0);
   try
    _Expression:=texpression.Create(infix);
  except
   raise   ECalculateurFonctionSyntaxError.Create('Syntaxe de fonction incorrecte');
   exit;
  end;
  _ListeVariables:=_Expression.SymbolicValueNames;
   for i:=1 to _ListeVariables.Count do
    if ListeVariablesAutorisees.IndexOf(_ListeVariables[i-1])=-1 then begin
     raise   ECalculateurFonctionListeVariablesError.Create('Variables non autorisées');
   exit;
  end;
   SetLength(_ListeValeursVariables,_ListeVariables.Count);
   _Evaluator:=TEvaluator.Create(_ListeVariables,_Expression);
     end;


  destructor TCalculateurFonction.Destroy;
   begin
    if _Evaluator<>nil then _Evaluator.Destroy;
  if _Expression<>nil then _Expression.Destroy;
 inherited destroy;

   end;

  procedure TCalculateurFonction.SetVar(const VarName: string; const Value: float);
   var index:integer;
   begin

  index:=_ListeVariables.IndexOf(UpCase(VarName));
  if index<>-1 then  _ListeValeursVariables[index]:=Value;
    end;

 function TCalculateurFonction.GetVariable(const VarName: string): float;
  var index:integer;
   begin

  index:=_ListeVariables.IndexOf(UpCase(VarName));
  if index<>-1 then  result:=_ListeValeursVariables[index];
    end;


 function TCalculateurFonction.CalcValue: arbfloat;
 var resultat:arbfloat;
 begin
 try
 resultat:=_Evaluator.Evaluate(_ListeValeursVariables);
 except
  result:=NAN;
  exit;
 end;
  result:=resultat;
 end;

 function TCalculateurFonction.LitListeVariables:TStringList;
 begin
 result:=_ListeVariables;
 end;

 function TCalculateurFonction.LitExpression:TExpression;
 begin
 result:=_Expression;
 end;


end.