Add book "Занимательное программирование"

This commit is contained in:
2025-12-13 16:35:46 +01:00
parent 98329e0a3d
commit c1147629f7
78 changed files with 4530 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

View File

@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="11"/>
<General>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="gas"/>
<Scaled Value="True"/>
<ResourceType Value="res"/>
<UseXPManifest Value="True"/>
<XPManifest>
<DpiAware Value="True"/>
</XPManifest>
<Icon Value="0"/>
</General>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
<Modes Count="0"/>
</RunParams>
<RequiredPackages Count="1">
<Item1>
<PackageName Value="LCL"/>
</Item1>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="gas.lpr"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="unit1.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="gas"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Options>
<Win32>
<GraphicApplication Value="True"/>
</Win32>
</Options>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@@ -0,0 +1,22 @@
program gas;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, Unit1
{ you can add units after this };
{$R *.res}
begin
RequireDerivedFormResource:=True;
Application.Scaled:=True;
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectSession>
<Version Value="11"/>
<BuildModes Active="Default"/>
<Units Count="2">
<Unit0>
<Filename Value="gas.lpr"/>
<IsPartOfProject Value="True"/>
<EditorIndex Value="-1"/>
<WindowIndex Value="-1"/>
<TopLine Value="-1"/>
<CursorPos X="-1" Y="-1"/>
<UsageCount Value="35"/>
</Unit0>
<Unit1>
<Filename Value="unit1.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="Form1"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="Unit1"/>
<IsVisibleTab Value="True"/>
<EditorIndex Value="-1"/>
<TopLine Value="115"/>
<CursorPos X="21" Y="169"/>
<UsageCount Value="35"/>
</Unit1>
</Units>
<General>
<ActiveWindowIndexAtStart Value="-1"/>
</General>
<JumpHistory HistoryIndex="-1"/>
<RunParams>
<FormatVersion Value="2"/>
<Modes Count="0" ActiveMode=""/>
</RunParams>
</ProjectSession>
</CONFIG>

View File

@@ -0,0 +1,43 @@
object Form1: TForm1
Left = 1198
Height = 684
Top = 326
Width = 732
Caption = 'Form1'
ClientHeight = 684
ClientWidth = 732
DesignTimePPI = 93
LCLVersion = '2.0.12.0'
object StartStopBtn: TButton
Left = 296
Height = 27
Top = 512
Width = 140
Caption = 'Пуск'
OnClick = StartStopBtnClick
TabOrder = 0
end
object Screen: TPaintBox
Left = 16
Height = 416
Top = 15
Width = 702
OnPaint = ScreenPaint
end
object Cold: TLabel
Left = 144
Height = 16
Top = 448
Width = 7
Caption = '0'
ParentColor = False
end
object Warm: TLabel
Left = 528
Height = 16
Top = 448
Width = 7
Caption = '0'
ParentColor = False
end
end

View File

