1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
| #include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX 1000000
typedef char string[1024];//def du type de variable string
typedef struct reference {
int AGED;
long long int SURF;
string PROF;
}ref;
ref reftab[MAX];
long int limite=0;
void clear_stdin(void)
{
int c;
do
{
c=getchar();
} while (c!='\n'&& c!=EOF);
}
void triParInsertion() //Permet de trier les données par un tri par insertion
{
//Déclaration des variables
int cpt; //compteur
int unit; //stockage temporaire
int dureeTriInsert; //duree du tri
long int unit2; //stockage temporaire
int cpt2; //compteur 2
string unitPROF;//stockage temporaire pour PROF
int PROFCOMP;
clock_t debutTriInsert; //2 variables pour le temps de tri
clock_t finTriInsert;
char TriInsertBuffer[50];
char TriInsertBufferProf[50];
FILE* fdonneesInsertion = fopen("donneeInsertion.csv", "at"); //ouverture/création du fictier csv
for (cpt=0;cpt<50;cpt++) //On met le buffer vide
{
TriInsertBuffer[cpt]='\0';
}
printf("Tri par insertion......Tri par insertion \n");
debutTriInsert=clock();
for (cpt=0;cpt<limite;cpt++)//début du tri
{
//tri AGED
unit=reftab[cpt].AGED;
cpt2=cpt;
while ((cpt2>0)&&(reftab[cpt2-1].AGED>unit))
{
reftab[cpt2].AGED=reftab[cpt2-1].AGED;
cpt2=cpt2-1;
}
reftab[cpt2].AGED=unit;
//tri PROF
strcpy(TriInsertBufferProf,reftab[cpt].PROF);
cpt2=cpt;
PROFCOMP=strncmp(reftab[cpt2-1].PROF,TriInsertBufferProf,4);
while( (cpt2 > 0) && (PROFCOMP > 0))
{
strcpy(reftab[cpt2].PROF,reftab[cpt2-1].PROF);
cpt2=cpt2-1;
}
strcpy(reftab[cpt2].PROF,TriInsertBufferProf);
//tri SURF
unit2=reftab[cpt].AGED;
cpt2=cpt;
while ((cpt2>0)&&(reftab[cpt2-1].AGED>unit))
{
reftab[cpt2].AGED=reftab[cpt2-1].AGED;
cpt2=cpt2-1;
}
reftab[cpt2].AGED=unit2;
}
finTriInsert=clock();
//affichage après tri
for(cpt=0;cpt<limite;cpt++)
{
printf("TriInsertionAged=%d TriInsertionProf=%s TriInsertionSurf=%lld \n",reftab[cpt].AGED,reftab[cpt].PROF,reftab[cpt].SURF); //affichage dans le terminal
}
dureeTriInsert=(finTriInsert-debutTriInsert)/1000;//calcul du temps
//mise des données dans le fichier csv
sprintf(TriInsertBuffer,"%d,%d",cpt,dureeTriInsert);
fprintf(fdonneesInsertion, "%s\n",TriInsertBuffer);
}
void TriSelection()//permet de triée un tableau en plaçant les éléments par permutations à la fin du tableau
{
//ouverture des fichiers
//Déclaration de variables
int cpt; //compteur
int cpt2; //compteur 2
int unit; //stockage temporaire
int top; //stockage de la case du tableau top
long int unit2; //stockage temporaire
int TriSelectionDuree; //stockage de la durée
string unitPROF;//stockage temporaire pour PROF
clock_t debutTriSelection;
clock_t finTriSelection;
char BuffTriSelection[50];
FILE* fdonneesSelection = fopen("donneeSelection.csv", "at");
//Début
//Tri SELECTION AGED
cpt=MAX; //mise au max du compteur pour le décompagte
while (cpt>0)
{
top=0;
for (cpt2=0;cpt2<=cpt;cpt2++)
{
if (reftab[cpt2].AGED>reftab[top].AGED)
{
top=cpt2;
}
}
reftab[top].AGED;
reftab[top].AGED=reftab[cpt].AGED;
reftab[cpt].AGED=unit;
cpt=cpt-1;
}
//Tri Selection SURF
cpt=MAX;
while (cpt>0)
{
top=0;
for (cpt2=0;cpt2<=cpt;cpt2++)
{
if (reftab[cpt2].SURF>reftab[top].SURF)
{
top=cpt2;
}
}
unit2=reftab[top].SURF;
reftab[top].SURF=reftab[cpt].SURF;
reftab[cpt].SURF=unit2;
cpt=cpt-1;
}
//Tri Selection PROF
cpt=MAX;
while (cpt>0)
{
top=0;
for (cpt2=0;cpt2<=cpt;cpt2++)
{
if (reftab[cpt2].PROF>reftab[top].PROF)
{
top=cpt2;
}
}
strcpy(unitPROF,reftab[top].PROF); //changement de égal vers strcpy car chaîne de caractères
strcpy(reftab[top].PROF,reftab[cpt].PROF);
strcpy(reftab[cpt].PROF,unitPROF);
cpt=cpt-1;
}
for(cpt=0;cpt<limite;cpt++)
{
printf("TriInsertionAged=%d TriInsertionProf=%s TriInsertionSurf=%lld \n",reftab[cpt].AGED,reftab[cpt].PROF,reftab[cpt].SURF); //affichage dans le terminal
}
TriSelectionDuree=(finTriSelection-debutTriSelection)/1000;//calcul du temps
//Mise dans le fichier csv
sprintf(BuffTriSelection,"%d,%d",cpt,TriSelectionDuree);
fprintf(fdonneesSelection, "%s\n",BuffTriSelection);
}
int main()
{
//Déclaration de variables
int cpt;
int cpt_2;
int cpt_struct;
char element[1024];
char buffer[50];
int chiffre=-1;
int x;
int i;
int rep;
for (cpt=0; cpt < 1024; cpt++)
{
element[cpt] = '\0';
}
//ouverture des fichiers
FILE* f = fopen("FD_INDREGZD_2018.csv","r");
FILE* fdonnees = fopen("donnee.csv", "at");
if ((f==NULL))
{
//Indique si il y a une erreur d'ouverture de fichier
printf("Erreur d'ouverture de fichier \n");
}
else
{
while (chiffre<0)
{
printf("Rentrer un multiple supérieur ou égal à 0 pour le nombre de ligne à analyser \n");
scanf("%d",&chiffre);
}
clear_stdin();
limite=chiffre*10000;
clock_t debut=clock(); //nombre pas processeur seconde
cpt_struct=0;//permet de réinitialiser
x=0;
while ((fgets(element, 1024 ,f)!=NULL) && (x<limite ))
{
for (i=0; i<strlen(element);i++)
{
if (element[i]==';')
{
element[i]=' ';
}
}
//récupération des bonnes colonnes
sscanf(element, "%*d%*d%*d%*d%*d%d", &reftab[cpt_struct].AGED);
sscanf(element, "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s",reftab[cpt_struct].PROF);
sscanf(element, "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%lld", &reftab[cpt_struct].SURF);
printf("AGED =%d, PROF=%s, SURF=%lld \n",reftab[cpt_struct].AGED,reftab[cpt_struct].PROF,reftab[cpt_struct].SURF);
cpt_struct++;
x++;
}
clock_t fin=clock();
//calcul du temps en secondes
int duree=(fin-debut)/1000;
printf("La durée d'éxecution est de %d millisecondes pour %d lignes",duree,x);
//intégration au fichier csv
sprintf(buffer,"%d,%d",cpt_struct,duree);
fprintf(fdonnees, "%s\n",buffer);
printf("Quel tri voulez-vous faire ? taper 1 pour sélection taper 2 pour insertion \n");
scanf("%d",&rep);
clear_stdin();
while ((rep > 2)|| (rep<1))
{
printf("Quel tri voulez-vous faire ? taper 1 pour sélection taper 2 pour insertion \n");
scanf("%d",&rep);
clear_stdin();
}
if (rep==1)
{
TriSelection();
}
else if (rep==2)
{
triParInsertion();
}
else
{
printf("Impossible \n");
}
return 0;
}
fclose(f);
fclose(fdonnees); |
Partager