1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
Unit Unit1;
{$mode objfpc}{$H+}
Interface
Uses
Classes, Sysutils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
Spin;
Const
CR = #13#10;
CoulFond = $0085ACF7;
CoulPanel = $000003E2;
CoulCel = clBlack;
Type
TDim = 1..3;
{ Tform1 }
Tform1 = Class(Tform)
SpEdDim: Tspinedit;
Statictext3: Tstatictext;
Sttmax: Tstatictext;
Sttpop: Tstatictext;
Procedure Formcreate(Sender: Tobject);
Procedure Speddimchange(Sender: Tobject);
private
Public
Dim : TDim;
Xc,Yc,Zc,Tc : Word; //
NCellMax : QWord;
NCellMaxStr : String;
End;
Var
Form1: Tform1;
function NumStrToSepMil(const NumStr: string): string;
{==============================================================================}
Implementation
{$R *.lfm}
{--- CompareStrNumbers ---------------------------------------------------------
Compare deux chaines de carctères qui sont des nombres entiers positifs
Resultat = -1 => Str1 est plus petit
Resultat = +1 => Str1 est plus grand
Resultat = 0 => Str1 = Str2
-------------------------------------------------------------------------------}
function CompareStrNumbers(Const Str1, Str2 : string) : Integer;
begin
{Vérifier si les chaines ont la même longueur}
if Length(Str1) < Length(Str2)
then Result := -1 // Str1 < Str2
else if Length(Str1) > Length(Str2)
then Result := 1 // Str1 > Str2
else begin
{L(Str1) = L(Str2) => Comparaison caractère par caractère}
Result := CompareStr(Str1, Str2);
end;
end;
{--- NumStrSepMil --------------------------------------------------------------
Sépare les milliers dans un nombre en string
-------------------------------------------------------------------------------}
function NumStrToSepMil(const NumStr: string): string;
var
i, L : Integer;
begin
Result := '';
L := Length(NumStr);
for i := L downto 1 do
begin
Result := NumStr[i] + Result;
if (L - i) mod 3 = 2 then Result := ' ' + Result;
end;
end;
{--- fCellMax2 -----------------------------------------------------------------
Fonction calculant le population maximale selon la dimension demandée et les
valeurs dans chaque dimension (Utilisation de UIntToStr)
On donne les résultats sous forme numérique et également sous forme de String
pour gagner des recoversions à faire pour l'affichage
-------------------------------------------------------------------------------}
function [ATTACH]657460[/ATTACH](aDim : Tdim; X,Y,Z,T : Word; Var ResStr : String) : QWord;
const
CR = #13#10;
var
P,QM : QWord; // Produit des quatre variables
P64,QWStr : String; // Produit et Qmax convertis en string
procedure MsgER;
var FmtP, FmtQM : String;
begin
FmtP := NumStrToSepMil(UIntToStr(P));
FmtQM:= NumStrToSepMil(UIntToStr(QM));
MessageDlg('Dépassement des limites :'+CR+CR
+'Nbre de cellules demandées : ' +FmtP+CR
+'Valeur Maximale autorisée : ' +FmtQM
, mtError, [mbOk], 0);
end;
begin {---}
{initialisation des resultats fournis}
result := 0;
resStr := '';
{Selon la dimension du système pour éviter les X par 0}
Case aDim of
2 : begin P := X * Y; QM := 65535 * 65535; end;
3 : begin P := X * Y * Z; QM := 65535 * 65535 * 65535; end;
4 : begin P := X * Y * Z * T; QM := 65535 * 65535 * 65535 * 65535; end;
end;
// pour test débordement P := QM + 2;
if P > QM then
begin
MsgER;
Exit;
end;
{Comparaison des Chaines :
1. Si L(QWstr)= 20 > L(P64) => P64 est évidemment un QWord autorisé
2. Si LQWstr = L(P64) => Il faut regarder chaque digit jusqu'à ce qu'il y ait
une différence}
P64 := UIntToStr(P);
QWStr := UIntToStr(QM);
If CompareStrNumbers(QWStr,P64) > -1 then
begin
Result := P; // C'est OK, On peut calculer la suite
ResStr := QWStr;
end else
begin
MsgER;
ResStr := '';
end;
end;
{--- Tform1 -------------------------------------------------------------------}
Procedure Tform1.Formcreate(Sender: Tobject);
Begin
{1. Dimensions de la Grille par défaut et position dans la Fiche}
Dim := 2;
Xc := 65535;
Yc := 65535;
Zc := 65535;
Tc := 65535;
{2. Variables}
NCellMax := fCellMax2(Dim,Xc,Yc,Zc,Tc,NCellMaxStr);
{3. Affichages}
StTPop.Caption := 'Population Effective : '+ NumStrToSepMil(UIntToStr(NCellMax));
StTMax.Caption := 'Popul. Max Théorique : '+ NumStrToSepMil(NCellMaxStr);
End;
Procedure Tform1.Speddimchange(Sender: Tobject);
Begin
Dim := SpEdDim.Value;
Xc := 65535;
Yc := 65535;
Zc := 65535;
Tc := 65535;
{2. Variables}
NCellMax := fCellMax2(Dim,Xc,Yc,Zc,Tc,NCellMaxStr);
{3. Affichages}
StTPop.Caption := 'Population Effective : '+ NumStrToSepMil(UIntToStr(NCellMax));
StTMax.Caption := 'Popul. Max Théorique : '+ NumStrToSepMil(NCellMaxStr);
End;
End. |
Partager