1.напишите рекурсивную функцию,определяющую количество единиц в двоичном представлении числа
либо написать другую по этому заданию.Я проги зделал а в надо в консольном режиме)А я не знаю как((
Код: Выделить всё
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DecToBin(q: integer): string;
var w: integer;
begin
if (q=0) or (q=1) then result:=IntToStr(q)
else
begin
w:=q mod 2;
result:=DecToBin(q div 2)+IntToStr(w);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var n, i, count: integer;
begin
If TryStrToInt(Edit1.Text, n) then
begin
Edit2.Text:=DecToBin(StrToInt(Edit1.Text));
count:=0;
for i:=1 to Length(Edit2.Text) do
if edit2.Text[i]='1' then inc(count);
Label3.Caption:='Кол-во единиц = '+IntToStr(count);
end;
end;
end.
Код: Выделить всё
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Label2: TLabel;
Edit1: TEdit;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
list: Tstrings;
i, key: integer;
begin
i:= 0;
key:= 0;
list:= TStringlist.Create;
list.AddStrings(Memo1.Lines);
while List.Count >= i + 2 do
begin
if (list.Strings[i+1] > list[i]) and (list.Strings[i+1]> list.Strings[i+2] )
then key:= key+1;
i:= i+1;
end;
list.Free;
Label1.Caption:= IntToStr(key);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Edit1.Text = '' then Exit;
Memo1.Lines.Add(Edit1.Text);
Edit1.Clear;
Edit1.SetFocus;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case key of
'0'..'9':
else Key:= Chr(0);
end;
end;
end.