//解决方案二:$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
var vText: string;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function Reckon(mText: string): string;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
var i, CountLeft, CountRight: Integer;
Bool: Boolean;
begin
nLStr := '';
nCStr := '';
nRStr := '';
CountLeft := 0;
CountRight := 0;
Bool := true;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i := 1 to Length(mText) do
if Bool then
begin
if mText = '(' then Inc(CountLeft)
else if mText = ')' then Inc(CountRight);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if CountLeft = 0 then nLStr := nLStr + mText
else if CountLeft > CountRight then nCStr := nCStr + mText
else Bool := false;
end else nRStr := nRStr + mText;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Delete(nCStr, 1, 1);
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var vLStr, vCStr, vRStr: string;
i, j, k, mTextLength: Integer;
begin
mTextLength := Length(mText);
if Pos('(', mText) > 0
then begin
Bracket(mText, vLStr, vCStr, vRStr);
Result := Reckon(vLStr + Reckon(vCStr) + vRStr);
end
else if (Pos('+', mText) > 0) or (Pos('-', mText) > 0)
then begin
i := Pos('+', mText);
j := Pos('-', mText);
if i = 0 then i := mTextLength;
if j = 0 then j := mTextLength;
k := Min(i, j);
vLStr := Copy(mText, 1, Pred(k));
vRStr := Copy(mText, Succ(k), mTextLength);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if i = k
then Result := FloatToStr(StrToFloat(Reckon(vLStr)) + StrToFloat(Reckon(vRStr)))
else Result := FloatToStr(StrToFloat(Reckon(vLStr)) - StrToFloat(Reckon(vRStr)))
end
else if (Pos('*', mText) > 0) or (Pos('/', mText) > 0)
then begin
i := Pos('*', mText);
j := Pos('/', mText);
if i = 0 then i := mTextLength;
if j = 0 then j := mTextLength;
k := Min(i, j);
vLStr := Copy(mText, 1, Pred(k));
vRStr := Copy(mText, Succ(k), mTextLength);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if i = k
then Result := FloatToStr(StrToFloat(Reckon(vLStr)) * StrToFloat(Reckon(vRStr)))
else Result := FloatToStr(StrToFloat(Reckon(vLStr)) / StrToFloat(Reckon(vRStr)))
end
else if Pos('_', mText) = 1
then Result := FloatToStr(-StrToFloat(Reckon(Copy(mText, 2, mTextLength))))
else Result := FloatToStr(StrToFloat(mText));
end;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var i, ExpLength: integer;
begin
vText := '';
ExpLength := Length(Expression);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for i := 1 to ExpLength do
if (Expression = '-') and (i < ExpLength) and (not (Expression[Succ(i)] in ['+', '-', '(', ')']))
then if (i = 1) or ((i > 1) and (Expression[Pred(i)] in ['*', '/']))
then vText := vText + '_'
else if ((i > 1) and (Expression[Pred(i)] in ['+', '-'])) or ((i < ExpLength) and (not (Expression[Succ(i)] in ['+', '-', '(', ')'])))
then vText := vText + '+_'
else vText := vText + Expression
else vText := vText + Expression;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Result := Reckon(vText);
end;