algo fini
This commit is contained in:
104
exo3/tri_spec.c
Normal file
104
exo3/tri_spec.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LEN 5
|
||||
#define LIMIT 1000
|
||||
#define BUF_LIM 500
|
||||
|
||||
long racineCarree(long n){
|
||||
for(long i=0; i<(n/2); i++){
|
||||
if((i*i)==n){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
long* racineCarreeTab(long* tab, int count){
|
||||
long* rst = (long*) malloc(LIMIT*sizeof(long));
|
||||
for(long i=0; i<count; i++){
|
||||
rst[i] = racineCarree(tab[i]);
|
||||
if(rst[i] = -1){
|
||||
rst[i] = tab[i];
|
||||
}
|
||||
}
|
||||
return rst;
|
||||
}
|
||||
|
||||
|
||||
long tabSomme(long* tab, int count){
|
||||
long rst = 0;
|
||||
for(int i=0; i<count; i++){
|
||||
rst += tab[i];
|
||||
}
|
||||
return rst;
|
||||
}
|
||||
|
||||
void triSpecial(long* tab, int nb_racine_carre, int count){
|
||||
if((nb_racine_carre%2)==0){
|
||||
/*pair*/
|
||||
int somme = tabSomme(tab, count);
|
||||
for(int i=1; i<count; i+=2){
|
||||
tab[i] = somme*tab[i];
|
||||
}
|
||||
} else {
|
||||
/*impair*/
|
||||
int somme = tabSomme(racineCarreeTab(tab, count), count);
|
||||
for(int i=0; i<count; i+=2){
|
||||
tab[i] = racineCarree(tab[i]);
|
||||
tab[i+1] = somme*tab[i+1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Affichage(long* tab, int nb_racine_carre, int count){
|
||||
printf("Le tableau a %d racines carrées.\nIl prend la forme suivante:\n [", nb_racine_carre);
|
||||
for(int i=0; i<count; i++){
|
||||
printf("%ld, ", tab[i]);
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int main(int argc, char**argv){
|
||||
/*variables*/
|
||||
/*tableau prédéfini pour des questions de temps*/
|
||||
long pre_tableau[LEN] = {9, 25, 36, 17, 21};
|
||||
int nb_racine_carre, count;
|
||||
long* tableau = (long*) malloc(LIMIT*sizeof(long));
|
||||
|
||||
FILE* f = fopen(argv[1], "r");
|
||||
char * buffer = (char*) malloc(BUF_LIM*sizeof(char));
|
||||
|
||||
/*prog*/
|
||||
if(argc >= 2){
|
||||
/*faire avec fichier*/
|
||||
count=0;
|
||||
if(f==NULL){
|
||||
printf("ERROR:echec d'ouverture du fichier. EOP");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
while(fgets(buffer, BUF_LIM*sizeof(char), f)){
|
||||
tableau[count] = strtol(buffer, NULL, 10);
|
||||
count++;
|
||||
}
|
||||
fclose(f);
|
||||
} else {
|
||||
/*faire avec tableau prédéfini*/
|
||||
for(int i=0; i<LEN; i++){
|
||||
tableau[i] = pre_tableau[i];
|
||||
if(racineCarree(pre_tableau[i])!=-1){
|
||||
nb_racine_carre++;
|
||||
}
|
||||
}
|
||||
count = LEN;
|
||||
}
|
||||
|
||||
|
||||
triSpecial(tableau, nb_racine_carre, count);
|
||||
|
||||
free(tableau);
|
||||
free(buffer);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
Reference in New Issue
Block a user