0% found this document useful (0 votes)
99 views

Hassan Math 3

The document describes a Delphi program that defines classes and functions for evaluating mathematical expressions from strings and working with lines. The program defines classes like TLine for representing lines and TEvaluator for evaluating expressions from strings. It also defines functions like Line() to create line objects and StringEval() to evaluate string expressions.

Uploaded by

fb-610722264
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Hassan Math 3

The document describes a Delphi program that defines classes and functions for evaluating mathematical expressions from strings and working with lines. The program defines classes like TLine for representing lines and TEvaluator for evaluating expressions from strings. It also defines functions like Line() to create line objects and StringEval() to evaluate string expressions.

Uploaded by

fb-610722264
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, TXT or read online on Scribd
You are on page 1/ 3

{*********************************************************}

{ Created by Hassan Mahmood }


{ Last updated around mid July 2007 }
{*********************************************************}

unit Hassan_Math_3;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Hassan_Gradient, ComCtrls, math;

type
tRealPoint = record x,y:Real; end;
tLabledConstants = record names:array of strings; values:array of Extended; end;

TLine = class
private
public
m,c:Integer;
constructor Create(gradient,intercept:Integer);
function CalcY(x:Real):Integer;
function intersection(line:Tline):TPoint;
end;

TEvaluator = class
private
fVariables: tLabledConstants; //AnsiIndexText function is needed
f
public
expression:string;

end;

function Line(m,c:integer):TLine;
function StringEval(s:string):Extended;

implementation

constructor TLine.Create(gradient,intercept:Integer);
begin
m:=gradient;
c:=intercept;
end;

function TLine.CalcY(x:real):Integer;
begin
Result:=round(m*x+c);
end;

function TLine.intersection(line:Tline):TPoint;
var x:Real;
begin
x:=(line.c-c)/(m-line.m);
Result:=Point(round(x),CalcY(x));
end;

function Line(m,c:integer):TLine;
begin
Result:=TLine.create(m,c);
end;

function StringEval(s:string):Extended;
var operator:Char; i,p,BracketLevel,x,MaxBL:Integer;
begin
//ShowMessage(s);
// ShowMessage('s = '+s);
MaxBL:=0;
BracketLevel:=0;
x:=999;
p:=0;
if length(s)=0 then result:=0
else
begin
for i:=1 to Length(s) do
begin
// ShowMessage(IntToStr(i));
case s[i] of
'(':begin Inc(BracketLevel); inc(MaxBL); end;
')':dec(BracketLevel);
'-':if not(s[i-1] in ['-','+','*','/']) then if (BracketLevel*10<=x) then
begin p:=i; x:=BracketLevel*10; end;
'+':if not(s[i-1] in ['-','+','*','/']) then if (BracketLevel*10<=x) then
begin p:=i; x:=BracketLevel*10+1; end;
'*':if (BracketLevel*10+2<=x) then begin p:=i; x:=BracketLevel*10+2; end;
'/':if (BracketLevel*10+3<=x) then begin p:=i; x:=BracketLevel*10+3; end;
'^':if (BracketLevel*10+4<=x) then begin p:=i; x:=BracketLevel*10+4; end;
end;
end;

BracketLevel:=x div 10;


if ((BracketLevel>0) and (x<999)) then
begin
s:=copy(s,BracketLevel+1,length(s)-BracketLevel*2);
// ShowMessage('s [changed] = '+s);
p:=p-BracketLevel;
end;

// ShowMessage('p = '+IntToStr(p));

if p<=0 then
begin
if MaxBL>0 then s:=copy(s,MaxBL+1,length(s)-MaxBL*2);
result:=StrToFloat(s);
end
else
begin
operator:=s[P];
case operator of
'-':result:=StringEval(copy(s,1,p-1))-StringEval(copy(s,p+1,length(s)));
'+':result:=StringEval(copy(s,1,p-1))+StringEval(copy(s,p+1,length(s)));
'*':result:=StringEval(copy(s,1,p-1))*StringEval(copy(s,p+1,length(s)));
'/':result:=StringEval(copy(s,1,p-1))/StringEval(copy(s,p+1,length(s)));
'^':result:=power(StringEval(copy(s,1,p-
1)),StringEval(copy(s,p+1,length(s))));
end;
end;
end;

end;

end.

You might also like