Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- //Armazena o primeiro item da fila
- typedef struct _stack STACK;
- //Armazena o conteudo do no e um ponteiro para o proximo no da fila
- typedef struct _node NODE;
- struct _stack
- {
- NODE* head;
- };
- struct _node
- {
- int element;
- NODE* next;
- };
- //Cria uma stack com o a head NULL
- STACK* Create_stack()
- {
- STACK *new = malloc(sizeof(STACK));
- new->head = NULL;
- return new;
- }
- //Recebe um elemento e cria e retorna um novo node
- // com o elemento passado
- NODE* create_node(int element)
- {
- NODE *new = malloc(sizeof(NODE));
- new->element = element;
- new->next = NULL;
- return new;
- }
- //Verifica se a pilha esta vazia
- int IS_EMPTY(STACK* stack)
- {
- if(stack->head==NULL)
- {
- return 1;
- }
- else return 0;
- }
- //Recebe uma pilha e Retorna o elemento que esta no topo da fila
- int POP(STACK* stack)
- {
- if(IS_EMPTY(stack)==1)
- {
- return 1;
- }
- else
- {
- int aux=stack->head->element;
- stack->head= stack->head->next;
- //free(stack->head->next);
- return aux;
- }
- }
- //Recebe uma pilha e um inteiro e retorna a nova pilha
- //Adiciona um novo node no topo da pilha
- void PUSH(STACK* stack, int element)
- {
- NODE *aux = create_node(element);
- aux->next = stack->head;
- stack->head = aux;
- }
- //Recebe a pilha e a operacao a ser feita
- //faz a operacao da calculadora
- void result(STACK* stack, char operation)
- {
- int number1,number2;
- if(operation=='+')
- {
- number1=POP(stack);
- number2=POP(stack);
- number1=number1 + number2;
- PUSH(stack,number1);
- }
- else if(operation=='-')
- {
- number1=POP(stack);
- number2=POP(stack);
- number2=number2-number1;
- PUSH(stack,number2);
- }
- else if(operation=='/')
- {
- number1=POP(stack);
- number2=POP(stack);
- number2=number2/number1;
- PUSH(stack,number2);
- }
- else if(operation=='*')
- {
- number1=POP(stack);
- number2=POP(stack);
- number2=number2*number1;
- PUSH(stack,number2);
- }
- }
- //Recebe uma pilha vazia e quantas strings serao lidas
- //Le as n strings que vao seguir e resolve as expressoes
- void Calculadora(STACK* calculadora, int size)
- {
- int i,number;
- char caractere;
- for(i=0;i<size;i++)
- {
- scanf(" %c", &caractere);
- if(caractere=='+' || caractere=='-' || caractere=='/' || caractere=='*')
- {
- result(calculadora,caractere);
- }
- else
- {
- number = caractere - '0';
- PUSH(calculadora,number);
- }
- }
- }
- int main()
- {
- STACK* calculadora = Create_stack();
- int k;
- scanf("%d", &k);
- Calculadora(calculadora, k);
- printf("Coordenada 1: %d\n", POP(calculadora));
- scanf("%d", &k);
- Calculadora(calculadora, k);
- printf("Coordenada 2: %d\n", POP(calculadora));
- }
- //Insira o código aqui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement