3个回答
展开全部
可以使用异或来一个巧妙的解决,代码如下(仅仅贴单元文件)
(Delphi 7 + Windows XP sp2)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure Swap(var a:integer;var b: integer);
begin
a := a xor b;
b := a xor b;
a := a xor b;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
n1,n2:integer;
begin
n1 := 100;
n2 := 200;
ShowMessage('n1='+IntToStr(n1) + ' ' + 'n2=' + IntToStr(n2));
Swap(n1,n2);
ShowMessage('n1='+IntToStr(n1) + ' ' + 'n2=' + IntToStr(n2));
end;
end.
(Delphi 7 + Windows XP sp2)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure Swap(var a:integer;var b: integer);
begin
a := a xor b;
b := a xor b;
a := a xor b;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
n1,n2:integer;
begin
n1 := 100;
n2 := 200;
ShowMessage('n1='+IntToStr(n1) + ' ' + 'n2=' + IntToStr(n2));
Swap(n1,n2);
ShowMessage('n1='+IntToStr(n1) + ' ' + 'n2=' + IntToStr(n2));
end;
end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
举个例子:
var a, b : string; list,list1 : tstrings;
begin
a := 'eee';
b := 'aaa';
list := tstringlist.Create;
list1 := tstringlist.Create;
list.Add(a);
list1.Add(b);
a := list1.Text;
b := list.Text;
end;
var a, b : string; list,list1 : tstrings;
begin
a := 'eee';
b := 'aaa';
list := tstringlist.Create;
list1 := tstringlist.Create;
list.Add(a);
list1.Add(b);
a := list1.Text;
b := list.Text;
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
a := a+b;
b := a-b;
a := a-b;
b := a-b;
a := a-b;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询