ZeDab

Untitled

Nov 10th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 11.49 KB | None | 0 0
  1. unit UOperator;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes,sysutils,math,strutils,Graphics, windows, dialogs;
  7.  
  8. type
  9.   TTypeOperation = (OPaddition, OPsoustraction, OPmultiplication, OPdivision);
  10.   TTypeEtape=(ET_Lancement,ET_Poser,ET_EcrireChiffre,ET_EcrireRetenue,ET_EcrireSigne);
  11.   PEtape = ^TEtape;
  12.   TEtape = record
  13.               Ordre : Integer;          // permet de n'afficher que certaines étapes
  14.                                         // typiquement  : 0 = avant alignement
  15.                                         //                1 = Aligner
  16.                                         // 2...n          calculs détailés
  17.               TypeEtape:TTypeEtape;     // identifie ce que fait l'étape
  18.               nombre : Integer;         // éventuellement le chiffre ou la retenue à écrire
  19.               commentaire:string[255];  // pour éventuelle sortie
  20.               x,y : integer;            // coordonnées du carreau concerné
  21.  
  22.            end;
  23.  
  24.   TOperation = class
  25.   private
  26.     FTypeOperation: TTypeOperation;
  27.     fN1, fN2: Currency;
  28.     fListeEtapes: TList;
  29.     fSLNombres : TStrings;
  30.     // taille d'un carreau
  31.     fcote : Integer;
  32.     // marges gauche et heut pour centrer dans le canevas
  33.     fMargeG,fMargeH : Integer;
  34.     // puissance minimum et maximum atteinte par les nombres
  35.     fPmax,fPmin : Integer;
  36.     // nombre de carreaux nécessaires pour poser l'opération
  37.     fnbx,fnby : integer;
  38.     // Tstrings pour sortie débuggage
  39.     FSLDebug: TStrings;
  40.     fCanvas : Tcanvas;
  41.     // couleurs
  42.     fCoulFond: Tcolor;
  43.     fCoulTrait : Tcolor;
  44.     fCoulTraitFin : Tcolor;
  45.     fCoulChiffres : Tcolor;
  46.     fCoulRetenues : Tcolor;
  47.  
  48.     property ListeEtapes: TList read fListeEtapes;
  49.     procedure WriteDebug(const Msg: string);
  50.     procedure SetSLDebug(const Value: TStrings);
  51.  
  52.     procedure SetTypeOperation(ATypeOperation: TTypeOperation);
  53.     function GetTypeOperation: TTypeOperation;
  54.     procedure SetN1(Value: Currency);
  55.     function GetN1: Currency;
  56.     procedure SetN2(Value: Currency);
  57.     function GetN2: Currency;
  58.  
  59.  
  60.     Procedure RAZ;
  61.     procedure TraceCarreaux;
  62.     procedure PrepareAddSous(TypeOperation : TTypeOperation = OPaddition);
  63.     procedure AjouteEtape(aOrdre : integer ; aTypeetape : TTypeEtape; aNombre : integer;aCommentaire : string; ax,ay : INteger );
  64.     procedure MoteurAddition;
  65.  
  66.   public
  67.     constructor Create(ATypeOperation: TTypeOperation; AN1, AN2: Currency;ACanvas : TCanvas);
  68.     destructor Destroy; override;
  69.     procedure PrepareOperation;
  70.     property TypeOperation: TTypeOperation read GetTypeOperation write SetTypeOperation;
  71.     property N1: Currency read GetN1 write SetN1;
  72.     property N2: Currency read GetN2 write SetN2;
  73.  
  74.     property SLDebug: TStrings read FSLDebug write SetSLDebug;
  75.  
  76.     Procedure AfficheOperation;
  77.  
  78.   end;
  79.  
  80. implementation
  81. Procedure TOperation.AfficheOperation;
  82.   var i: Integer;
  83.       Etape:PEtape;
  84.   begin
  85.  
  86.     WriteDebug('AfficheOperation ****');
  87.     WriteDebug(format(' nb étapes : %d',[fListeEtapes.Count]));
  88.     For i := 0 to fListeEtapes.Count-1 do
  89.       begin
  90.         Etape := fListeEtapes[i];
  91.         WriteDebug(format(' étape : %d %s',[i, etape^.commentaire]));
  92.         case etape^.TypeEtape of
  93.           ET_Lancement : begin
  94.  
  95.                             TraceCarreaux;
  96.                             fCanvas.TextOut(50,50,'LANCEMENT');
  97.                             fCanvas.Refresh;
  98.                          end;
  99.           ET_Poser :     begin
  100.                          end;
  101.         end; //case etape.TypeEtape of
  102.       end;
  103.  
  104.   end;
  105. procedure TOperation.AjouteEtape(aOrdre : integer ; aTypeetape : TTypeEtape; aNombre : integer;aCommentaire : string; aX,ay : INteger );
  106.   var  Etape:PEtape;
  107.   begin
  108.  
  109.      new(Etape);
  110.      with Etape^ do
  111.       begin
  112.         Ordre         :=aOrdre;
  113.         nombre        :=aNombre;
  114.         commentaire   :=aCommentaire;
  115.         x             := ax;
  116.         y             := ay;
  117.       end;
  118.      fListeEtapes.Add(Etape);
  119.      //WriteDebug( format('AjouteEtape : ordre %d type,n° d n=%d, comm=%s x,y = %d,%d',[aOrdre,{ord(aTypeetape),} aNombre, aCommentaire,aX,ay]));
  120.   end;
  121. procedure TOperation.MoteurAddition;
  122. var i,j : Integer;
  123.     ordre_etape : Integer;
  124.  
  125.   begin
  126.  
  127.     WriteDebug('moteuraddition *******************');
  128.     writedebug(Format('puiss mini %d maxi %d',[fPmin,fPmax]));
  129.     writedebug(Format('n1  n2 = %g et %g',[fN1,fN2]));
  130.     WriteDebug('fSLNombres :');
  131.     for i := 0 to fSLNombres.Count -1 do
  132.        begin
  133.        WriteDebug(format('%d [%s]',[i,fSLNombres[i]]));
  134.        end;
  135.  
  136.     AjouteEtape(0,
  137.                 ET_Lancement,-1,
  138.                 format('Addition : %g + %g : prochaine étape : opération posée',[fn1,fn2]),
  139.                 -1,-1);
  140.  
  141.     for i := 0 to 1 do
  142.       begin
  143.  
  144.       end;
  145.  
  146.   end;
  147. procedure TOperation.TraceCarreaux;
  148.   var i,j : Integer;
  149.   begin
  150.     with fCanvas do
  151.       begin
  152.         brush.Style  := bsSolid;
  153.         brush.Color := fCoulFond;
  154.         FillRect(ClipRect);
  155.  
  156.  
  157.  
  158.         // tracé des lignes horizontales
  159.         for i := 0 to 3 do
  160.           begin
  161.             // traits épais
  162.             pen.Width := 3;
  163.             pen.Color := fCoulTrait;
  164.             polyline([point(fmargeG,fmargeH+i*fcote),point(fmargeG+fnbx*fcote,fmargeH+i*fcote)]);
  165.             // traits fins
  166.             if i<3 then
  167.             for j := 1 to 3 do
  168.               begin
  169.                 pen.Width := 1;
  170.                 pen.Color := fCoulTraitFin;
  171.                 polyline([point(fmargeG,fmargeH+i*fcote + fcote div 4 *j),point(fmargeG+fnbx*fcote,fmargeH+i*fcote+fcote div 4 *j)]);
  172.               end;
  173.           end;
  174.  
  175.         // tracé des lignes verticales
  176.         pen.Width := 3;
  177.         pen.Color := fCoulTrait;
  178.  
  179.         for i := 0 to fnbx do
  180.           polyline([point(fMargeG + i*fcote,fMargeH ),point(fMargeG + i*fcote,fMargeH+fnby *fcote)]);
  181.         brush.Style  := bsClear;
  182.  
  183.         // affichage nom colonne
  184.         font.Height := fcote div 10;
  185.         font.color := clBlack ;
  186.  
  187.       end;
  188.  
  189.   end;
  190. (*
  191.     Aligner l'opération
  192.     mettre à jour pPmin, fPmax, fnbx, fnby
  193. *)
  194.  
  195. procedure TOperation.PrepareAddSous(TypeOperation : TTypeOperation = OPaddition);
  196. var
  197.   placevirg: Integer;
  198.   i: Integer;
  199.   Decimales,MaxDecimals: Integer;
  200.   Entiere, MaxEntiere : Integer;
  201.   OB: integer;
  202.   FormatStr: string;
  203.   StrN : string;
  204.   TabNombres: array[0..2] of currency;
  205. begin
  206.     if (TypeOperation = opSoustraction) and(n1<n2) then
  207.         begin
  208.           fn1 := n2;
  209.           fn2 := n1;
  210.         end
  211.     else
  212.         begin
  213.           fn1 := n1;
  214.           fn2 :=n2;
  215.         end;
  216.      WriteDebug(format('PrepareAddSous  : n1 %g n2 %g', [fn1,fn2]));
  217.  
  218.    TabNombres[0]:=fN1;
  219.    TabNombres[1]:=fN2;
  220.    case TypeOperation  of
  221.     opAddition :     begin
  222.                        TabNombres[2]:=fn1+fn2;
  223.                      end;
  224.     opSoustraction : begin
  225.                        TabNombres[2]:=fn1-fn2;
  226.                      end;
  227.    end;
  228.  
  229.  
  230.  
  231.    // Initialisations
  232.    MaxDecimals := 0;
  233.    MaxEntiere :=0;
  234.  
  235.   for i := 0 to 2 do
  236.     begin
  237.       (* copier les nombres au format court %g dans le StringList
  238.          calculer maxdecimales et maxentieres
  239.          Mémoriser la longueur de la partie entière et cele de la partie décimale
  240.          dans le Tobject de la liste SL 1000*longueurentiere + longueur décimale
  241.       *)
  242.       StrN := format('%g',[TabNombres[i]]);
  243.        placevirg := pos(DecimalSeparator,StrN);
  244.       if placevirg > 0 then
  245.         begin
  246.            Decimales   :=length(StrN) - placevirg;
  247.            Entiere := placevirg -1;
  248.            MaxDecimals := Max(MaxDecimals, Decimales);
  249.            MaxEntiere  := Max(MaxEntiere , Entiere);
  250.         end
  251.       else // entier
  252.         begin
  253.            Entiere := length(StrN);
  254.            Decimales := 0;
  255.            MaxEntiere  := Max(MaxEntiere ,Entiere);
  256.        end;
  257.        OB := 1000* Entiere + Decimales;
  258.        fSLNombres.AddObject(Strn,Tobject(OB)) ;
  259.     end;
  260.     // formater les nombres SANS separateur décimal
  261.     // des espaces à gauche de la partie entière
  262.     // des 'o' pour compléter la partie décimale
  263.  
  264.   for i := 0 to fSLNombres.Count-1 do
  265.     begin
  266.       OB := Integer(fSLNombres.Objects[i]);
  267.       Entiere   := OB div 1000;
  268.       decimales := OB mod 1000;
  269.  
  270.       StrN := StringReplace(fSLNombres[i],DecimalSeparator,'',[rfReplaceAll]) ;
  271.  
  272.       StrN := StrN + DupeString('o',MaxDecimals-Decimales);
  273.  
  274.       StrN := DupeString(' ',MaxEntiere-entiere) + StrN ;
  275.  
  276.       fSLNombres[i]:=strn;
  277.  
  278.     end;
  279.   // ajouter le signe
  280.   case TypeOperation  of
  281.     OPaddition     : begin
  282.                       fSLNombres[0] := ' ' + fSLNombres[0];
  283.                       fSLNombres[1] := '+' + fSLNombres[1];
  284.                       fSLNombres[2] := ' ' + fSLNombres[2];
  285.  
  286.                      end;
  287.     OPsoustraction : begin
  288.                       fSLNombres[0] := ' ' + fSLNombres[0];
  289.                       fSLNombres[1] := '-' + fSLNombres[1];
  290.                       fSLNombres[2] := ' ' + fSLNombres[2];
  291.                      end;
  292.   end;
  293.  
  294.   fPmax :=  MaxEntiere;
  295.   fPmin := -MaxDecimals;
  296.   fnbx := (fpmax - fPmin) + 1;
  297.   fnby := 3;
  298.     fcote := Min((fCanvas.ClipRect.Right - 1) div fnbx, (fCanvas.ClipRect.Bottom ) div fnby -1);
  299.  
  300.     fMargeG := (fCanvas.ClipRect.Right   - fnbx * fcote) div 2;
  301.     fMargeH := (fCanvas.ClipRect.Bottom  - fnby * fcote) div 2;
  302.    case TypeOperation of
  303.     OPaddition : MoteurAddition;
  304.     OPsoustraction : ;
  305.     OPmultiplication : ;
  306.     OPdivision : ;
  307.    end;
  308.  
  309.     //ecrirevirgule := (fpmin<0);
  310.   //Result := Point(MaxEntiere, MaxDecimals );
  311. end;
  312.  
  313. procedure TOperation.WriteDebug(const Msg: string);
  314.   begin
  315.     if Assigned(FSLDebug) then FSLDebug.Add(Msg);
  316.   end;
  317. procedure TOperation.SetSLDebug(const Value: TStrings);
  318. begin
  319.  FSLDebug := Value;
  320. end;
  321.  
  322.  
  323. constructor TOperation.Create(ATypeOperation: TTypeOperation; AN1, AN2: Currency;ACanvas : TCanvas);
  324. begin
  325.   fCoulFond :=      clWhite; // Couleur de fond par défaut
  326.   fCoulTrait :=     rgb(167,221,245); // Couleur des traits par défaut
  327.   fCoulTraitFin :=  rgb(161,226,250);
  328.   fCoulChiffres :=  rgb(0,4,246);
  329.   fCoulRetenues :=  clRed ;
  330.  
  331.  
  332.   FTypeOperation := ATypeOperation;
  333.   fN1 := AN1;
  334.   fN2 := AN2;
  335.  
  336.   fListeEtapes := TList.Create;
  337.   fSLNombres:=TStringList.Create;
  338.   fSLNombres.Clear;
  339.  
  340.   fCanvas := ACanvas;
  341.  
  342.   PrepareOperation;
  343.   AfficheOperation;
  344.  
  345. end;
  346. Procedure TOperation.RAZ;
  347. var
  348.   i: Integer;
  349. begin
  350.   fSLNombres.Clear;
  351.   for i := 0 to fListeEtapes.Count - 1 do
  352.     Dispose(PEtape(fListeEtapes[i]));
  353. end;
  354.  
  355. destructor TOperation.Destroy;
  356. var
  357.   i: Integer;
  358. begin
  359.   RAZ;
  360.   fListeEtapes.Free;
  361.   inherited Destroy;
  362. end;
  363.  
  364. procedure TOperation.SetTypeOperation(ATypeOperation: TTypeOperation);
  365. begin
  366.   FTypeOperation := ATypeOperation;
  367. end;
  368.  
  369. function TOperation.GetTypeOperation: TTypeOperation;
  370. begin
  371.   Result := FTypeOperation;
  372. end;
  373.  
  374. procedure TOperation.SetN1(Value: Currency);
  375. begin
  376.   fN1 := Value;
  377. end;
  378.  
  379. function TOperation.GetN1: Currency;
  380. begin
  381.   Result := fN1;
  382. end;
  383.  
  384. procedure TOperation.SetN2(Value: Currency);
  385. begin
  386.   fN2 := Value;
  387. end;
  388.  
  389. function TOperation.GetN2: Currency;
  390. begin
  391.   Result := fN2;
  392. end;
  393.  
  394. procedure TOperation.PrepareOperation;
  395. begin
  396.   RAZ;
  397.   case FTypeOperation of
  398.     OPaddition, OPsoustraction:
  399.       begin
  400.         PrepareAddSous(FTypeOperation);
  401.       end;
  402.     OPmultiplication:
  403.       begin
  404.         // Code pour préparer la multiplication
  405.       end;
  406.     OPdivision:
  407.       begin
  408.         // Code pour préparer la division
  409.       end;
  410.   end;
  411. end;
  412.  
  413. end.
  414.  
  415.  
  416.  
  417.  
  418.  
Advertisement
Add Comment
Please, Sign In to add comment