I was trying to make a program that checks if an expression is well formed.
Something of type {[()()]}
is well formed whereas something of type {{())({]
is not.
I've implemented a "stack.c" library from which I take some functions of the program I left below.
Basically if the read character is "open" it is stacked on the stack. When reading a "close" character it checks if it is the character that corresponds to the opening character at the top of the stack; if it does, it pops the read character and proceeds to read. If not the expression is poorly formed. Could someone tell me what's wrong there?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pilha.h"
int bemFormada(char v[]) {
apontaPilha p = criaPilha(strlen(v) * sizeof(char));
int i = 0;
char c;
for (i = 0; i < strlen(v); i++)
if (v[i] != '{' || v[i] != '}' || v[i] != '[' || v[i] != ']' || v[i] != '(' || v[i] != ')')
v[i] = 0;
for (i = 0; i < strlen(v); i++)
printf("%d", v[i]);
while (i < strlen(v) && v[i] != 0) {
c = v[i];
if (c == '{' || c == '[' || c == '(') {
empilha(p, c);
}
else {
if(pilhaVazia(p))
return 0;
else if ((c == '}' && eleTopo(p) == '{') || (c == ']' && eleTopo(p) == '[') || (c == ')' && eleTopo(p) == '('))
c = desempilha(p);
}
i++;
}
if (!pilhaVazia(0)) {
destroiPilha(p);
return 1;
}
return 0;
}
int main() {
char v[100], c;
int i;
printf("Informe a sequencia: ");
for (i = 0; i < 100; i++){
c = scanf("%s", v);
}
if (bemFormada(v))
printf("\nBem formada!\n");
else
printf("\nMal formada!\n");
return 0;
}
--- Stack.c ---
#include <stdio.h>
#include <stdlib.h>
#include "pilha.h"
#define tipoDaPilha int
typedef struct pilha Pilha;
typedef Pilha * apontaPilha;
apontaPilha criaPilha(int tamanho);
struct pilha {
char *v; /*mudar dependendo do tipo de dado usado*/
int topo;
int tamanho;
};
apontaPilha criaPilha(int tamanho) {
apontaPilha p = malloc(sizeof(Pilha));
p->v = malloc(tamanho * sizeof(Pilha));
p->topo = 0;
p->tamanho = tamanho;
return p;
}
tipoDaPilha pilhaCheia(Pilha *p) {
if (p->topo > p->tamanho)
return 1;
return 0;
}
tipoDaPilha pilhaVazia(Pilha *p) {
return (p->topo == 0);
}
void empilha(Pilha *p, char elemento) {
if (!pilhaCheia(p)) {
p->v[p->topo] = elemento;
p->topo++;
}
else
printf("Pilha cheia!\n");
}
char desempilha(Pilha *p) {
if (!pilhaVazia(p)) {
p->topo--;
return p->v[p->topo];
}
else
return '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pilha.h"
int bemFormada(char v[]) {
apontaPilha p = criaPilha(strlen(v) * sizeof(char));
int i = 0;
char c;
for (i = 0; i < strlen(v); i++)
if (v[i] != '{' || v[i] != '}' || v[i] != '[' || v[i] != ']' || v[i] != '(' || v[i] != ')')
v[i] = 0;
for (i = 0; i < strlen(v); i++)
printf("%d", v[i]);
while (i < strlen(v) && v[i] != 0) {
c = v[i];
if (c == '{' || c == '[' || c == '(') {
empilha(p, c);
}
else {
if(pilhaVazia(p))
return 0;
else if ((c == '}' && eleTopo(p) == '{') || (c == ']' && eleTopo(p) == '[') || (c == ')' && eleTopo(p) == '('))
c = desempilha(p);
}
i++;
}
if (!pilhaVazia(0)) {
destroiPilha(p);
return 1;
}
return 0;
}
int main() {
char v[100], c;
int i;
printf("Informe a sequencia: ");
for (i = 0; i < 100; i++){
c = scanf("%s", v);
}
if (bemFormada(v))
printf("\nBem formada!\n");
else
printf("\nMal formada!\n");
return 0;
}
';
}
tipoDaPilha topo(Pilha *p) {
return p->topo;
}
char eleTopo(Pilha *p) {
return p->v[p->topo - 1];
}
void destroiPilha(Pilha *p) {
free(p->v);
free(p);
}