TP06 Listes Chainées Début

This commit is contained in:
HORVILLE 2022-01-04 13:49:41 +01:00
parent 743b3436df
commit ba81c711f4
6 changed files with 361 additions and 0 deletions

40
APL1.2/TP06/arc_en_ciel.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include "graph_sup.h"
struct block_ {
char* value;
struct block_* next;
};
typedef struct block_ block;
typedef block* list;
int main(int argc, char * argv[]) {
InitialiserGraphique();
CreerFenetre(100, 100, 800, 200);
block violet = {"purple", NULL};
block indigo = {"indigo", &violet};
block bleu = {"blue", &indigo};
block vert = {"green", &bleu};
block jaune = {"yellow", &vert};
block orange = {"orange", &jaune};
block rouge = {"red", &orange};
violet.next = &rouge;
list l = &rouge;
while(1) {
if (DrawNextFrame()) {
SetColorN(l->value);
EcrireTexte(200, 50, argv[1], 2);
l = l->next;
}
}
FermerGraphique();
return EXIT_SUCCESS;
}

52
APL1.2/TP06/circulation.c Normal file
View File

@ -0,0 +1,52 @@
#include<stdio.h>
#include<stdlib.h>
struct block_ {
int value;
struct block_* next;
};
typedef struct block_ block;
typedef block* list;
void remLast(list l) {
block* b = l;
while (b->next->next != NULL) b = b->next;
b->next = NULL;
}
void showList(list l) {
block* b = l;
while (b != NULL) {
printf("%d ", b->value);
b = b->next;
}
puts("");
}
list circularRotation(list l) {
block* b = l;
while (b->next->next != NULL) b = b->next;
list first = b->next;
first->next = l;
b->next = NULL;
return first;
}
int main(int argc, char * argv[]) {
block b4 = {19, NULL};
block b3 = {15, &b4};
block b2 = {10, &b3};
block b1 = {5, &b2};
block b0 = {1, &b1};
list l = &b0;
showList(l);
remLast(l);
showList(l);
l = circularRotation(l);
showList(l);
return EXIT_SUCCESS;
}

71
APL1.2/TP06/graph_sup.c Normal file
View File

@ -0,0 +1,71 @@
#include "utils.h"
#include <graph.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graph_sup.h"
#define FPS 2.0
/*La liste des bouttons et leur nombre*/
button* Buttons;
int BT_Count = 0;
/*Des variables afin de faciliter le calcul du temps entre chaque image pour int DrawNextFrame().*/
double delta = (1/FPS)*1000000;
unsigned long suivant = (1/FPS)*1000000;
/*Un set de fonction permettant de facilement mettre en place des boutons, pouvoir les manipuler et les supprimer
Ces fonctions utilisent la structure struct Button.*/
void ClearButtons() {
BT_Count = 0;
Buttons = (button*)realloc(Buttons, sizeof(button) * 1);
}
void AddButton(int x, int y, int w, int h, int id) {
button BT = {x, y, w, h, id};
BT_Count++;
Buttons = (button*) realloc(Buttons, (BT_Count+1) * sizeof(button));
Buttons[BT_Count-1] = BT;
}
int GetButton(int x, int y) {
for (int ID = 0; ID < BT_Count; ID++) {
button BT = Buttons[ID];
if (x >= BT.x && y >= BT.y && x <= BT.x + BT.w && y <= BT.y + BT.h) {
return BT.id;
}
}
return -1;
}
/*Des fonctions appelant celles de la bibliothèque graphique afin de réduire la longueur des noms..*/
couleur GetColorN(char* name) {
return CouleurParNom(name);
}
couleur GetColor(unsigned char r, unsigned char g, unsigned char b) {
return CouleurParComposante(r, g, b);
}
void SetColor(unsigned char r, unsigned char g, unsigned char b) {
ChoisirCouleurDessin(CouleurParComposante(r, g, b));
}
void SetColorC(couleur Color) {
ChoisirCouleurDessin(Color);
}
void SetColorN(char* name) {
ChoisirCouleurDessin(CouleurParNom(name));
}
/*Permet de faire tourner la partie graphique à X images par secondes définit par la constante FPS.*/
int DrawNextFrame() {
if (Microsecondes() >= suivant) {
suivant = Microsecondes() + delta;
return 1;
}
return 0;
}

