Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************************
- * bep.c
- * examen de janvier 2013 : gestion d'un parc à conteneurs du BEP
- * Janv 2013
- ***********************************************************************/
- /* fichiers d'entête */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* définition du type utile à la compatibilité */
- /* pour la fonction qsort */
- typedef int (* fctCompare)(const void *, const void *);
- /* definitions des structures de données */
- /* structure déchet un nom et un volume */
- typedef struct dechet dechet;
- struct dechet
- {
- char *nom;
- float volume;
- };
- /* structure nœud de la chaine */
- typedef struct noeud noeud;
- struct noeud
- {
- dechet *dechet;
- noeud *suivant;
- };
- /* structure client (nr identité, nom, prénom, liste chaînée de déchets*/
- typedef struct client client;
- struct client
- {
- long numIdentite;
- char *nom;
- char *prenom;
- noeud *dechets;
- };
- /* structure enreg (enregistrement à écrire dans le fichier) */
- /* nr identité, nom, prénom, nom d’un déchet, quantité) */
- typedef struct enreg enreg;
- struct enreg
- {
- long numIdentite;
- char nom[26];
- char prenom[15];
- char *nomDechet;
- int quantite;
- };
- // déclaration des fonctions " dechets "
- noeud *extraireDechets(char *ligne);
- void ajouterDechets(client *cl, noeud *dechets);
- void afficherDechet(dechet *d);
- void afficheListeDechets(noeud *dechets);
- // définition des fonctions clients
- client *extraireClient(char *ligne);
- int ajouterClient(client ***table, client *cl);
- int cmpClient(client *cl1, client *cl2);
- void afficherClient(client *cl);
- int ecrireClient(client *cl, FILE *f);
- // Pas de variables globles
- int main (int argc, char **argv) {
- /* definition des variables */
- char ligne[256];
- char *nomFichier;
- int nbClients = 0;
- int i = 0;
- int nbre = 0;
- FILE *fout;
- client **clients;
- client *cl;
- /* traitement du fichier de donnees */
- while (fgets(ligne, 256, stdin)) {
- cl = extraireClient(ligne);
- nbClients = ajouterClient(&clients, cl);
- }
- printf("%d clients\n", nbClients);
- /* tri de la table des clients */
- qsort(clients, nbClients, sizeof(client), (fctCompare)cmpClient);
- /* traitements des arguments de la ligne de commande */
- if (argc == 2) {
- nomFichier = *(argv + 1);
- } else {
- fprintf(stderr, "Usage: ./bep <output file>");
- return 1;
- }
- /* ouverture du fichier d'output */
- fout = fopen(nomFichier, "r");
- /* en fin d'application, affichage de la table */
- /* et ecriture des clients dans le fichier (un enreg par déchet)*/
- for (i = 0; i < nbClients; ++i) {
- cl = *(clients + i * sizeof(client *));
- afficherClient(cl);
- }
- /* fin de l'application : affichage du */
- /* nombre total d’enregistrements sauvés dans le fichier */
- printf("Il y a %d enregistrements \n", nbre);
- fclose(fout);
- }
- // fonctions dechets
- /* fonction qui cree une liste de dechets */
- /* a partir d'une chaine de caractere */
- /* la fonction renvoie un pointeur sur un noeud */
- noeud *extraireDechets(char *ligne) {
- noeud *n;
- noeud *precedent = NULL;
- dechet *d;
- char *token;
- char *token2;
- char *brkt;
- if ((n = (noeud *) malloc(sizeof(noeud))) == NULL) {
- printf("Fuck you\n");
- }
- for(token = strtok(ligne, ",");
- token;
- token = strtok(NULL, ",")) {
- if ((d = (dechet *) malloc(sizeof(dechet))) == NULL) {
- printf("Fuck you\n");
- }
- token2 = strtok_r(token, "\t", &brkt);
- d->nom = token2;
- token2 = strtok_r(NULL, "\0", &brkt);
- d->volume = atof(token2);
- n->dechet = d;
- n->suivant = precedent;
- precedent = n;
- }
- printf("%s\n", n->dechet->nom);
- return n;
- }
- /* fonction qui ajoute une liste de dechets a un client */
- /* elle recoit en parametre un client et une liste de dechet */
- void ajouterDechets(client *cl, noeud *dechets) {
- cl->dechets = dechets;
- }
- /* fonction qui affiche un dechet (nom et quantite)*/
- void afficherDechet(dechet *d) {
- printf("%s : %f m3\n", d->nom, d->volume);
- }
- /* fonction qui affiche une liste de dechets */
- /* ou un message adapté si la liste est vide */
- void afficheListeDechets(noeud *dechets) {
- noeud *dechetCourant;
- if (dechets->dechet) {
- for(dechetCourant = dechets;
- dechetCourant->suivant;
- dechetCourant = dechetCourant->suivant) {
- afficherDechet(dechetCourant->dechet);
- }
- } else {
- printf("Liste de déchets vide.");
- }
- }
- // définition des fonctions clients
- /* fonction qui cree un client a partir d'une chaine de caractere */
- /* la fonction renvoie un client */
- client *extraireClient(char *ligne) {
- client *cl;
- char *data;
- noeud *dechets;
- printf("Next client\n");
- if ((cl = (client *) malloc(sizeof(client))) == NULL)
- {
- fprintf(stderr, "Fuck off\n");
- }
- data = strtok(ligne, "\t");
- cl->numIdentite = atol(data);
- data = strtok(NULL, "\t");
- cl->nom = data;
- data = strtok(NULL, ":");
- cl->prenom = data;
- data = strtok(NULL, "\0");
- ajouterDechets(cl, extraireDechets(data));
- return cl;
- }
- /* fonction qui ajoute un client à la table */
- /* la fonction utilise 2 parametres : */
- /* la table a modifier et un client */
- /* la fonction doit creer les premiers elements lors du 1er appel */
- /* et agrandir la table si necessaire. */
- /* elle renvoie le nombre d’éléments stockés dans la table */
- int ajouterClient(client ***table, client *cl) {
- static taillePhysique = 0;
- static tailleLogique = 0;
- if (taillePhysique == 0) {
- taillePhysique = 10;
- *table = (client **) malloc(sizeof(client *) * taillePhysique);
- }
- if (tailleLogique == taillePhysique) {
- taillePhysique *= 2;
- *table = (client **) realloc(table, sizeof(client *) * taillePhysique);
- }
- *(*table + tailleLogique * sizeof(client *)) = cl;
- return ++tailleLogique;
- }
- /* fonction de comparaison de clients */
- /* sur le nom en 1ere cle, puis le prenom et le numero d'identite */
- int cmpClient(client *cl1, client *cl2) {
- int result;
- printf("%s %s\n", cl1->nom, cl2->nom);
- if (result = strcmp(cl1->nom, cl2->nom)) {
- return result;
- }
- if (result = strcmp(cl1->prenom, cl2->prenom)) {
- return result;
- }
- return cl1->numIdentite < cl2->numIdentite;
- }
- /* fonction qui affiche un client */
- void afficherClient(client *cl) {
- printf("%ld %s %s\n", cl->numIdentite, cl->nom, cl->prenom);
- afficheListeDechets(cl->dechets);
- printf("\n");
- }
- /* fonction qui ecrit les enregistrements concernant un client */
- /* la fonction recoit 2 paramètres : */
- /* un pointeur sur un client et un stream en parametre */
- /* et renvoie -1 en cas d'erreur */
- /* apres avoir afficher le message d'erreur standard */
- /* ou le nombre d'enregistrements deja ecrits dans le fichier */
- int ecrireClient(client *cl, FILE *f) {
- static int numWritten = 0;
- // fwrite()
- return ++numWritten;
- }
Advertisement
Add Comment
Please, Sign In to add comment