diff --git a/Makefile b/Makefile index 4d99fd57e00b8d81f41c8418d08e692d4a3bfa8e..50868ee053224e3f5a8f909f56e98beca4c09df1 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,15 @@ all : run run : run.o test.o - gcc -o run run.o test.o -I${HOME}/local/include -lcunit -L${HOME}/local/lib + gcc -g -o run run.o test.o -I${HOME}/local/include -lcunit -L${HOME}/local/lib run.o : run.c - gcc -o run.o -c run.c -W -Wall + gcc -g -o run.o -c run.c -W -Wall test.o : test.c run.h - gcc -o test.o -c test.c -I${HOME}/local/include -W -Wall -lcunit + gcc -g -o test.o -c test.c -I${HOME}/local/include -W -Wall -lcunit clean : diff --git a/run.c b/run.c index 6a73e604287ca0232ad156d36173c20c483a7a0b..8cad8ac5821b3a64c28884e86766494d560c6c6f 100644 --- a/run.c +++ b/run.c @@ -5,6 +5,11 @@ #include <unistd.h> #include <sys/stat.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + int is_div(int number, int i) { // Vérifie si i est un diviseur de number. return (number % i == 0) ; // revoi 0 si le nombre n'est pas divisible par i et 1 si il est divisible } @@ -28,14 +33,15 @@ typedef struct queue{ int size; }queue_t; -int enqueue(queue_t* q, int val){ +queue_t* enqueue(queue_t* q, int val){ struct node new_node; new_node.value = val; + new_node.next = malloc(sizeof(node_t)); struct node *ptr; ptr = malloc(sizeof(node_t)); - if (ptr == NULL){return -1;} + if (ptr == NULL){return NULL;} *ptr = new_node; if (q->size == 0) @@ -48,8 +54,8 @@ int enqueue(queue_t* q, int val){ *q->tail->next = *ptr; } q->size++; - return 0; - + return q; + } @@ -65,13 +71,13 @@ queue_t* prime_divs(int number){ for (int i = 2; i < number; i++){ if (is_prime(i) == 1 && is_div(number, i)){ - int err = enqueue(ptr,i); - if (err == -1){return NULL;} + ptr = enqueue(ptr,i); + if (ptr == NULL){return NULL;} - } + } } - int err1 = enqueue(ptr,-1); - if (err1 == -1){return NULL;} + //ptr = enqueue(ptr,-1); + //if (ptr == NULL){return NULL;} return ptr; @@ -83,7 +89,7 @@ int run (char *input,char *output){ FILE *file1 = NULL; FILE *file2 = NULL; - //char chaine[50]; + char chaine[50]; file1 = fopen(input,"r"); if (file1 == NULL){return -1;} @@ -94,17 +100,29 @@ int run (char *input,char *output){ return -1; } - /* while (fgets(chaine,50,file1) != NULL){ + while (fgets(chaine,50,file1) != NULL){ + + struct queue *pr_divs; + pr_divs = malloc(sizeof(queue_t)); + if (pr_divs == NULL){return -1;} + pr_divs = prime_divs(atoi(chaine)); - queue_t *pr_divs = prime_divs(atoi(chaine)); + char *ptr; + ptr = malloc(50); + ptr = strchr(chaine,'\n'); + *ptr = '\0'; + struct node *current; + current = malloc(sizeof(node_t)); + *current = *pr_divs->tail; fprintf(file2,"%s ",chaine); - for (int i = 0; pr_divs[i] != -1; i++) + for (int i = 0; i < pr_divs->size; i++) { - fprintf(file2,"%d ",pr_divs[i]); + fprintf(file2,"%d ",current->value); + current = current->next; } fputc('\n',file2); - - } */ + + } fclose(file1); fclose(file2); @@ -113,21 +131,9 @@ int run (char *input,char *output){ } -/* int main() { - // test pour is_div(int number, int i) - int number = 10 ; - int number1 = 11 ; - int i = 3 ; - printf("%d\n",is_div(number,i)) ; // 1 - printf("%d\n",is_div(number1,i)) ; // 0 - // test pour is_prime(int number) - int number2 = 1498498411 ; - int number3 = 5 ; - int number4 = 14545144 ; - printf("%d\n",is_prime(number2)) ; // 0 - printf("%d\n",is_prime(number3)) ; // 1 - printf("%d\n",is_prime(number4)) ; // 0 - //prime divis - afficherListe(prime_divs(number)); - return 0; -} */ \ No newline at end of file +/* int main(){ + int err = run("input.txt","actual_output.txt"); + + return err; +} */ + diff --git a/test.c b/test.c index 2912045aa3cacdb3e995db8844e7ef9ef965940c..b43e52e55b609bf1195f71240765427cd0245ed5 100644 --- a/test.c +++ b/test.c @@ -34,15 +34,15 @@ void file_test(void){ } file2 = fopen("actual_output.txt","r"); - if (file2 == NULL){ - CU_FAIL("actual_output opening fail"); - return; - } + if (file2 == NULL){ + CU_FAIL("actual_output opening fail"); + return; + } - char chaine1[500]; - char chaine2[500]; + char chaine1[50]; + char chaine2[50]; - while (fgets(chaine1,500,file1) != NULL && fgets(chaine2,500,file2) != NULL) + while (fgets(chaine1,50,file1) != NULL && fgets(chaine2,50,file2) != NULL) { CU_ASSERT_STRING_EQUAL(chaine1,chaine2); } @@ -81,8 +81,8 @@ int main(){ if (NULL == CU_add_test(pSuite,"test_is_div",test_is_div) || NULL == CU_add_test(pSuite, "test_is_prime",test_is_prime) || - NULL == CU_add_test(pSuite, "test_is_not_prime",test_is_not_prime)) - //|| NULL == CU_add_test(pSuite,"file_test",file_test)) + NULL == CU_add_test(pSuite, "test_is_not_prime",test_is_not_prime) + || NULL == CU_add_test(pSuite,"file_test",file_test)) { CU_cleanup_registry(); return CU_get_error();