Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. //Armazena o primeiro item da fila
  6. typedef struct _stack STACK;
  7.  
  8. //Armazena o conteudo do no e um ponteiro para o proximo no da fila
  9. typedef struct _node NODE;
  10.  
  11. struct _stack
  12. {
  13.     NODE* head;
  14. };
  15. struct _node
  16. {
  17.     int element;
  18.     NODE* next;
  19. };
  20.  
  21. //Cria uma stack com o a head NULL
  22. STACK* Create_stack()
  23. {
  24.     STACK *new = malloc(sizeof(STACK));
  25.     new->head = NULL;
  26.     return new;
  27. }
  28.  
  29. //Recebe um elemento e cria e retorna um novo node
  30. // com o elemento passado
  31. NODE* create_node(int element)
  32. {
  33.     NODE *new = malloc(sizeof(NODE));
  34.     new->element = element;
  35.     new->next = NULL;
  36.     return new;
  37. }
  38.  
  39. //Verifica se a pilha esta vazia
  40. int IS_EMPTY(STACK* stack)
  41. {
  42.     if(stack->head==NULL)
  43.     {
  44.         return 1;
  45.     }
  46.     else return 0;
  47.  
  48. }
  49.  
  50. //Recebe uma pilha e Retorna o elemento que esta no topo da fila
  51. int POP(STACK* stack)
  52. {
  53.     if(IS_EMPTY(stack)==1)
  54.     {
  55.         return 1;
  56.     }
  57.     else
  58.     {
  59.         int aux=stack->head->element;
  60.         stack->head= stack->head->next;
  61.         //free(stack->head->next);
  62.         return aux;
  63.  
  64.     }
  65.  
  66. }
  67.  
  68. //Recebe uma pilha e um inteiro e retorna a nova pilha
  69. //Adiciona um novo node no topo da pilha
  70. void PUSH(STACK* stack, int element)
  71. {
  72.     NODE *aux = create_node(element);
  73.     aux->next = stack->head;
  74.     stack->head = aux;
  75.  
  76. }
  77.  
  78. //Recebe a pilha e a operacao a ser feita
  79. //faz a operacao da calculadora
  80. void result(STACK* stack, char operation)
  81. {
  82.     int number1,number2;
  83.     if(operation=='+')
  84.     {
  85.         number1=POP(stack);
  86.         number2=POP(stack);
  87.         number1=number1 + number2;
  88.         PUSH(stack,number1);
  89.  
  90.     }
  91.     else if(operation=='-')
  92.     {
  93.         number1=POP(stack);
  94.         number2=POP(stack);
  95.         number2=number2-number1;
  96.         PUSH(stack,number2);
  97.     }
  98.     else if(operation=='/')
  99.     {
  100.         number1=POP(stack);
  101.         number2=POP(stack);
  102.         number2=number2/number1;
  103.         PUSH(stack,number2);
  104.     }
  105.     else if(operation=='*')
  106.     {
  107.         number1=POP(stack);
  108.         number2=POP(stack);
  109.         number2=number2*number1;
  110.         PUSH(stack,number2);
  111.     }
  112. }
  113.  
  114. //Recebe uma pilha vazia e quantas strings serao lidas
  115. //Le as n strings que vao seguir e resolve as expressoes
  116. void Calculadora(STACK* calculadora, int size)
  117. {
  118.     int i,number;
  119.     char caractere;
  120.     for(i=0;i<size;i++)
  121.     {
  122.         scanf(" %c", &caractere);
  123.         if(caractere=='+' || caractere=='-' || caractere=='/' || caractere=='*')
  124.         {
  125.             result(calculadora,caractere);
  126.         }
  127.         else
  128.         {
  129.             number = caractere - '0';
  130.             PUSH(calculadora,number);
  131.         }
  132.  
  133.     }
  134.  
  135.  
  136. }
  137.  
  138. int main()
  139. {
  140.     STACK* calculadora = Create_stack();
  141.     int k;
  142.     scanf("%d", &k);
  143.     Calculadora(calculadora, k);
  144.     printf("Coordenada 1: %d\n", POP(calculadora));
  145.     scanf("%d", &k);
  146.     Calculadora(calculadora, k);
  147.     printf("Coordenada 2: %d\n", POP(calculadora));
  148.  
  149. }
  150. //Insira o código aqui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement