push
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
}*/
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}*/
|
||||
|
||||
Reference in New Issue
Block a user