This commit is contained in:
2026-03-02 02:12:28 +01:00
parent 88b0c96605
commit 77cd47526c
7 changed files with 572 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
CC = gcc
CFLAGS = -Wextra -Wall -Werror
SRCS = main.c quotes.c village_manager.c villagers.c
TARGET = main
all:
$(CC) -o $(TARGET) $(SRCS) $(CFLAGS)
+143
View File
@@ -0,0 +1,143 @@
#include "village.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
char *get_quote(const struct villager *village, unsigned int population, char *name){
if (!village || population <= 0) return NULL;
unsigned int index = 0;
while(index < population){
if(!strcmp(village[index].name, name))
return village[index].favorite_quote;
index ++;
}
//if(index == population) return NULL;
return NULL;
}
int list_quotes(const char *filename, const struct villager *village, unsigned int population){
if(!filename || !village) return -1;
FILE *file = fopen(filename, "w");
if(!file) return -2;
unsigned int index = 0;
while(index < population){
fprintf(file, "%s\n", village[index].favorite_quote);
index ++;
}
fclose(file);
return 0;
}
char *second_longest_quote(const char *filename){
if(!filename) return NULL;
FILE *file = fopen(filename, "r");
if(!file)return NULL;
//utiliser getline
//utiliser strcmp() //comparaison entre deux str
//char *max_line = malloc(sizeof(char));
//char *second_line = malloc(sizeof(char));
char *max_line = NULL;
char *sc_line = NULL;
char *tmp_line = NULL;
size_t index = 0;
ssize_t len_max = 0;
ssize_t len_sec = 0;
ssize_t len_tmp = 0;
//char *c = fgets(char *restrict s, int n, FILE *restrict stream);
//lecture fichier
while((len_tmp = getline(&tmp_line, &index, file)) != -1){ // si la ligne est plus grande que max_line
if(len_tmp > len_max){
len_sec = len_max;
len_max = len_tmp;
free(sc_line);
sc_line = max_line;
max_line = strdup(tmp_line);
}
else if (len_tmp > len_sec){
len_sec = len_tmp;
free(sc_line);
sc_line = strdup(tmp_line);
}
}
free(tmp_line);
free(max_line);
fclose(file);
return sc_line;
}
/*
int main(void)
{
struct villager village[2] = {
{
.name = "Tom Nook",
.age = 99,
.job = SELLER,
.favorite_quote = "It's 42 bells",
},
{
.name = "Marie",
.age = 23,
.job = WORKER,
.favorite_quote = "It's so expensive!",
},
};
unsigned int population = 2;
char *quote = get_quote(village, population, "Tom Nook");
printf("- %s\n", quote);
quote = get_quote(village, population, "Marie");
printf("- %s\n", quote);
quote = get_quote(village, population, "TC");
printf("%p\n", quote);
int err = list_quotes("/tmp/list_quotes", village, population);
printf("%d\n", err);
err = list_quotes(NULL, village, population);
printf("%d\n", err);
printf("the last\n");
quote = second_longest_quote("/tmp/list_quotes");
printf("%s\n", quote);
free(quote);
quote = second_longest_quote("/");
printf("%p\n", quote);
free(quote);
}*/
+31
View File
@@ -0,0 +1,31 @@
#ifndef VILLAGE_H
#define VILLAGE_H
enum works{
WORKER,
SELLER,
MUSICIAN,
UNEMPLOYED,
};
struct villager{
char *name;
unsigned int age;
enum works job ;
char *favorite_quote;
};
char *get_quote(const struct villager *village, unsigned int population, char *name);
int list_quotes(const char *filename, const struct villager *village, unsigned int population);
char *second_longest_quote(const char *filename);
char *oldest_villager(const struct villager *village, unsigned int population);
float average_village_age(const struct villager *village, unsigned int population);
void sort_village(struct villager *village, unsigned int population);
int add_villager(struct villager **village, unsigned int *population, struct villager villager);
int remove_villager(struct villager **village, unsigned int *population, char *name);
void clear_village(struct villager **village, unsigned int *population);
#endif
+113
View File
@@ -0,0 +1,113 @@
#include "village.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int add_villager(struct villager **village, unsigned int *population, struct villager villager){
if(!village || population == 0) return -1;
struct villager *tmp = realloc(*village, (*population + 1) * sizeof(struct villager));
if(!tmp) return -2;
*village = tmp;
(*village)[*population] = villager;
(*population) ++;
return 0;
}
int remove_villager(struct villager **village, unsigned int *population, char *name){
if(!village || population == 0 || !name) return -1;
if(*population == 1){
if(!strcmp(village[0]->name, name)){
free(*village);
*village = NULL;
*population = 0;
}
return 0;
}
size_t index = 0;
int found = 0;
while(index < *population){
if(found != 0){
(*village)[index - 1] = (*village)[index];
}
if(!strcmp((*village)[index].name, name)) found ++;
index ++;
}
*population -= found;
if(*population > 0) *village = realloc(*village, (*population) * sizeof(struct villager));
else {
free(*village);
*village = NULL;
}
return 0;
}
void clear_village(struct villager **village, unsigned int *population){
if(!village || population == 0) return ;
/*for(size_t i = 0; i < *population; i ++){
free(village[i]);
village[i] = NULL;
}
//free(village);
*/
for (unsigned int i = 0; i < *population; i++){
village[i] = NULL;
}
*population = 0;
}
/*
int main(void)
{
struct villager *village = NULL;
unsigned int population = 0;
struct villager tom_nook = {
.name = "Tom Nook",
.age = 99,
.job = SELLER,
.favorite_quote = "It's 42 bells",
};
add_villager(&village, &population, tom_nook);
struct villager marie = {
.name = "Marie",
.age = 23,
.job = WORKER,
.favorite_quote = "It's so expensive!",
};
add_villager(&village, &population, marie);
clear_village(&village, &population);
for (unsigned int i = 0; i < population; i++)
{
printf("I'm %s (%d)\n", village[i].name, village[i].age);
}
printf("%p\n", village);
printf("%u\n", population);
}
*/
+105
View File
@@ -0,0 +1,105 @@
#include "village.h"
#include <ctype.h>
#include <stddef.h>
#include <strings.h>
#include <stdio.h>
char *oldest_villager(const struct villager *village, unsigned int population){
if(!village || population <= 0) return NULL;
unsigned int oldest = 0;
char *name = NULL;
size_t index = 0;
while(index < population){
if(village[index].age > oldest){
name = village[index].name;
oldest = village[index].age;}
index ++;
}
return name;
}
float average_village_age(const struct villager *village, unsigned int population){
if(!village || population <= 0) return -1;
float tot = 0;
size_t index = 0;
while(index < population){
tot += village[index].age;
index ++;
}
return tot / population;
}
void sort_village(struct villager *village, unsigned int population){
//ordre : WORKER - SELLER - MUSICIAN - UNEMPLOYED
if(!village || population <= 0) return ;
size_t index = 0;
int is_sort = 0;
while(!is_sort){
is_sort = 1;
index = 0;
while(index < population - 1){
if(village[index].job > village[index + 1].job){
struct villager tmp = village[index];
village[index] = village[index + 1];
village[index + 1] = tmp;
is_sort = 0;
}
index ++;
}
}
}
/*
int main(void)
{
struct villager village[2] = {
{
.name = "Tom Nook",
.age = 99,
.job = SELLER,
.favorite_quote = "It's 42 bells",
},
{
.name = "Marie",
.age = 23,
.job = WORKER,
.favorite_quote = "It's so expensive!",
},
};
unsigned int population = 2;
char *oldest = oldest_villager(village, population);
printf("Hi %s!\n", oldest);
oldest = oldest_villager(NULL, 0);
printf("%p\n", oldest);
float avg = average_village_age(village, population);
printf("Avg: %.2f\n", avg);
avg = average_village_age(village, 0);
printf("Avg: %.2f\n", avg);
sort_village(village, population);
for (unsigned int i = 0; i < population; i++)
{
printf("I'm %s (%d)\n", village[i].name, village[i].age);
}
}*/
+146
View File
@@ -0,0 +1,146 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "strange_list.h"
void print_strange_list(const struct data *list, unsigned int size){
if(!list) return ;
for (unsigned int i = 0; i < size; i ++){
if(list[i].type == INT_TYPE) printf("INT : %i\n", list[i].value.i);
else if (list[i].type == FLOAT_TYPE) printf("FLOAT : %f\n", list[i].value.f);
else printf("STRING : %s\n", list[i].value.s);
}
}
int append_strange_list(struct data **list, unsigned int *size, struct data element){
if(!list || size == 0) return -1;
struct data *tmp = realloc(*list, (*size + 1) * sizeof(struct data));
if(!tmp) return -2;
*list = tmp;
(*list)[*size ] = element;
(*size) ++;
return 0;
}
int remove_at_strange_list(struct data **list, unsigned int *size, int index){
if(!list || *size == 0 || index < 0 || index > (int)*size) return -1;
/*if(*size == 1 && index == 0){
free(*list);
*list = NULL;
*size = 0;
return 0;
//free(list);
}*/
for (unsigned int i = index; i < *size; i ++){
(*list)[i] = (*list)[i + 1];
}
(*size) --;
if(*size > 0) *list = realloc(*list, (*size) * sizeof(struct data));
else {
free(*list);
*list = NULL;
//free(list);
}
return 0;
}
int list_strange_list(const char *filename, const struct data *list, unsigned int size){
if(!filename || !list) return -1;
FILE *file = fopen(filename, "w");
if(!file) return -2;
for (unsigned int i = 0; i < size; i ++){
if(list[i].type == INT_TYPE) fprintf(file, "INT : %i\n", list[i].value.i);
else if (list[i].type == FLOAT_TYPE) fprintf(file, "FLOAT : %f\n", list[i].value.f);
else fprintf(file, "STRING : %s\n", list[i].value.s);
}
return 0;
}
/*
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
struct data *list = NULL;
unsigned int size = 0;
struct data elt1 = { .type = INT_TYPE, .value = { .i = 42 } };
struct data elt2 = { .type = STRING_TYPE, .value = { .s = "QUOI" } };
//int err = append_strange_list(&list, &size, elt1);
//printf("%d\n", err);
//append_strange_list(&list, &size, elt2);
//printf("%d\n", err);
//append_strange_list(&list, NULL, elt1);
//printf("%d\n", err);
int err = remove_at_strange_list(&list, &size, 1);
printf("%d\n", err);
err = remove_at_strange_list(&list, &size, 42);
printf("%d\n", err);
print_strange_list(list, size);
free(list);
struct data *list = NULL;
unsigned int size = 0;
struct data elt1 = { .type = INT_TYPE, .value = { .i = 42 } };
struct data elt2 = { .type = STRING_TYPE, .value = { .s = "FEUR" } };
append_strange_list(&list, &size, elt1);
append_strange_list(&list, &size, elt2);
append_strange_list(&list, &size, elt2);
int err = remove_at_strange_list(&list, &size, 1);
printf("%d\n", err);
err = remove_at_strange_list(&list, &size, 42);
printf("%d\n", err);
print_strange_list(list, size);
free(list);
struct data *list = NULL;
unsigned int size = 0;
struct data elt1 = { .type = INT_TYPE, .value = { .i = 42 } };
struct data elt2 = { .type = STRING_TYPE, .value = { .s = "welcome2u" } };
append_strange_list(&list, &size, elt1);
append_strange_list(&list, &size, elt2);
int err = list_strange_list("/tmp/list", list, size);
printf("%d\n", err);
err = list_strange_list("/", list, size);
printf("%d\n", err);
free(list);
}*/
+28
View File
@@ -0,0 +1,28 @@
#ifndef STRANGE_LIST_H
#define STRANGE_LIST_H
#include <stdio.h>
enum data_type{
FLOAT_TYPE,
INT_TYPE,
STRING_TYPE,
};
union value {
float f;
int i;
char *s;
};
struct data{
enum data_type type;
union value value;
};
void print_strange_list(const struct data *list, unsigned int size);
int remove_at_strange_list(struct data **list, unsigned int *size, int index);
int list_strange_list(const char *filename, const struct data *list, unsigned int size);
int append_strange_list(struct data **list, unsigned int *size, struct data element);
#endif