TP17 Organisation du code

This commit is contained in:
2021-10-19 14:22:25 +02:00
parent 8c5c9946bf
commit acd09bd426
15 changed files with 390 additions and 0 deletions

45
APL1.1/TP17/exo1/Makefile Normal file
View File

@@ -0,0 +1,45 @@
# TP 19 Exercice 1 : fichier Makefile
# CHAPITRE 1 : BUT FINAL
but : exo1
# CHAPITRE 2 : VARIABLES
OFILES = lire.o \
personne.o \
repertoire.o \
options.o \
main.o
CC = gcc
CFLAGS = -Wall -ansi -pedantic -g
# CHAPITRE 3 : DEPENDANCES (REGLES IMPLICITES)
options.o : options.h
personne.o : personne.h lire.h
repertoire.o : repertoire.h personne.h
main.o : personne.h repertoire.h options.h
#CHAPITRE 4 : DEPENDANCES AVEC COMMANDES
lire.o : lire.s lire.h
as -o lire.o lire.s
exo1 : $(OFILES)
$(CC) $(CFLAGS) -o exo1 $(OFILES)
#CHAPITRE 5 : NETTOYAGE DES FICHIERS GENERES
clean :
-rm -f $(OFILES) exo1
#CHAPITRE 6 : BUTS FACTICES
.PHONY : but clean

BIN
APL1.1/TP17/exo1/exo1 Executable file

Binary file not shown.

8
APL1.1/TP17/exo1/lire.h Normal file
View File

@@ -0,0 +1,8 @@
/* TP 19 Exercice 1 : fichier lire.h */
#ifndef LIRE_H
#define LIRE_H
void lire(char*, int);
#endif /* LIRE_H */

30
APL1.1/TP17/exo1/lire.s Normal file
View File

@@ -0,0 +1,30 @@
.section .text
.globl lire
.type lire, @function
lire:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
xorq %rdx, %rdx
movl %esi, %edx # taille max
movq %rdi, %rsi # adresse chaine
movq $0, %rax # read
movq $0, %rdi # stdin
decq %rdx # place du \0
syscall # call read
cmpb $10, -1(%rsi, %rax, 1) # si \n
jne lire_1
decq %rax
lire_1: movb $0, (%rsi, %rax, 1) # place \0
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size lire, .-lire

25
APL1.1/TP17/exo1/main.c Normal file
View File

@@ -0,0 +1,25 @@
/* TP19 Exercice 1 : fichier main.c */
#include <stdlib.h>
#include <stdio.h>
#include "personne.h"
#include "repertoire.h"
#include "options.h"
int main(void) {
options opt;
repertoire r = construire_repertoire();
while ((opt=saisir_option())!=SORTIR)
switch(opt) {
case AJOUTER :
ajouter_personne(r, saisir_personne());
break;
case AFFICHER :
afficher_repertoire(r);
break;
default :
; /* rien a faire */
}
detruire_repertoire(r);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,19 @@
#include<stdio.h>
#include<stdlib.h>
#include "options.h"
options saisir_option() {
short o;
printf("\nChoisissez une option :\n");
printf("1] Ajouter une personne.\n");
printf("2] Afficher le repertoire.\n");
printf("3] Sortir\n");
printf("? ");
scanf("%hd", &o);
switch(o) {
case 1 : return AJOUTER;
case 2 : return AFFICHER;
case 3 : return SORTIR;
default : return AFFICHER;
}
}

View File

@@ -0,0 +1,8 @@
#ifndef OPTIONS_H
#define OPTIONS_H
typedef enum {AJOUTER, AFFICHER, SORTIR} options;
options saisir_option();
#endif /* OPTIONS_H */

View File

@@ -0,0 +1,33 @@
/* TP 19 Exercice 1 : fichier personne.c */
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "personne.h"
#include "lire.h"
personne construire_personne(const char *nom, const char *tel) {
personne p = (personne) malloc(sizeof(struct s_personne));
strcpy(p->nom, nom);
strcpy(p->tel, tel);
return p;
}
personne saisir_personne() {
personne p = (personne) malloc(sizeof(struct s_personne));
printf("\nEntrez le nom de la personne : ");
fflush(stdout);
lire(p->nom, 30);
printf("Entrez son numero de telephone : ");
fflush(stdout);
lire(p->tel, 20);
return p;
}
void afficher_personne(personne p) {
printf("%-30s %-20s\n", p->nom, p->tel);
}
void detruire_personne(personne p) {
free(p);
}

View File

@@ -0,0 +1,16 @@
/* TP 19 Exercice 1 : fichier personne.h */
#ifndef PERSONNE_H
#define PERSONNE_H
typedef struct s_personne {
char nom[30];
char tel[20];
} * personne;
personne construire_personne(const char*, const char*);
personne saisir_personne();
void afficher_personne(personne);
void detruire_personne(personne);
#endif /* PERSONNE_H */

View File

@@ -0,0 +1,37 @@
/* TP 19 Exercice 1 : fichier repertoire.c */
#include<stdlib.h>
#include<stdio.h>
#include "repertoire.h"
#include "lire.h"
repertoire construire_repertoire() {
repertoire r = (repertoire) malloc(sizeof(struct s_repertoire));
r->taille = 0;
return r;
}
void afficher_repertoire(repertoire r) {
int i = 0;
printf("\n%-30s %-20s\n", "Nom", "Telephone");
for(; i<r->taille; i++)
afficher_personne((r->personnes)[i]);
}
int ajouter_personne(repertoire r, personne p) {
if (r->taille<CAPACITE) {
(r->personnes)[r->taille] = p;
(r->taille)++;
return 0;
} else
return 1;
}
void detruire_repertoire(repertoire r) {
int i = r->taille;
while(i-->0) {
free((r->personnes)[i]);
}
free(r);
}

View File

@@ -0,0 +1,20 @@
/* TP 19 Exercice 1 : fichier repertoire.h */
#ifndef REPERTOIRE_H
#define REPERTOIRE_H
#include "personne.h"
#define CAPACITE 100
typedef struct s_repertoire {
int taille;
personne personnes[CAPACITE];
} * repertoire;
repertoire construire_repertoire();
void afficher_repertoire(repertoire);
int ajouter_personne(repertoire, personne);
void detruire_repertoire(repertoire);
#endif /* REPERTOIRE_H */