moitié tp 3
This commit is contained in:
20
DEV1.1/TP28/Makefile
Normal file
20
DEV1.1/TP28/Makefile
Normal file
@@ -0,0 +1,20 @@
|
||||
but : pile
|
||||
|
||||
OFILES = main.o\
|
||||
chaine.o\
|
||||
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -Wall -ansi -pedantic -g
|
||||
|
||||
main.o : chaine.h
|
||||
|
||||
chaine.o : chaine.h
|
||||
|
||||
pile : $(OFILES)
|
||||
$(CC) $(CFLAGS) -o pile $(OFILES)
|
||||
|
||||
clean :
|
||||
-rm -f $(OFILES) pile
|
||||
|
||||
.PHONY : but clean
|
||||
@@ -1,28 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "chaine.h"
|
||||
|
||||
struct maillon_s {
|
||||
char valeur;
|
||||
struct maillon_s* suivant;
|
||||
};
|
||||
typedef struct maillon_s maillon ;
|
||||
|
||||
void push(char nouv, maillon* debut){
|
||||
void push(char nouv, maillon** debut){
|
||||
maillon* m = (maillon*) malloc (sizeof(maillon));
|
||||
m->valeur = nouv;
|
||||
if (debut == NULL)
|
||||
m->suivant = NULL;
|
||||
else
|
||||
m->suivant = debut;
|
||||
debut = m;
|
||||
m->suivant = *debut;
|
||||
*debut = m;
|
||||
}
|
||||
|
||||
void pop(maillon* debut){
|
||||
maillon* m = (maillon*) malloc (sizeof(maillon));
|
||||
m->valeur = nouv;
|
||||
if (debut == NULL)
|
||||
m->suivant = NULL;
|
||||
double pop(maillon** debut){
|
||||
maillon m = **debut;
|
||||
free(*debut);
|
||||
if (m.suivant == NULL)
|
||||
*debut = NULL;
|
||||
else
|
||||
m->suivant = debut;
|
||||
debut = m;
|
||||
*debut = m.suivant;
|
||||
return m.valeur;
|
||||
}
|
||||
|
||||
int empty(const maillon* debut){
|
||||
return debut == NULL;
|
||||
}
|
||||
|
||||
double top (const maillon* debut){
|
||||
return debut->valeur;
|
||||
}
|
||||
|
||||
void clear (maillon** debut){
|
||||
while (empty(*debut)){
|
||||
pop(debut);
|
||||
}
|
||||
}
|
||||
14
DEV1.1/TP28/chaine.h
Normal file
14
DEV1.1/TP28/chaine.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef CHAINE_H
|
||||
#define CHAINE_H
|
||||
|
||||
struct maillon_s {
|
||||
char valeur;
|
||||
struct maillon_s* suivant;
|
||||
};
|
||||
typedef struct maillon_s maillon ;
|
||||
|
||||
void push(char nouv, maillon** debut);
|
||||
double pop(maillon** debut);
|
||||
int empty(const maillon* debut);
|
||||
|
||||
#endif
|
||||
14
DEV1.1/TP28/extra.c
Normal file
14
DEV1.1/TP28/extra.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "extra.h"
|
||||
#include "chaine.h"
|
||||
|
||||
double top (const maillon* debut){
|
||||
return debut->valeur;
|
||||
}
|
||||
|
||||
void clear (maillon** debut){
|
||||
while (empty(*debut)){
|
||||
pop(debut);
|
||||
}
|
||||
}
|
||||
7
DEV1.1/TP28/extra.h
Normal file
7
DEV1.1/TP28/extra.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef EXTRA_H
|
||||
#define EXTRA_H
|
||||
|
||||
double top (const maillon* debut);
|
||||
void clear (maillon** debut);
|
||||
|
||||
#endif
|
||||
40
DEV1.1/TP28/main.c
Normal file
40
DEV1.1/TP28/main.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "chaine.h"
|
||||
#include "extra.h"
|
||||
|
||||
int main(void){
|
||||
char op, valeur;
|
||||
maillon* debut = NULL;
|
||||
printf("La pile attend vos ordres\n");
|
||||
printf("> ");
|
||||
op = getchar();
|
||||
while (op != 'q'){
|
||||
if (op == '-'){
|
||||
if (empty(debut)){
|
||||
printf("La pile est vide !\n");
|
||||
printf("> ");
|
||||
}else{
|
||||
valeur = pop(&debut);
|
||||
printf("Le caractère %c a été supprimé\n", valeur);
|
||||
printf("> ");
|
||||
}
|
||||
}else if (op == '+'){
|
||||
valeur = getchar();
|
||||
printf("Le caractère %c a été ajouté\n", valeur);
|
||||
push(valeur, &debut);
|
||||
printf("> ");
|
||||
}else if (op == '/'){
|
||||
clear(&debut);
|
||||
printf("La pile est vide !\n");
|
||||
printf("> ");
|
||||
}else if (op == '='){
|
||||
valeur = top(debut);
|
||||
printf("Le caractère le plus récent est %c\n", valeur);
|
||||
printf("> ");
|
||||
}
|
||||
op = getchar();
|
||||
}
|
||||
printf("au revoir\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
BIN
DEV1.1/TP28/pile
Executable file
BIN
DEV1.1/TP28/pile
Executable file
Binary file not shown.
Reference in New Issue
Block a user