T
Tspeng
Unregistered / Unconfirmed
GUEST, unregistred user!
procedure ProcA(vcExpress:string);
var
AStackSrc,AStackDst:TStack;
i,iCount:integer
AChar:Char
pAChar:^Char
pAItemChar
AString,AStringRet:String
pAString:^String
begin
Try
AStackSrc:=TStack.Create
AStackDst:=TStack.Create
//================================
for iCount:=0 to Length(vcExpress) do
begin
pAChar:=@vcExpress[iCount]
AStackSrc.Push(pAChar)
end
//================================
iCount:=AStackSrc.Count
for i:=0 to iCount-1 do
begin
pAChar:=AStackSrc.Pop
AChar:=pAChar^
AStackDst.Push(pAChar)
if AChar=LBracket then
Begin
// new(pAString)
Repeat
// new(pAItem)
pAItem:=AStackDst.Pop
AString:=AString+pAItem^
Until (pAItem^=RBracket)
AStringRet:=AString
// pAString:=AllocMem(Length(AStringRet));
// strCopy(pAString,pChar(AStringRet))
pAString:=@AStringRet
AStackDst.Push(pAString)
AString:=''
memo1.Lines.Add(pAString)
End
end
Finally
AStackSrc.Free
AStackDst.Free
End
End;
一个简单的解析表达式的过程
用表达式 vcExpress:='(((A+B)/(C+D+E)>15)and((D+E/(F+B))<16))'测试的结果:
(F+B)
(D+E/()
(<<16)
(C+D+E)
(A+B)
(??15)
(豠nd?
如果正确的话应该是如下的结果:
(F+B)
(D+E/(F+B))
((D+E/(F+B))<16)
(C+D+E)
(A+B)
((A+B)/(C+D+E)>15)
(((D+E/(F+B))<16)and((A+B)/(C+D+E)>15))
我估计问题出在
pAItem:=AStackDst.Pop
AString:=AString+pAItem^;
而在AString入栈之后,再pop的时候,取得的不是当初入栈的字符串了;
var
AStackSrc,AStackDst:TStack;
i,iCount:integer
AChar:Char
pAChar:^Char
pAItemChar
AString,AStringRet:String
pAString:^String
begin
Try
AStackSrc:=TStack.Create
AStackDst:=TStack.Create
//================================
for iCount:=0 to Length(vcExpress) do
begin
pAChar:=@vcExpress[iCount]
AStackSrc.Push(pAChar)
end
//================================
iCount:=AStackSrc.Count
for i:=0 to iCount-1 do
begin
pAChar:=AStackSrc.Pop
AChar:=pAChar^
AStackDst.Push(pAChar)
if AChar=LBracket then
Begin
// new(pAString)
Repeat
// new(pAItem)
pAItem:=AStackDst.Pop
AString:=AString+pAItem^
Until (pAItem^=RBracket)
AStringRet:=AString
// pAString:=AllocMem(Length(AStringRet));
// strCopy(pAString,pChar(AStringRet))
pAString:=@AStringRet
AStackDst.Push(pAString)
AString:=''
memo1.Lines.Add(pAString)
End
end
Finally
AStackSrc.Free
AStackDst.Free
End
End;
一个简单的解析表达式的过程
用表达式 vcExpress:='(((A+B)/(C+D+E)>15)and((D+E/(F+B))<16))'测试的结果:
(F+B)
(D+E/()
(<<16)
(C+D+E)
(A+B)
(??15)
(豠nd?
如果正确的话应该是如下的结果:
(F+B)
(D+E/(F+B))
((D+E/(F+B))<16)
(C+D+E)
(A+B)
((A+B)/(C+D+E)>15)
(((D+E/(F+B))<16)and((A+B)/(C+D+E)>15))
我估计问题出在
pAItem:=AStackDst.Pop
AString:=AString+pAItem^;
而在AString入栈之后,再pop的时候,取得的不是当初入栈的字符串了;