@@ -0,0 +1,181 @@
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
Math;
type
Molecule = record
X, Y: Integer;
Vx, Vy: Integer;
end;
{ TForm1 }
TForm1 = class(TForm)
Cold: TLabel;
Warm: TLabel;
StartStopBtn: TButton;
Screen: TPaintBox;
procedure ScreenPaint(Sender: TObject);
procedure StartStopBtnClick(Sender: TObject);
private
public
end;
var
Form1: TForm1;
IsRunning: Boolean = false;
implementation
{$R *.lfm}
procedure TForm1.ScreenPaint(Sender: TObject);
begin
end;
procedure TForm1.StartStopBtnClick(Sender: TObject);
const RMolecule: Integer = 10; { Радиус молекулы }
const RHole: Integer = 15;
const V: Integer = 7; { Скорость молекулы }
const N = 20; { Количество молекул }
var angle: Real; { Угол, задающий начальное направление полета }
i: Integer; { Счетчик цикла }
Mol: array[1..N * 2] of Molecule; { Массив молекул }
CurV: Integer; { Выбранаая случайная скорость молекулы }
halfScreen: Integer;
T1, T2: Integer; { Температуры и количество, статистика }
begin
if IsRunning then
begin
IsRunning := false;
StartStopBtn.Caption := 'Пуск';
Exit;
end;
StartStopBtn.Caption := 'Стоп';
IsRunning := true;
Randomize;
Screen.Refresh;
halfScreen := Screen.Width div 2;
T1 := V * N;
T2 := V * N;
for i := 1 to N * 2 do { Цикл по все молекулам }
begin
{ Выбор начального положения молекулы }
if i <= N then
begin
Mol[i].X := RandomRange(RMolecule, halfScreen - RMolecule);
end
else
begin
Mol[i].X := RandomRange(halfScreen + RMolecule,
Screen.Width - RMolecule);
end;
Mol[i].Y := RandomRange(RMolecule, Screen.Height - RMolecule);
{ Выбор нправления и скорости молекулы (1 - V) }
angle := Random(360) * Pi / 180;
CurV := RandomRange(1, V);
Mol[i].Vx := Round(CurV * Sin(angle)); { Получение составляющих }
Mol[i].Vy := Round(CurV * Cos(angle)); { скорости молекулы }
end;
while IsRunning do { Основной цикл }
begin
{ Черчение перегородки }
Screen.Canvas.Line(halfScreen, 0, halfScreen,
Screen.Height div 2 - RHole - RMolecule);
Screen.Canvas.Line(halfScreen, Screen.Height div 2 + RHole + RMolecule,
halfScreen, Screen.Height);
for i := 1 to N * 2 do { Цикл по всем молекулам }
begin
Screen.Canvas.Pen.Color := clBtnFace;
Screen.Canvas.Ellipse(Mol[i].X - RMolecule, Mol[i].Y - RMolecule,
Mol[i].X + RMolecule, Mol[i].Y + RMolecule);
{ Сдвигаем на новую позицию }
Mol[i].X := Mol[i].X + Mol[i].Vx;
Mol[i].Y := Mol[i].Y + Mol[i].Vy;
if (Mol[i].X > halfScreen - RMolecule) and
(Mol[i].X - Mol[i].Vx <= halfScreen - RMolecule) then
begin
if (Mol[i].Y >= Screen.Height div 2 - RHole) and
(Mol[i].Y <= Screen.Height div 2 + RHole) then
begin
T1 := T1 - V; T2 := T2 + V;
end
else
begin
Mol[i].X := halfScreen - RMolecule;
Mol[i].Vx := -Mol[i].Vx;
end;
end
else if (Mol[i].X < halfScreen + RMolecule) and
(Mol[i].X - Mol[i].Vx >= halfScreen + RMolecule) then
begin
if (Mol[i].Y >= Screen.Height div 2 - RHole) and
(Mol[i].Y <= Screen.Height div 2 + RHole) then
begin
T2 := T2 - V;
T1 := T1 + V;
end
else
begin
Mol[i].X := halfScreen + RMolecule;
Mol[i].Vx := -Mol[i].Vx;
end;
end;
{ Определяем, не вышла ли молекула за границы аквариума }
if Mol[i].X > Screen.Width - RMolecule then
begin
Mol[i].X := Screen.Width - RMolecule;
Mol[i].Vx := -Mol[i].Vx;
end;
if Mol[i].X < RMolecule then
begin
Mol[i].X := RMolecule;
Mol[i].Vx := -Mol[i].Vx;
end;
if Mol[i].Y > Screen.Height - RMolecule then
begin
Mol[i].Y := Screen.Height - RMolecule;
Mol[i].Vy := -Mol[i].Vy;
end;
if Mol[i].Y < RMolecule then
begin
Mol[i].Y := RMolecule;
Mol[i].Vy := -Mol[i].Vy;
end;
Cold.Caption := IntToStr(T1);
Warm.Caption := IntToStr(T2);
Screen.Canvas.Pen.Color := clBlue; { Рисуем молекулу на новой }
Screen.Canvas.Ellipse(Mol[i].X - RMolecule, Mol[i].Y - RMolecule, { позиции }
Mol[i].X + RMolecule, Mol[i].Y + RMolecule);
end;
Sleep(10); { Пауза на 10 миллисекунд }
Application.ProcessMessages;
end;
end;
end.