TP 08 Récursivité

This commit is contained in:
HORVILLE 2022-01-10 17:28:28 +01:00
parent 02a55cc3e3
commit 2528801966
6 changed files with 207 additions and 0 deletions

13
APL1.2/TP08/fibonacci.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>
int fib(int n) {
if (n > 1) return n + fib(n-1);
else if (n == 1) return 1;
else return 0;
}
int main(void) {
fibp(15);
return EXIT_SUCCESS;
}

71
APL1.2/TP08/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/TP08/graph_sup.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef _GRAPH_SUP_H
#define _GRAPH_SUP_H
#define WIDTH 1600
#define HEIGHT 300
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

57
APL1.2/TP08/hanoi.c Normal file
View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include "graph_sup.h"
int game_size;
unsigned char* makeSite(unsigned char size) {
unsigned char* site = calloc(size, sizeof(unsigned char));
return site;
}
void populateSite(unsigned char* site, int size) {
for (int i = 0; i < size; i++) {
site[i] = i+1;
}
}
void showSite(unsigned char* site, int pos) {
for (int i = 0; i < game_size; i++) {
}
}
int main(void) {
printf("Number of plates : ");
scanf("%d", &game_size);
unsigned char* site1 = makeSite(game_size);
unsigned char* site2 = makeSite(game_size);
unsigned char* site3 = makeSite(game_size);
int block_size = ((HEIGHT - HEIGHT/10) / game_size);
populateSite(site1, game_size);
InitialiserGraphique();
CreerFenetre(100, 100, WIDTH, HEIGHT);
SetColor(100, 100, 200);
RemplirRectangle(0, 0, WIDTH, HEIGHT);
SetColor(0, 170, 0);
RemplirRectangle(0, HEIGHT - block_size, WIDTH, block_size);
SetColor(255, 255, 255);
for (int i = 0; i < game_size; i++) {
for (int j = 0; j < 3; j++) {
}
}
while (1) {
if (DrawNextFrame()) {
}
}
FermerGraphique();
}

17
APL1.2/TP08/phases.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
void exemple(unsigned n) {
if (n != 0) {
putchar('>');
exemple(n-1);
putchar('<');
} else {
putchar('O');
}
}
int main(void) {
exemple(5);
return EXIT_SUCCESS;
}

17
APL1.2/TP08/tableau.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
void printTable(double* t, int n) {
if (n > 0) {
printf("%.5lf", *t);
if (n > 1) printf(", ");
printTable(t+1, n-1);
} else puts("");
}
int main(void) {
double t[5] = {1.72, 5.28, 9.025, 36.14, 3.14159};
printTable(t, 5);
return EXIT_SUCCESS;
}