effg
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
CC=gcc
|
||||
SRC = fish.c insects.c vector.c main.c
|
||||
SRC = fish.c insect.c vector.c main.c
|
||||
CFLAGS= -Wall -Wextra -Werror
|
||||
LDFLAGS= -fsanitize=address -g
|
||||
# Computes the basic main for tests
|
||||
@@ -11,4 +11,4 @@ debug: ${SRC}
|
||||
gcc $(CFLAGS) $^ -o debug $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
$(RM) main debug
|
||||
$(RM) main debug
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef ANIMALS_H
|
||||
#define ANIMALS_H
|
||||
#include <stdio.h>
|
||||
enum animal_type
|
||||
{
|
||||
INSECT,
|
||||
FISH,
|
||||
};
|
||||
|
||||
struct fish
|
||||
{
|
||||
char* species;
|
||||
int swimming_speed;
|
||||
size_t size;
|
||||
int lives_in_fresh_water;
|
||||
};
|
||||
|
||||
struct insect
|
||||
{
|
||||
char* species;
|
||||
size_t size;
|
||||
int can_fly;
|
||||
int can_swim;
|
||||
};
|
||||
|
||||
union animals
|
||||
{
|
||||
struct insect* insect;
|
||||
struct fish* fish;
|
||||
};
|
||||
|
||||
struct animal
|
||||
{
|
||||
enum animal_type type;
|
||||
union animals animal;
|
||||
char* color;
|
||||
};
|
||||
|
||||
struct fish* fish_init(const char* species, int speed, size_t size, int fresh_water);
|
||||
struct animal* animal_from_fish(const char* color, struct fish* fish);
|
||||
void free_fish(struct fish* fish);
|
||||
struct insect *insect_init(const char *species, size_t size, int can_fly, int can_swim);
|
||||
struct animal* animal_from_insect(const char* color, struct insect* insect);
|
||||
void free_insect(struct insect* insect);
|
||||
void free_animal(struct animal* animal);
|
||||
|
||||
#endif
|
||||
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,68 @@
|
||||
#include "animals.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
struct fish *fish_init(const char *species, int speed, size_t size, int fresh_water){
|
||||
//je sens que la mouli vas tester ça grr
|
||||
if(species == NULL) return NULL;
|
||||
|
||||
struct fish *fish = malloc(sizeof(struct fish));
|
||||
if (fish == NULL) return NULL;
|
||||
|
||||
int index = 0;
|
||||
while (*(species + index) != '\0') index ++;
|
||||
|
||||
fish->species = malloc((index + 1 ) * sizeof(char *));
|
||||
if (fish->species == NULL){
|
||||
free(fish);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(fish->species, species);
|
||||
fish->lives_in_fresh_water = fresh_water;
|
||||
fish->size = size;
|
||||
fish->swimming_speed = speed;
|
||||
|
||||
return fish;
|
||||
}
|
||||
|
||||
struct animal *animal_from_fish(const char *color, struct fish *fish){
|
||||
|
||||
if (color == NULL || fish == NULL) return NULL;
|
||||
|
||||
struct animal *animal = malloc(sizeof(struct animal));
|
||||
if(animal == NULL)return NULL;
|
||||
|
||||
int index = 0;
|
||||
while(*(color + index) != '\0') index ++;
|
||||
|
||||
animal->color = malloc((index + 1) * sizeof(char*));
|
||||
if(animal->color == NULL) {
|
||||
//free(animal->color);
|
||||
free(animal);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(animal->color, color);
|
||||
animal->type = 1;
|
||||
/*
|
||||
union animals *animals = malloc(sizeof(union animals));
|
||||
if(animals == NULL){
|
||||
free(animal->color);
|
||||
free(animal);
|
||||
return NULL;
|
||||
}*/
|
||||
//animals->fish = fish;
|
||||
animal->animal.fish = fish;
|
||||
return animal;
|
||||
}
|
||||
|
||||
void free_fish(struct fish *fish){
|
||||
|
||||
if(fish == NULL) return;
|
||||
|
||||
if(fish->species != NULL) free(fish->species);
|
||||
free(fish);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "animals.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
struct insect *insect_init(const char *species, size_t size, int can_fly, int can_swim){
|
||||
|
||||
if(species == NULL) return NULL;
|
||||
|
||||
struct insect *insect = malloc(sizeof(struct insect));
|
||||
if (insect == NULL) return NULL;
|
||||
|
||||
int index = 0;
|
||||
while (*(species + index) != '\0') index ++;
|
||||
|
||||
insect->species = malloc((index + 1)*sizeof(char*));
|
||||
if (insect->species == NULL) {
|
||||
free(insect);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(insect->species, species);
|
||||
insect->size = size;
|
||||
insect->can_fly = can_fly;
|
||||
insect->can_swim = can_swim;
|
||||
|
||||
return insect;
|
||||
}
|
||||
|
||||
struct animal *animal_from_insect(const char *color, struct insect *insect){
|
||||
|
||||
if (color == NULL || insect == NULL) return NULL;
|
||||
|
||||
struct animal *animal = malloc(sizeof(struct animal));
|
||||
if(animal == NULL) return NULL;
|
||||
|
||||
int index = 0;
|
||||
while(*(color + index) != '\0') index ++;
|
||||
|
||||
animal->color = malloc((index + 1) * sizeof(char*));
|
||||
if(animal->color == NULL){
|
||||
free(animal);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(animal->color, color);
|
||||
animal->type = 0;
|
||||
animal->animal.insect = insect;
|
||||
return animal;
|
||||
}
|
||||
|
||||
void free_insect(struct insect *insect){
|
||||
|
||||
if (insect == NULL) return;
|
||||
|
||||
//Normalement il ne devrait pas etre NULL
|
||||
//if(insect->species != NULL) free(insect->species);
|
||||
free(insect->species);
|
||||
free(insect);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,115 @@
|
||||
|
||||
#include "animals.h"
|
||||
#include "vector.h"
|
||||
#include <stdlib.h>
|
||||
int main(){
|
||||
// ================= FISH ===============
|
||||
/*
|
||||
struct fish *f = fish_init("Salmon", 10, 5, 1);
|
||||
printf("Fish species: %s\n", f->species);
|
||||
printf("Swimming speed: %d\n", f->swimming_speed);
|
||||
printf("Size: %zu\n", f->size);
|
||||
printf("Lives in fresh water: %d\n", f->lives_in_fresh_water);
|
||||
free_fish(f);
|
||||
*/
|
||||
/*
|
||||
struct fish *f = fish_init("Trout", 8, 4, 1);
|
||||
struct animal *a = animal_from_fish("Blue", f);
|
||||
printf("Animal type: %d\n", a->type);
|
||||
printf("Animal color: %s\n", a->color);
|
||||
printf("Fish species from animal: %s\n", a->animal.fish->species);
|
||||
free_fish(f);
|
||||
free(a->color);
|
||||
free(a);
|
||||
*/
|
||||
/*
|
||||
struct fish *f = fish_init("Carp", 6, 3, 0);
|
||||
free_fish(f);
|
||||
*/
|
||||
|
||||
int main()
|
||||
{}
|
||||
// ================ INSECT ==============
|
||||
/* struct insect *i = insect_init("Dragonfly", 2, 1, 0);
|
||||
printf("Insect species: %s\n", i->species);
|
||||
printf("Size: %zu\n", i->size);
|
||||
printf("Can fly: %d\n", i->can_fly);
|
||||
printf("Can swim: %d\n", i->can_swim);
|
||||
free(i->species);
|
||||
free(i);
|
||||
*/
|
||||
/*
|
||||
struct insect *i = insect_init("Butterfly", 1, 1, 0);
|
||||
struct animal *a = animal_from_insect("Yellow", i);
|
||||
printf("Animal type: %d\n", a->type);
|
||||
printf("Animal color: %s\n", a->color);
|
||||
printf("Insect species from animal: %s\n", a->animal.insect->species);
|
||||
free(i->species);
|
||||
free(i);
|
||||
free(a->color);
|
||||
free(a);
|
||||
*/
|
||||
/* struct insect *i = insect_init("Beetle", 3, 0, 1);
|
||||
free_insect(i);
|
||||
*/
|
||||
// ============ VECTOR ==============
|
||||
/*
|
||||
struct vector *v = vector_init(5);
|
||||
printf("Initial size: %zu\n", v->size);
|
||||
printf("Initial capacity: %zu\n", v->capacity);
|
||||
free(v->animal);
|
||||
free(v);
|
||||
*/
|
||||
/*
|
||||
struct vector *v = vector_init(2);
|
||||
v = vector_resize(v, 4);
|
||||
printf("Resized capacity: %zu\n", v->capacity);
|
||||
free(v->animal);
|
||||
free(v);
|
||||
*/
|
||||
/*
|
||||
struct vector *v = vector_init(2);
|
||||
|
||||
struct fish *f1 = fish_init("Salmon", 10, 5, 1);
|
||||
struct animal *a1 = animal_from_fish("Blue", f1);
|
||||
v = vector_append(v, a1);
|
||||
|
||||
struct fish *f2 = fish_init("Trout", 8, 4, 1);
|
||||
struct animal *a2 = animal_from_fish("Green", f2);
|
||||
v = vector_append(v, a2);
|
||||
|
||||
struct fish *f3 = fish_init("Carp", 6, 3, 0);
|
||||
struct animal *a3 = animal_from_fish("Red", f3);
|
||||
v = vector_append(v, a3);
|
||||
|
||||
printf("Vector size after appends: %zu\n", v->size);
|
||||
printf("Vector capacity after appends: %zu\n", v->capacity);
|
||||
free(f1->species);
|
||||
free(f1);
|
||||
free(a1->color);
|
||||
free(a1);
|
||||
|
||||
free(f2->species);
|
||||
free(f2);
|
||||
free(a2->color);
|
||||
free(a2);
|
||||
|
||||
free(f3->species);
|
||||
free(f3);
|
||||
free(a3->color);
|
||||
free(a3);
|
||||
|
||||
free(v->animal);
|
||||
free(v);
|
||||
*/
|
||||
/* struct fish *f = fish_init("Salmon", 10, 5, 1);
|
||||
struct animal *a = animal_from_fish("Blue", f);
|
||||
free_animal(a);
|
||||
*/
|
||||
struct vector *v = vector_init(5);
|
||||
|
||||
struct fish *f = fish_init("Trout", 8, 4, 1);
|
||||
struct animal *a = animal_from_fish("Green", f);
|
||||
v = vector_append(v, a);
|
||||
|
||||
vector_destroy(v);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include "vector.h"
|
||||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#include "animals.h"
|
||||
struct vector *vector_init(size_t n){
|
||||
|
||||
//if (n < 0) return NULL;
|
||||
struct vector *vector = malloc(sizeof(struct vector));
|
||||
|
||||
vector->animal = malloc((n) * sizeof(struct animal));
|
||||
if(vector->animal == NULL) return NULL;
|
||||
|
||||
vector->capacity = n;
|
||||
vector->size = 0;
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
struct vector *vector_resize(struct vector *v, size_t new_capacity){
|
||||
|
||||
if(v == NULL) return NULL;
|
||||
|
||||
v->animal = realloc(v->animal, (new_capacity + 1) * sizeof(struct animal));
|
||||
v->capacity = new_capacity;
|
||||
|
||||
return v;
|
||||
|
||||
}
|
||||
|
||||
struct vector *vector_append(struct vector *v, struct animal *animal){
|
||||
|
||||
if(v == NULL || animal == NULL) return NULL;
|
||||
|
||||
if (v->capacity <= v->size) v = vector_resize(v, v->capacity * 2);
|
||||
// TODO A voir si le realloc ajoute de l'espace a gauche ou a droite
|
||||
// car sinon y a ecrasement de data
|
||||
*(v->animal + (v->capacity - v->size )) = animal;
|
||||
v->size ++;
|
||||
return v;
|
||||
}
|
||||
|
||||
void free_animal(struct animal *animal){
|
||||
|
||||
if (animal == NULL) return;
|
||||
|
||||
//if(animal->color != NULL) free(animal->color);
|
||||
printf("%s\n",animal->animal.fish->species);
|
||||
printf("%s\n", animal->animal.insect->species);
|
||||
if(animal->type == INSECT) free_insect(animal->animal.insect);
|
||||
if(animal->type == FISH) free_fish(animal->animal.fish);
|
||||
free(animal->color);
|
||||
free(animal);
|
||||
}
|
||||
|
||||
void vector_destroy(struct vector *v){
|
||||
|
||||
if(v == NULL) return;
|
||||
if(v->animal != NULL){
|
||||
|
||||
//size_t index = 1;
|
||||
/*while (index < (v->capacity - v->size)){
|
||||
free_animal(*(v->animal + (v->size - index)));
|
||||
}*/
|
||||
for(size_t i = 0; i < v->size; i++) free_animal(v->animal[i]);
|
||||
}
|
||||
|
||||
free(v->animal);
|
||||
free(v);
|
||||
//free_animal(v->animal);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
#include <stdio.h>
|
||||
|
||||
struct vector
|
||||
{
|
||||
struct animal **animal;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
struct vector *vector_init(size_t n);
|
||||
struct vector *vector_resize(struct vector *v, size_t new_capacity);
|
||||
struct vector *vector_append(struct vector *v, struct animal *animal);
|
||||
void vector_destroy(struct vector *v);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user