ChaotiCc

Untitled

Jan 13th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.80 KB | None | 0 0
  1. /***********************************************************************
  2.  * bep.c
  3.  * examen de janvier 2013 : gestion d'un parc à conteneurs du BEP
  4.  * Janv 2013
  5.  ***********************************************************************/
  6. /* fichiers d'entête */
  7.  #include <stdio.h>
  8.  #include <stdlib.h>
  9.  #include <string.h>
  10.  
  11. /* définition du type utile à la compatibilité */
  12. /* pour la fonction qsort */
  13. typedef int (* fctCompare)(const void *, const void *);
  14.  
  15. /* definitions des structures de données */
  16. /* structure déchet un nom et un volume */
  17.  typedef struct dechet dechet;
  18.  struct dechet
  19.  {
  20.     char *nom;
  21.     float volume;
  22.  };
  23.  
  24. /* structure nœud de la chaine */
  25.  typedef struct noeud noeud;
  26.  struct noeud
  27.  {
  28.     dechet *dechet;
  29.     noeud *suivant;
  30.  };
  31.  
  32. /* structure client (nr identité, nom, prénom, liste chaînée de déchets*/
  33.  typedef struct client client;
  34.  struct client
  35.  {
  36.     long numIdentite;
  37.     char *nom;
  38.     char *prenom;
  39.     noeud *dechets;
  40.  };
  41.  
  42. /* structure enreg (enregistrement à écrire dans le fichier) */
  43. /* nr identité, nom, prénom, nom d’un déchet, quantité) */
  44.  typedef struct enreg enreg;
  45.  struct enreg
  46.  {
  47.     long numIdentite;
  48.     char nom[26];
  49.     char prenom[15];
  50.     char *nomDechet;
  51.     int quantite;
  52.  };
  53.  
  54. // déclaration des fonctions " dechets "
  55. noeud *extraireDechets(char *ligne);
  56. void ajouterDechets(client *cl, noeud *dechets);
  57. void afficherDechet(dechet *d);
  58. void afficheListeDechets(noeud *dechets);
  59.  
  60. // définition des fonctions clients
  61. client *extraireClient(char *ligne);
  62. int ajouterClient(client ***table, client *cl);
  63. int cmpClient(client *cl1, client *cl2);
  64. void afficherClient(client *cl);
  65. int ecrireClient(client *cl, FILE *f);
  66.  
  67. // Pas de variables globles
  68. int main (int argc, char **argv) {
  69.     /* definition des variables */
  70.     char ligne[256];
  71.     char *nomFichier;
  72.     int nbClients = 0;
  73.     int i = 0;
  74.     int nbre = 0;
  75.     FILE *fout;
  76.     client **clients;
  77.     client *cl;
  78.  
  79.     /* traitement du fichier de donnees */
  80.     while (fgets(ligne, 256, stdin)) {
  81.         cl = extraireClient(ligne);
  82.         nbClients = ajouterClient(&clients, cl);
  83.     }
  84.  
  85.     printf("%d clients\n", nbClients);
  86.  
  87.     /* tri de la table des clients */
  88.     qsort(clients, nbClients, sizeof(client), (fctCompare)cmpClient);
  89.  
  90.     /* traitements des arguments de la ligne de commande */
  91.     if (argc == 2) {
  92.         nomFichier = *(argv + 1);
  93.     } else {
  94.         fprintf(stderr, "Usage: ./bep <output file>");
  95.         return 1;
  96.     }
  97.  
  98.     /* ouverture du fichier d'output */
  99.     fout = fopen(nomFichier, "r");
  100.  
  101.     /* en fin d'application, affichage de la table */
  102.     /* et ecriture des clients dans le fichier (un enreg par déchet)*/
  103.     for (i = 0; i < nbClients; ++i) {
  104.         cl = *(clients + i * sizeof(client *));
  105.         afficherClient(cl);
  106.     }
  107.  
  108.     /* fin de l'application : affichage du */
  109.     /* nombre total d’enregistrements sauvés dans le fichier */
  110.     printf("Il y a %d enregistrements \n", nbre);
  111.     fclose(fout);
  112. }
  113.  
  114. // fonctions dechets
  115.  
  116. /* fonction qui cree une liste de dechets */
  117. /* a partir d'une chaine de caractere */
  118. /* la fonction renvoie un pointeur sur un noeud */
  119. noeud *extraireDechets(char *ligne) {
  120.     noeud *n;
  121.     noeud *precedent = NULL;
  122.     dechet *d;
  123.     char *token;
  124.     char *token2;
  125.     char *brkt;
  126.  
  127.     if ((n = (noeud *) malloc(sizeof(noeud))) == NULL) {
  128.         printf("Fuck you\n");
  129.     }
  130.  
  131.     for(token = strtok(ligne, ",");
  132.         token;
  133.         token = strtok(NULL, ",")) {
  134.  
  135.         if ((d = (dechet *) malloc(sizeof(dechet))) == NULL) {
  136.             printf("Fuck you\n");
  137.         }
  138.  
  139.         token2 = strtok_r(token, "\t", &brkt);
  140.         d->nom = token2;
  141.  
  142.         token2 = strtok_r(NULL, "\0", &brkt);
  143.         d->volume = atof(token2);
  144.  
  145.         n->dechet = d;
  146.  
  147.         n->suivant = precedent;
  148.  
  149.         precedent = n;
  150.     }
  151.  
  152.     printf("%s\n", n->dechet->nom);
  153.  
  154.     return n;
  155. }
  156.  
  157. /* fonction qui ajoute une liste de dechets a un client */
  158. /* elle recoit en parametre un client et une liste de dechet */
  159. void ajouterDechets(client *cl, noeud *dechets) {
  160.     cl->dechets = dechets;
  161. }
  162.  
  163. /* fonction qui affiche un dechet (nom et quantite)*/
  164. void afficherDechet(dechet *d) {
  165.     printf("%s : %f m3\n", d->nom, d->volume);
  166. }
  167.  
  168. /* fonction qui affiche une liste de dechets */
  169. /* ou un message adapté si la liste est vide */
  170. void afficheListeDechets(noeud *dechets) {
  171.     noeud *dechetCourant;
  172.  
  173.     if (dechets->dechet) {
  174.         for(dechetCourant = dechets;
  175.             dechetCourant->suivant;
  176.             dechetCourant = dechetCourant->suivant) {
  177.             afficherDechet(dechetCourant->dechet);
  178.         }
  179.     } else {
  180.         printf("Liste de déchets vide.");
  181.     }
  182. }
  183.  
  184. // définition des fonctions clients
  185.  
  186. /* fonction qui cree un client a partir d'une chaine de caractere */
  187. /* la fonction renvoie un client */
  188. client *extraireClient(char *ligne) {
  189.     client *cl;
  190.     char *data;
  191.     noeud *dechets;
  192.  
  193.     printf("Next client\n");
  194.  
  195.     if ((cl = (client *) malloc(sizeof(client))) == NULL)
  196.     {
  197.         fprintf(stderr, "Fuck off\n");
  198.     }
  199.  
  200.     data = strtok(ligne, "\t");
  201.     cl->numIdentite = atol(data);
  202.  
  203.     data = strtok(NULL, "\t");
  204.     cl->nom = data;
  205.  
  206.     data = strtok(NULL, ":");
  207.     cl->prenom = data;
  208.  
  209.     data = strtok(NULL, "\0");
  210.  
  211.     ajouterDechets(cl, extraireDechets(data));
  212.  
  213.     return cl;
  214. }
  215.  
  216. /* fonction qui ajoute un client à la table */
  217. /* la fonction utilise 2 parametres : */
  218. /* la table a modifier et un client */
  219. /* la fonction doit creer les premiers elements lors du 1er appel */
  220. /* et agrandir la table si necessaire. */
  221.  
  222. /* elle renvoie le nombre d’éléments stockés dans la table */
  223. int ajouterClient(client ***table, client *cl) {
  224.     static taillePhysique = 0;
  225.     static tailleLogique = 0;
  226.  
  227.     if (taillePhysique == 0) {
  228.         taillePhysique = 10;
  229.         *table = (client **) malloc(sizeof(client *) * taillePhysique);
  230.     }
  231.  
  232.     if (tailleLogique == taillePhysique) {
  233.         taillePhysique *= 2;
  234.         *table = (client **) realloc(table, sizeof(client *) * taillePhysique);
  235.     }
  236.  
  237.     *(*table + tailleLogique * sizeof(client *)) = cl;
  238.  
  239.     return ++tailleLogique;
  240. }
  241.  
  242. /* fonction de comparaison de clients */
  243. /* sur le nom en 1ere cle, puis le prenom et le numero d'identite */
  244. int cmpClient(client *cl1, client *cl2) {
  245.     int result;
  246.  
  247.     printf("%s %s\n", cl1->nom, cl2->nom);
  248.  
  249.     if (result = strcmp(cl1->nom, cl2->nom)) {
  250.         return result;
  251.     }
  252.  
  253.     if (result = strcmp(cl1->prenom, cl2->prenom)) {
  254.         return result;
  255.     }
  256.  
  257.     return cl1->numIdentite < cl2->numIdentite;
  258. }
  259.  
  260. /* fonction qui affiche un client */
  261. void afficherClient(client *cl) {
  262.     printf("%ld %s %s\n", cl->numIdentite, cl->nom, cl->prenom);
  263.  
  264.     afficheListeDechets(cl->dechets);
  265.  
  266.     printf("\n");
  267. }
  268.  
  269. /* fonction qui ecrit les enregistrements concernant un client */
  270. /* la fonction recoit 2 paramètres : */
  271. /* un pointeur sur un client et un stream en parametre */
  272. /* et renvoie -1 en cas d'erreur */
  273. /* apres avoir afficher le message d'erreur standard */
  274. /* ou le nombre d'enregistrements deja ecrits dans le fichier */
  275. int ecrireClient(client *cl, FILE *f) {
  276.     static int numWritten = 0;
  277.  
  278.     // fwrite()
  279.  
  280.     return ++numWritten;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment