lonsomehell

working tp enrg

Feb 19th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //Something is wrong with fflush because the program is not going well.Something is stuck in memory.
  5. //Need sometime to figure out what is the probleme.Program is working but glitchy.
  6. typedef struct Personne Personne;
  7. struct Personne {
  8.   char nom[32];
  9.   char telephone[16];
  10. };
  11. typedef struct carnet carnet;
  12. struct carnet {
  13.   Personne Tableau[20];
  14.   int compteur;
  15. };
  16. Personne creer(Personne p)
  17. {
  18.   printf("\nDonner le nom de la personne : ");
  19.   vider_stream();
  20.   gets(p.nom);
  21.   do {
  22.  
  23.     printf("\nDonner le numero du telephone : ");
  24.     gets(p.telephone);
  25.   } while (atoi(p.telephone) == 0);
  26.   return p;
  27. };
  28. void affichep(Personne p)
  29. {
  30.   printf("\nle nom de la personne : ");
  31.   puts(p.nom);
  32.   printf("\nle telephone de la personne : ");
  33.   puts(p.telephone);
  34. }
  35. void add(Personne p, carnet *c)
  36. {
  37.   if ((*c).compteur < 19) {
  38.     c->Tableau[c->compteur] = p;
  39.     c->compteur = (c->compteur) + 1;
  40.   } else
  41.     printf("\nProbleme ajout personne : nombre max atteint");
  42. }
  43. void affichec(carnet c)
  44. {
  45.   int i;
  46.   for (i = 0; i < ((c.compteur)); i++) {
  47.     affichep(c.Tableau[i]);
  48.   }
  49. }
  50. void Del(carnet *c)
  51. {
  52.   int n;
  53.   do {
  54.     printf("\nDonner l element a supprimer : ");
  55.     scanf("%d", &n);
  56.     n--;
  57.   } while ((n < 0) || (n > (*c).compteur - 1)); //bug here need some fixing.Working at last.
  58.   int i;
  59.   for (i = n; i < (*c).compteur - 1; i++) {
  60.     c->Tableau[i] = c->Tableau[i + 1];
  61.   }
  62.   (c->compteur)--;
  63. }
  64.  
  65. void trier(carnet *c)
  66. {
  67.   Personne aux;
  68.   int i, j;
  69.   for (i = 1; i < (c->compteur); i++) {
  70.     aux = (c->Tableau[i]);
  71.     j = i;
  72.     while ((j > 0) && (strcmp(c->Tableau[j - 1].nom, aux.nom) > 0)) {
  73.       c->Tableau[j] = c->Tableau[j - 1];
  74.       j--;
  75.     }
  76.     c->Tableau[j] = aux;
  77.   }
  78. }
  79. int recherche(carnet c)
  80. {
  81.   char ch[32];
  82.   printf("\n Donner le nom de la personne : ");
  83.   gets(ch);
  84.   int i = 0;
  85.   while ((strcmp(c.Tableau[i].nom, ch) != 0) && (i < (c.compteur + 1)))
  86.     i++;
  87.   if (i <= c.compteur)
  88.     return i + 1;
  89.   else
  90.     return 0;
  91. }
  92.  
  93. vider_stream()
  94. {
  95.   char c;
  96.   while ((c = getchar()) != '\n' && c != EOF);
  97. }
  98. int main()
  99. {
  100.   char cont;
  101.   int n, r, i;
  102.   Personne p;
  103.   carnet c;
  104.   c.compteur = 0;
  105.   printf("##################################################\n");
  106.   printf("#\n#\t\tTP STRUCTURES\n#\n");
  107.   printf("##################################################\n");
  108.   printf("\nDonner la commande voulue:");
  109.   printf("\nA Ajout personne");
  110.   printf("\nD Supprimmer une personne");
  111.   printf("\nV Affichage carnet");
  112.   printf("\nS Recherche d une personne");
  113.   printf("\nT Trie;");
  114.   printf("\nQ Quitte");
  115.   do {
  116.     do {
  117.       printf("\nWaiting for instructions : ");
  118.       scanf("%c", &cont);
  119.       vider_stream();
  120.     } while ((cont != 'A') && (cont != 'D') && (cont != 'V') && (cont != 'S') && (cont != 'T') && (cont != 'Q'));
  121.  
  122.     switch (cont) {
  123.     case 'A':
  124.       printf("\nDonner le nombre de personne a ajouter : ");
  125.       scanf("%d", &n);
  126.  
  127.       for (i = 0; i < n; i++) {
  128.         p = creer(p);
  129.         add(p, &c);
  130.       }
  131.       printf("\n***************Task Done***************");
  132.       break;
  133.     case 'D':
  134.       Del(&c);
  135.       printf("\n***************Task Done***************");
  136.       break;
  137.     case 'V':
  138.       affichec(c);
  139.       printf("\n***************Task Done***************");
  140.       break;
  141.     case 'S':
  142.       r = recherche(c);
  143.       if (r == 0) {
  144.         printf("\n L element rechercher n existe pas");
  145.         printf("\n***************Task Done***************");
  146.       } else {
  147.         printf("\nLa premiere occurence de l'element recherche est : %d", r);
  148.         printf("\n***************Task Done***************");
  149.       }
  150.       break;
  151.     case 'T':
  152.       trier(&c);
  153.       break;
  154.     default:
  155.       printf("\nAu revoir\nClosing.....\n");
  156.     }
  157.   } while (cont != 'Q');
  158.   return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment