-1
Guys I got this code down, it works, only it’s weird, it doesn’t print the last position.
#include <stdio.h>
#include <stdlib.h>
#define TAM_PILHA 2
#define NUM_ENTRA 5
void empilha(int *pilha, int *topo, int valor)
{
if (*topo < TAM_PILHA)
{
pilha[*topo] = valor;
(*topo)++;
}
else
{
printf("Pilha Cheia\n");
}
}
int desempilha(int *pilha, int *topo)
{
if (*topo > 0)
{
(*topo)--;
return pilha[*topo];
}
else
{
printf("Pilha Vazia\n");
}
}
int main()
{
int pilha_par[TAM_PILHA], pilha_impar[TAM_PILHA], topo_par = 0, topo_impar = 0;
int i = 0, num_do_usuario = 0;
for (i = 0; i < NUM_ENTRA; i++)
{
printf("\nEntre com um numero: ");
scanf("%d", &num_do_usuario);
if ((num_do_usuario % 2) == 0)
{
empilha(pilha_par, &topo_par, num_do_usuario);
}
else
{
empilha(pilha_impar, &topo_impar, num_do_usuario);
}
}
printf("\n\nNumeros pares sao: ");
while (topo_par > 0)
{
printf("%d ", desempilha(pilha_par, &topo_par));
}
printf("\n\nNumeros impares sao: ");
while (topo_impar > 0)
{
printf("%d ", desempilha(pilha_impar, &topo_impar));
}
printf("\n\n");
system("pause");
return 0;
}
For me it’s working, of course there was an error and it didn’t compile: http://ideone.com/qD47TT
– Maniero
Here is a time that comes up to the 3 question and you type the number already gives full stack. And there are times when you ask five times, which is right, and you don’t print the last number I type.
– André
In fact in the form made to change the
define
can give some problem, the code is not anything robust.– Maniero
Type number 3 first, then 4, 2 and 6, it’s gone from the full stack! Since it wasn’t meant to give, you have to go to the fifth vector, the last vector and print the last vector too, the last vector doesn’t print on the screen.
– André