1
0
lazarus-tutorials/numberediting/numeditmain.pas
2021-10-12 15:20:49 -04:00

115 lines
2.2 KiB
ObjectPascal

unit numeditmain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
DividerBevel, math;
type
{ TForm1 }
TForm1 = class(TForm)
DividerBevel1: TDividerBevel;
lblB: TLabel;
lblA: TLabel;
lbResults: TListBox;
seA: TSpinEdit;
seB: TSpinEdit;
procedure ChangeAorB(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
procedure UpdateCalculations;
function CalcSum: int64;
function CalcProduct: int64;
function CalcPower: double;
function CalcIntDivision(var isValid: boolean): int64;
function CalcModulus(var isValid: boolean): int64;
function CalcSumOfSquares: int64;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.ChangeAorB(Sender: TObject);
begin
UpdateCalculations;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
ChangeAOrB(nil);
end;
procedure TForm1.UpdateCalculations;
var OK: boolean;
n: Int64;
begin
OK := true;
lbResults.Items.Clear;
lbResults.Items.Add('A + B : ' + IntToStr(CalcSum));
n := CalcIntDivision(OK);
if OK then
begin
lbResults.Items.Add('A DIV B : ' + IntToStr(n));
lbResults.Items.Add('A MOD B : ' + IntToStr(CalcModulus(OK)));
end
else lbResults.Items.Add('A DIV B, A MOD B are not calculable');
lbResults.Items.Add('A x B : ' + IntToStr(CalcProduct));
lbResults.Items.Add('A*A + B*B : ' + IntToStr(CalcSumOfSquares));
lbResults.Items.Add('A to POWER B : ' + FloatToStr(CalcPower));
end;
function TForm1.CalcSum: int64;
begin
Result := seA.Value + seB.Value;
end;
function TForm1.CalcProduct: int64;
begin
Result := seA.Value * seB.Value;
end;
function TForm1.CalcPower: double;
begin
Result := intpower(seA.Value, seB.Value);
end;
function TForm1.CalcIntDivision(var isValid: boolean): int64;
begin
Result := 0;
isValid := (seB.Value <> 0);
if isValid
then Result := seA.Value div seB.Value;
end;
function TForm1.CalcModulus(var isValid: boolean): int64;
begin
Result := 0;
isValid := (seB.Value <> 0);
if isValid
then Result := seA.Value mod seB.Value;
end;
function TForm1.CalcSumOfSquares: int64;
begin
Result := seA.Value * seA.Value + seB.Value * seB.Value;
end;
end.