Created
December 21, 2013 14:23
-
-
Save leobetosouza/8069923 to your computer and use it in GitHub Desktop.
Programas que controlam uma pilhas, listas e matrizes feitos na faculdade em algum ponto de 2006 quando eu pensava que sabia C. Saca a formatação e identação zoada desse troço xD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
/**************************************************** | |
******* DECLARAÇAO DAS ESTRUTURAS UTILIZADAS ******* | |
****************************************************/ | |
struct Elista3A { | |
int elem; | |
struct Elista3A *prox3A; | |
}; | |
struct Elista3B { | |
char nome; | |
struct Elista3B *prox3B; | |
}; | |
struct Elista2 { | |
int elem; | |
struct Elista2 *prox2; | |
struct Elista3B *prox3B; | |
struct Elista3A *prox3A; | |
}; | |
struct Elista1 { | |
int elem; | |
char nome; | |
struct Elista1 *prox1; | |
struct Elista2 *prox2; | |
}; | |
struct Emonitor { | |
int qtde; | |
struct Elista1 *prim; | |
struct Elista1 *fim; | |
}; | |
/**************************************************** | |
******************* PROTÓTIPOS ********************* | |
****************************************************/ | |
char getC(void); | |
int getint(void); | |
void iniciar(void); | |
void menu(void); | |
void menubuscar(void); | |
void imprimirgeral(void); | |
void inserirN1(int elem, char nome); | |
void inserirN2(int elem2, int elem1); | |
void inserirN3A(int elem3A, int elem2, int elem1); | |
void inserirN3B(char elem3B, int elem2, int elem1); | |
void excluirN1(int elem); | |
void excluirN2(int elem2, int elem1); | |
void excluirN3A(int elem3A, int elem2, int elem1); | |
void excluirN3B(char elem3B, int elem2, int elem1); | |
void imprimirN1(void); | |
void imprimirN2(int elem); | |
void imprimirN3A(int elem2, int elem1); | |
void imprimirN3B(int elem2, int elem1); | |
struct Elista1 *buscarN1(int elem); | |
struct Elista2 *buscarN2(int elem2, int elem1); | |
struct Elista3A *buscarN3A(int elem3A, int elem2, int elem1); | |
struct Elista3B *buscarN3B(char elem3B, int elem2, int elem1); | |
struct Elista1 *buscarAntN1(int elem, struct Elista1 **p); | |
struct Elista2 *buscarAntN2(int elem, struct Elista2 **p2, struct Elista1 **p1); | |
struct Elista3A *buscarAntN3A(int elem, struct Elista3A **p3, struct Elista2 **p2, struct Elista1 **p1); | |
struct Elista3B *buscarAntN3B(char elem3B, struct Elista3B **p3, struct Elista2 **p2, struct Elista1 **p1); | |
/**************************************************** | |
**************** VARIÁVEIS GLOBAIS ***************** | |
****************************************************/ | |
struct Emonitor monitor; | |
/**************************************************** | |
********************* MAIN ************************* | |
****************************************************/ | |
main(){ | |
//iniciar(); | |
menu(); | |
} | |
/**************************************************** | |
**************** FUNÇOES DE BUSCA ****************** | |
****************************************************/ | |
struct Elista1 *buscarN1(int elem) { | |
struct Elista1 *p; | |
p = monitor.prim; | |
while (p) { | |
if (p->elem == elem) | |
return p; | |
if (p->elem > elem) { | |
printf("O elemento %d nao foi encontrado", elem); | |
return NULL; | |
} | |
p = p->prox1; | |
} | |
return NULL; | |
} | |
//------------------------------------------------------------------------------ | |
struct Elista2 *buscarN2(int elem2,struct Elista1 *p1) { | |
struct Elista2 *p2; | |
if (!p1) | |
return NULL; | |
p2 = p1->prox2; | |
while (p2) { | |
if (p2->elem == elem2) | |
return p2; | |
if (p2->elem > elem2) { | |
printf("O elemento %d nao foi encontrado",p2->elem); | |
return NULL; | |
} | |
p2 = p2->prox2; | |
} | |
return NULL; | |
} | |
//------------------------------------------------------------------------------ | |
struct Elista3A *buscarN3A(int elem3A,struct Elista2 *p2) { | |
struct Elista3A *p3; | |
if (!p2) | |
return NULL; | |
p3 = p2->prox3A; | |
while (p3) { | |
if (p3->elem == elem3A) | |
return p3; | |
if (p3->elem > elem3A) { | |
printf("O elemento %d nao foi encontrado", elem3A); | |
return NULL; | |
} | |
p3 = p3->prox3A; | |
} | |
return NULL; | |
} | |
//------------------------------------------------------------------------------ | |
struct Elista3B *buscarN3B(char elem3B,struct Elista2 *p2) { | |
struct Elista3B *p3; | |
if (!p2) | |
return NULL; | |
p3 = p2->prox3B; | |
while (p3) { | |
if (p3->nome == elem3B) | |
return p3; | |
if (p3->nome > elem3B) { | |
printf("O elemento %d nao foi encontrado", elem3B); | |
return NULL; | |
} | |
p3 = p3->prox3B; | |
} | |
return NULL; | |
} | |
/**************************************************** | |
************ FUNÇOES DE BUSCA - AUXILIAR *********** | |
****************************************************/ | |
struct Elista1 *buscarAntN1(int elem) { | |
struct Elista1 *p; | |
struct Elista1 *ant; | |
p = monitor.prim; | |
ant = NULL; | |
while (p) { | |
if (p->elem == elem) | |
return ant; | |
ant = p; | |
p = p->prox1; | |
} | |
return NULL; | |
} | |
//------------------------------------------------------------------------------ | |
struct Elista2 *buscarAntN2(int elem,struct Elista1 *p1) { | |
struct Elista2 *p2; | |
struct Elista2 *ant; | |
p2 = p1->prox2; | |
ant = NULL; | |
while (p2) { | |
if (p2->elem == elem) | |
return ant; | |
ant = p2; | |
p2 = p2->prox2; | |
} | |
return NULL; | |
} | |
//------------------------------------------------------------------------------ | |
struct Elista3A *buscarAntN3A(int elem,struct Elista2 *p2) { | |
struct Elista3A *p3; | |
struct Elista3A *ant; | |
p3 = p2->prox3A; | |
ant=NULL; | |
while (p3) { | |
if (p3->elem == elem) | |
return ant; | |
ant = p3; | |
p3 = p3->prox3A; | |
} | |
return NULL; | |
} | |
//------------------------------------------------------------------------------ | |
struct Elista3B *buscarAntN3B(char elem3B,struct Elista2 *p2) { | |
struct Elista3B *p3; | |
struct Elista3B *ant; | |
p3 = p2->prox3B; | |
ant=NULL; | |
while (p3) { | |
if (p3->nome == elem3B) | |
return ant; | |
ant = p3; | |
p3 = p3->prox3B; | |
} | |
return NULL; | |
} | |
/**************************************************** | |
**************** FUNÇOES DE IMPRESSAO ************** | |
****************************************************/ | |
void imprimirN1(void) { | |
struct Elista1 *p; | |
p = monitor.prim; | |
printf("("); | |
while (p) { | |
printf("%d "," %c", p->elem, p->nome); | |
p = p->prox1; | |
if (p) printf(","); | |
} | |
printf(")"); | |
} | |
//------------------------------------------------------------------------------ | |
void imprimirN2(struct Elista1 *p1) { | |
struct Elista2 *p2; | |
p2 = p1->prox2; | |
printf("("); | |
while (p2) { | |
printf("%d", p2->elem); | |
p2 = p2->prox2; | |
if (p2) printf(","); | |
} | |
printf(")"); | |
} | |
//------------------------------------------------------------------------------ | |
void imprimirN3A(struct Elista2 *p2) { | |
struct Elista3A *p3A; | |
p3A = p2->prox3A; | |
printf("("); | |
while (p3A) { | |
printf("%d", p3A->elem); | |
p3A = p3A->prox3A; | |
if (p3A) printf(","); | |
} | |
printf(")"); | |
} | |
//------------------------------------------------------------------------------ | |
void imprimirN3B(struct Elista2 *p2) { | |
struct Elista3B *p3B; | |
p3B = p2->prox3B; | |
printf("("); | |
while (p3B) { | |
printf("%d", p3B->nome); | |
p3B = p3B->prox3B; | |
if (p3B) printf(","); | |
} | |
printf(")"); | |
} | |
//------------------------------------------------------------------------------ | |
void imprimirgeral(void) { | |
struct Elista1 *p; | |
struct Elista2 *p2; | |
p = monitor.prim; | |
p2 = p->prox2; | |
while (p) { | |
printf("%d %c", p->elem, p->nome); | |
if (p->prox2) { | |
printf("["); | |
while (p2) { | |
printf("%d", p->elem); | |
if (p2->prox3A) | |
imprimirN3A(p2); | |
if (p2->prox3B) | |
imprimirN3B(p2); | |
} | |
if (p2->prox2) | |
printf(","); | |
p2 = p2->prox2; | |
printf("]"); | |
} | |
if (p->prox1) | |
printf("\n"); | |
p = p->prox1; | |
} | |
} | |
/**************************************************** | |
**************** FUNÇOES DE EXCLUSAO *************** | |
****************************************************/ | |
void excluirN1(struct Elista1 *p) { | |
struct Elista1 *ant; | |
if (!p->prox2){ | |
ant = buscarAntN1(p->elem); | |
monitor.qtde--; | |
if (monitor.prim == monitor.fim) { | |
monitor.prim = monitor.fim = NULL; | |
return; | |
} | |
if (p->elem == monitor.prim->elem) { | |
monitor.prim = monitor.prim->prox1; | |
return; | |
} | |
if (p->elem == monitor.fim->elem) { | |
monitor.fim = ant; | |
monitor.fim->prox1 = NULL; | |
return; | |
} | |
if ((p->elem != monitor.fim->elem) && (p->elem != monitor.prim->elem)) { | |
ant->prox1 = p->prox1; | |
return; | |
} | |
printf("O elemento %d foi excluido",p->elem); | |
free(p); | |
} | |
else | |
printf("O elemento %d nao pode ser excluido pois ha elementos vinculados a ele",p->elem); | |
} | |
//------------------------------------------------------------------------------ | |
void excluirN2(struct Elista2 *aux2,struct Elista1 *aux) { | |
struct Elista2 *ant; | |
if ((!(aux2)->prox3A) && (!(aux2)->prox3B)) { | |
ant = buscarAntN2(aux2->elem,aux); | |
if (aux->prox2 == aux) | |
ant->prox2 = NULL; | |
if (aux2->prox2) | |
ant->prox2 = (aux2)->prox2; | |
if (!(aux2->prox2)) | |
ant->prox2 = NULL; | |
printf("O elemento %d foi excluido",aux2->elem); | |
free(aux2); | |
} | |
else | |
printf("O elemento %d nao pode ser excluido pois ha elementos vinculados a ele, no nivel 3",aux2->elem); | |
} | |
//------------------------------------------------------------------------------ | |
void excluirN3A(struct Elista3A *aux3,struct Elista2 *aux2) { | |
struct Elista3A *ant; | |
ant = buscarAntN3A(aux3->elem,aux2); | |
if (aux2->prox3A == aux3) | |
aux2->prox3A = NULL; | |
if (aux3->prox3A) | |
ant->prox3A = aux3->prox3A; | |
if (!(aux3->prox3A)) | |
ant->prox3A = NULL; | |
printf("O elemento %d foi excluido",aux3->elem); | |
free(aux3); | |
} | |
//------------------------------------------------------------------------------ | |
void excluirN3B(struct Elista3B *aux3,struct Elista2 *aux2) { | |
struct Elista3B *ant; | |
ant = buscarAntN3B(aux3->nome,aux2); | |
if (aux2->prox3B == aux3) | |
aux2->prox3B = NULL; | |
if (aux3->prox3B) | |
(ant->prox3B = aux3->prox3B); | |
if (!(aux3->prox3B)) | |
ant->prox3B = NULL; | |
printf("O elemento %d foi excluido",aux3->nome); | |
free(aux3); | |
} | |
/**************************************************** | |
**************** FUNÇOES DE INSERÇAO *************** | |
****************************************************/ | |
void inserirN1(int elem,char nome) { | |
struct Elista1 *p; | |
struct Elista1 *ant; | |
struct Elista1 *atual; | |
p=(struct Elista1 *)malloc(sizeof(struct Elista1)); | |
if(!p){ | |
printf("Não foi possível alocar memoria"); | |
return; | |
} | |
p->elem = elem; | |
p->prox1 = NULL; | |
p->prox2 = NULL; | |
p->nome = nome; | |
monitor.qtde++; | |
if (!monitor.prim) { | |
monitor.prim = monitor.fim = p; | |
return; | |
} | |
if (p->elem <= monitor.prim->elem) { | |
p->prox1 = monitor.prim; | |
monitor.prim = p; | |
return; | |
} | |
if (p->elem >= monitor.fim->elem) { | |
monitor.fim->prox1 = p; | |
monitor.fim = p; | |
return; | |
} | |
if((elem != monitor.prim->elem) && (elem != monitor.fim->elem)) { | |
ant = monitor.prim; | |
atual = ant->prox1; | |
while(p->elem > atual->elem) { | |
ant = atual; | |
atual = atual->prox1; | |
} | |
ant->prox1 = p; | |
p->prox1 = atual; | |
return; | |
} | |
printf("Os elementos %d - %c foram inseridos", elem, nome); | |
} | |
//------------------------------------------------------------------------------ | |
void inserirN2(int elem2, struct Elista1 *p1) { | |
struct Elista2 *p; | |
struct Elista2 *ant; | |
struct Elista2 *atual; | |
p=(struct Elista2 *)malloc(sizeof(struct Elista2)); | |
if(!p){ | |
printf("Não foi possível alocar memoria"); | |
return; | |
} | |
p->elem = elem2; | |
p->prox2 = NULL; | |
p->prox3A = NULL; | |
p->prox3B = NULL; | |
if (!(p1->prox2)) { | |
p1->prox2 = p; | |
return; | |
} | |
else { | |
ant = p1->prox2; | |
atual = ant->prox2; | |
while((p->elem > atual->elem)&&(atual->prox2)) { | |
ant = atual; | |
atual = atual->prox2; | |
} | |
ant->prox2 = p; | |
p->prox2 = atual; | |
return; | |
} | |
//printf("O elemento %d foi inserido",elem2); | |
} | |
//------------------------------------------------------------------------------ | |
void inserirN3A(int elem3A,struct Elista2 *p2) { | |
struct Elista3A *p; | |
struct Elista3A *ant; | |
struct Elista3A *atual; | |
p=(struct Elista3A *)malloc(sizeof(struct Elista3A)); | |
if(!p){ | |
printf("Não foi possível alocar memoria"); | |
return; | |
} | |
p->elem = elem3A; | |
p->prox3A = NULL; | |
if (!(p2->prox3A)) { | |
p2->prox3A = p; | |
return; | |
} | |
else { | |
ant = p2->prox3A; | |
atual = ant->prox3A; | |
while((p->elem > atual->elem)&&(atual->prox3A)) { | |
ant = atual; | |
atual = atual->prox3A; | |
} | |
ant->prox3A = p; | |
p->prox3A = atual; | |
return; | |
} | |
//printf("O elemento %d foi inserido",elem3A); | |
} | |
//------------------------------------------------------------------------------ | |
void inserirN3B(char elem3B,struct Elista2 *p2) { | |
struct Elista3B *p; | |
struct Elista3B *ant; | |
struct Elista3B *atual; | |
p=(struct Elista3B *)malloc(sizeof(struct Elista3B)); | |
if(!p){ | |
printf("Não foi possível alocar memoria"); | |
return; | |
} | |
p->nome = elem3B; | |
p->prox3B = NULL; | |
if (!(p2->prox3B)) { | |
p2->prox3B = p; | |
return; | |
} | |
else { | |
ant = p2->prox3B; | |
atual = ant->prox3B; | |
while((p->nome > atual->nome) && (atual->prox3B)) { | |
ant = atual; | |
atual = atual->prox3B; | |
} | |
ant->prox3B = p; | |
p->prox3B = atual; | |
return; | |
} | |
//printf("O elemento %c foi inserido",elem3B); | |
} | |
/**************************************************** | |
************* FUNÇOES DE IINICIALIZAÇAO ************ | |
****************************************************/ | |
void iniciar(void) { | |
monitor.qtde = 0; | |
monitor.prim = NULL; | |
monitor.fim = NULL; | |
} | |
/**************************************************** | |
****************** FUNÇOES AUXILIARES ************** | |
****************************************************/ | |
int getint(void) { | |
int x; | |
scanf("%d",&x); | |
return x; | |
} | |
char getC(void) { | |
char x; | |
scanf("%c",&x); | |
return x; | |
} | |
/**************************************************** | |
******************* FUNÇOES DO MENU **************** | |
****************************************************/ | |
void menuinserir(void) { | |
char op,c; | |
int n; | |
struct Elista1 *aux1; | |
struct Elista2 *aux2; | |
printf("Digite um em qual nivel deseja inserir um elemento (1, 2, 3): "); | |
do{ | |
op = getC(); | |
switch(op){ | |
case '1': printf("Digite o numero que sera inserido"); | |
n = getint(); | |
printf("\nDigite o caracter que sera inserido"); | |
c = getC(); | |
inserirN1(n,c); | |
break; | |
case '2': printf("Escolha um elemento no nivel 1 para vincular o elemento que sera inserido\n"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1){ | |
printf("\nDigite o numero que sera inserido"); | |
n = getint(); | |
inserirN2(n,aux1); | |
} | |
break; | |
case '3': printf("Digite \"a\" para numeral ou \"b\" para caracter"); | |
do { | |
op = getC(); | |
switch(op){ | |
case 'a': printf("\nEscolha um elemento no nivel 1 para vincular o elemento que será inserido"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1){ | |
printf("\nDigite o numero que sera inserido"); | |
imprimirN2(aux1); | |
printf("\n Escolha um dos numerais"); | |
n = getint(); | |
aux2=buscarN2(n ,aux1); | |
if(aux2) { | |
printf("\nDigite o numero q sera inserido"); | |
n = getint(); | |
inserirN3A(n,aux2); | |
} | |
} | |
break; | |
case 'b': printf("\nEscolha um elemento no nivel 1 para vincular o elemento que sera inserido\n"); | |
imprimirN1(); | |
printf("\digite somente o numeral: "); | |
n = getint(); | |
aux1=buscarN1(n); | |
if(aux1) { | |
printf("\n Escolha um elemento no nivel 2 para vincular o elemento que sera inserido\n"); | |
imprimirN2(aux1); | |
printf("\n Escolha um dos numerais"); | |
n=getint(); | |
aux2=buscarN2(n,aux1); | |
if(aux2) { | |
printf("Digite o caracter que sera inserido: "); | |
c = getC(); | |
inserirN3B(c,aux2); | |
} | |
} | |
break; | |
} | |
}while(op != 'a' && op != 'b'); | |
} | |
}while(op != '1' && op != '2' && op != '3'); | |
} | |
//------------------------------------------------------------------------------ | |
void menubuscar(void){ | |
char op,ops,c; | |
int n; | |
struct Elista1 *aux1; | |
struct Elista2 *aux2; | |
struct Elista3A *aux3A; | |
struct Elista3B *aux3B; | |
printf("Digite um em qual nivel deseja buscar um elemento (1, 2, 3): "); | |
do{ | |
op = getC(); | |
switch(op){ | |
case '1': printf("\nDigite somente o numeral que será buscado"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1) | |
printf("\nElemento encontrado"); | |
break; | |
case '2': printf("\nEscolha o elemento no nivel 1 que esta ligado ao seu elemento\n"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1){ | |
printf("\nDigite o numeral que será buscado"); | |
n = getint(); | |
aux2=buscarN2(n,aux1); | |
if (aux2) | |
printf("\nElemento encontrado"); | |
} | |
break; | |
case '3': printf("Digite \"a\" para numeral ou \"b\" para caracter"); | |
do { | |
ops = getC(); | |
switch(ops){ | |
case 'a': printf("Escolha o elemento no nivel 1 que esta ligado ao seu elemento\n"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1){ | |
printf("\nEscolha o elemento no nivel 2 que esta ligado ao seu elemento\n"); | |
imprimirN2(aux1); | |
printf("\n Escolha um dos numerais"); | |
n = getint(); | |
aux2=buscarN2(n ,aux1); | |
if(aux2) { | |
printf("\nDigite o numeral que será buscado"); | |
n = getint(); | |
aux3A=buscarN3A(n ,aux2); | |
if(aux3A) | |
printf("\nElemento encontrado"); | |
} | |
} | |
break; | |
case 'b': printf("Escolha o elemento no nivel 1 que esta ligado ao seu elemento\n"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1){ | |
printf("\nEscolha o elemento no nivel 2 que esta ligado ao seu elemento\n"); | |
imprimirN2(aux1); | |
printf("\n Escolha um dos numerais"); | |
n = getint(); | |
aux2=buscarN2(n ,aux1); | |
if(aux2) { | |
printf("\nDigite o caracter que será buscado"); | |
c = getC(); | |
aux3B=buscarN3B(c,aux2); | |
if(aux3B) | |
printf("\nElemento encontrado"); | |
} | |
} | |
break; | |
} | |
}while(ops != 'a' && ops != 'b'); | |
} | |
}while(op != '1' && op != '2' && op != '3'); | |
} | |
void menu(void){ | |
char op; | |
do{ | |
clrscr(); | |
printf("Programa Multi-listas\n\n"); | |
printf("Menu Principal\n\n"); | |
printf(" I - Inserir elementos\n"); | |
printf(" E - Excluir elementos\n"); | |
printf(" B - Buscar um elemento\n"); | |
printf(" P - Imprimir\n"); | |
printf(" S - Sair\n\n"); | |
printf("Digite a opcao desejada: "); | |
op = getC(); | |
switch (op) { | |
case 'i': | |
case 'I': menuimprimir(); | |
break; | |
case 'e': | |
case 'E': menuexcluir(); | |
break; | |
case 'b': | |
case 'B': menubuscar(); | |
break; | |
case 'p': | |
case 'P': menuimprimir(); | |
} | |
}while(op == 's' || op == 'S'); | |
} | |
//------------------------------------------------------------------------------ | |
void menuimprimir(void) { | |
char op,c,ops; | |
int n; | |
struct Elista1 *aux1; | |
struct Elista2 *aux2; | |
struct Elista3A *aux3A; | |
struct Elista3B *aux3B; | |
printf("Digite um em qual nivel deseja imprimir (1, 2, 3): "); | |
do{ | |
op = getC(); | |
switch(op){ | |
case '1':imprimirN1(); | |
break; | |
case '2': printf("Escolha um elemento no nivel 1 para desvincular o elemento que sera inserido\n"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (!aux1->prox2) | |
printf("\nNao existem elementos no nivel 2 vinculados a esse elemento"); | |
else | |
imprimirN2(aux1); | |
break; | |
case '3': printf("Digite \"a\" para numeral ou \"b\" para caracter"); | |
do { | |
ops = getC(); | |
switch(ops){ | |
case 'a': printf("\nEscolha um elemento no nivel 1 para desvincular o elemento que será excluido"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1){ | |
printf("\nDigite o numero que sera excluido"); | |
imprimirN2(aux1); | |
printf("\n Escolha um dos numerais"); | |
n = getint(); | |
aux2=buscarN2(n ,aux1); | |
if(!aux2->prox3A) | |
printf("\nNao existem elementos no nivel 3A vinculados a esse elemento"); | |
else | |
imprimirN3A(aux2); | |
} | |
break; | |
case 'b': printf("\nEscolha um elemento no nivel 1 para desvincular o elemento que será excluido"); | |
imprimirN1(); | |
printf("\nDigite somente o numeral"); | |
n = getint(); | |
aux1 = buscarN1(n); | |
if (aux1){ | |
printf("\nEscolha um elemento no nivel 2 para desvincular o elemento que será excluido"); | |
imprimirN2(aux1); | |
printf("\n Escolha um dos numerais"); | |
n = getint(); | |
aux2=buscarN2(n ,aux1); | |
if(!aux2->prox3B) | |
printf("\nNao existem elementos no nivel 3B vinculados a esse elemento"); | |
else | |
imprimirN3A(aux2); | |
} break; | |
} | |
}while(ops != 'a' && ops != 'b'); | |
} | |
}while(op != '1' && op != '2' && op != '3'); | |
} | |
//------------------------------------------------------------------------------ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//--------------------------TRABALHO-PROF-SÉRGIO-------------------------------- | |
//--------------------------------PILHAS---------------------------------------- | |
//-LEONARDO-ALBERTO-SOUZA----------------------------------RAFAEL-MOURA-MARQUES- | |
//-BIBLIOTECAS------------------------------------------------------------------ | |
# include <stdio.h> | |
# include <stdlib.h> | |
# include <conio.h> | |
//-ESTRUTURAS------------------------------------------------------------------- | |
struct EMatriz{ | |
int lin; | |
int col; | |
int **elem; | |
}; | |
struct EPilhaM{ | |
int topo; | |
struct EMatriz *p; | |
int tam; | |
}; | |
//-PROTÓTIPO-DAS-FUNÇÕES-------------------------------------------------------- | |
void imprimir(void); | |
void zerarMatriz(int x); | |
void menu(void); | |
int leOpcao(void); | |
void iniciar(void); | |
void dimensionarMat(int a,int b,int c); | |
void inserirElem(void); | |
void lerMatriz(int x); | |
void imprimirTopo(int x); | |
void multTopo(void); | |
void excluirElem(void); | |
int getint(void); | |
int definirMat(void); | |
int definirTam(void); | |
struct EMatriz *alocaVet(int n); | |
int **alocaMat2D(int Lin,int Col); | |
//-ESTRUTURA-PRINCIPAL---------------------------------------------------------- | |
struct EPilhaM pilha[3]; | |
//-MAIN------------------------------------------------------------------------- | |
main(){ | |
iniciar(); | |
menu(); | |
} | |
//-FUNÇÕES---------------------------------------------------------------------- | |
//-LEITURA-DA-OPÇÃO-DIGITADA-PELO-USUÁRIO--------------------------------------- | |
int leOpcao(void){ | |
int ch; | |
do{ | |
clrscr(); | |
printf("PROGRAMA PILHAS DE MATRIZES - Menu Principal\n\n"); | |
printf(">Use as pilhas 1 e 2 para armazenar dados.\n"); | |
printf(">A pilha 0 eh a pilha resultante quarda a multiplicacao do topo das duas pilhas.\n\n"); | |
printf(" 1. Inserir matriz em uma pilha\n"); | |
printf(" 2. Apagar o topo de uma pilha\n"); | |
printf(" 3. Imprimir o topo de uma pilha\n"); | |
printf(" 4. Multiplicar Topo das pilhas\n"); | |
printf(" 5. Sair do programa\n\n"); | |
printf("Digite sua opcao: "); | |
scanf("%d",&ch); | |
}while ((ch < 1) || (ch > 5)); | |
return ch; | |
} | |
//-MENU-PRINCIPAL--------------------------------------------------------------- | |
void menu(void){ | |
int op; | |
for(;;){ | |
op = leOpcao(); | |
switch(op){ | |
case 1: inserirElem(); | |
break; | |
case 2: excluirElem(); | |
break; | |
case 3: imprimir(); | |
break; | |
case 4: multTopo(); | |
break; | |
case 5: exit(0); | |
} | |
} | |
} | |
//-INICIAR-AS-ESTRUTURAS-[CHAMA-AS-FUNÇÕES-PARA-ALOCAÇÃO-DOS-PONTEIROS]--------- | |
void iniciar(void){ | |
int i,j,t,w; | |
printf("PROGRAMA PILHAS DE MATRIZES - Configuracoes Iniciais\n\n"); | |
t = definirTam(); | |
w = definirMat(); | |
for (i=0;i<3;i++){ | |
pilha[i].topo = -1; | |
pilha[i].tam = t; | |
pilha[i].p = alocaVet(t); | |
for (j=0;j<t;j++) | |
pilha[i].p[j].lin = pilha[i].p[j].col = w; | |
dimensionarMat(i,t,w); | |
} | |
} | |
//-PERGUNTA-AO-USUÁRIO-O-TAMANHO-DAS-MATRIZES-CONTIDAS-NAS-PILHAS-[N x N]------- | |
int definirMat(void){ | |
int n; | |
printf("\nDefina o tamanho das matrizes contidas nas pilhas: "); | |
n = getint(); | |
return n; | |
} | |
//-ALOCA-AS-MATRIZES------------------------------------------------------------ | |
void dimensionarMat(int a,int b,int c){ | |
int i; | |
for (i=0;i<b;i++) | |
pilha[a].p[i].elem = alocaMat2D(c,c); | |
} | |
//-INSERE-UM-ELEMENTO-NA-PILHA-EM-QUE-O-USUÁRIO-ESCOLHER------------------------ | |
void inserirElem(void){ | |
int op; | |
printf("\nDigite em que pilha deseja inserir o elemento <1/2> : "); | |
do{ | |
op = getint(); | |
if ((op != 0)&&(pilha[op].topo<pilha[op].tam-1)){ | |
pilha[op].topo++; | |
lerMatriz(op); | |
} | |
if(op==0) | |
printf("\nPROIBIDO INSERIR ELEMENTOS NA PILHA RESULTANTE!!!"); | |
if ((pilha[op].topo>=pilha[op].tam-1)&&((op==1)||(op==2))) | |
printf("\nA PILHA %d ESTA CHEIA!!!",op); | |
if (((op<1)||(op>2))&&(op!=0)) | |
printf("\nA PILHA %d NAO EXISTE!!!",op); | |
getch(); | |
}while((op<1)&&(op>=pilha[op].tam)); | |
} | |
//-FAZ-A-LEITURA-DA-MATRIZ------------------------------------------------------ | |
void lerMatriz(int x){ | |
int i,j; | |
printf("\nPreencha a matriz %d da pilha %d!\n",pilha[x].topo+1,x); | |
for (i=0;i<pilha[x].p[pilha[x].topo].lin;i++){ | |
for (j=0;j<pilha[x].p[pilha[x].topo].col;j++){ | |
printf("Digite o elemento [%d][%d]: ",i,j); | |
pilha[x].p[pilha[x].topo].elem[i][j] = getint(); | |
printf("\n"); | |
} | |
} | |
} | |
//-PERGUNTA-AO-USUÁRIO-QUAL-PILHA-ELE-DESEJA-IMPRIMIR--------------------------- | |
void imprimir(void){ | |
int n; | |
printf("\nDiga o topo de qual matriz vc deseja imprimir <0..2>: "); | |
scanf("%d",&n); | |
if (((n<1)||(n>2))&&(n!=0)) | |
printf("\nA PILHA %d NAO EXISTE!!!",n); | |
if(pilha[n].topo==-1) | |
printf("\nA PILHA %d ESTA VAZIA!!!",n); | |
else | |
imprimirTopo(n); | |
getch(); | |
} | |
//-IMPRIME-O-TOPO-DA-PILHA-ESCOLHIDA-PELO-USUÁRIO------------------------------- | |
void imprimirTopo(int x){ | |
int i,j; | |
printf("\n\n"); | |
for (i=0;i<pilha[x].p[pilha[x].topo].lin;i++){ | |
for (j=0;j<pilha[x].p[pilha[x].topo].col;j++){ | |
printf("%d\t ",pilha[x].p[pilha[x].topo].elem[i][j]); | |
} | |
printf("\n\n"); | |
} | |
} | |
//-MULTIPLICA-O-TOPO-DAS-PILHAS------------------------------------------------- | |
void multTopo(void){ | |
int i,j,k; | |
if ((pilha[1].topo==-1)&&(pilha[2].topo==-1)) | |
printf("\nUMA(OU AMBAS) DAS PILHAS ESTA VAZIA!!!"); | |
if((pilha[0].topo<pilha[0].tam)&&(pilha[1].topo!=-1)&&(pilha[2].topo!=-1)){ | |
pilha[0].topo++; | |
zerarMatriz(0); | |
for (i=0;i<pilha[1].p[pilha[1].topo].lin;i++) | |
for (j=0;j<pilha[2].p[pilha[2].topo].col;j++) | |
for (k=0;k<pilha[1].p[pilha[1].topo].col;k++) | |
pilha[0].p[pilha[0].topo].elem[i][j]+=pilha[1].p[pilha[1].topo].elem[i][k]*pilha[2].p[pilha[2].topo].elem[k][j]; | |
imprimirTopo(0); | |
printf("\n"); | |
} | |
if(pilha[0].topo>=pilha[0].tam){ | |
printf("\nA PILHA RESULTANTE ESTA CHEIA!!! "); | |
} | |
getch(); | |
} | |
//-EXCLUI-UM-ELEMENTO-NA-PILHA-EM-QUE-O-USUÁRIO-ESCOLHER------------------------ | |
void excluirElem(void){ | |
int op; | |
printf("\nDigite de qual pilha deseja apagar o topo: "); | |
do{ | |
op = getint(); | |
if (pilha[op].topo>-1) | |
pilha[op].topo--; | |
if (pilha[op].topo==-1) | |
printf("\nA PILHA %d ESTA VAZIA!!!"); | |
else | |
printf("\nA pilha %d ainda possui %d matrizes!",op,pilha[op].topo+1); | |
getch(); | |
}while ((op<1)&&(op >=pilha[op].tam)); | |
} | |
//-FUNÇÃO-AUXILIAR-PARA-LEITURA-DE-UM-INTEIRO----------------------------------- | |
int getint(void){ | |
int n; | |
scanf("%d",&n); | |
return n; | |
} | |
//-DEFINE-O-NÚMERO-DE-MATRIZES-NAS-PILHAS--------------------------------------- | |
int definirTam(void){ | |
int n; | |
printf("Defina o numero de matrizes em cada pilha : "); | |
n = getint(); | |
return n; | |
} | |
//-FUNÇÃO-DE-ALOCAÇÃO-[CRIA-UM-VETOR-DE-MATRIZES]------------------------------- | |
struct EMatriz *alocaVet(int n){ | |
struct EMatriz *v; | |
v = (struct EMatriz*)malloc(sizeof(struct EMatriz)*n); | |
if(!v) | |
return NULL; | |
return v; | |
} | |
//-FUNÇÃO-DE-ALOCAÇÃO-DE-MATRIZES----------------------------------------------- | |
int **alocaMat2D(int Lin,int Col){ | |
int i,**m; | |
m = (int**)malloc(sizeof(int)*Lin); | |
if(!m) | |
return NULL; | |
for(i=0;i<Lin;i++){ | |
m[i] = (int*)malloc(sizeof(int)*Col); | |
if(!m[i]) | |
return NULL; | |
} | |
return m; | |
} | |
//------------------------------------------------------------------------------ | |
void zerarMatriz(int x){ | |
int i,j; | |
for(i=0;i<pilha[x].p[pilha[x].topo].lin;i++) | |
for (j=0;j<pilha[x].p[pilha[x].topo].col;j++) | |
pilha[x].p[pilha[x].topo].elem[i][j]=0; | |
} | |
//------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment