المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : [تمرين] التحويل من النظام العشري الى النظام الثنائى


hanipino
25-12-2008, 05:43 PM
انشاء برنامج لتحويل الاعداد من النضام العشرى (النضام الدى نعرفه نحن [0..9]) الى
النضام الثنائى (0 , 1) النضام التى تعرفه الالة .
مثلا العدد 6 :

0 = 2/6 = 3
1 = 2/3 = 1
1 = 2/1 = 0
-----------
110 = 6
-----------
***********
لنتحقق :
=(4*1)+(2*1)+(1*0)
6 = 4 + 2 + 0
---------------------

1,2,4,8,16,32 ... الخ .

:) الاعداد من نوع Integer فقط .

B.M.AbdelAziZ
25-12-2008, 06:37 PM
حل على السريع

Function Int2Bin(x: Integer):String;
Var
s : String;
Begin
s:='';
repeat
case x mod 2 of
0 : s:='0'+ s;
1 : s:='1'+ s;
end;
x := x div 2;
until x=0;
Result :=s;
End;

والسلام عليكم

B.M.AbdelAziZ
25-12-2008, 06:49 PM
توفير قليل من الذاكرة بالاستغناء عن المتغير s


Function Int2Bin(x: Integer):String;
Begin
result:='';
repeat
case x mod 2 of
0 : result:='0'+ result;
1 : result:='1'+ result;
end;
x := x div 2;
until x=0;
End;

hanipino
25-12-2008, 06:56 PM
DeltaAziz لا يازال التمرين لم يبرد بعد :)... لا تضحك على طريقتى ... بالرغم من انى معها من الصباح

var
I,Y,S,Z,L,Count2:integer;
LblStr:Array [0..1000]of Integer;
Str:String;
begin
Y:=StrToInt(edit1.Text);
for I := 0 to Y do
begin
if S < 1 then Break;
S:=(Y div 2);
Z:=(Y mod 2);
If Z=0 then L:=0 else L:=1;
LblStr[I]:= L;
Y:=S;
Count2:=I;
end;
For I:=0 to Count2 do
Str:=Str+IntToStr(LblStr[I]);
Label1.Caption:=Str;
end;

B.M.AbdelAziZ
25-12-2008, 07:10 PM
بالعكس اخي،
جيد انك تحاول وفي الاخير تجد طريقة تعطي نتيجة صحيحة
هذا هو المهم ولايهم كم من وقت امضيت او شيئ اخر

ملاحظة: لم تعلق على حلي ان كان صحيح او خطأ كونك صاحب التمرين

والسلام عليكم

STRELiTZIA
25-12-2008, 07:15 PM
لا يازال التمرين لم يبرد بعد


من الأفضل ان تكون الحلول على صيغة Functions ليسهل تصديرها.

بالتوفيق ان شاء الله

hanipino
26-12-2008, 12:15 AM
ملاحظة: لم تعلق على حلي ان كان صحيح او خطأ كونك صاحب التمرين

الحل 100 % صحيح . صغير الحجم و مفيد . و تمت الاستفادة منه ان شاء الله . :)


من الأفضل ان تكون الحلول على صيغة Functions ليسهل تصديرها.

تم تحويلها الى Functions . :)

Function SystemBinary(Binary:Integer):String;
var
I,S,Z,L,Count2:integer;
LblStr:Array [0..1000]of Integer;
begin
Result:='';
for I := 0 to Binary do
begin
if S < 1 then Break;
S:=(Binary div 2);
Z:=(Binary mod 2);
If Z=0 then L:=0 else L:=1;
LblStr[I]:= L;
Binary:=S;
Count2:=I;
end;
For I:=0 to Count2 do
Result:=Result+IntToStr(LblStr[I]);
end;

hanipino
25-01-2009, 03:56 PM
تعديل على طريقتى . على حسب الدروس المقدمة . :)


Function SystemBinary(Binary:Integer):String;
var
I,S,Z,L:Integer;
LblStr:Array of Word;
begin
Result:='';
for I := 0 to Binary do
begin
SetLength(LblStr,I+1);
S:=(Binary div 2);
Z:=(Binary mod 2);
If Z=0 then L:=0 else L:=1;
LblStr[I]:= L;
Binary:=S;
if S < 1 then Break;
end;
For I:=High(LblStr) Downto 0 do
Result:=Result+IntToStr(LblStr[I]);
end;