首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

现在要函数(delphi),在线急等MMMMM

2012-03-31 
现在要求一个函数(delphi),在线急等MMMMM现在要求一个函数(delphi)输入一个字符串变量如 a1,3,4,6-8,15,1

现在要求一个函数(delphi),在线急等MMMMM
现在要求一个函数(delphi)

输入一个字符串变量如 a=1,3,4,6-8,15,13-14,14
写一个这样的函数
function getYm(str:string):Tstringlist;
得到页码
返回:1,3,4,6,7,8,15,13,14,14

[解决办法]
很简单的阿,为什么自己不动手呢???BS

Delphi(Pascal) code
function AddSubRange(AList: TStrings; const Data: String): Integer;var  iPos: Integer;  iBegin, iEnd: Integer;begin  Result := 0;  if Assigned(AList) then  begin    iPos := Pos('-', Data);    if iPos <> 0 then    begin      iBegin := StrToIntDef(Copy(Data, 1, iPos - 1), MaxInt);      iEnd := StrToIntDef(Copy(Data, iPos + 1, MaxInt), 0);      for iPos := iBegin to iEnd do        AList.Add(IntToStr(iPos));    end else AList.Add(Data);  end;end;function GetData(AList: TStrings; const Data: String): Integer;var  FList: TStringList;  I: Integer;begin  Result := 0;  if Assigned(AList) then  begin    FList := TStringList.Create;    try      FList.CommaText := Data;      for I := 0 to FList.Count - 1 do        Result := Result + AddSubRange(AList, FList[I]);    finally      FList.Free;    end;  end;end;procedure TForm1.Button1Click(Sender: TObject);begin  GetData(ListBox1.Items, '1,3,4,6-8,15,13-14,14')end;
[解决办法]
换种方案:

[code=Delphi(Pascal)][/code]

procedure TForm1.Button1Click(Sender: TObject);
var
a : string;
begin
a := Edit1.Text;
a :=StringReplace(a,'-',#13,[rfReplaceAll]);
a :=StringReplace(a,',',#13,[rfReplaceAll]);
ListBox1.Items.Text := a;
end;
[解决办法]
[code=Delphi(Pascal)][/code]
procedure TForm1.Button1Click(Sender: TObject);
begin
 ListBox1.Items.Text := getYm (Edit1.Text);
end;

function TForm1.getYm(str: string): string;
begin
str :=StringReplace(str,'-',#13,[rfReplaceAll]);
str :=StringReplace(str,',',#13,[rfReplaceAll]);
result := str;
end;
[解决办法]
var
Form1: TForm1;
s : string = '1,3,4,6-8,15,13-14,14';

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
function GetData(str: string):String; //3-12
var
i,a,b:integer;
thestr, str1, str2:string;
begin
result:='';
i := Pos('-', str);
str1 := copy(str, 1, i-1);
a := StrToInt(str1);
str2 := copy(str, i+1, Length(str)-i);
b := StrToInt(str2);

for i:=a to b do
begin
if i = b then
thestr := thestr + IntToStr(i)
else
thestr := thestr + IntToStr(i) + ',';
end;
result := thestr;
end;

var
s1:string;
arrayss: array of string;
i, num:integer;
begin
num := 0;
for i:=1 to Length(s) do
if s[i] = ',' then
Inc(num);

SetLength(arrayss, num + 1);

s1 := s;
num := -1;
i:=pos(',', s1);
while i > 0 do
begin
inc(num);
arrayss[num] := copy(s1, 1, i-1);
Delete(s1, 1, i);
i:=pos(',', s1);
if i <= 0 then
begin
arrayss[num+1] := s1;
break;
end;
end;

s1:='';
for i:=0 to high(arrayss) do
begin
if pos('-', arrayss[i]) > 0 then
s1:=s1+GetData(arrayss[i])+','
else
s1:=s1+arrayss[i]+',';
end;

caption:=s1;
end;

[解决办法]
自己动手,自己学习。
授人鱼不如授人以渔。
人如果懒到这种程度,活着还有多少意义?

热点排行