Contrôl de format / propos de sortie (string & integer)

Description

####################
DELPHI 6 LIBRARY
####################
UNIT : CHCKENTRY
FUNCTION : CHKCHCKOP
####################

========================
Description
===
Cette fonction contrôle la validité d'une entrée,
ainsi, elle formatte la valeur de retour suivant l'opérande souhaité.
La valeur "sa4lut' renvoie donc 'salut' si le paramètre 's' est passé
Si le paramètre 'i' est spécifié, la valeur de retour sera donc '4'.

========================
Paramètre de la fonction
===
Description Type Exemple
Contenu du texte string 'sal4ut'
Opérande Char 'i'

A noter les éléments suivants:
's' >> Contrôle sur le string
'i' >> Contrôle sur l'entier

========================
Retour de la fonction
===
Variables ... Type Exemple
chkop(param1,param2) Boolean True (-1)
strTextFinal String '4'

========================
Exemple d'utilisatation de la fonction
===

{Déclaration des variables}
var
strText:string; {Contenu dans champs texte ....}
chrOperande:Char; {Caractère qui spécifie l'opérande}
begin
{Initialisation des variables}
strText:=Input1.Text;
chrOperande := 'i'; {Integer}

{apelle de function : verification de l'opérande}
{le retour de la fonction est traitée comme un accusé de récpetion}
if chckop(strText, chrOperande)=true then begin
{Type entré correct - valeurs entières}
messagedlg('Le type entré est correct',mtInformation,[mbOk],0);
end
else begin
{Type entré incorrect - valeur textes ou symboles ...}
messagedlg('Seuls les nombres caractères sont pris en compte', mtWarning,

[mbOk],0);
end;

{Suggerer la valeur possible ... Reporter les entiers}
Input1.Text:=strTextFinal;
end;

________________
Crée le : 25.04.2002
Maj le : 26.04.2002
Auteur : Zechs Merquise :: vivien.steffen@urbanet.ch

Source / Exemple :


unit chckentry;

interface

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

  function chckop(strText_:string; chrOperande:Char):boolean;

type
  TForm1 = class(TForm)
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  Form1: TForm1;
  {Globale}
  strVectInput : array [1..255] of string;      {compteur de vecteur}
  iCountVect : integer;                         {compteur de vecteur}
  strTextFinal:string;                          {Valeur du texte traité}

implementation

{$R *.dfm}

function chckop(strText_:string; chrOperande:Char):boolean;
{Déclaration des variables}
var
        i:integer;      {compteur de boucle}
        chrAsci:string; {caractère asci}
        strChr:string;  {caractère en cours de traitement}

        bReturn:boolean;        {Retour de fonction}

        iLenText:integer;       {Longueur du texte passé en paramètre}
begin

                for i:=1 to iCountVect do begin
                        strVectInput[i]:='0';
                end;
                iCountVect:=0;          {Compteur de vecteur - GLOBALE}
                strTextFinal:='';       {Texte traité}
                iLenText:=length(strText_);

                {Convertion du charactère en asci}
                for i:=1 to iLenText do begin

                        {Initialisation des variables }
                        bReturn:=true;   {Retour de fonction}
                        chckop:=bReturn;

                        strChr:=midstr(strText_,i,1);
                        chrAsci:=IntToStr(ord(strChr[1]));

                        {Controle sur le cacartère ASCII}
                        {Si c'est un nombre, donc entre 48 et 57}

                        case chrOperande of
{============================================================================================= INTEGER}
                                'i':begin{correspobd à Entier : integer}

                                        {Controle sur le cacartère ASCII}
                                        {Si c'est un nombre, donc entre 48 et 57}
                                        if (StrToInt(chrAsci)>47) and (StrToInt(chrAsci)<58) or (StrToInt(chrAsci)=46) then begin
                                                {Initialisation du vecteur suivant i}

                                                if (StrToInt(chrAsci)=46) then begin
                                                end
                                                else begin
                                                        iCountVect:=iCountVect+1;
                                                        strVectInput[iCountVect]:=chr(StrToInt(chrAsci));
                                                end; {end if}
                                        end
                                        else begin {Si le caractère est valide}
                                                {Resultat: opérande non-valide}
                                                bReturn:=false;
                                        end; {End If}

                                        {Si il y'a un caractère point (virgule), donc le laisser dans la suggestion}
                                        if (iCountVect>1) and (StrToInt(chrAsci)=46) then begin
                                                iCountVect:=iCountVect+1;
                                                strVectInput[iCountVect]:='.';
                                        end

                                end;{fin case 'i':}
{============================================================================================= FIN INTEGER}

{>> opérandes: Integer, String}

{============================================================================================ STRING}
                                's':begin{correspond à Chaîne de caractères : string}

                                        if (StrToInt(chrAsci)<65) then begin
                                                {faux}
                                                if (StrToInt(chrAsci)=32) then begin
                                                        bReturn:=true; {barre d'espace}
                                                end
                                                else begin
                                                        bReturn:=false;
                                                end;
                                        end
                                        else if (StrToInt(chrAsci)>90) then begin
                                                if (StrToInt(chrAsci)<97) then begin
                                                        {faux}
                                                        bReturn:=false;
                                                end;
                                                if (StrToInt(chrAsci)>122) then begin
                                                        {faux}
                                                        bReturn:=false;
                                                end;
                                        end;

                                        if bReturn=true then begin
                                                iCountVect:=iCountVect+1;
                                                strVectInput[iCountVect]:=chr(StrToInt(chrAsci));
                                        end;

                                {Si il y'a un caractère point (virgule), donc le laisser dans la suggestion}
                                if (StrToInt(chrAsci)=46) then begin
                                        iCountVect:=iCountVect+1;
                                        strVectInput[iCountVect]:='.';
                                        bReturn:=true;
                                end;
                                end;{fin case 's':}
{============================================================================================= FIN STRING}
                        end;

                chckop:=bReturn;
                end; {End For}

        {Ajouter un 0 après la virgule si c'est un nombre}
        if chrOperande='i' then begin
                if (strVectInput[iCountVect])='.' then begin
                        iCountVect:=iCountVect+1;
                        strVectInput[iCountVect]:='0';
                end;
        end;

        {Renvoie de la valeur obtenue}
        for i:=1 to iCountVect do begin
                strTextFinal:=strTextFinal + strVectInput[i];
        end;

end; {End Function}

end.

Conclusion :


Une mise à jour est prévue, mais je ne saurais vous dire quand, car j'ai des études axés Intelligence Artificielle en ce moment.
Je reste ouvert à toutes remarques et suggestions.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.