Advertisement
Kimossab

BULLSHIT

Apr 15th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. typedef struct no
  5. {
  6.     void *info;
  7.     struct no *prox;
  8. }NO;
  9.  
  10. typedef struct pilha
  11. {
  12.     NO *inicio;
  13.     int Nelementos;
  14. }PILHA;
  15.  
  16. PILHA *CriarPilha()
  17. {
  18.     PILHA *P = (PILHA *)malloc(sizeof(PILHA));
  19.     P->inicio = NULL;
  20.     P->Nelementos = 0;
  21.     return P;
  22. }
  23.  
  24. void PUSH(PILHA *Plenha, void *item)
  25. {
  26.     NO *aux = (NO *)malloc(sizeof(NO));
  27.     aux->info = item;
  28.     aux->prox = Plenha->inicio;
  29.     Plenha->inicio = aux;
  30.     Plenha->Nelementos++;
  31. }
  32.  
  33. void *POP(PILHA *P)
  34. {
  35.     if (!P->inicio)
  36.         return NULL;
  37.     void *Valor = P->inicio->info;
  38.     NO *aux = P->inicio->prox;
  39.     free(P->inicio);
  40.     P->inicio = aux;
  41.     P->Nelementos--;
  42.     return Valor;
  43. }
  44.  
  45. bool vazia(PILHA *p)
  46. {
  47.     return !p || !p->inicio;
  48. }
  49.  
  50. float factorial(int n)
  51. {
  52.     PILHA *p = CriarPilha();
  53.     for (; n > 0; n--)
  54.     {
  55.         int *y = (int *)malloc(sizeof(int));
  56.         *y = n;
  57.         PUSH(p, y);
  58.     }
  59.  
  60.     float res = 1;
  61.  
  62.     while (!vazia(p))
  63.         res *= *((int*)POP(p));
  64.  
  65.     return res;
  66. }
  67.  
  68. void main()
  69. {
  70.     printf("%f\n", factorial(5));
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement