prosím vás vie mi niekto poradit ako ma vyzerat program, ktorý prevedie číslo z desiatkovej do dvojkovej sústavy
súrne to potrebujem
Fórum › Delphi
Prevod z desiatkovej do dvojkovej sústavy

Například takto, nebo dalšímožnost je to ukládat do pole odzadu a odpustit si volání ReerseString.
function Dec2Bin(num: Integer): String;
begin
Result := '';
repeat
Result := Result + char(num mod 2+ord('0'));
num := num div 2;
until num = 0;
Result := ReverseString(Result);
end;
a čo sa ti nepáči na riešení ktoré dal Jura? Tých riešení je neurekom, môžeš to spraviť napríklad aj rekurzívne do jediného riadku:kubala píše:#
# mate niekto iny navrh?
function TForm1.dectobin(desat:integer):string;
begin
if desat>0 then result:=dectobin(desat div 2)+inttostr(desat mod 2);
end;
To kubala:
To je vtip?
function Dec2Bin(num: Integer): String;
begin
Result := '';
repeat
Result := Result + char(num mod 2+ord('0'));
num := num div 2;
until num = 0;
Result := ReverseString(Result);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption := Dec2Bin(StrToInt(Edit1.Text));
end;
No jenom takhle zkopírovat to tam asi nepude:D
Zaprvý musíš nastavit v object inspectoru událost onclick buttonu na Button1Click a zadruhý zkontroluj názvy komponent (label1 vs Label1).
Jo a eště se asi musí dát ta hlavička funkce Dec2Bin někam do toho programu, ale tim si nejsem jistej v delphi už sem dlouho nedělal.
Otvor delphi, všetko čo tam je písané zmaž a zkopíruj tam toto:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
function dectobin(vstup:integer):string;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.caption:=dectobin(strtoint(edit1.text));
end;
function TForm1.dectobin(vstup:integer):string;
begin
if vstup>0 then result:=dectobin(vstup div 2)+inttostr(vstup mod 2);
end;
end.
potom na formulár vlož button, label a edit. Klikni raz na button, v object inspectore klikni na záložku events a v riadku OnClick z menu vyber Button1Click aby si priradil tlačidlu procedúru. Ak už toto nezvládneš tak fakt pochybujem že si na túto stránku prišiel sám bez pomoci rodičov
program Sustava;kubala píše:#
# prosím vás vie mi niekto poradit ako ma vyzerat program, ktorý prevedie číslo z desiatkovej do dvojkovej sústavy#
#súrne to potrebujem
uses crt;
Function VypocetMocniny(number,mocnitel : integer) : integer;
var
k,i : integer;
begin
k := number;
For i := 1 To mocnitel - 1 Do
begin
k := k * number;
end;
IF mocnitel = 0 THEN
k := 1;
VypocetMocniny := k;
end;
Function UrcenieCifier(Var Pole : array of real;number : longint) : integer;
var
index : integer;
module : longint;
i : integer;
module2 : integer;
str_number : string;
numeral_count : integer;
begin
module := 1;
module2 := 1;
Str(number,str_number);
numeral_count := length(str_number);
For index := 1 To numeral_count Do
begin
module := module * 10;
Pole[index] := number mod module;
IF index >= 2 THEN
begin
For i := 1 To index - 1 Do
begin
Pole[index] := Pole[index] - Pole[i];
end;
module2 := module2 * 10;
Pole[index] := (Pole[index]/module2);
end;
IF Pole[index] <> 0 THEN
Pole[index] := Round(Pole[index] - 0.5);
end;
UrcenieCifier := numeral_count;
end;
Function IsBinary(number : integer) : Boolean;
var
IsTrue : Boolean;
a,temp : integer;
Pole1 : Array [1..100] of real;
begin
temp := UrcenieCifier(Pole1,number);
IsTrue := True;
For a := 2 To temp + 1 Do
begin
IF Not((Pole1[a] = 1) or (Pole1[a] = 0)) THEN
IsTrue := False;
end;
IsBinary := IsTrue;
end;
var
Pole1 : array [1..100] of real;
number,zvysok : longint;
x,temp : integer;
temp2 : integer;
binary : boolean;
zapis : string;
Cstr : string;
Cstr1 : string;
znak : char;
Cstr2 : string;
index : integer;
dlzka : integer;
menu : integer;
begin
clrscr;
writeln('Vyber aky prevod chces spravit');
writeln('1. Z dvojkovej sustavy do desiatkovej');
writeln('2. Z desiatkovej susatvy do dvojkovej');
writeln('3. Koniec');
readln(menu);
IF menu = 1 THEN
begin
Repeat
clrscr;
writeln('Zadaj cislo');
readln(number);
Until IsBinary(number);
temp2 := UrcenieCifier(Pole1,number);
For x := 2 To temp2 + 1 Do
begin
IF Pole1[x] = 1 THEN
begin
temp := temp + VypocetMocniny(2,x - 2);
end;
end;
writeln(temp);
readln;
end;
IF menu = 2 THEN
begin
writeln('Zadaj cislo');
readln(number);
Repeat
zvysok := number mod 2;
number := number div 2;
Str(zvysok,zapis);
Cstr := zapis;
Cstr1 := Cstr1 + Cstr;
Until number = 0;
dlzka := length(Cstr1);
For index := dlzka downto 1 Do
begin
Cstr2 := Cstr2 + Cstr1[index];
end;
writeln(Cstr2);
readln;
end;
IF menu = 3 THEN
end.
To je zaruceny prevod do dvojkovej a jednotkovej sustavy funguje na 100%
Phoenix píše:#
# No jenom takhle zkopírovat to tam asi nepude:D#
#Zaprvý musíš nastavit v object inspectoru událost onclick buttonu na Button1Click a zadruhý zkontroluj názvy komponent (label1 vs Label1).#
#Jo a eště se asi musí dát ta hlavička funkce Dec2Bin někam do toho programu, ale tim si nejsem jistej v delphi už sem dlouho nedělal.
Ja len ze, je jedno ci napise Label1, LABEL1, label1, alebo aj LaBeL1, pretoze delphi na to neberie ohlad (pokial nejde o nazov funkcie, alebo tak).
sneakfast píše:#
##laco797 píše:#
#Ja len ze, je jedno ci napise Label1, LABEL1, label1, alebo aj LaBeL1, pretoze delphi na to neberie ohlad (pokial nejde o nazov funkcie, alebo tak).
##
#i u funkci a procedur to je jedno:)
Ajo:D sem zapomněl že delphi je case-insensitive.

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Prevod z 10 do 2 sústavy — založil Eddy777
Prevod z hexadecimalnej sustavy — založil street punk
Prevod do 10-tkovej sustavy (asm8080) — založil kukucak
Prevod z 10tkovej do 2jkovej sústavy — založil Michal
Ciselne sustavy — založil devilfish