Handling batteries in C

I am a beginner in C and have the following exercise:

The parking lot here contains a single mall that holds up to ten cars. There is only one entrance/exit in the parking lot, in a end of the mall. If a customer arrives to pick up a car that do not be the closest to the exit, all cars blocking your way will exit the parking lot, the customer's car will be maneuvered out of the parking lot, and the other cars will occupy the same initial sequence. Write a program in C that processes a set of inputs. Each entry contains an' E', input, or a 'S', output, and the license plate number of the car. It is assumed that cars arrive and depart in the order specified by the entrance. The program should print a message whenever a car arrives or leaves. When a car arrive, the message must specify whether or not there is vacancy for the car in the parking lot. If there is no vacancy, the car will leave without enter the parking lot. When a car leaves the parking lot, the message should include the number of times the car has been maneuvered out of the parking lot to allow other cars to leave.

The push and pop functions seem to be working normally, but when I assign the push function to another (maneuver for example), I cannot save to the stack. Following codes:

Struct:

#define 10
struct veiculo
{
    int placa;
    int manobra;
};
struct pilha
{
    struct veiculo item[tamanho];
    int topo;
};

Push:

void push(struct pilha *pEstacionamento, struct veiculo *carro, int placaDig, int manobraCar)
{
    if(pCheia(pEstacionamento))
    {
        printf("Estacionamento cheio.");
        getch();
    }
    pEstacionamento->topo = pEstacionamento->topo+1;
    pEstacionamento->item[pEstacionamento->topo].placa = placaDig;
    pEstacionamento->item[pEstacionamento->topo].manobra = manobraCar;

}

struct veiculo pop(struct pilha *pEstacionamento)
{
    struct veiculo valor;
    if(pVazia(pEstacionamento))
    {
        printf("Estacionamento vazio");
        getch();
        valor.placa = -1;
        return valor;
    }

    valor = pEstacionamento->item[pEstacionamento->topo];
    pEstacionamento->topo = pEstacionamento->topo - 1;
    return valor;
}

Maneuver:

void manobra(struct pilha *pEstacionamento, char status, int placa )
{
    struct pilha pEstacionamentoAux;
    inicializa(&pEstacionamentoAux);
    struct veiculo carro;
    int manobraAux;
    int placaAux;

    if(status == 'e')
    {
        if(pCheia(pEstacionamento))
        {
            printf("Estacionamento cheio.");
            getch();
        }

        manobraAux = 0;
        //pega o valor da placa e manobra como zero e add na pilha
        push(&pEstacionamento, &carro, placa, manobraAux);

    }
    else if(status == 's')
    {
        if(pVazia(pEstacionamento))
        {
            printf("Estacionamento vazio");
            getch();

        }

        while(!pVazia(pEstacionamento))
        {
            carro = pop(&pEstacionamento->topo);
            placaAux = carro.placa;
            manobraAux = carro.manobra;

            if(placaAux == placa)
            {
                printf("Seu carro com a placa: %d , foi retirado do estacionamento com %d manobras.", placaAux, manobraAux);
                break;
            }
            else
            {
                manobraAux = manobraAux + 1;
                push(&pEstacionamentoAux, &carro, placaAux, manobraAux);
            }
        }

        while(!pVazia(&pEstacionamentoAux))
        {
            carro = pop(&pEstacionamentoAux);
            placaAux = carro.placa;
            manobraAux = carro.manobra;
            push(&pEstacionamento, &carro, placaAux, manobraAux);
        }
    }

If anyone can help with any tips on how to proceed, I appreciate it.

Author: Pena Pintada, 2015-09-04

1 answers

Follows a (tested) solution to the proposed exercise, the logic is basically the same as your code:

/* ****************************************************************** */
/* *                         estacionamento.c                       * */
/* ****************************************************************** */

#include <stdio.h>
#include <string.h>


#define ESTACIONAMENTO_MAX_TAM        (10)
#define PLACA_NULL                    (-1)
#define SUCESSO                       (0)
#define ERRO_ESTACIONAMENTO_CHEIO     (-1)
#define ERRO_MANOBRA_INVALIDA         (-2)


typedef enum manobra_e manobra_t;
typedef struct veiculo_s veiculo_t;
typedef struct estacionamento_s estacionamento_t;


enum manobra_e
{
    manobEntrada,
    manobSaida
};


struct veiculo_s
{
    int placa;
};


struct estacionamento_s
{
    veiculo_t vaga[ ESTACIONAMENTO_MAX_TAM ];
    int qtd;
};


void estacionamento_inicializar( estacionamento_t * e )
{
    memset( e, 0, sizeof(estacionamento_t) );
}


int estacionamento_push( estacionamento_t * e, veiculo_t v )
{
    if( e->qtd == ESTACIONAMENTO_MAX_TAM )
        return ERRO_ESTACIONAMENTO_CHEIO;

    e->vaga[ e->qtd++ ] = v;

    return SUCESSO;
}


veiculo_t estacionamento_pop( estacionamento_t * e )
{
    if( e->qtd == 0 )
    {
        veiculo_t v;
        v.placa = PLACA_NULL;
        return v;
    }

    return e->vaga[ --e->qtd ];
}


int estacionamento_manobrar_veiculo( estacionamento_t * e, manobra_t m, int placa )
{
    switch( m )
    {
        case manobEntrada :
        {
            veiculo_t v;
            v.placa = placa;

            if( estacionamento_push( e, v ) == ERRO_ESTACIONAMENTO_CHEIO )
                return ERRO_ESTACIONAMENTO_CHEIO;

            return 1;
        }

        case manobSaida :
        {
            veiculo_t v;
            estacionamento_t aux;
            int qtd_manobras = 0;

            estacionamento_inicializar( &aux );

            while(1)
            {               
                v = estacionamento_pop( e );

                if( v.placa == PLACA_NULL )
                    break;

                qtd_manobras++;

                if( v.placa == placa )
                    break;

                estacionamento_push( &aux, v );
            }

            while(1)
            {               
                v = estacionamento_pop( &aux );

                if( v.placa == PLACA_NULL )
                    break;

                estacionamento_push( e, v );
            }

            return qtd_manobras;
        }

        default :
        {
            return ERRO_MANOBRA_INVALIDA;
        }
    }
}

void estacionamento_listar_veiculos( estacionamento_t * e )
{
    int i = 0;

    if( !e->qtd )
    {
        printf("Estacionamento Vazio!\n");
        return;
    }

    printf( "Estacionamento:\n" );

    for( i = 0; i < e->qtd; i++ )
        printf( "   Vaga %d / Placa: %d\n", i, e->vaga[i].placa );
}


int main( int argc, char * argv[] )
{
    int qtd = 0;
    estacionamento_t e;

    estacionamento_inicializar( &e );

    estacionamento_listar_veiculos( &e );

    estacionamento_manobrar_veiculo( &e, manobEntrada, 1010 );
    estacionamento_manobrar_veiculo( &e, manobEntrada, 2020 );
    estacionamento_manobrar_veiculo( &e, manobEntrada, 3030 );
    estacionamento_manobrar_veiculo( &e, manobEntrada, 4040 );
    estacionamento_manobrar_veiculo( &e, manobEntrada, 5050 );

    estacionamento_listar_veiculos( &e );

    qtd = estacionamento_manobrar_veiculo( &e, manobSaida, 3030 );

    printf( "Retirei veiculo do estacionamento com %d manobras!\n", qtd );

    estacionamento_listar_veiculos( &e );

    return 0;
}

/* fim-de-arquivo */

Hope I helped!

 1
Author: Lacobus, 2016-05-08 04:36:44