Erreur aléatoire lors de l'ajout d'un ShowMessage
Bonsoir à tous,
J'ai une erreur dans les résultats, mais que je n'arrive pas à la détecter. Je m'explique :
Je suis en train de programmer la méthode hongroise, j'ai dans mon application un Edit pour la taille de la matrice, un Speedbutton pour fixer les lignes et les colonnes des matrices, et un autre pour l'exécution. J'ai trois StringGrid en tout. Bon, le code marche très bien, j'obtiens le résultat escompté, mais quand je rajoute les instructions suivantes :
Code:
1 2 3 4 5 6 7 8 9
|
if(sol==true)
{
for(i=1;i<=n;i++)
if(StringGrid3->Cells[0][i]=="1")
{
ShowMessage("coucou"); // Comme exemple
}
} |
ça me donne n'importe quoi !!! En fait, c'est les valeurs du StringGrid2 qui changent alors que je ne le mentionne même pas dans le précédent code 8O.
Voici le code complet :
Code:
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
|
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int n;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
n=StrToInt(Edit1->Text);
StringGrid1->RowCount=n;
StringGrid1->ColCount=n;
StringGrid2->RowCount=n;
StringGrid2->ColCount=n;
StringGrid3->RowCount=n+1;
StringGrid3->ColCount=n+1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
int i,j;
float min;
for(i=0;i<=n-1;i++)
{
min=StrToFloat(StringGrid1->Cells[0][i]);
for(j=1;j<=n-1;j++) //Je cherche le min dans chaque ligne
if (StrToFloat(StringGrid1->Cells[j][i])<min) //et je soustrais à toute la ligne cette valeur
min=StrToFloat(StringGrid1->Cells[j][i]);
for(j=0;j<=n-1;j++)
StringGrid1->Cells[j][i]=FloatToStr(StrToFloat(StringGrid1->Cells[j][i])-min);
}
for(j=0;j<=n-1;j++)
{
min=StrToFloat(StringGrid1->Cells[j][0]); //Je cherche le min dans chaque colonne
for(i=1;i<=n-1;i++) //et je le soustrais à toute la colonne cette valeur
if (StrToFloat(StringGrid1->Cells[j][i])<min)
min=StrToFloat(StringGrid1->Cells[j][i]);
for(i=0;i<=n-1;i++)
StringGrid1->Cells[j][i]=FloatToStr(StrToFloat(StringGrid1->Cells[j][i])-min);
}
//Programme 2
int s,k,l;
bool sol;
s=0;
for(i=0;i<=n-1;i++)
{
sol=true;
if(s==0)
{
for(j=0;j<=n-1;j++)
{
if(StringGrid1->Cells[j][i]=="0")
for(k=0;k<=i-1;k++)
if(StringGrid2->Cells[j][k]=="0")
break;
if(k==i)
{
sol=false;
break;
}
}
s=1;
}
else
{
for(j=n-1;j>=0;j--) //Je cherche une affactation possible
{ // Cad si je peux trouver un ensemble de zéros
if(StringGrid1->Cells[j][i]=="0") //tels que un zéro est affecté dans chaque ligne et dans chaque colonne,
for(k=0;k<=i-1;k++) //si c'est le cas sol=true
if(StringGrid2->Cells[j][k]=="0")
break;
if(k==i)
{
sol=false;
break;
}
}
s=0;
}
if(sol==false)
StringGrid2->Cells[j][i]="0";
}
sol=false;
for(i=0;i<=n-1;i++) //Si on trouve une ligne non affectée
{ //StringGrid3 prend des valeurs égales "1"
for(j=0;j<=n-1;j++)
if(StringGrid2->Cells[j][i]=="0")
break;
if(j==n)
{
sol=true;
for (k=0;k<=n;k++)
StringGrid3->Cells[k][i+1]="1";
}
}
if(sol==true) //c'est là où le problème se pose
{ //Vous remarquez que je n'ai pas touché au StrinGrid2
for(i=1;i<=n;i++) //alors que ses valeurs changent ???!!!
if(StringGrid3->Cells[0][i]=="1")
{
ShowMessage("coucou");
}
}
} |
Essayez cet exemple et vous verrez mieux de quoi je parle :
une matrice de dimension 5 :
100/45/34/45/51
45/100/5/10/20/15
34/5/100/20/26
40/10/20/100/29
51/15/26/29/100
Si vous exécutez sans le premier code vous obtiendrez un StringGrid2 différent de celui obtenu avec le code !!!
Je n'arrive pas à voir où se situe le problème :(.
Si quelqu'un à une idée ... ;)
En attente de vos suggestions.