push
This commit is contained in:
@@ -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