利用Pos~~
定义数组,然后分别查找四则运算可能用到的符号~~找到一个就把之前的数字符号存到数组里面~~最后在写成算术表达式~~
定义数组,然后分别查找四则运算可能用到的符号~~找到一个就把之前的数字符号存到数组里面~~最后在写成算术表达式~~
解决方案 »
- delphi 关于继承
- gdi+中的超难问题
- 我想了解一下,人家做的客户端防火墙拦截的时候,提示的是DNS请求
- keybd_event模拟游戏按键出现问题,请高手指点.
- 如何向自身程序写入数据??????????????????????//急!!!!!
- delphi中IntraWeb的FishFact例子,为什么我每次只能同时连接7个用户的??
- 用FindNearest进行查询的问题?
- 如何对access表定义别名?
- dll是否可以解决查询大数据时的动画问题
- 请教:分析一段小代码
- 图标提取器1.0源代码发布,http://icnw.myrice.com或http://icnwgreen.go.163.com !
- 关于数据库复制
1、用二叉数或堆栈(数值栈和运算符栈)将表达式转换成逆波兰式计算;
2、用第三方的控件(只要你能找到);
3、建立一个excel的对象,将你的表达式放入excel的cell中,让excel帮你计算,简单极了。
uses
Math;procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
var
L, R: Integer;
I: Integer;
B: Boolean;
begin
nLStr := '';
nCStr := '';
nRStr := '';
B := True;
L := 0;
R := 0;
for I := 1 to Length(mText) do
if B then begin
if mText[I] = '(' then
Inc(L)
else if mText[I] = ')' then
Inc(R);
if L = 0 then
nLStr := nLStr + mText[I]
else if L > R then
nCStr := nCStr + mText[I]
else B := False;
end else nRStr := nRStr + mText[I];
Delete(nCStr, 1, 1);
end; { Bracket }function Calc(mText: string): string;
var
vText: string; function fCalc(mText: string): string;
var
vLStr, vCStr, vRStr: string;
I, J, K, L: Integer;
begin
L := Length(mText);
if Pos('(', mText) > 0 then begin
Bracket(mText, vLStr, vCStr, vRStr);
Result := Calc(vLStr + fCalc(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 := L;
if J = 0 then J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) + StrToFloat(fCalc(vRStr)))
else Result := FloatToStr(StrToFloat(fCalc(vLStr)) - StrToFloat(fCalc(vRStr)))
end else if (Pos('*', mText) > 0) or (Pos('/', mText) > 0) then begin
I := Pos('*', mText);
J := Pos('/', mText);
if I = 0 then I := L;
if J = 0 then J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) * StrToFloat(fCalc(vRStr)))
else Result := FloatToStr(StrToFloat(fCalc(vLStr)) / StrToFloat(fCalc(vRStr)))
end else if Pos('_', mText) = 1 then
Result := FloatToStr(-StrToFloat(fCalc(Copy(mText, 2, L))))
else Result := FloatToStr(StrToFloat(mText));
end;
var
I, L: Integer;
begin
vText := '';
L := Length(mText);
for I := 1 to L do
if (mText[I] = '-') and (I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')'])) then
if (I = 1) or ((I > 1) and (mText[Pred(I)] in ['*', '/'])) then
vText := vText + '_'
else if (I > 1) and (mText[Pred(I)] in ['+', '-']) then
vText := vText + '+_'
else vText := vText + mText[I]
else vText := vText + mText[I];
Result := fCalc(vText);
end; { Calc }
Math;procedure Bracket(mText: string; var nLStr, nCStr, nRStr: string);
var
L, R: Integer;
I: Integer;
B: Boolean;
begin
nLStr := '';
nCStr := '';
nRStr := '';
B := True;
L := 0;
R := 0;
for I := 1 to Length(mText) do
if B then begin
if mText[I] = '(' then
Inc(L)
else if mText[I] = ')' then
Inc(R);
if L = 0 then
nLStr := nLStr + mText[I]
else if L > R then
nCStr := nCStr + mText[I]
else B := False;
end else nRStr := nRStr + mText[I];
Delete(nCStr, 1, 1);
end; { Bracket }function Calc(mText: string): string;
var
vText: string; function fCalc(mText: string): string;
var
vLStr, vCStr, vRStr: string;
I, J, K, L: Integer;
begin
L := Length(mText);
if Pos('(', mText) > 0 then begin
Bracket(mText, vLStr, vCStr, vRStr);
Result := Calc(vLStr + fCalc(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 := L;
if J = 0 then J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) + StrToFloat(fCalc(vRStr)))
else Result := FloatToStr(StrToFloat(fCalc(vLStr)) - StrToFloat(fCalc(vRStr)))
end else if (Pos('*', mText) > 0) or (Pos('/', mText) > 0) then begin
I := Pos('*', mText);
J := Pos('/', mText);
if I = 0 then I := L;
if J = 0 then J := L;
K := Min(I, J);
vLStr := Copy(mText, 1, Pred(K));
vRStr := Copy(mText, Succ(K), L);
if vLStr = '' then vLStr := '0';
if vRStr = '' then vRStr := '0';
if I = K then
Result := FloatToStr(StrToFloat(fCalc(vLStr)) * StrToFloat(fCalc(vRStr)))
else Result := FloatToStr(StrToFloat(fCalc(vLStr)) / StrToFloat(fCalc(vRStr)))
end else if Pos('_', mText) = 1 then
Result := FloatToStr(-StrToFloat(fCalc(Copy(mText, 2, L))))
else Result := FloatToStr(StrToFloat(mText));
end;
var
I, L: Integer;
begin
vText := '';
L := Length(mText);
for I := 1 to L do
if (mText[I] = '-') and (I < L) and (not (mText[Succ(I)] in ['+', '-', '(', ')'])) then
if (I = 1) or ((I > 1) and (mText[Pred(I)] in ['*', '/'])) then
vText := vText + '_'
else if (I > 1) and (mText[Pred(I)] in ['+', '-']) then
vText := vText + '+_'
else vText := vText + mText[I]
else vText := vText + mText[I];
Result := fCalc(vText);
end; { Calc }