algorytm.org

Implementacja w Delphi/Pascal



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Silnia - Implementacja w Delphi/Pascal
Ocena użytkownikóww: *****  / 4
SłabyŚwietny
Nadesłany przez Michał Knasiecki, 26 lipca 2005 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.
Pobierz pełne rozwiązanie.

Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.

Silnia Delphi/Silnia/Unit1.pas:
//www.algorytm.org
//(c)Michal Knasiecki
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var i:integer; //Zmienna pomocnicza
wynik:integer;
begin
wynik:=1;
if edit1.text<>'0' then begin //jeżeli 'n<>0'
for i:=2 to strtoint(edit1.text) do  //to zwiekszaj 'i' od '2' co '1' do 'n'
wynik:=wynik*i;                //i pomnazaj przez 'i' 'wynik'
edit2.text:=inttostr(wynik);   //wypisz 'wynik'
end
else edit2.text:='1'; //jeżeli 'n=1' to 'wynik=1'
end;

end.

Silnia Delphi/SilniaRek/Unit1.pas:
//Program pobrano z www.algorytm.org
//Opracowal Michal Knasiecki
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
function silnia(n:word):integer;
begin
if (n=0)or(n=1) then silnia:=1 else
silnia:=n*silnia(n-1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.text:=inttostr(silnia(strtoint(edit1.text)));
end;

end.
Dodaj komentarz