Advertisement
progamer_denys

Figurinhas-final

Dec 11th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. //VARIAVEIS GLOBAIS
  7.  
  8. int tampilha;
  9.  
  10. struct figuras
  11. {
  12.     char nome[100];
  13.     char idade[5];
  14.     char selecao[100];
  15. };
  16. typedef struct elemento* Pilha;
  17.  
  18. struct elemento
  19. {
  20.     struct figuras dados;
  21.     struct elemento *prox;
  22. };
  23. typedef struct elemento Elem;
  24. //FUNCOES
  25. Pilha* cria_Pilha();
  26. int Pilha_vazia(Pilha* pi);
  27. int insere_Pilha(Pilha* pi, struct figuras jog, int *p);
  28. int tamanho_Pilha(Pilha *pi);
  29. void imprime_Pilha(Pilha* pi);
  30. bool Pilha_cheia(Pilha* pi);
  31. bool remove_Pilha(Pilha* pi, int *p);
  32. void libera_Pilha(Pilha* pi);
  33. void quebra(char dados[], struct figuras *jog);
  34.  
  35. void menu()
  36. {
  37.     fflush(stdin);
  38.     printf("Digite qualquer tecla para continuar\n");
  39.     getchar();
  40.     fflush(stdin);
  41.     system("cls");
  42.     printf("Digite uma opcao\n");
  43.     printf("1 - Criar Pilha Dinamicamente\n2 - Inserir Elementos\n3 - Remover Elementos\n4 - Imprimir Elementos\n5 - Checar Pilha vazia\n6 - Checar Pilha Cheia\n7 - Destruir Pilha\n8 - Sair\n");
  44.  
  45. }
  46.  
  47. int main()
  48. {
  49.  
  50.     struct figuras jog;
  51.     Pilha *pi;
  52.     int tam_Pilha;
  53.     int controlador = 0;
  54.     int *p = &controlador;
  55.     controlador = 0;
  56.     int op;
  57.     do{
  58.     menu();
  59.     scanf("%d", &op);
  60.     if(op == 1)//CRIAR
  61.     {
  62.         system("cls");
  63.         printf("Digite o Tamanho da sua Pilha\n");
  64.         scanf("%d", &tampilha);
  65.         pi = cria_Pilha();
  66.     }else
  67.         if(op == 2)//PUSh (INSERIR)
  68.         {
  69.             system("cls");
  70.             fflush(stdin);
  71.  
  72.             insere_Pilha(pi, jog, p);
  73.  
  74.  
  75.         }else
  76.             if(op == 3)//POP(remover1)
  77.             {
  78.                 system("cls");
  79.                 if(Pilha_vazia(pi))
  80.                 {
  81.                     printf("Erro na remocao: Pilha vazia! \n");
  82.                 }else{
  83.                     remove_Pilha(pi, p);
  84.                     printf("Dados removidos com sucesso\n");
  85.                 }
  86.  
  87.  
  88.             }else
  89.                 if(op == 4)//IMRPIMIR
  90.                 {
  91.                         system("cls");
  92.                         imprime_Pilha(pi);
  93.  
  94.                 }else
  95.                     if(op == 5)//Checar pilha vazia
  96.                     {
  97.                         system("cls");
  98.                         if(Pilha_vazia(pi))
  99.                         {
  100.                             printf("\t---A Pilha vazia---\n");
  101.                         }else{
  102.                             printf("\t---A pilha nao esta vazia---\n");
  103.                             tam_Pilha = tamanho_Pilha(pi);
  104.                             printf("Possui %d elementos\n", tam_Pilha);
  105.  
  106.                         }
  107.  
  108.  
  109.                     }else
  110.                         if(op == 6)//CHECAR PILHA CHEIA
  111.                         {
  112.                             system("cls");
  113.                             if(Pilha_cheia(pi))
  114.                             {
  115.                                 printf("A Pilha esta cheia\n");
  116.                             }else{
  117.                                 printf("Ainda restam %d nos\n", tampilha - *p);
  118.                             }
  119.  
  120.  
  121.                         }else
  122.                             if(op == 7)
  123.                             {//DESTRUIR PILHA
  124.                                 system("cls");
  125.  
  126.                                 libera_Pilha(pi);
  127.                                 printf("Memoria alocada liberada\n");
  128.                             }else{
  129.                                 system("cls");
  130.                                 printf("programa encerrado\n");
  131.                                 exit(0);
  132.  
  133.                             }
  134.  
  135.  }while(op != 8);
  136.  
  137.     return 0;
  138. }
  139.  
  140. Pilha* cria_Pilha()
  141. {
  142.     Pilha* pi = (Pilha*) malloc(sizeof(Pilha));
  143.     if(pi != NULL){
  144.         *pi = NULL;
  145.     }
  146.  
  147.     return pi;
  148. }
  149. //PILHA VAZIA
  150. int Pilha_vazia(Pilha* pi)
  151. {
  152.     if(pi == NULL)
  153.         return 1;
  154.     if(*pi == NULL)
  155.         return 1;
  156.     return 0;
  157. }
  158. //PSUH
  159. int insere_Pilha(Pilha* pi, struct figuras jog, int *p)
  160. {
  161.     system("cls");
  162.     char info[200];
  163.     if(*p == tampilha)
  164.     {
  165.        printf("Tamanho maximo atingido\n");
  166.        return 0;
  167.     }
  168.     else{
  169.         printf("Digite as informacoes\n Ao digitar 'stop' o programa sera encerrado\n");
  170.         gets(info);
  171.         if(strcmp(info, "stop")== 0)
  172.         {
  173.             printf("Acabou\n");
  174.             exit(0);
  175.         }
  176.         quebra(info, &jog);//FUNCAO QUBRA STRING
  177.         if(pi == NULL)
  178.         return 0;
  179.         Elem* no;
  180.         no = (Elem*) malloc(sizeof(Elem));
  181.         if(no == NULL)
  182.         return 0;
  183.         no->dados = jog;
  184.         no->prox = (*pi);
  185.         *pi = no;
  186.         ++*p;
  187.         return 1;
  188.     }
  189.  
  190. }
  191.  
  192. //TAMANHO
  193. int tamanho_Pilha(Pilha *pi)
  194. {
  195.     if(pi == NULL)
  196.         return 0;
  197.     int cont = 0;
  198.     Elem* no = *pi;
  199.     while(no != NULL){
  200.         cont++;
  201.         no = no->prox;
  202.     }
  203.     return cont;
  204. }
  205.  
  206.  
  207. //QUEBRAR STRING
  208. void quebra(char dados[], struct figuras *jog)
  209. {
  210.     char nome[50], idade[100], selecao[100];
  211.     int i, pos = 0, j;
  212.     for(i=0; dados[i] != ' '; i++)//quebrando nome
  213.     {
  214.         nome[i] = dados[i];
  215.         pos = i;
  216.  
  217.     }
  218.     nome[i] = '\0';
  219.     strcpy(jog->nome, nome);
  220.     for(i=pos+2, j =0; dados[i] != ' '; i++, j++)//quebrando idade
  221.     {
  222.         idade[j] = dados[i];
  223.         pos = i;
  224.     }
  225.         idade[j] = '\0';
  226.         strcpy(jog->idade, idade);
  227.  
  228.     for(i=pos+1, j=0; dados[i]!= '\0'; i++, j++)
  229.     {
  230.         selecao[j] = dados[i];
  231.     }
  232.     selecao[j] = '\0';
  233.     strcpy(jog->selecao, selecao);
  234. }
  235. //IMPRIME
  236. void imprime_Pilha(Pilha* pi)
  237. {
  238.     if(Pilha_vazia(pi))
  239.     {
  240.         printf("---PILHA VAZIA---\n");
  241.         //return ;
  242.     }
  243.     Elem* no = *pi;
  244.     while(no != NULL)
  245.     {
  246.         printf("-----Figurinhas-----\n");
  247.         printf("Nome: %s\n",no->dados.nome);
  248.         printf("Idade: %s\n",no->dados.idade);
  249.         printf("Selecao: %s\n", no->dados.selecao);
  250.         printf("---------------------\n");
  251.         no = no->prox;
  252.     }
  253. }
  254. //PILHA CHEIA
  255. bool Pilha_cheia(Pilha* pi)
  256. {
  257.     if(Pilha_vazia(pi))
  258.     {
  259.         printf("---PILHA VAZIA---\n");
  260.         return false;
  261.     }else {
  262.         int tam = tamanho_Pilha(pi);
  263.         if(tam == tampilha){
  264.             return true;
  265.         }else{
  266.             return false;
  267.  
  268.         }
  269.  
  270.  
  271.  
  272.     }
  273. }
  274. //REMOVE
  275. bool remove_Pilha(Pilha* pi, int *p)
  276. {
  277.     Elem *no = *pi;
  278.     *pi = no->prox;
  279.     free(no);
  280.     --*p;
  281.     return true;
  282. }
  283. //DELETA TODA PILHA
  284. void libera_Pilha(Pilha* pi)
  285. {
  286.     if(pi != NULL){
  287.         Elem* no;
  288.         while((*pi) != NULL){
  289.             no = *pi;
  290.             *pi = (*pi)->prox;
  291.             free(no);
  292.         }
  293.         free(pi);
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement