Advertisement
Jvsierra

main pilha (exs)

Mar 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include "TADPilha.h"
  7. #include <conio.h>
  8.  
  9. #define TFS 10
  10.  
  11. void ImprimePilha(TpPilha P)
  12. {
  13.     while(!PilhaVazia(P.Topo))
  14.         printf("%d\n", Desempilha(P));
  15. }
  16.  
  17. void ConcatenaPilha(TpPilha &P1, TpPilha &P2)
  18. {
  19.     //Exercício 1 Pilhas
  20.     TpPilha Aux;
  21.    
  22.     InicializaPilha(Aux);
  23.    
  24.     while(!PilhaVazia(P2.Topo))
  25.         Empilha(Aux, Desempilha(P2));
  26.        
  27.     while(!PilhaVazia(Aux.Topo))
  28.         Empilha(P1, Desempilha(Aux));
  29. }
  30.  
  31. void ExclusaoPilha(TpPilha &P, int N)
  32. {
  33.     //Exercício 2 Pilhas
  34.     TpPilha Aux;
  35.     int R;
  36.    
  37.     while(!PilhaVazia(P.Topo) && ElementoTopo(P) != N)
  38.         Empilha(Aux, Desempilha(P));
  39.        
  40.     if(!PilhaVazia(P.Topo))
  41.         R = Desempilha(P);
  42.        
  43.     while(!PilhaVazia(Aux.Topo))
  44.             Empilha(P, Desempilha(Aux));
  45. }
  46.  
  47. void SimulacaoPilhas(int n)
  48. {
  49.     int i, op, num, pos, numComandos;
  50.     TpPilha Vet[TFS];
  51.     system("cls");
  52.    
  53.     printf("\n\nSimulação de Pilhas\n\n");
  54.    
  55.     for(i = 0; i < n; i++)
  56.         InicializaPilha(Vet[i]);
  57.        
  58.     numComandos = rand() % 100 + 1;
  59.    
  60.     printf("Serao realizadas %d operacoes\n\n", numComandos);
  61.    
  62.     for(i = 0; i < numComandos; i++)
  63.     {
  64.         pos = rand() % n;
  65.        
  66.         printf("Pilha escolhida: %d\n", pos);
  67.        
  68.         op = rand() % 2;
  69.        
  70.         if(PilhaVazia(Vet[pos].Topo))
  71.             op = 0;
  72.            
  73.         if(op == 0)
  74.         {
  75.             num = rand() % 100 + 1;
  76.             printf("Inserir o numero %d\n", num);
  77.             Empilha(Vet[pos], num);
  78.         }
  79.         else
  80.         {
  81.             printf("Remover o elemento do topo\n");
  82.             Desempilha(Vet[pos]);
  83.         }
  84.        
  85.         printf("\n\n");
  86.     }  
  87.    
  88.     printf("\n\nProcesso Concluido\n\n");
  89.    
  90.     for(i = 0; i < n; i++)
  91.     {
  92.         printf("PILHA %d\n\n", i + 1);
  93.         ImprimePilha(Vet[i]);
  94.         printf("\n\n");
  95.     }
  96.        
  97.     getch();
  98. }
  99.  
  100. int ResultadoPosfixa(char Exp[100])
  101. {
  102.     //Exercício 3 pilhas (resolver expressão posfixa)
  103.     TpPilha P;
  104.     int i, n1, n2;
  105.    
  106.     InicializaPilha(P);
  107.    
  108.     for(i = 0; i < strlen(Exp); i++)
  109.         if(Exp[i] >= 48 && Exp[i] <= 57)
  110.             Empilha(P, Exp[i] - 48);
  111.         else
  112.         {
  113.             if(Exp[i] == '+')
  114.                 Empilha(P, Desempilha(P) + Desempilha(P));
  115.             else if(Exp[i] == '*')
  116.                 Empilha(P, Desempilha(P) * Desempilha(P));
  117.             else if(Exp[i] == '-')
  118.             {
  119.                 n1 = Desempilha(P);
  120.                 n2 = Desempilha(P);
  121.                 Empilha(P, n2 - n1);
  122.             }
  123.             else
  124.             {
  125.                 n1 = Desempilha(P);
  126.                 n2 = Desempilha(P);
  127.                 Empilha(P, n2 / n1);
  128.             }
  129.         }
  130.            
  131.     return Desempilha(P);
  132. }
  133.  
  134. int InverteVet(int Vet[], int TL)
  135. {
  136.     //Exercício 6
  137.     TpPilha P;
  138.     int i;
  139.    
  140.     InicializaPilha(P);
  141.    
  142.     for(i = 0; i < TL; i++)
  143.         Empilha(P, Vet[i]);
  144.  
  145.     printf("Vetor invertido: \n");
  146.    
  147.     while(!PilhaVazia(P.Topo))
  148.         printf("%d ", Desempilha(P));
  149.  
  150.     printf("\n");
  151. }
  152.  
  153. void InverteArquivo(char NomeArq[101])
  154. {
  155.     FILE *PtrTxt;
  156.     char L;
  157.     TpPilha P;
  158.    
  159.     system("cls");
  160.    
  161.     printf("\n\nInverter Arquivo\n\n");
  162.    
  163.     PtrTxt = fopen(NomeArq, "r");
  164.    
  165.     if(PtrTxt == NULL)
  166.         printf("Arquivo %s inexistente\n\n", NomeArq);
  167.     else
  168.     {
  169.         FILE *PtrTemp = fopen("Temp.txt", "ab");
  170.        
  171.         L = toupper(fgetc(PtrTxt));
  172.        
  173.         while(!feof(PtrTxt))
  174.         {
  175.             Empilha(P, L);
  176.            
  177.             L = toupper(fgetc(PtrTxt));
  178.         }
  179.        
  180.         fclose(PtrTxt);
  181.        
  182.         while(!PilhaVazia(P.Topo))
  183.             fputc(Desempilha(P), PtrTemp);
  184.            
  185.         fclose(PtrTemp);
  186.         remove(NomeArq);
  187.        
  188.         rename("Temp.txt", NomeArq);
  189.        
  190.         printf("Processo concluido\n");
  191.     }
  192.    
  193.     getch();
  194. }
  195.  
  196. int Palindromo(char S[100])
  197. {
  198.     //Exercício 8 Pilhas
  199.     TpPilha P;
  200.     int i;
  201.    
  202.     InicializaPilha(P);
  203.    
  204.     for(i = 0; i < strlen(S); i++)
  205.         Empilha(P, S[i]);
  206.        
  207.     i = 0;
  208.    
  209.     while(i < strlen(S) && S[i] == Desempilha(P))
  210.         i++;
  211.        
  212.     if(i == strlen(S))
  213.         return 1;
  214.     else
  215.         return 0;
  216. }
  217.  
  218. void InvertePalavras(char S[100])
  219. {
  220.     //Exercício 9 Pilhas
  221.     TpPilha P;
  222.     int i, j, TL;
  223.     char Aux[100];
  224.    
  225.     InicializaPilha(P);
  226.    
  227.     TL = 0;
  228.    
  229.     strcpy(Aux, "\0");
  230.    
  231.     for(i = 0; i <= strlen(S); i++)
  232.         if(toupper(S[i]) >= 65 && toupper(S[i]) <= 90)
  233.             Aux[TL++] = S[i];
  234.         else
  235.         {
  236.             for(j = TL - 1; j >= 0; j--)
  237.                 Empilha(P, Aux[j]);
  238.                
  239.             TL = 0;
  240.             strcpy(Aux, "\0");
  241.            
  242.             if(S[i] != '\0')
  243.                 Empilha(P, S[i]);
  244.         }
  245.        
  246.     while(!PilhaVazia(P.Topo))
  247.         printf("%c ", Desempilha(P));
  248.    
  249.     printf("\n");
  250. }
  251.  
  252. int BuscaPilha(TpPilha P, int Chave)
  253. {
  254.     int pos = P.Topo;
  255.    
  256.     while(!PilhaVazia(P.Topo) && Desempilha(P) != Chave)
  257.         pos--;
  258.        
  259.     if(PilhaVazia(P.Topo))
  260.         return -1;
  261.     else
  262.         return 0;
  263. }
  264.  
  265. char Menu(void)
  266. {
  267.     system("cls");
  268.    
  269.     printf("\n\n\t\t****Menu*****\n\n");
  270.     printf("[A] - Inserir P1\n");
  271.     printf("[B] - Retirar P1\n");
  272.     printf("[C] - Inserir P2\n");
  273.     printf("[D] - Retirar P2\n");
  274.     printf("[E] - Exibir pilha 1\n");
  275.     printf("[F] - Exibir pilha 2\n");
  276.     printf("[G] - Concatenar pilhas\n");
  277.     printf("[H] - Excluir elemento (pilha 1)\n");
  278.     printf("[I] - Excluir elemento (pilha 2)\n");
  279.     printf("[ESC] - Sair\n");
  280.  
  281.     return toupper(getche());
  282. }
  283.  
  284. int main(void)
  285. {
  286.     char Op;
  287.     int n;
  288.     TpPilha P1, P2;
  289.    
  290.     InicializaPilha(P1);
  291.     InicializaPilha(P2);
  292.     srand(time(NULL));
  293.    
  294.     Op = Menu();
  295.    
  296.     while(Op != 27)
  297.     {
  298.         printf("\n");
  299.         switch(Op)
  300.         {
  301.             case 'A':
  302.                 if(PilhaCheia(P1.Topo))
  303.                     printf("Pilha cheia");
  304.                 else
  305.                 {
  306.                     do
  307.                     {
  308.                         printf("Digite o valor:\n");
  309.                         scanf("%d", &n);
  310.                     }while(n <= 0);
  311.                    
  312.                     Empilha(P1, n);
  313.                    
  314.                     printf("Valor inserido\n");
  315.                 }
  316.                
  317.                 getch();
  318.             break;
  319.             case 'B':
  320.                 if(PilhaVazia(P1.Topo))
  321.                     printf("Pilha vazia");
  322.                 else
  323.                 {
  324.                     n = Desempilha(P1);
  325.                     printf("Valor retirado (%d)\n", n);
  326.                 }
  327.                
  328.                 getch();
  329.             break;
  330.             case 'C':
  331.                 if(PilhaCheia(P2.Topo))
  332.                     printf("Pilha cheia");
  333.                 else
  334.                 {
  335.                     do
  336.                     {
  337.                         printf("Digite o valor:\n");
  338.                         scanf("%d", &n);
  339.                     }while(n <= 0);
  340.                    
  341.                     Empilha(P2, n);
  342.                    
  343.                     printf("Valor inserido\n");
  344.                 }
  345.                
  346.                 getch();
  347.             break;
  348.             case 'D':
  349.                 if(PilhaVazia(P2.Topo))
  350.                     printf("Pilha vazia");
  351.                 else
  352.                 {
  353.                     n = Desempilha(P2);
  354.                     printf("Valor retirado (%d)\n", n);
  355.                 }
  356.                
  357.                 getch();
  358.             break;
  359.             case 'E':
  360.                 ImprimePilha(P1);
  361.                 getch();
  362.             break;
  363.             case 'F':
  364.                 ImprimePilha(P2);
  365.                 getch();
  366.             break;
  367.             case 'G':
  368.                 ConcatenaPilha(P1, P2);
  369.                
  370.                 printf("Pilhas concatenadas\n");
  371.                
  372.                 getch();
  373.             break;
  374.             case 'H':
  375.                 printf("Digite o elemento:\n");
  376.                 scanf("%d", &n);
  377.                
  378.                 if(BuscaPilha(P1, n) == -1)
  379.                     printf("Elemento inexistente\n");
  380.                 else
  381.                 {
  382.                     ExclusaoPilha(P1, n);
  383.                     printf("Elemeno excluido\n\n");
  384.                 }
  385.                
  386.                 getch();
  387.             break;
  388.             case 'I':
  389.                 printf("Digite o elemento:\n");
  390.                 scanf("%d", &n);
  391.                
  392.                 if(BuscaPilha(P2, n) == -1)
  393.                     printf("Elemento inexistente\n");
  394.                 else
  395.                 {
  396.                     ExclusaoPilha(P2, n);
  397.                     printf("Elemeno excluido\n\n");
  398.                 }
  399.                
  400.                 getch();
  401.             break;
  402.             default:
  403.                 printf("Opcao invalida\n\n");
  404.                 getch();
  405.         }
  406.        
  407.         Op = Menu();
  408.     }
  409.    
  410.     return 0;
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement