打印中选择页数的算法,比如1,3,6-10 ( 积分: 10 )

  • 主题发起人 主题发起人 muhx
  • 开始时间 开始时间
M

muhx

Unregistered / Unconfirmed
GUEST, unregistred user!
我的一段算法,有兴趣的朋友可以看看
如果有错误,请EMail给我,谢谢
muhx1981@126.com

打印中选择页数的算法,比如1,3,6-10
type
TByteSet = set of Byte;
{ 从指定格式的字符串中解析出序列序号 }
{ 输入: 字符串}
{ 输出: 解析后的集合 }
{ 功能: 比如'1, 2, 5-7'解析成1, 2, 5, 6, 7 }
{ 2005-12-12 19:05 by muhx}
function DecodeIndexSet(const AString: string): TByteSet;
var
tmpOldStr, tmpFront, tmpBack: string;
i: Byte;
begin
Result := [];
tmpOldStr := Trim(AString);
{ 1, 3-5, 8 }
while Pos(',', tmpOldStr) <> 0 do
begin
DivideStrByDelemiter(tmpOldStr, ',', tmpFront, tmpBack);
tmpOldStr := tmpFront;
{ 8 }
if Pos('-', tmpBack) = 0 then
begin
if ChangeStrToInt(Trim(tmpBack)) < 0 then
begin
Result := [];
Exit;
end
else
Include(Result, ChangeStrToInt(Trim(tmpBack)));
end
{ 3-5 }
else
begin
DivideStrByDelemiter(tmpBack, '-', tmpFront, tmpBack);
if (ChangeStrToInt(Trim(tmpFront)) >= 0) and (ChangeStrToInt(Trim(tmpBack)) >= 0) and
(ChangeStrToInt(Trim(tmpFront)) <= ChangeStrToInt(Trim(tmpBack))) then
for i := ChangeStrToInt(Trim(tmpFront)) to ChangeStrToInt(Trim(tmpBack)) do
Include(Result, i)
else
begin
Result := [];
Exit;
end;
end;
end;
{ '5' }
if (Pos(',', tmpOldStr) = 0) and (Pos('-', tmpOldStr) = 0) then
begin
if ChangeStrToInt(Trim(tmpOldStr)) >= 0 then
Include(Result, ChangeStrToInt(Trim(tmpOldStr)))
else
Result := [];
Exit;
end;
{ '3-5' }
if (Pos(',', tmpOldStr) = 0) and (Pos('-', tmpOldStr) <> 0) then
begin
DivideStrByDelemiter(tmpOldStr, '-', tmpFront, tmpBack);
if (ChangeStrToInt(Trim(tmpFront)) >= 0) and (ChangeStrToInt(Trim(tmpBack)) >= 0) and
(ChangeStrToInt(Trim(tmpFront)) <= ChangeStrToInt(Trim(tmpBack))) then
for i := ChangeStrToInt(Trim(tmpFront)) to ChangeStrToInt(Trim(tmpBack)) do
Include(Result, i)
else
Exit;
end;
end;
 
我的一段算法,有兴趣的朋友可以看看
如果有错误,请EMail给我,谢谢
muhx1981@126.com

打印中选择页数的算法,比如1,3,6-10
type
TByteSet = set of Byte;
{ 从指定格式的字符串中解析出序列序号 }
{ 输入: 字符串}
{ 输出: 解析后的集合 }
{ 功能: 比如'1, 2, 5-7'解析成1, 2, 5, 6, 7 }
{ 2005-12-12 19:05 by muhx}
function DecodeIndexSet(const AString: string): TByteSet;
var
tmpOldStr, tmpFront, tmpBack: string;
i: Byte;
begin
Result := [];
tmpOldStr := Trim(AString);
{ 1, 3-5, 8 }
while Pos(',', tmpOldStr) <> 0 do
begin
DivideStrByDelemiter(tmpOldStr, ',', tmpFront, tmpBack);
tmpOldStr := tmpFront;
{ 8 }
if Pos('-', tmpBack) = 0 then
begin
if ChangeStrToInt(Trim(tmpBack)) < 0 then
begin
Result := [];
Exit;
end
else
Include(Result, ChangeStrToInt(Trim(tmpBack)));
end
{ 3-5 }
else
begin
DivideStrByDelemiter(tmpBack, '-', tmpFront, tmpBack);
if (ChangeStrToInt(Trim(tmpFront)) >= 0) and (ChangeStrToInt(Trim(tmpBack)) >= 0) and
(ChangeStrToInt(Trim(tmpFront)) <= ChangeStrToInt(Trim(tmpBack))) then
for i := ChangeStrToInt(Trim(tmpFront)) to ChangeStrToInt(Trim(tmpBack)) do
Include(Result, i)
else
begin
Result := [];
Exit;
end;
end;
end;
{ '5' }
if (Pos(',', tmpOldStr) = 0) and (Pos('-', tmpOldStr) = 0) then
begin
if ChangeStrToInt(Trim(tmpOldStr)) >= 0 then
Include(Result, ChangeStrToInt(Trim(tmpOldStr)))
else
Result := [];
Exit;
end;
{ '3-5' }
if (Pos(',', tmpOldStr) = 0) and (Pos('-', tmpOldStr) <> 0) then
begin
DivideStrByDelemiter(tmpOldStr, '-', tmpFront, tmpBack);
if (ChangeStrToInt(Trim(tmpFront)) >= 0) and (ChangeStrToInt(Trim(tmpBack)) >= 0) and
(ChangeStrToInt(Trim(tmpFront)) <= ChangeStrToInt(Trim(tmpBack))) then
for i := ChangeStrToInt(Trim(tmpFront)) to ChangeStrToInt(Trim(tmpBack)) do
Include(Result, i)
else
Exit;
end;
end;
 
有兴趣的朋友请帮提一下
希望可以对需要的朋友有用,谢谢
 
大家有谁随便说个话
我随便放个分把帖子结了吧:)
 
muhx,我看了你的代码,比较整洁,流程思路清晰,有价值。我收藏了。
 
后退
顶部