Calculer la longueur (nombre de chiffres) des nombres contenus dans un tableau

Résolu
Aliboudjemai Messages postés 11 Date d'inscription samedi 2 décembre 2017 Statut Membre Dernière intervention 28 janvier 2020 - Modifié le 28 nov. 2020 à 20:21
korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 - 12 mars 2018 à 17:32
bonjour tout le monde j'espère que vous allez bien, je vous adresse afin de trouver une solution à une question
voilà j'ai un tableau T qui contient des nombres , je veut créer un autre tableau R qui me donne le nombre de chiffre de chaque nombre
exemple : j'ai un nombre 425
le nombre de chiffre c'est 3
et Merci d'avance.

2 réponses

dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
24 févr. 2018 à 07:55
Salut
tu peux utiliser 2 fonctions :
IntToStr(nombre) te transforme le nombre en chaîne,
Length(Chaîne) te donne le nombre de caractères
Tu fait une boucle qui lit le tableau de nombres et qui remplit le deuxième tableau avec le nombre de chiffre
Bon courage
0
Aliboudjemai Messages postés 11 Date d'inscription samedi 2 décembre 2017 Statut Membre Dernière intervention 28 janvier 2020
24 févr. 2018 à 16:08
merci beaucoup
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
9 mars 2018 à 02:01
Un methode avec des performances constantes :

function howManyNumbers(n : Int64): Int64;
begin
if n < 100000 then
begin
if n < 100 then
begin
if n < 10 then
begin
result := 1; exit;
end
else
begin
result := 2; exit;
end;
end
else
begin
if n < 1000 then
begin
result := 3; exit;
end
else
if n < 10000 then
begin
result := 4; exit;
end
else
begin
result := 5; exit;
end;
end;
end
else
begin
if n < 10000000 then
begin
if n < 1000000 then
begin
result := 6; exit;
end
else
begin
result := 7; exit;
end
end
else
begin
if n < 100000000 then
begin
result := 8; exit;
end
else
begin
if n < 1000000000 then
begin
result := 9; exit;
end
else
begin
result := 10; exit;
end;
end;
end;
end;
end;
0
dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
9 mars 2018 à 16:07
Maintenant, tu peux aussi faire cela :

VAR {global}
TB1 : array[0..9] of integer = (5,18,556,24,8,4000,555,7777,45,8546);
TB2 : array[0..9] of integer;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i := Low(TB1) to High(TB1) do TB2[i] := length(inttostr(TB1[i]));
end;
0
korgis Messages postés 420 Date d'inscription samedi 17 mai 2003 Statut Membre Dernière intervention 6 mai 2019 17
12 mars 2018 à 17:32
Salut,

La fonction de f0xi est 30 à 40 fois plus rapide que l'utilisation de Length(IntToStr(n)).

J'en fais la démonstration ici.

Pour la reproduire, il faut disposer sur une fiche :
- 2 TListBox d'aspect vertical pour lister les tableaux, (et la Font "Courier New" taille 9 ça le fait...),
- 3 TButton,
- 1 TLabel.
et coller le code dans les évènements "FormCreate", "Button1Click", "Button2Click", "Button3Click".

J'avais également testé une fonction utilisant case of qui semble encore plus rapide, mais qui ne gère pas les valeurs de type Int64.
Elle semble légèrement plus rapide que la première.
Je l'ajoute donc pour comparaison.

Voici le code :

{ la fonction à f0xi, je l'ai mise en forme pour mieux l'analyser, je suis un peu maniaque ^^ }
function howManyNumbers(n: Int64): Byte;
begin
  if n < 100000 then
  begin
    if n < 100 then
    begin
      if n < 10 then
      begin
        Result := 1;
        Exit;
      end
      else
      begin
        Result := 2;
        Exit;
      end;
    end
    else
    begin
      if n < 1000 then
      begin
        Result := 3;
        Exit;
      end
      else
      if n < 10000 then
      begin
        Result := 4;
        Exit;
      end
      else
      begin
        Result := 5;
        Exit;
      end;
    end;
  end
  else
  begin
    if n < 10000000 then
    begin
      if n < 1000000 then
      begin
        Result := 6;
        Exit;
      end
      else
      begin
        Result := 7;
        Exit;
      end
    end
    else
    begin
      if n < 100000000 then
      begin
        Result := 8;
        Exit;
      end
      else
      begin
        if n < 1000000000 then
        begin
          Result := 9;
          Exit;
        end
        else
        begin
          Result := 10;
          Exit;   // <- utile ?
        end;
      end;
    end;
  end;
end;

{ ma fonction }
function GetLengthNumber(Number: Int64): Byte;
begin
  case Number of
    0..9: Result := 1;
    10..99: Result := 2;
    100..999: Result := 3;
    1000..9999: Result := 4;
    10000..99999: Result := 5;
    100000..999999: Result := 6;
    1000000..9999999: Result := 7;
    10000000..99999999: Result := 8;
    100000000..999999999: Result := 9;
    else
      Result := 10;
  end;
end;

//******************************************************************************

var
  arrNumbers: array of Integer;

// On remplit de manière aléatoire le tableau qui servira à tester les 2 méthodes :
procedure FillArray;
const
  LENGTH_ARRAY = 100000;  // longueur du tableau
const
  NumberWidth: array[0..9] of Integer = (10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, High(Integer));
var
  i: Integer;
begin
  Randomize;
  SetLength(arrNumbers, LENGTH_ARRAY);
  for i := 0 to High(arrNumbers) do
    arrNumbers[i] := Random(NumberWidth[Random(Length(NumberWidth))]);
end;

//******************************************************************************

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  // Mise en forme de la fiche :
  Caption := 'Longueur d''un nombre';
  Button1.Width := 141;
  Button1.Caption := 'howManyNumbers';
  Button2.Width := 141;
  Button2.Caption := 'Length(IntToStr(n))';
  Button3.Width := 141;
  Button3.Caption := 'GetLengthNumber';
  Label1.Caption := '0';

  // Création du tableau de nombres :
  FillArray;

  // Affichage du tableau dans la ListBox1 :
  ListBox1.Clear;
  ListBox1.Items.BeginUpdate;
  for i := 0 to High(arrNumbers) do
    ListBox1.Items.Append(IntToStr(arrNumbers[i]));
  ListBox1.Items.EndUpdate;
end;

const
  // on peut augmenter cette valeur (ou la longueur du tableau) pour plus de précision :
  LOOP_MAXI = 100;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
  arrResults: array of Byte;
  TickCount: DWORD;
begin
  SetLength(arrResults, Length(arrNumbers));

  TickCount := GetTickCount;

  for j := 0 to LOOP_MAXI do
    for i := Low(arrNumbers) to High(arrNumbers) do
      arrResults[i] := howManyNumbers(arrNumbers[i]);

  TickCount := GetTickCount - TickCount;

  // Affichage du temps écoulé :
  Label1.Caption := IntToStr(TickCount);

  // Visualisation du résultat dans ListBox2
  ListBox2.Clear;
  ListBox2.Items.BeginUpdate;
  for i := Low(arrResults) to High(arrResults) do
    ListBox2.Items.Append(IntToStr(arrResults[i]));
  ListBox2.Items.EndUpdate;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  i, j: Integer;
  arrResults: array of Byte;
  TickCount: DWORD;
begin
  SetLength(arrResults, Length(arrNumbers));

  TickCount := GetTickCount;

  for j := 0 to LOOP_MAXI do
    for i := Low(arrNumbers) to High(arrNumbers) do
      arrResults[i] := Length(IntToStr(arrNumbers[i]));

  TickCount := GetTickCount - TickCount;

  // Affichage du temps écoulé :
  Label1.Caption := IntToStr(TickCount);

  // Visualisation du résultat dans ListBox2
  ListBox2.Clear;
  ListBox2.Items.BeginUpdate;
  for i := Low(arrResults) to High(arrResults) do
    ListBox2.Items.Append(IntToStr(arrResults[i]));
  ListBox2.Items.EndUpdate;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i, j: Integer;
  arrResults: array of Byte;
  TickCount: DWORD;
begin
  SetLength(arrResults, Length(arrNumbers));

  TickCount := GetTickCount;

  for j := 0 to LOOP_MAXI do
    for i := Low(arrNumbers) to High(arrNumbers) do
      arrResults[i] := GetLengthNumber(arrNumbers[i]);

  TickCount := GetTickCount - TickCount;

  // Affichage du temps écoulé :
  Label1.Caption := IntToStr(TickCount);

  // Visualisation du résultat dans ListBox2
  ListBox2.Clear;
  ListBox2.Items.BeginUpdate;
  for i := Low(arrResults) to High(arrResults) do
    ListBox2.Items.Append(IntToStr(arrResults[i]));
  ListBox2.Items.EndUpdate;
end;


Cordialement,

korgis

et un coucou amical à f0xi qui vient de temps en temps - trop rarement - nous dépoussiérer les neurones (hi hi, ça chatouille) ^^
0
Rejoignez-nous