Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //Something is wrong with fflush because the program is not going well.Something is stuck in memory.
- //Need sometime to figure out what is the probleme.Program is working but glitchy.
- typedef struct Personne Personne;
- struct Personne {
- char nom[32];
- char telephone[16];
- };
- typedef struct carnet carnet;
- struct carnet {
- Personne Tableau[20];
- int compteur;
- };
- Personne creer(Personne p)
- {
- printf("\nDonner le nom de la personne : ");
- vider_stream();
- gets(p.nom);
- do {
- printf("\nDonner le numero du telephone : ");
- gets(p.telephone);
- } while (atoi(p.telephone) == 0);
- return p;
- };
- void affichep(Personne p)
- {
- printf("\nle nom de la personne : ");
- puts(p.nom);
- printf("\nle telephone de la personne : ");
- puts(p.telephone);
- }
- void add(Personne p, carnet *c)
- {
- if ((*c).compteur < 19) {
- c->Tableau[c->compteur] = p;
- c->compteur = (c->compteur) + 1;
- } else
- printf("\nProbleme ajout personne : nombre max atteint");
- }
- void affichec(carnet c)
- {
- int i;
- for (i = 0; i < ((c.compteur)); i++) {
- affichep(c.Tableau[i]);
- }
- }
- void Del(carnet *c)
- {
- int n;
- do {
- printf("\nDonner l element a supprimer : ");
- scanf("%d", &n);
- n--;
- } while ((n < 0) || (n > (*c).compteur - 1)); //bug here need some fixing.Working at last.
- int i;
- for (i = n; i < (*c).compteur - 1; i++) {
- c->Tableau[i] = c->Tableau[i + 1];
- }
- (c->compteur)--;
- }
- void trier(carnet *c)
- {
- Personne aux;
- int i, j;
- for (i = 1; i < (c->compteur); i++) {
- aux = (c->Tableau[i]);
- j = i;
- while ((j > 0) && (strcmp(c->Tableau[j - 1].nom, aux.nom) > 0)) {
- c->Tableau[j] = c->Tableau[j - 1];
- j--;
- }
- c->Tableau[j] = aux;
- }
- }
- int recherche(carnet c)
- {
- char ch[32];
- printf("\n Donner le nom de la personne : ");
- gets(ch);
- int i = 0;
- while ((strcmp(c.Tableau[i].nom, ch) != 0) && (i < (c.compteur + 1)))
- i++;
- if (i <= c.compteur)
- return i + 1;
- else
- return 0;
- }
- vider_stream()
- {
- char c;
- while ((c = getchar()) != '\n' && c != EOF);
- }
- int main()
- {
- char cont;
- int n, r, i;
- Personne p;
- carnet c;
- c.compteur = 0;
- printf("##################################################\n");
- printf("#\n#\t\tTP STRUCTURES\n#\n");
- printf("##################################################\n");
- printf("\nDonner la commande voulue:");
- printf("\nA Ajout personne");
- printf("\nD Supprimmer une personne");
- printf("\nV Affichage carnet");
- printf("\nS Recherche d une personne");
- printf("\nT Trie;");
- printf("\nQ Quitte");
- do {
- do {
- printf("\nWaiting for instructions : ");
- scanf("%c", &cont);
- vider_stream();
- } while ((cont != 'A') && (cont != 'D') && (cont != 'V') && (cont != 'S') && (cont != 'T') && (cont != 'Q'));
- switch (cont) {
- case 'A':
- printf("\nDonner le nombre de personne a ajouter : ");
- scanf("%d", &n);
- for (i = 0; i < n; i++) {
- p = creer(p);
- add(p, &c);
- }
- printf("\n***************Task Done***************");
- break;
- case 'D':
- Del(&c);
- printf("\n***************Task Done***************");
- break;
- case 'V':
- affichec(c);
- printf("\n***************Task Done***************");
- break;
- case 'S':
- r = recherche(c);
- if (r == 0) {
- printf("\n L element rechercher n existe pas");
- printf("\n***************Task Done***************");
- } else {
- printf("\nLa premiere occurence de l'element recherche est : %d", r);
- printf("\n***************Task Done***************");
- }
- break;
- case 'T':
- trier(&c);
- break;
- default:
- printf("\nAu revoir\nClosing.....\n");
- }
- } while (cont != 'Q');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment