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
|
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Find.h"
#include <stdio.h>
#include <stdlib.h >
#include <system.hpp>
#include <SysUtils.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
static W16 N, Total;
static FILE *t ;
static void add(W16 Xi[], W16 SL[])
{
char C[255];
AnsiString As = "";
for (W16 U=0; U < N; U++) As = As + "Coef[" + IntToStr(U) + "] = " + IntToStr(SL[U]) + " ";
As = As.Trim() ;
for (W16 i=0; i < As.Length(); i++)
C[i] = As[i+1];
C[As.Length()] = '\n';
C[As.Length()+1] = '\0';
fprintf(t,C);
W16 Sm=0;
for (W16 U=0; U < N; U++) Sm += Xi[U]* SL[U];
As = "Résultat : " + IntToStr(Sm) ;
for (W16 i=0; i < As.Length(); i++)
C[i] = As[i+1];
C[As.Length()] = '\n';
C[As.Length()+1] = '\0';
fprintf(t,C);
}
static void Reduc ( W16 n, W16 SL[], W16 Xi[], W16 Reste )
{
W16 or = Reste;
short np = Reste / Xi[n]; // trunc
if ( n < N-1 )
for ( W16 u=n+1; u < N; u++) SL[u] =0;
if ( Reste == np * Xi[n] )
{
SL[n] = np;
add(Xi, SL);
np--;
}
if ( n < N-2 )
for ( W16 i =0; i <= np; i++)
{
SL[n] = i;
Reduc(n+1,SL, Xi, or - i * Xi[n]);
}
}
void Search_sol( W16 Xi[], W16 npts, W16 Sigma)
{
Total = Sigma;
N = npts;
for ( W16 i=0; i < N; i++) if ( ! Xi[i] )
{
return; // coef nul pas de sens => le supprimer
}
W16 *SL = new W16[N];
for ( W16 i=0; i < N; i++) SL[i] =0;
W16 a = 0;
t = fopen("Possibilitees.txt","wt");
char C[255];
AnsiString As = "";
for (W16 U=0; U < N; U++) As = As + "X[" + IntToStr(U) + "] = " + IntToStr(Xi[U]) + " ";
As = As.Trim() + "\n" + "\0";
for (W16 i=0; i < As.Length(); i++)
C[i] = As[i+1];
fprintf(t,C);
Reduc(a,SL, Xi,Total);
fclose(t);
delete [] SL;
} |
Partager