Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <time.h>
- #include "TADPilha.h"
- #include <conio.h>
- #define TFS 10
- void ImprimePilha(TpPilha P)
- {
- while(!PilhaVazia(P.Topo))
- printf("%d\n", Desempilha(P));
- }
- void ConcatenaPilha(TpPilha &P1, TpPilha &P2)
- {
- //Exercício 1 Pilhas
- TpPilha Aux;
- InicializaPilha(Aux);
- while(!PilhaVazia(P2.Topo))
- Empilha(Aux, Desempilha(P2));
- while(!PilhaVazia(Aux.Topo))
- Empilha(P1, Desempilha(Aux));
- }
- void ExclusaoPilha(TpPilha &P, int N)
- {
- //Exercício 2 Pilhas
- TpPilha Aux;
- int R;
- while(!PilhaVazia(P.Topo) && ElementoTopo(P) != N)
- Empilha(Aux, Desempilha(P));
- if(!PilhaVazia(P.Topo))
- R = Desempilha(P);
- while(!PilhaVazia(Aux.Topo))
- Empilha(P, Desempilha(Aux));
- }
- void SimulacaoPilhas(int n)
- {
- int i, op, num, pos, numComandos;
- TpPilha Vet[TFS];
- system("cls");
- printf("\n\nSimulação de Pilhas\n\n");
- for(i = 0; i < n; i++)
- InicializaPilha(Vet[i]);
- numComandos = rand() % 100 + 1;
- printf("Serao realizadas %d operacoes\n\n", numComandos);
- for(i = 0; i < numComandos; i++)
- {
- pos = rand() % n;
- printf("Pilha escolhida: %d\n", pos);
- op = rand() % 2;
- if(PilhaVazia(Vet[pos].Topo))
- op = 0;
- if(op == 0)
- {
- num = rand() % 100 + 1;
- printf("Inserir o numero %d\n", num);
- Empilha(Vet[pos], num);
- }
- else
- {
- printf("Remover o elemento do topo\n");
- Desempilha(Vet[pos]);
- }
- printf("\n\n");
- }
- printf("\n\nProcesso Concluido\n\n");
- for(i = 0; i < n; i++)
- {
- printf("PILHA %d\n\n", i + 1);
- ImprimePilha(Vet[i]);
- printf("\n\n");
- }
- getch();
- }
- int ResultadoPosfixa(char Exp[100])
- {
- //Exercício 3 pilhas (resolver expressão posfixa)
- TpPilha P;
- int i, n1, n2;
- InicializaPilha(P);
- for(i = 0; i < strlen(Exp); i++)
- if(Exp[i] >= 48 && Exp[i] <= 57)
- Empilha(P, Exp[i] - 48);
- else
- {
- if(Exp[i] == '+')
- Empilha(P, Desempilha(P) + Desempilha(P));
- else if(Exp[i] == '*')
- Empilha(P, Desempilha(P) * Desempilha(P));
- else if(Exp[i] == '-')
- {
- n1 = Desempilha(P);
- n2 = Desempilha(P);
- Empilha(P, n2 - n1);
- }
- else
- {
- n1 = Desempilha(P);
- n2 = Desempilha(P);
- Empilha(P, n2 / n1);
- }
- }
- return Desempilha(P);
- }
- int InverteVet(int Vet[], int TL)
- {
- //Exercício 6
- TpPilha P;
- int i;
- InicializaPilha(P);
- for(i = 0; i < TL; i++)
- Empilha(P, Vet[i]);
- printf("Vetor invertido: \n");
- while(!PilhaVazia(P.Topo))
- printf("%d ", Desempilha(P));
- printf("\n");
- }
- void InverteArquivo(char NomeArq[101])
- {
- FILE *PtrTxt;
- char L;
- TpPilha P;
- system("cls");
- printf("\n\nInverter Arquivo\n\n");
- PtrTxt = fopen(NomeArq, "r");
- if(PtrTxt == NULL)
- printf("Arquivo %s inexistente\n\n", NomeArq);
- else
- {
- FILE *PtrTemp = fopen("Temp.txt", "ab");
- L = toupper(fgetc(PtrTxt));
- while(!feof(PtrTxt))
- {
- Empilha(P, L);
- L = toupper(fgetc(PtrTxt));
- }
- fclose(PtrTxt);
- while(!PilhaVazia(P.Topo))
- fputc(Desempilha(P), PtrTemp);
- fclose(PtrTemp);
- remove(NomeArq);
- rename("Temp.txt", NomeArq);
- printf("Processo concluido\n");
- }
- getch();
- }
- int Palindromo(char S[100])
- {
- //Exercício 8 Pilhas
- TpPilha P;
- int i;
- InicializaPilha(P);
- for(i = 0; i < strlen(S); i++)
- Empilha(P, S[i]);
- i = 0;
- while(i < strlen(S) && S[i] == Desempilha(P))
- i++;
- if(i == strlen(S))
- return 1;
- else
- return 0;
- }
- void InvertePalavras(char S[100])
- {
- //Exercício 9 Pilhas
- TpPilha P;
- int i, j, TL;
- char Aux[100];
- InicializaPilha(P);
- TL = 0;
- strcpy(Aux, "\0");
- for(i = 0; i <= strlen(S); i++)
- if(toupper(S[i]) >= 65 && toupper(S[i]) <= 90)
- Aux[TL++] = S[i];
- else
- {
- for(j = TL - 1; j >= 0; j--)
- Empilha(P, Aux[j]);
- TL = 0;
- strcpy(Aux, "\0");
- if(S[i] != '\0')
- Empilha(P, S[i]);
- }
- while(!PilhaVazia(P.Topo))
- printf("%c ", Desempilha(P));
- printf("\n");
- }
- int BuscaPilha(TpPilha P, int Chave)
- {
- int pos = P.Topo;
- while(!PilhaVazia(P.Topo) && Desempilha(P) != Chave)
- pos--;
- if(PilhaVazia(P.Topo))
- return -1;
- else
- return 0;
- }
- char Menu(void)
- {
- system("cls");
- printf("\n\n\t\t****Menu*****\n\n");
- printf("[A] - Inserir P1\n");
- printf("[B] - Retirar P1\n");
- printf("[C] - Inserir P2\n");
- printf("[D] - Retirar P2\n");
- printf("[E] - Exibir pilha 1\n");
- printf("[F] - Exibir pilha 2\n");
- printf("[G] - Concatenar pilhas\n");
- printf("[H] - Excluir elemento (pilha 1)\n");
- printf("[I] - Excluir elemento (pilha 2)\n");
- printf("[ESC] - Sair\n");
- return toupper(getche());
- }
- int main(void)
- {
- char Op;
- int n;
- TpPilha P1, P2;
- InicializaPilha(P1);
- InicializaPilha(P2);
- srand(time(NULL));
- Op = Menu();
- while(Op != 27)
- {
- printf("\n");
- switch(Op)
- {
- case 'A':
- if(PilhaCheia(P1.Topo))
- printf("Pilha cheia");
- else
- {
- do
- {
- printf("Digite o valor:\n");
- scanf("%d", &n);
- }while(n <= 0);
- Empilha(P1, n);
- printf("Valor inserido\n");
- }
- getch();
- break;
- case 'B':
- if(PilhaVazia(P1.Topo))
- printf("Pilha vazia");
- else
- {
- n = Desempilha(P1);
- printf("Valor retirado (%d)\n", n);
- }
- getch();
- break;
- case 'C':
- if(PilhaCheia(P2.Topo))
- printf("Pilha cheia");
- else
- {
- do
- {
- printf("Digite o valor:\n");
- scanf("%d", &n);
- }while(n <= 0);
- Empilha(P2, n);
- printf("Valor inserido\n");
- }
- getch();
- break;
- case 'D':
- if(PilhaVazia(P2.Topo))
- printf("Pilha vazia");
- else
- {
- n = Desempilha(P2);
- printf("Valor retirado (%d)\n", n);
- }
- getch();
- break;
- case 'E':
- ImprimePilha(P1);
- getch();
- break;
- case 'F':
- ImprimePilha(P2);
- getch();
- break;
- case 'G':
- ConcatenaPilha(P1, P2);
- printf("Pilhas concatenadas\n");
- getch();
- break;
- case 'H':
- printf("Digite o elemento:\n");
- scanf("%d", &n);
- if(BuscaPilha(P1, n) == -1)
- printf("Elemento inexistente\n");
- else
- {
- ExclusaoPilha(P1, n);
- printf("Elemeno excluido\n\n");
- }
- getch();
- break;
- case 'I':
- printf("Digite o elemento:\n");
- scanf("%d", &n);
- if(BuscaPilha(P2, n) == -1)
- printf("Elemento inexistente\n");
- else
- {
- ExclusaoPilha(P2, n);
- printf("Elemeno excluido\n\n");
- }
- getch();
- break;
- default:
- printf("Opcao invalida\n\n");
- getch();
- }
- Op = Menu();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement