Delphi5 代码转 C++ Builder5-6,请各位跨越这个界限的高手看看,谢谢!

李志林HZ 2002-11-11 11:24:30
///////源代码开始
uses
Math;

function StrLeft(const mStr: string; mDelimiter: string): string;
begin
Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
end; { StrLeft }

function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;
var
I, L, K: Integer;
begin
L := Length(mList);
I := Pos(mDelimiter, mList);
K := 0;
Result := '';
while (I > 0) and (K <> mIndex) do begin
mList := Copy(mList, I + Length(mDelimiter), L);
I := Pos(mDelimiter, mList);
Inc(K);
end;
if K = mIndex then Result := StrLeft(mList + mDelimiter, mDelimiter);
end; { ListValue }

function StringGridToText(mStringGrid: TStringGrid;
mStrings: TStrings): Boolean;
var
I, J: Integer;
T: string;
begin
Result := False;
if (not Assigned(mStringGrid)) or (not Assigned(mStrings)) then Exit;
with mStringGrid do try
mStrings.Clear;
for J := 0 to RowCount - 1 do begin
T := '';
for I := 0 to ColCount - 1 do
T := T + #9 + Cells[I, J];
Delete(T, 1, 1);
mStrings.Add(T);
end;
except
Exit;
end;
Result := True;
end; { StringGridToText }

function TextToStringGrid(mStrings: TStrings;
mStringGrid: TStringGrid): Boolean;
var
I, J: Integer;
T: string;
begin
Result := False;
if (not Assigned(mStringGrid)) or (not Assigned(mStrings)) then Exit;
with mStrings, mStringGrid do try
for I := 0 to ColCount - 1 do begin
T := '';
for J := 0 to Min(RowCount - 1, Count - 1) do
Cells[I, J] := ListValue(Strings[J], I, #9);
end;
except
Exit;
end;
Result := True;
end; { TextToStringGrid }
///////源代码结束

///////使用示例开始
procedure TForm1.Button1Click(Sender: TObject);
begin
StringGridToText(StringGrid1, Memo1.Lines);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
TextToStringGrid(Memo1.Lines, StringGrid1);
end;
///////使用示例结束

菜鸟我试了一下,没成功,请各位帮个忙,谢谢!
...全文
62 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
myy 2002-11-12
  • 打赏
  • 举报
回复
你能不能好好自己调试一下?!
李志林HZ 2002-11-12
  • 打赏
  • 举报
回复
哎,编译是过了,可是运行结果不是我要的效果,它竟然不会分布在网格的各个单元,而是集中在第一列!各位说是什么原因?
copy_paste 2002-11-12
  • 打赏
  • 举报
回复
RichEditToStringGrid
函数中,那个List->Clear()把它去了,不然没数据就没法转了。
我顺手打的,呵呵。
copy_paste 2002-11-12
  • 打赏
  • 举报
回复
bool RichEditToStringGrid(TStringGrid *Grid, TStrings *List)
{
if ((!Grid) || (!List)) return false;
List->BeginUpdate();
try
{
List->Clear();
for (int i = 0; i < Grid->ColCount; i++)
for (int j = 0; j < Grid->RowCount; j++)
Grid->Cells[i][j] = ListValue(List->Strings[j], i, '\9');
}
__finally
{
List->EndUpdate();
}
return true;
}

bool StringGridToRichEdit(TStringGrid *Grid, TStrings *List)
{
if((!Grid) || (!List)) return false;
List->BeginUpdate();
try
{
List->Clear();
for (int j = 0; j < Grid->RowCount; j++)
{
String S = EmptyStr;
for (int i = 0; i < Grid->ColCount; i++)
S = S + '\9' + Grid->Cells[i][j];
S.Delete(1, 1);
List->Add(S);
}
}
__finally
{
List->EndUpdate();
}
return true;
}
李志林HZ 2002-11-12
  • 打赏
  • 举报
回复
To:copy_paste(木石三) 如果有时间,帮我改一下我上面翻译的程序,我编译过不了,谢谢!
copy_paste 2002-11-12
  • 打赏
  • 举报
回复
还是有小错,我真是小错不断啊。。。
while ((c = *++p) != '\0')

while ((c = *p++) != '\0')
Huy 2002-11-12
  • 打赏
  • 举报
回复
请问有没有办法将BCB写的VCL安装到delphi啊,高手给点意见。
李志林HZ 2002-11-12
  • 打赏
  • 举报
回复
搞完了,谢谢大家!
李志林HZ 2002-11-11
  • 打赏
  • 举报
回复
我翻译的程序,请大家指点,分数分布为问题后所示,谢谢:

AnsiString __fastcall TForm1::StrLeft(const AnsiString mStr,AnsiString mDelimiter)
{
//
AnsiString TempStr;
int i=mStr.Pos(",");
TempStr=mStr.SubString(1,i-1).Trim();
return TempStr;
}
(10)

bool __fastcall TForm1::RichEditToStringGrid(TStrings *mStrings,TStringGrid *mStringGrid)
{
//
int i,j;
AnsiString t;
if(!mStringGrid||!mStrings)return false;
else
{
for(i=0;i<mStringGrid->ColCount;i++)
{
t="";
for(j=0;j<min(mStringGrid->RowCount,mStrings->Count);j++)
mStringGrid->Cells[i,j]=ListValue(mStrings[j],i,#9);

}
}
return true;
}

(20)

bool __fastcall TForm1::StringGridToRichEdit(TStringGrid *mStringGrid,TStrings *mStrings)
{
//
int i,j;
AnsiString t;

if(!mStringGrid||!mStrings)return false;
else
{
mStrings->Clear();
for(j=0;j<mStringGrid->RowCount;j++)
{
t="";
for(i=0;i<mStringGrid->ColCount;i++)
t=t+#9+mStringGrid->Cells[i,j];
t=t.Delete(1,1);
mStrings->Add(t);
}
}
return true;
}
(20)

ListValue(10)哪位老兄帮忙重写一下,谢谢!
RomanticProgrammer 2002-11-11
  • 打赏
  • 举报
回复
MIN是系统定义的宏...
你可以在头文件中加上:
#define MIN(a,b) ((a)>(b)?(b):(a))
#9就是\0x9//9的十进制和16十一样的.
copy可用SubString代替.
我马上要去吃饭.
pzoon 2002-11-11
  • 打赏
  • 举报
回复
太长了,你拣一些短的压
李志林HZ 2002-11-11
  • 打赏
  • 举报
回复
MIN,#9系统不认,POS,COPY操作我会,STRINGGRID的操作我会,不过就是转过来以后怎么调试都不过。

最好您能将原代码部分翻译过来,现在是午休时间嘛!....:)
RomanticProgrammer 2002-11-11
  • 打赏
  • 举报
回复
你可以把你不会改的部分提出来我可以告诉你..嘻嘻..
RomanticProgrammer 2002-11-11
  • 打赏
  • 举报
回复
很简单.不过太长了..没有那么多的时间..呵呵..
李志林HZ 2002-11-11
  • 打赏
  • 举报
回复
是网格和MEMO间的转换。
copy_paste 2002-11-11
  • 打赏
  • 举报
回复
上面有点小错,:)
String ListValue(const String List, int Index, char Delimiter = ',')
{
int I = 0;
char *p = List.c_str(), c = *p;
String Result = EmptyStr;
while ((c = *++p) != '\0')
{
if (c == Delimiter)
{
I++;
continue;
}
if (I == Index)
Result += c;
if (I > Index) break;
}
return Result;
}
copy_paste 2002-11-11
  • 打赏
  • 举报
回复
String ListValue(const String List, int Index, char Delimiter = ',')
{
int I = 0;
char *p = List.c_str(), c = *p;
String Result = EmptyStr;
while (c != '\0')
{
if (c == Delimiter) I++;
if (I == Index)
{
Result += c;
}
if (I > Index) break;
c = *++p;
}
return Result;
}
copy_paste 2002-11-11
  • 打赏
  • 举报
回复
看看这篇文章,对字符串的了解会多一点。
http://www.delphibbs.com/delphibbs/dispq.asp?LID=1411217
bittcn 2002-11-11
  • 打赏
  • 举报
回复
时间不多,没有完成,也没有验证,请原谅。
bittcn 2002-11-11
  • 打赏
  • 举报
回复
AnsiString ListValue(AnsiString mList,int mIndex,AnsiString mDelimiter = ",")
{
int I, L, K = 0;
L = mList.Length();
I = mList.Pos(mDelimiter);
while (I > 0) and (K < mIndex)
{
mList = mList.SubString(mList,I + mDelimiter.Length(),L);
I := Pos(mDelimiter, mList);
K++;
}
if(K = mIndex)
return StrLeft(mList + mDelimiter, mDelimiter);
}
加载更多回复(3)

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