-
Notifications
You must be signed in to change notification settings - Fork 0
/
brinde.c
169 lines (135 loc) · 4.07 KB
/
brinde.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Tarefa de laboratorio 08 - Avaliacao 1 - Parte 1 - Brinde FACE
*
* GEX605 AB 2020/1
*
* Nome: XXXX
* Matricula: XXXX
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAM_MAX_STR_PAR 100
typedef char Item;
typedef struct elemPilha {
Item item;
struct elemPilha *proximo;
} ElemPilha;
typedef struct {
ElemPilha *topo;
} Pilha;
void inicializaPilha(Pilha *pilha) {
pilha->topo = NULL;
}
int pilhaVazia(Pilha *pilha) {
return (pilha->topo == NULL);
}
void inserePilha(Pilha *pilha, Item item) {
ElemPilha *aux;
// Cria um novo elemento da lista encadeada que representa a pilha e
// armazena neste novo elemento o item a ser inserido na pilha
aux = malloc(sizeof(ElemPilha));
aux->item = item;
// Insere o novo elemento no inicio da lista encadeada que representa a
// pilha
aux->proximo = pilha->topo;
pilha->topo = aux;
}
void removePilha(Pilha *pilha, Item *item) {
ElemPilha *aux;
if (pilhaVazia(pilha) != 0) {
printf("Erro na operacao removePilha: pilha vazia!\n");
return;
}
// Armazena o item a ser removido da pilha
*item = pilha->topo->item; // ATENCAO: Depende da definicao do tipo do item
// Armazena o primeiro elemento da lista encadeada que representa a pilha
// e remove este primeiro elemento da lista
aux = pilha->topo;
pilha->topo = pilha->topo->proximo;
// Libera a memoria alocada para o elemento removido
free(aux);
}
void liberaPilha(Pilha *pilha) {
ElemPilha *aux;
while (pilha->topo != NULL) {
// Armazena o primeiro elemento da lista encadeada que representa a
// pilha e remove este primeiro elemento da lista
aux = pilha->topo;
pilha->topo = pilha->topo->proximo;
// Libera a memoria alocada para o elemento removido
free(aux);
}
}
int main() {
char seqParent[TAM_MAX_STR_PAR + 1];
Pilha pilha;
int parentNaoEmpar;
int i;
Item item;
Item itemEsperado;
printf("Digite a sequencia de parenteses a ser avaliada: ");
fgets(seqParent, sizeof(seqParent), stdin);
seqParent[strcspn(seqParent, "\n")] = '\0';
printf("Sequencia de parenteses a ser avaliada:\n%s\n", seqParent);
inicializaPilha(&pilha);
parentNaoEmpar = 0; // Abaixo esta variavel recebera valor 1 se a
// sequencia de parenteses contem pelo menos um par de
// parenteses que nao esta corretamente emparelhado
for (i = 0; i < strlen(seqParent); i++) {
item = seqParent[i];
if ((item == '(') || (item == '[') || (item == '{')) {
inserePilha(&pilha, item);
}
else {
if (item == ')') {
itemEsperado = '(';
}
else if (item == ']') {
itemEsperado = '[';
}
else if (item == '}') {
itemEsperado = '{';
}
else {
printf("Erro no processamento da sequencia de parenteses: "
"caractere invalido!\n");
liberaPilha(&pilha);
return 1;
}
if (pilhaVazia(&pilha) != 0) { // Se a pilha esta vazia
parentNaoEmpar = 1;
break;
}
removePilha(&pilha, &item);
if (item != itemEsperado) {
parentNaoEmpar = 1;
break;
}
}
}
if ((parentNaoEmpar != 0) ||
(pilhaVazia(&pilha) == 0) /* a pilha nao esta vazia */) {
printf("Incorreto\n");
}
else {
printf("Correto\n");
}
liberaPilha(&pilha);
/* Dica:
*
* Suponha que voce declarou as seguintes variaveis:
*
* char c1, c2, c3, c4;
*
* Suponha tambem que voce quer ler da entrada varias linhas que contem 4
* caracteres separados por um espaco em branco. Voce pode ler cada uma
* destas linhas usando os seguintes comandos:
*
* scanf("\n");
* scanf("%c %c %c %c", &c1, &c2, &c3, &c4);
*
*/
return 0;
}