Skip to content
Extraits de code Groupes Projets
Valider f0f2b35f rédigé par Arnaud Lefebvre's avatar Arnaud Lefebvre
Parcourir les fichiers

Upload New File

parent 2680fb0b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #7346 réussi
#include "fonctions.h"
char* input;
char *output;
int NTHREADS = 1;
/*
Function used to put a node_t at the end of a list_t
@my_list : the list_t where we want to put a node_t at the end
@val : the value of the node_t that we will put in the list_t
@return : /
*/
void include_node_t(list_t *my_list, int val) {
node_t *new = malloc(sizeof(node_t));
if (!new) return;
new->value = val;
new->next = NULL;
if (my_list->length == 0) {
my_list->first = new;
my_list->last = new;
} else {
my_list->last->next = new;
new->next = my_list->first;
my_list->last = new;
}
my_list->length += 1;
}
/*
Function used to put a list_t at the end of a list_lst
@my_list_list : the list_lst where we want to put a node_lst at the end
@my_list : the list_t of the node_lst that we will put in the list_lst
@return : /
*/
void include_node_lst(list_lst *my_list_list, list_t *my_list) {
node_lst *new = malloc(sizeof(node_lst));
if (!new) return;
new->lst = my_list;
new->next = NULL;
if (my_list_list->length == 0) {
my_list_list->first = new;
my_list_list->last = new;
} else {
my_list_list->last->next = new;
new->next = my_list_list->first;
my_list_list->last = new;
}
my_list_list->length += 1;
}
/*
Function used to get the value of node_t at index of list_t
@my_list : the list_t that we want to get the value of the node_t
@index : the index of the node_t that we want the value
@return : the value of the node_t at index
*/
int value_at_index(list_t *my_list, int index) {
if (my_list->length == 0 || index > my_list->length) return -1;
node_t *temp = my_list->first;
for (int i = 0; i < index; i++) {
temp = temp->next;
}
return temp->value;
}
/*
Function used to remove the first node_t of a list_t
@my_list : the list_t that we want to remove the first node_t
@return : the value of the node_t removed
*/
int remove_node_t(list_t *my_list) {
if (my_list->length == 0) return -1;
int removed = my_list->first->value;
node_t *temp = my_list->first;
my_list->first = my_list->first->next;
my_list->length -= 1;
free(temp);
return removed;
}
/*
Function used to remove the first list_t of a list_lst
@my_list : the list_lst that we want to remove the first node_lst
@return : a pointer to the list_t of the node_lst removed
*/
list_t *remove_list_t(list_lst *my_list) {
if (my_list->length == 0) return NULL;
list_t *removed = my_list->first->lst;
node_lst *temp = my_list->first;
my_list->first = my_list->first->next;
my_list->length -= 1;
free(temp);
return removed;
}
/*
Function used to clear a list_t
@my_list : the list_t that we want to clear
@return : /
*/
void clear_list_t(list_t *my_list) {
if (!my_list || !my_list->first || !my_list->last) return;
node_t *temp = my_list->first->next;
while (!my_list->first) {
free(temp);
my_list->first = temp;
if (!temp) temp = temp->next;
}
my_list->first = NULL;
}
/*
Function used to clear a list_lst
@my_list : the list_lst that we want to clear
@return : /
*/
void clear_list_lst(list_lst *my_list) {
while (my_list->length != 0) {
list_t *temp = remove_list_t(my_list);
clear_list_t(temp);
free(temp);
}
}
/*
Function to know is a number divide an other
@chiffre : the number that we want to divide
@div : the dividor
@return : 1 if div divides chiffre, else 0
*/
int is_div(int chiffre, int div) {
return (chiffre % div == 0);
}
/*
Function to know is a number is prime
@chiffre : the number that we want to know if it is prime
@return : 1 if chiffre is prime, else 0
*/
int is_prime(int chiffre) {
if (chiffre <= 1) return 0;
if (chiffre <= 3) return 1;
if (is_div(chiffre,2) || is_div(chiffre,3)) return 0;
for (int i = 5; i*i <= chiffre; i+=6) {
if (is_div(chiffre, i) || is_div(chiffre, i+2)) return 0;
}
return 1;
}
/*
Function that create a list_t with all prime numbers that divide a given number
@chiffre : the number that we want to have the prime divisers
@return : a list_t with the number and all the divisers
*/
list_t *prime_list(int chiffre) {
list_t* prime = malloc(sizeof(list_t));
if (!prime) return NULL;
prime->first = NULL;
// prime->last = NULL;
prime->length = 0;
include_node_t(prime, chiffre);
int old_prime = 0;
int p = 2;
while (p*p <= chiffre) {
if (chiffre % p == 0) {
if (p != old_prime) {
include_node_t(prime, p);
chiffre = chiffre / p;
}
p++;
}
p++;
}
return prime;
}
/*
Function that initialize the first buffer
@NTHREADS : the number of threads given
@return : the buffer with all values initialized
*/
buf1 *init_buffer_1(int NTHREADS) {
buf1 *buffer_1 = (buf1 *) malloc(sizeof(buf1));
if (!buffer_1) return NULL;
if (sem_init(&(buffer_1->free), 0, NTHREADS + 2) == -1) return NULL;
if (sem_init(&(buffer_1->full), 0, 0) == -1) return NULL;
if (pthread_mutex_init(&(buffer_1->mutex), NULL) == -1) return NULL;
buffer_1->numbers = (list_t *) malloc(sizeof(list_t));
if (buffer_1->numbers == NULL) return NULL;
buffer_1->numbers->first = NULL;
buffer_1->numbers->last = NULL;
buffer_1->numbers->length = 0;
buffer_1->length = 0;
return buffer_1;
}
/*
Function that initialize the second buffer
@NTHREADS : the number of threads given
@return : the buffer with all values initialized
*/
buf2 *init_buffer_2(int NTHREADS) {
buf2 *buffer_2 = (buf2 *) malloc(sizeof(buf2));
if (!buffer_2) return NULL;
if (sem_init(&(buffer_2->free), 0, NTHREADS + 2) == -1) return NULL;
if (sem_init(&(buffer_2->full), 0, 0) == -1) return NULL;
if (pthread_mutex_init(&(buffer_2->mutex), NULL) == -1) return NULL;
buffer_2->prime_numbers = (list_lst *) malloc(sizeof(list_lst));
if (buffer_2->prime_numbers == NULL) return NULL;
buffer_2->prime_numbers->first = NULL;
buffer_2->prime_numbers->last = NULL;
buffer_2->prime_numbers->length = 0;
buffer_2->length = 0;
return buffer_2;
}
/*
Function that count the number of lines with a number in a file
@filename : the name of the file that we want to analyze
@return : the count of lines with a number
*/
int count_lines(char *filename) {
FILE *f = fopen(filename, "r");
if (f == NULL) return -1;
char line[100];
int lines = 0;
while (fscanf(f, "%s", line) == 1) {
lines++;
}
fclose(f);
return lines;
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter