TP07 Listes Chainées (suite)

This commit is contained in:
HORVILLE 2022-01-04 15:55:34 +01:00
parent ba81c711f4
commit e032e57920
6 changed files with 455 additions and 0 deletions

58
APL1.2/TP07/chaines.c Normal file
View File

@ -0,0 +1,58 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct char_ {
char letter;
struct char_* next;
};
typedef struct char_ string_char;
typedef string_char* string;
void printString(string str) {
while (str != NULL) {
printf("%c", str->letter);
str = str->next;
}
printf("\n");
}
string toString(char* str) {
string_char* last_c = NULL;
for (int i = strlen(str)+1; i >= 0; i--) {
string_char* c = malloc(sizeof(string_char));
c->letter = str[i];
c->next = last_c;
last_c = c;
}
return last_c;
}
char* toCharT(string str) {
int count = 0;
string str2 = str;
while (str2 != NULL) {
count++;
str2 = str2->next;
}
char* new_str = calloc(sizeof(char), count);
for (int i = 0; i < count; i++) {
new_str[i] = str->letter;
str = str->next;
}
return new_str;
}
int main(int argc, char * argv[]) {
string c = toString("Hello World !");
printString(c);
char* c2 = toCharT(c);
printf("%s\n", c2);
return EXIT_SUCCESS;
}

63
APL1.2/TP07/extensions.c Normal file
View File

@ -0,0 +1,63 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct char_ {
char letter;
struct char_* next;
};
typedef struct char_ string_char;
typedef string_char* string;
string toString(char* str) {
string_char* last_c = NULL;
for (int i = strlen(str)+1; i >= 0; i--) {
string_char* c = malloc(sizeof(string_char));
c->letter = str[i];
c->next = last_c;
last_c = c;
}
return last_c;
}
char* toCharT(string str) {
int count = 0;
string str2 = str;
while (str2 != NULL) {
count++;
str2 = str2->next;
}
char* new_str = calloc(sizeof(char), count);
for (int i = 0; i < count; i++) {
new_str[i] = str->letter;
str = str->next;
}
return new_str;
}
void sputs(string str) {
while (str != NULL) {
printf("%c", str->letter);
str = str->next;
}
printf("\n");
}
void sfgets(string* str, int maxLength, FILE * stream) {
char line[2048];
fgets(line, maxLength, stream);
*str = toString(line);
}
int main(int argc, char * argv[]) {
string c;
sfgets(&c, 1000, stdin);
sputs(c);
return EXIT_SUCCESS;
}

109
APL1.2/TP07/flocon.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include <math.h>
#include "graph_sup.h"
struct vertex {
double x;
double y;
struct vertex* next;
};
typedef struct vertex vert;
typedef vert* poly;
double rad(double deg) {
return deg/180 * M_PI;
}
poly makeTri(double x, double y, double r) {
vert* p1 = malloc(sizeof(vert));
p1->x = x + cos(rad(30)) * r;
p1->y = y + sin(rad(30)) * r;
vert* p2 = malloc(sizeof(vert));
p2->x = x + cos(rad(150)) * r;
p2->y = y + sin(rad(150)) * r;
vert* p3 = malloc(sizeof(vert));
p3->x = x + cos(rad(270)) * r;
p3->y = y + sin(rad(270)) * r;
p1->next = p2;
p2->next = p3;
p3->next = p1;
return p1;
}
void incPoly(poly* tri_add) {
poly tri = *tri_add;
vert* start = tri;
int has_started = 0;
while (tri != start || has_started == 0) {
has_started = 1;
int x = tri->x;
int y = tri->y;
vert* next = tri->next;
int xx = next->x;
int yy = next->y;
vert* p1 = malloc(sizeof(vert));
vert* p2 = malloc(sizeof(vert));
vert* p3 = malloc(sizeof(vert));
double tx = (xx - x) / 3;
double ty = (yy - y) / 3;
p1->x = x + tx;
p1->y = y + ty;
p3->x = x + tx * 2;
p3->y = y + ty * 2;
p2->x = p1->x + (cos(rad(-60)) * tx - sin(rad(-60)) * ty);
p2->y = p1->y + (sin(rad(-60)) * tx + cos(rad(-60)) * ty);
tri->next = p1;
p1->next = p2;
p2->next = p3;
p3->next = next;
tri = tri->next->next->next->next;
}
}
void drawPoly(poly tri) {
vert* start = tri;
int has_started = 0;
while (tri != start || has_started == 0) {
has_started = 1;
int x = tri->x;
int y = tri->y;
vert* next = tri->next;
int xx = next->x;
int yy = next->y;
tri = tri->next;
DessinerSegment(x, y, xx, yy);
}
}
int main(int argc, char * argv[]) {
poly tri = makeTri(1920/2, 1080/2, 550);
InitialiserGraphique();
CreerFenetre(100, 100, 1920, 1080);
SetColor(255, 0, 0);
for (int i = 0; i < 10; i++) incPoly(&tri);
drawPoly(tri);
Touche();
FermerGraphique();
return EXIT_SUCCESS;
}

71
APL1.2/TP07/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/TP07/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

122
APL1.2/TP07/raffinements.c Normal file
View File

@ -0,0 +1,122 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct char_ {
char letter;
struct char_* next;
struct char_* prev;
};
typedef struct char_ string_char;
typedef string_char* string;
string toString(char* str) {
string_char* last_c = NULL;
for (int i = strlen(str); i >= 0; i--) {
string_char* c = malloc(sizeof(string_char));
c->letter = str[i];
c->next = last_c;
if (last_c != NULL) last_c->prev = c;
last_c = c;
}
return last_c;
}
char* toCharT(string str) {
int count = 0;
string str2 = str;
while (str2 != NULL) {
count++;
str2 = str2->next;
}
char* new_str = calloc(sizeof(char), count);
for (int i = 0; i < count; i++) {
new_str[i] = str->letter;
str = str->next;
}
return new_str;
}
void sputs(string str) {
while (str != NULL) {
printf("%c", str->letter);
str = str->next;
}
printf("\n");
}
void sfgets(string* str, int maxLength, FILE * stream) {
char line[2048];
fgets(line, maxLength, stream);
*str = toString(line);
}
int sstrlen(string str) {
int count = 0;
while (str != NULL) {
count++;
str = str->next;
}
return count - 1;
}
void sstrcpy(string* str1, string str2) {
string_char* last_c = NULL;
int length = sstrlen(str2);
while (str2->next != NULL) str2 = str2->next;
for (int i = length; i >= 0; i--) {
string_char* c = malloc(sizeof(string_char));
c->next = last_c;
c->letter = str2->letter;
str2 = str2->prev;
if (last_c != NULL) c->prev = last_c;
last_c = c;
}
*str1 = last_c;
}
int sstrcmp(string str1, string str2) {
while (str1 != NULL || str2 != NULL) {
if (str1 == NULL && str2 == NULL) break;
else if (str1 == NULL && str2 != NULL) return -1;
else if (str1 != NULL && str2 == NULL) return -1;
else if (str1->letter != str2->letter) return -1;
str1 = str1->next;
str2 = str2->next;
}
return 0;
}
string sstrcat(string* stra, string* strb) {
string str1 = *stra;
string str2 = *strb;
while (str1->next->next != NULL) str1 = str1->next;
str1->next = str2;
str2->prev = str1;
while (str1->prev != NULL) str1 = str1->prev;
}
int main(int argc, char * argv[]) {
string c = toString("Hello World !");
string d = NULL;
sstrcpy(&d, c);
sputs(d);
sputs(c);
sstrcat(&d, &c);
sputs(d);
return EXIT_SUCCESS;
}