function TForm1.CalculateAll(AExpression: string): string;
var
StartPos, EndPos: Integer; //子表达式的开始位置/结束位置
SubExpression: string; //子表达式
ResultValue: string; //计算结果
begin
while Length(AExpression) > 0 do
begin
SubExpression := GetExpression(StartPos, EndPos, AExpression);
ResultValue := Calculate(SubExpression);
AExpression := Format('%s%s%s', [Copy(AExpression, 1, StartPos - 1), ResultValue,
Copy(AExpression, EndPos + 1, MaxInt)]);
if (Pos('*', AExpression) = 0) and
(Pos('/', AExpression) = 0) and
(Pos('+', AExpression) = 0) and
(Pos('-', AExpression) = 0) then Break; //一个正数时
if (Pos('*', AExpression) = 0) and
(Pos('/', AExpression) = 0) and
(Pos('+', AExpression) = 0) and
(Pos('-', AExpression) = 1) then Break; //一个负数时
end;
Result := AExpression;
end;
function TForm1.CalculateEx(FirstNum, SecondNum, ASign: string): string;
var
fFirstNum, fSecondNum: Double;
begin
Result := '';
fFirstNum := StrToFloat(FirstNum);
fSecondNum := StrToFloat(SecondNum);
if ASign = '+' then
Result := FloatToStr(fFirstNum + fSecondNum);
if ASign = '-' then
Result := FloatToStr(fFirstNum - fSecondNum);
if ASign = '*' then
Result := FloatToStr(fFirstNum * fSecondNum);
if ASign = '/' then
Result := FloatToStr(fFirstNum / fSecondNum);
end;
{取括号内表达式}
function TForm1.GetExpression(var StartPos, EndPos: Integer; AExpression: string): string;
begin
if Pos('(', AExpression) > 0 then
begin
StartPos := GetLastSignPos('(', AExpression);
EndPos := GetLatestSignPos(StartPos, AExpression);
Result := Copy(AExpression, StartPos + 1, EndPos - StartPos - 1);
end
else
begin
StartPos := 1;
EndPos := Length(AExpression);
Result := AExpression;
end;
end;
function TForm1.GetLatestSignPos(StartPos: Integer;
AExpression: string): Integer;
var
iCount: Integer;
sLenght: Integer; //字符串长度
AExpr: string[250];
begin
Result := -1;
AExpr := AExpression;
sLenght := Ord(AExpr[0]);
for iCount := StartPos to sLenght do
begin
if AExpr[iCount] = ')' then
begin
Result := iCount;
Break;
end;
end;
end;
{取特定字符在表达式里的最后出现的位置}
function TForm1.GetLastSignPos(ASign, AExpression: string): Integer;
var
iCount: Integer;
sLenght: Integer; //字符串长度
AExpr: string[250];
begin
Result := -1;
AExpr := AExpression;
sLenght := Ord(AExpr[0]);
for iCount := 1 to sLenght do
begin
if AExpr[iCount] = ASign then
Result := iCount;
end;
end;
{在一个字符串里,根据指定的位置,向前/后取一个数字}
function TForm1.GetNumber(var ResultPos: Integer; AExpression: string;
Position: Integer; Direction: TDirection): string;
var
AExpr: string[250];
iCount: Integer;
AValue: string;
SpecialPos: Integer; //特殊位置
begin
AExpr := AExpression;
iCount := Position;
AValue := '';
case Direction of
dcBack:
begin
Dec(iCount);
while (iCount > 0) do
begin
if (iCount = 1) and (AExpr[iCount] = '-') then
begin
AValue := Format('%s%s', [AExpr[iCount], AValue]);
ResultPos := iCount;
Break;
end;
if (AExpr[iCount] = '+') or
(AExpr[iCount] = '-') or
(AExpr[iCount] = '*') or
(AExpr[iCount] = '/') then Break;
AValue := Format('%s%s', [AExpr[iCount], AValue]);
ResultPos := iCount;
Dec(iCount);
end;
end;
dcForward:
begin
Inc(iCount);
SpecialPos := iCount;
while iCount <= Ord(AExpr[0]) do
begin
if (iCount = SpecialPos) and (AExpr[iCount] = '-') then //乘(除)后面跟一个负数时
begin
AValue := Format('%s%s', [AValue, AExpr[iCount]]);
ResultPos := iCount;
Inc(iCount);
Continue;
end;
if (AExpr[iCount] = '+') or
(AExpr[iCount] = '-') or
(AExpr[iCount] = '*') or
(AExpr[iCount] = '/') then Break;
AValue := Format('%s%s', [AValue, AExpr[iCount]]);
ResultPos := iCount;
Inc(iCount);
end;
end;
end;
Result := AValue;
end;
{判断一个表达式是否为一个负数}
function TForm1.IsANegative(AExpression: string): Boolean;
var
OutVal: Double;
begin
Result := TryStrToFloat(AExpression, OutVal);
Result := OutVal < 0;
end;
type
TDLLForm = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
DLLForm: TDLLForm;
implementation
{$R *.dfm}
procedure SepNum(SourceS:string;var destS:array of string);
var
s1,s2:string;
i,j:integer;
begin
s1:=sourceS;
j:=1;
for i := 1 to length(s1) do
begin
if s1[i] in ['+','-','*','/','(',')'] then
begin
dests[j]:=s1[i];
inc(j);
end;
if s1[i] in ['0'..'9'] then
begin
dests[j]:=dests[j]+s1[i];
if i<length(s1) then
if s1[i+1] in ['+','-','*','/','(',')'] then
inc(j);
end;
end;
end;
procedure Ps(var S:array of string);
var
i,j:integer;
temp:array of string;
begin
j:=1;
setlength(temp,high(s));
for I := 1 to high(s) do
if s[i]<>'' then
begin
temp[j]:=s[i];
inc(j);
end;
for i := 1 to high(temp) do
s[i]:=temp[i];
end;
// 做* / 运算;
procedure sf1(var s1:array of string);
var
i,j:integer;
begin
for I := 2 to high(s1)-1 do
begin
if s1[i]='*' then
if (s1[i-1]<>'(') and (s1[i-1]<>')') and (S1[i+1]<>')') and (S1[i+1]<>'(') then
begin
s1[i+1]:=floattostr(strtofloat(s1[i-1])*strtofloat(s1[i+1]));
s1[i-1]:='';
s1[i]:='';
end;
if s1[i]='/' then
if (s1[i-1]<>'(') and (s1[i-1]<>')') and (S1[i+1]<>')') and (S1[i+1]<>'(') then
begin
s1[i+1]:=floattostr(strtofloat(s1[i-1])/strtofloat(s1[i+1]));
s1[i-1]:='';
s1[i]:='';
end;
end;
end;
// 做+ /
procedure sf2(var s1:array of string);
var
i:integer;
begin
for i := 2 to high(s1)-1 do
begin
if s1[i]='+' then
if (s1[i-1]<>'(') and (s1[i-1]<>')') and (S1[i+1]<>')') and (S1[i+1]<>'(') and (s1[i-2]<>'*') and (s1[i-2]<>'/') and (s1[i+2]
<>'*') and (s1[i+2]<>'/') then
begin
s1[i+1]:=floattostr(strtofloat(s1[i-1])+strtofloat(s1[i+1]));
s1[i-1]:='';
s1[i]:='';
end;
if s1[i]='-' then
if (s1[i-1]<>'(') and (s1[i-1]<>')') and (S1[i+1]<>')') and (S1[i+1]<>'(')and (s1[i-2]<>'*') and (s1[i-2]<>'/') and (s1[i+2]
<>'*') and (s1[i+2]<>'/') then
begin
s1[i+1]:=floattostr(strtofloat(s1[i-1])-strtofloat(s1[i+1]));
s1[i-1]:='';
s1[i]:='';
end;
end;
end;
// 去括号
procedure sf3(var s1:array of string);
var
i:integer;
begin
for i := 1 to high(s1)-2 do
if (s1[i]='(') and (s1[i+2]=')') then
begin
s1[i]:='';
s1[i+2]:='';
end;
end;
procedure TDLLForm.Button1Click(Sender: TObject);
var
s1:string;
s2:array of string;
i:integer;
begin
s1:=edit1.Text;
setlength(s2,length(s1)+2);
sepNum(s1,s2);
repeat
sf1(s2); // * /
ps(s2);
sf2(s2); // + -
ps(s2);
sf3(s2);
ps(s2);
until s2[2]=''; //是否还有括号
edit1.Text:=s2[1];
end;