Autor Beitrag
Jasmini
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20

Win Millenium
D7
BeitragVerfasst: Do 02.06.05 16:06 
Habe eine Aufgabe, bei der ich total auf dem Schlauch stehe...

Folgende Zahlenfolge soll per Klick auf einen Button (berechnet mit einer Schleife)
in einem Memo-Feld ausgegeben werden!


-1, 2, -3, 4, -5, 6, -7, 8, -9, 10, ... 20



Bitte um Hilfe...
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 02.06.05 16:09 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
for i := 1 to 20 do
  if i mod 2 = 0 then
    memo1.lines.text := memo1.lines.text + inttostr(i)+', ' else
    memo1.lines.text := memo1.lines.text +'-'+inttostr(i)+', ';


ungetestet

oder:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
for i := 1 to 20 do
  if i mod 2 = 0 then
    memo1.lines.add(inttostr(i)) else
    memo1.lines.add('-'+inttostr(i));

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit


Zuletzt bearbeitet von uall@ogc am Do 02.06.05 16:11, insgesamt 1-mal bearbeitet
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 02.06.05 16:11 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
var
  t : String;
begin
  for i := 1 to 20 do begin
    if odd(i) then
      t := IntToStr(i)
    else
      t := IntToStr(-i);
    memo1.lines.add(t);
  end;
end;

Auf die Schnell und ungetestet.
Jasmini Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 20

Win Millenium
D7
BeitragVerfasst: Do 02.06.05 16:16 
danke!

kleiner vorzeichenfehler *g*

:D :D
so gehts:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure TForm1.GoClick(Sender: TObject);
var
  i: Integer;
  t : String;
begin  
  for i := 1 to 20 do begin
    if odd(i) then  
      t := IntToStr(-i)
    else
      t := IntToStr(i);
    memo1.lines.add(t);
  end;  
end;

end.


Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt.
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Do 02.06.05 23:30 
Und noch ein anderer Lösungsweg mit einer repeat Schleife:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
  I, J: Integer;
  S: String;
begin

  I := -1;
  J := 2;

  repeat
    S := S + IntToStr(I) + ', ' + IntToStr(J) + ', ';
    Dec(I, 2);
    Inc(J, 2);
  until J > 20;

  Memo1.Lines.Add(S);

end;

_________________
Ciao, Sprint.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 03.06.05 00:05 
ausblenden Delphi-Quelltext
1:
2:
for i := 1 to 20 do
 Memo1.Lines.Add(FloatToStr(cos(i*pi)*i)); // oder alternativ Power(-1,i)*i


oder ohne Floats:
ausblenden Delphi-Quelltext
1:
Memo1.Lines.Add(inttostr(i*(1-i and 1*2)));					
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 03.06.05 10:55 
AFAIK gibt's doch auch den Befehl IntPower unter Delphi ???

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 03.06.05 11:32 
Ja, der nimmt einfach ganzzahlige Exponenten an. Die Basis ist aber Float und das Resultat auch.