
June 4th, 2005, 05:17 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Posts: 1
Time spent in forums: 1 m 30 sec
Reputation Power: 0
|
|
|
Please explain what this does?
My teacher gave us this code to analise and we are supposed to report back and tell him what each section does, but I have no clue what is happening. We are still doing loops and if then else statements in our syllabus, so I am new to all of this.
What this program does is take a 4 digit number, re-arrange it to 2 new numbers biggest to smallest and smallest to biggest (eg 3765 = 7653 and 3567) subtracts them from each other and does it again until it equals 6174, then displays all the results in a memo box.
heres the code:
Code:
var num1,num2,num3,num4,whole,hi,lo,x,hold,count : integer;
wholes,num1s,num2s,num3s,num4s,His,Los,pali:string;
begin
wholes:=edit1.text;
listbox1.Clear;
count:=0;
for x:=1 to 4 do
insert(copy(wholes,1,1),pali,1);
if ((strtoint(edit1.Text) > 1000) and (strtoint(edit1.Text) < 9998)) and ((wholes <> pali)) then
repeat
count:=count+1;
insert(copy(wholes,1,1),num1s,1);
insert(copy(wholes,2,1),num2s,1);
insert(copy(wholes,3,1),num3s,1);
insert(copy(wholes,4,1),num4s,1);
num1:=strtoint(num1s);
num2:=strtoint(num2s);
num3:=strtoint(num3s);
num4:=strtoint(num4s);
for x:=0 to 6 do
begin
if num1<num2 then
begin
hold:=num1;
num1:=num2;
num2:=hold;
end;
if num2<num3 then
begin
hold:=num2;
num2:=num3;
num3:=hold;
end;
if num3<num4 then
begin
hold:=num3;
num3:=num4;
num4:=hold;
end;
end;
num1s:=inttostr(num1);
num2s:=inttostr(num2);
num3s:=inttostr(num3);
num4s:=inttostr(num4);
his:=num1s+num2s+num3s+num4s;
los:=num4s+num3s+num2s+num1s;
hi:=strtoInt(his);
lo:=strtoInt(los);
whole:=hi-lo;
wholes:=inttostr(whole);
listbox1.Items.Add(inttostr(count)+': '+his+' - '+los+' = '+wholes);
//showmessage(his+' - '+los+' = '+wholes);
num1s:='';
num2s:='';
num3s:='';
num4s:='';
until whole=6174
else
showmessage('Please try again, the number you typed in cannot be less than 1001 and the values cannot be al the same. eg 1111 or 2222');
end;
|