ajout du tp 10 d'Algorithme

master
antoine.perederii 3 years ago
parent 44ac16c1a1
commit 56935ca509

Binary file not shown.

Binary file not shown.

@ -0,0 +1,5 @@
4
Martin algo1 12.5
Amélie maths 15.5
Gilbert based 17.0
Anne organ 10

@ -0,0 +1,7 @@
#include "tp10.h"
int main(void)
{
global();
return 0;
}

@ -0,0 +1,99 @@
#include "tp10.h"
Resultat lireResultat(FILE *fe)
{
Resultat c;
fscanf(fe, "%s\t%s\t%f", c.nom, c.cdmat, &c.moyenne);
return c;
}
void afficheResultat(Resultat c)
{
printf("%s\t%s\t%f\n", c.nom, c.cdmat, c.moyenne);
}
Resultat *chargeTresultat(char *nomFic, int *nbres)
{
Resultat *tab;
FILE *flot;
int i;
flot = fopen(nomFic, "r");
if(flot == NULL)
{
printf("Pb d'ouverture du fichier resultat.don\n");
exit(1);
}
fscanf(flot, "%d", nbres);
tab = (Resultat *)malloc(*nbres * sizeof(Resultat));
if(tab == NULL)
{
printf("Pb, il n'y a rien dans le fichier resultat.don\n");
exit(1);
}
for(i=0; i < *nbres; i++)
tab[i]=lireResultat(flot);
fclose(flot);
return tab;
}
void afficheTresultat(Resultat *tab, int nbres)
{
int i;
printf("\n");
for(i = 0; i < nbres; i++)
afficheResultat(tab[i]);
}
void sauveTresultat(Resultat *tRes, int nbres)
{
FILE *flot;
int i;
flot = fopen("resultat.bin", "w");
if(flot == NULL)
{
printf("Pb d'ouverture du fichier resultat.bin\n");
exit(1);
}
fprintf(flot, "%d\n", nbres);
fwrite(tRes, sizeof(Resultat), nbres, flot);
fclose(flot);
}
Resultat *restaureTresultat(char *nomfichier, int *nbres)
{
Resultat *tab;
FILE *flot;
flot = fopen(nomfichier, "r");
if(flot == NULL)
{
printf("Pb d'ouverture du fichier resultat.don\n");
exit(1);
}
fscanf(flot, "%d\n", nbres);
tab = (Resultat *)malloc(*nbres * sizeof(Resultat));
if(tab == NULL)
{
printf("Pb, il n'y a rien dans le fichier resultat.bin\n");
exit(1);
}
fread(tab, sizeof(Resultat), *nbres, flot);
fclose(flot);
return tab;
}
void global(void)
{
Resultat *tab;
int nb;
char nomFic[20];
printf("Entrez le nom du fichier à lire : ");
scanf("%s", nomFic);
tab = chargeTresultat(nomFic, &nb);
afficheTresultat(tab, nb);
sauveTresultat(tab, nb);
free(tab);
tab = restaureTresultat("resultat.bin", &nb);
afficheTresultat(tab, nb);
free(tab);
}

@ -0,0 +1,17 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
char nom[21];
char cdmat[6];
float moyenne;
} Resultat;
Resultat lireResultat(FILE *fe);
void afficheResultat(Resultat c);
Resultat *chargeTresultat(char *nomFic, int *nbres);
void afficheTresultat(Resultat *tab, int nbres);
void sauveTresultat(Resultat *tRes, int nbres);
Resultat *restaureTresultat(char *nomFic, int *nbres);
void global(void);
Loading…
Cancel
Save