DEV/DEV1.1/TP15/pile.c

141 lines
1.8 KiB
C
Raw Normal View History

2023-01-04 15:31:14 +01:00
#include <stdio.h>
#include <stdlib.h>
2023-01-11 14:14:52 +01:00
#include <string.h>
2023-01-04 15:31:14 +01:00
struct pile
{
int tete;
char ple[10];
};
typedef struct pile pile;
int empty(pile *p)
{
if(p==NULL)
{
return 1;
}
return p->tete==0;
}
char pop(pile **p)
{
(*p)->tete--;
return (*p)->ple[(*p)->tete];
}
void push(pile **p,char v)
{
if (*p==NULL)
{
pile *new=malloc(sizeof(pile));
new->tete=0;
*p=new;
push(p,v);
}else
{
(*p)->ple[(*p)->tete]=v;
(*p)->tete++;
}
}
char top(pile **p)
{
char temp = pop(p);
push(p,temp);
return temp;
}
void clear(pile **p)
{
while(!empty((*p)))
{
pop(p);
}
}
2023-01-11 14:14:52 +01:00
int main(int argc, char const *argv[])
{
FILE *f;
f=fopen(argv[1],"r");
pile *test = malloc(sizeof(pile));
char c;
char temp;
int verif;
fread(&c,1,1,f);
while(!feof(f)==1)
{
printf("%c\n", c);
if (c=='('||c=='['||c=='{')
{
push(&test,c);
}
else
{
if (c==')'||c==']'||c=='}')
{
verif = 0;
if (!empty(test))
{
temp = pop(&test);
printf("c = %c et test = %c\n",c,temp);
switch(c)
{
case ')':
if (temp == '(')
{
break;
}
else
{
verif++;
}
break;
case ']':
if (temp == '[')
{
break;
}
else
{
verif++;
}
break;
case '}':
if (temp == '{')
{
break;
}
else
{
verif++;
}
break;
default:
break;
}
if (verif!=0)
{
break;
}
}
else
{
printf("Le fichier est mal parenthesé (parenthese en trop) !\n");
return 1;
}
}
}
fread(&c,1,1,f);
}
if (verif!=0)
{
printf("Le fichier est mal parenthesé ! (parenthse au mauvaise endroit)\n");
}
else
{
printf("Le fichier est bien parenthesé !\n");
}
return 0;
}