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
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int reel;
int img;
} t_complex;
typedef struct {
unsigned short degre;
t_complex coeff;
} t_monome;
typedef struct s_noeud {
t_monome monome;
struct s_noeud* next;
} t_noeud;
typedef struct {
t_noeud *first;
} t_liste;
void liste_init(t_liste *l)
{
l->first=NULL;
}
t_noeud *noeud_ajout(unsigned short degre, int coeffr, int coeffi)
{
t_noeud *noeud;
//allocation mémoire
if((noeud = malloc(sizeof(t_noeud))) == NULL) return NULL;
//remplissage
noeud->monome.degre = degre;
noeud->monome.coeff.reel = coeffr;
noeud->monome.coeff.img = coeffi;
noeud->next = NULL;
return noeud;
}
t_liste *liste_insere(t_liste *liste, t_noeud *noeud)
{
t_noeud *courant;
if(noeud == NULL) return NULL;
// Quand la liste est vide
if(liste->first == NULL)
{
liste->first = noeud;
return liste;
}
// pour le premier élément
if(noeud->monome.degre > liste->first->monome.degre)
// Ici je cherche à placer un monome de degré supérieur à l'élément
// premier de la liste
{
noeud->next = liste->first;
liste->first = noeud;
return liste;
}
// J'insère des monomes en remontant la liste tout en cherchant à trier
// le polynome en placant les monomes de degré dans l'ordre décroissant
for (courant = liste->first; courant->next != NULL && courant->next->monome.degre > noeud->monome.degre; courant = courant->next)
{ // le bloc est vide
}
noeud->next = courant->next;
courant->next = noeud;
return liste;
}
void liste_affiche(t_liste *liste)
{
t_noeud *courant = liste->first;
if (liste == NULL)exit(EXIT_FAILURE);
while(courant != NULL)
{
printf("%di+%dx^%d",courant->monome.coeff.reel,courant->monome.coeff.img, courant->monome.degre); //Ici la variable n'est pas de type noeud mais de type noeud*. C'est donc un pointeur qui contient l'adresse d'un élément de type noeud. Pour accéder aux membres de l'élément pointé, soit on écrit (*noeud).monome, soit on écrit noeud->monome qui signifie la même chose mais écrit avec une écriture simplifiée.
courant = courant->next;
if (courant != NULL) {
if((rand()%2) == 1){
printf(" - ");
}
else{
printf(" + ");
}
}
}
printf("\n");
}
int main()
{
t_liste liste;
liste_init(&liste);
liste_affiche(&liste);
unsigned short i = 0;
unsigned short degre = 0;
int nombreDeMonomes = 0;
int coeffRe = 0;
int coeffIm = 0;
//insertion de monome
srand(time(NULL));
printf("Combien de monomes voulez vous insérer dans votre polynome: ");
printf("\n");
scanf("%d",&nombreDeMonomes);
for(i = 0; i < nombreDeMonomes; i++)
{
printf("Rentrer le degré, le coefficient réel et le coefficient imaginaire: ");
scanf("%hu",°re);
scanf("%d%d",&coeffRe,&coeffIm);
liste_insere(&liste, noeud_ajout(degre, coeffRe, coeffIm));
}
liste_affiche(&liste);
return 0;
} |
Partager