求一动态累加算法 ( 积分: 50 )

  • 主题发起人 kun.hong
  • 开始时间
K

kun.hong

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个数组,a1,a2,a3至到an,但是其中的n值不确定,现在如果要计算a1+a2+a3+a4+...+an的值,在delphi中应该采用什么方法比较好?是递归函数还是用for语句?
 
var
i,c:Integer;
a:array of Integer;
begin
...
c:=0;
for i:=Low(a) to High(a)do
c:=c+a;
//Done!
...
end;
 
int i;
i = length(ArrayA);
//ArrayA是你的数组,用length就可以得到数组长度
for xh = 0 to i-1do
......
 
尊敬的creation-zy大侠,再次非常感谢你的回答。我现在遇到一个难题,本来想和你邮件沟通,但是我实在不知道你的邮件只好用这种方式向你请教了!
上次我问了一个动态创建edit和label的问题,在您的大力帮助下终于可以顺利创建了,但我却又在这个地方遇到了一个新问题,我的本意是想在动态创建后的edit中填入数字然后计算平均值,我首先定义了一个数组,并通过累加来计算平均值,但是我现在才发现我定义了数组之后无法通过用s[0]:=edit1.text;s[1]:=edit2;的语句来获取每个动态创建后的edit中text的值,因为我首先就无法通过编译,错误提示为根本没有定义edit1和edit2等等,请问我该如何解决这个问题?是否是我的思路有问题?
期待您伸出援助之手!!!
 
尊敬的creation-zy大侠,尊敬的,
楼主真可爱 ^_^
递归的本质就是循环。大侠写了个循环,,既然进来了,
我就来写个递归吧
function sum(a,n):integer;
begin
if n<0 then
exit;
result:= a[n-1]+ sum(a,n-1);
end;
 
creation-zy大侠,我是在自动创建好n个edit的form中自己设定了一个button,并通过编辑这个button的onclick事件将所有已经创建的edit中的值传递给一个数组,但是在通过用s[1]:=edit1.text;语句将edit中的数值赋给数组的时候总是提示没有定义edit1,不管我在哪个form上定义form2上的edit的时候始终无法通过编译,错误仍然是没有定义对应的edit,现在只好请问大侠您,这个到底能不能实现?
 
当然有问题,控件都没创建,还什么s[1]:=edit1.text,
你换个方式做:
form1.onbuttonclick(sender:tobject)
var
i: integer;
begin
for i:= 0 to self.components.count-1do
if component is Tedit then
s:= Tedit(component).text;
end;
//这样就行了;方法很多,你自己再去想想有没其他的实现方案
 
lisongmagic大侠,请问你提供的这段代码是否是自己定义的一个过程?还是button的onclick事件?
 
唉,什么大侠的,只是比你早一点罢了
不是自定义,是button的onclick事件
 
动态创建的edit,你直接引用当然有问题了,只能通过FindComponent来操作吧
或者可以考虑把动态创建的edit和对应的button之间建立一个关联
比如button1.tag=longint(edit1)
之后就可以通过TEdit(button1.tag)来引用了...
具体用法可以参考开发指南中关于消息那章的最后一个例子.
 
你是说在我自己设定的button的onclick事件里面进行上面的定义吗?这个self.components.count-1是什么意思?
for i:= 0 to self.components.count-1do
这段语句的错误提示是 for i:= 0 to self.components.count-1do

if component is Tedit then
这段语句的错误提示是:[Error] Unit3.pas(77): Operator not applicable to this operand type
s:= Tedit(component).text;这段语句的错误提示是[Error] Unit3.pas(78): Incompatible types: 'Real' and 'TCaption'。
请问这些是什么原因?
 
你是说在我自己设定的button的onclick事件里面进行上面的定义吗?这个self.components.count-1是什么意思?
for i:= 0 to self.components.count-1do
这段语句的错误提示是 for i:= 0 to self.components.count-1do

if component is Tedit then
这段语句的错误提示是:[Error] Unit3.pas(77): Operator not applicable to this operand type
s:= Tedit(component).text;这段语句的错误提示是[Error] Unit3.pas(78): Incompatible types: 'Real' and 'TCaption'。
请问这些是什么原因?
 
你是说在我自己设定的button的onclick事件里面进行上面的定义吗?这个self.components.count-1是什么意思?
for i:= 0 to self.components.count-1do
这段语句的错误提示是 for i:= 0 to self.components.count-1do

if component is Tedit then
这段语句的错误提示是:[Error] Unit3.pas(77): Operator not applicable to this operand type
s:= Tedit(component).text;这段语句的错误提示是[Error] Unit3.pas(78): Incompatible types: 'Real' and 'TCaption'。
请问这些是什么原因?
 
老大,i服了you
我随便写写,应该是拼错了,你不是把我的照抄吧?
不过你现在可以照抄了,我自己编译了一下,唉.......
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
a: array[0..20] of string;
begin
{你要定义一个合适的数组大小}
for i:= 0 to Self.ComponentCount-1do
begin
if Components is TEdit then
a:= TEdit(Components).Text;
end;
end;
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部