fusion sur structures fait mais a rendre plus bo

master
parent 4dd6ef0522
commit 5612be678c

@ -1,95 +1,177 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../includes/recherche.h"
#include "../includes/structures.h"
void fusion(char *R[], int tR, char *S[], int tS, char *T[])
void fusionMaillonDevis(ListeDevis R, int tR, ListeDevis S, int tS, ListeDevis *T, int *tT)
{
int i;
int j;
int k;
i = 0;
j = 0;
ListeDevis pT;
int k;
*T = NULL;
pT = *T;
k = 0;
while (i < tR && j < tS)
while (R && S)
{
if (strcmp(R[i], S[j]) < 0)
if (strcmp(R->dev.nomE, S->dev.nomE) < 0)
{
T[k] = R[i];
i++;
if (!*T)
{
*T = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = *T;
}
else
{
pT->suiv = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = pT->suiv;
}
pT->dev = R->dev;
R = R->suiv;
}
else
{
T[k] = S[j];
j++;
if (!*T)
{
*T = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = *T;
}
else
{
pT->suiv = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = pT->suiv;
}
pT->dev = S->dev;
S = S->suiv;
}
pT->suiv = NULL;
k++;
}
while (i < tR)
while (R)
{
T[k] = R[i];
i++;
if (!*T)
{
*T = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = *T;
}
else
{
pT->suiv = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = pT->suiv;
}
pT->dev = R->dev;
R = R->suiv;
pT->suiv = NULL;
k++;
}
while (j < tS)
while (S)
{
T[k] = S[j];
j++;
if (!*T)
{
*T = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = *T;
}
else
{
pT->suiv = (ListeDevis)malloc(sizeof(MaillonDevis));
pT = pT->suiv;
}
pT->dev = S->dev;
S = S->suiv;
pT->suiv = NULL;
k++;
}
*tT = k;
}
void copy(char *T[], int i, int j, char *R[])
void triFusionListeDevis(ListeDevis *ldevis, int *tlog)
{
int k;
ListeDevis p;
ListeDevis q;
int tR;
int tS;
int i;
k = 0;
while (i < j)
tR = *tlog / 2;
tS = *tlog - tR;
p = *ldevis;
if (!p)
{
R[k] = T[i];
i++;
k++;
*tlog = 0;
return;
}
if (!(p->suiv))
{
*tlog = 1;
return;
}
q = p;
for (i = 0; i < tR - 1; i++)
{
if (!q)
break;
q = q->suiv;
}
if (!q)
return;
ListeDevis temp = q->suiv;
q->suiv = NULL;
triFusionListeDevis(ldevis, &tR);
triFusionListeDevis(&temp, &tS);
fusionMaillonDevis(*ldevis, tR, temp, tS, ldevis, tlog);
}
void triFusion(char *T[], int tlog)
/*------------------------affichage-----------------------------------*/
void afficherListeDevis(ListeDevis ldevis)
{
char **R;
char **S;
while (ldevis)
{
printf("%s, ", ldevis->dev.nomE);
ldevis = ldevis->suiv;
}
printf("\n");
}
if (tlog <= 1)
return;
R = (char **)malloc(sizeof(char *) * (tlog / 2));
S = (char **)malloc(sizeof(char *) * (tlog - tlog / 2));
if (!R || !S)
exit(1);
copy (T, 0, tlog / 2, R);
copy (T, tlog / 2, tlog, S);
triFusion (R, tlog / 2);
triFusion (S, tlog - tlog / 2);
fusion (R, tlog / 2, S, tlog - tlog / 2, T);
free(R);
free(S);
void afficherOffre(Offre *offre)
{
printf("Travaux: %s\n\t", offre->travaux);
afficherListeDevis(offre->ldevis);
}
int main(void)
/*---------------------------------------------------------------------*/
int main(void)
{
char *tab[5] = {"banana", "apple", "orange", "grape", "cherry"};
int tlog = 5;
int i;
Offre offre1 = {"Travaux1", NULL};
offre1.ldevis = (ListeDevis)malloc(sizeof(MaillonDevis));
offre1.ldevis->dev = (Devis){"Devis6", "Adresse1", 100, 5};
offre1.ldevis->suiv = (ListeDevis)malloc(sizeof(MaillonDevis));
offre1.ldevis->suiv->dev = (Devis){"Devis3", "Adresse3", 80, 3};
offre1.ldevis->suiv->suiv = NULL;
printf("Avant le tri : \n\t");
for (i = 0; i < tlog; i++)
printf("%s, ", tab[i]);
Offre offre2 = {"Travaux2", NULL};
offre2.ldevis = (ListeDevis)malloc(sizeof(MaillonDevis));
offre2.ldevis->dev = (Devis){"Devis2", "Adresse2", 120, 7};
offre2.ldevis->suiv = (ListeDevis)malloc(sizeof(MaillonDevis));
offre2.ldevis->suiv->dev = (Devis){"Devis4", "Adresse4", 90, 4};
offre2.ldevis->suiv->suiv = NULL;
triFusion(tab, tlog);
printf("Avant le tri : \n");
afficherOffre(&offre1);
afficherOffre(&offre2);
printf("\nApres le tri : \n\t");
for (i = 0; i < tlog; i++)
printf("%s, ", tab[i]);
printf("\n");
int tlog1 = 0;
triFusionListeDevis(&offre1.ldevis, &tlog1);
return 0;
int tlog2 = 2;
triFusionListeDevis(&offre2.ldevis, &tlog2);
printf("\nApres le tri : \n");
afficherOffre(&offre1);
printf("Taille logique : %d\n", tlog1);
afficherOffre(&offre2);
printf("Taille logique : %d\n", tlog2);
return (0);
}

Loading…
Cancel
Save