32
APL1.2/TP06/graph_sup.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef _GRAPH_SUP_H
#define _GRAPH_SUP_H
#define WIDTH 1200
#define HEIGHT 700
struct Button {
int x;
int y;
int w;
int h;
int id;
};
typedef struct Button button;
extern button* Buttons;
extern int BT_Count;
void ClearButtons();
int GetButton(int x, int y);
void AddButton(int x, int y, int w, int h, int id);
int DrawNextFrame(void);
couleur GetColorN(char* name);
couleur GetColor(unsigned char r, unsigned char g, unsigned char b);
void SetColor(unsigned char r, unsigned char g, unsigned char b);
void SetColorC(couleur Color);
void SetColorN(char* name);
#endif

55
APL1.2/TP06/maximum.c Normal file
View File

@ -0,0 +1,55 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct maillon_s {
unsigned short valeur;
struct maillon_s* suivant;
};
typedef struct maillon_s maillon;
typedef maillon* liste;
void list(liste l) {
maillon* m = l;
while (m != NULL) {
printf("%3hu ", m->valeur);
m = m->suivant;
}
puts("");
}
unsigned short max(liste l) {
maillon* m = l;
unsigned short max = 0;
while (m != NULL) {
if (m->valeur > max) max = m->valeur;
m = m->suivant;
}
return max;
}
int main(int argc, char * argv[]) {
liste l;
maillon* last_m = NULL;
srand(time(NULL));
for (int i = 0; i < 10; i++) {
srand(rand());
unsigned short random = rand() % (999-111) + 111;
maillon* new_m = malloc(sizeof(maillon));
new_m->valeur = random;
if (last_m != NULL) new_m->suivant = last_m;
last_m = new_m;
}
l = last_m;
list(l);
printf("%3hu\n", max(l));
return EXIT_SUCCESS;
}

111
APL1.2/TP06/selection.c Normal file
View File

@ -0,0 +1,111 @@
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct block_ {
int value;
struct block_* next;
};
typedef struct block_ block;
typedef block* list;
void showList(list l) {
block* b = l;
while (b != NULL) {
printf("%d ", b->value);
b = b->next;
}
puts("");
}
list insert(list l, int value, int index) {
block* b = l;
if (index == -1) {
while (b->next != NULL) b = b->next;
block* new_block = malloc(sizeof(block));
new_block->value = value;
b->next = new_block;
return l;
} else if (index == 0) {
block* new_block = malloc(sizeof(block));
new_block->value = value;
new_block->next = l;
return new_block;
} else {
for (int i = 0; i < index-1; i++) {
b = b->next;
}
block* new_block = malloc(sizeof(block));
new_block->value = value;
new_block->next = b->next;
b->next = new_block;
return l;
}
}
list suppress(list l, int index) {
if (index == 0) return l->next;
else {
block* b = l;
for (int i = 0; i < index-1; i++) {
b = b->next;
}
if (b->next->next != NULL) b->next = b->next->next;
else b->next = NULL;
return l;
}
}
list sort(list l) {
list sorted = NULL;
while (l != NULL) {
int index = 0;
int max_index = 0;
block* max = malloc(sizeof(block));
max->value = 0;
block* curr = l;
while (curr != NULL) {
if (curr->value > max->value) {
max_index = index;
max = curr;
}
index++;
curr = curr->next;
}
l = suppress(l, max_index);
sorted = insert(sorted, max->value, 0);
}
return sorted;
}
list randList() {
list l = NULL;
srand(time(NULL));
for (int i = 0; i < 10; i++) {
srand(rand());
int random = rand() % (999-111) + 111;
block* new_m = malloc(sizeof(block));
new_m->value = random;
if (l != NULL) new_m->next = l;
l = new_m;
}
return l;
}
int main(int argc, char * argv[]) {
list l = randList();
showList(l);
l = sort(l);
showList(l);
return EXIT_SUCCESS;
}