4
I’m a C beginner and I have the following exercise:
The Parking Stationhere contains a single boulevard that guards up to ten cars. There is only one entry/exit in the parking lot, in one edge of the mall. If a customer arrives to remove a car that not be as close to the exit, all cars blocking your the path will leave the parking lot, the customer’s car will be maneuvered out of the parking lot, and the other cars will again occupy the same initial sequence. Write a C script that processes a set of entries. Each entry contains an 'E', input, or an S, exit, and the number plate of the car. It is assumed that the cars arrive and depart in the order specified by the entry. The program must print a message whenever a car arrives or leaves. When a car arrive, the message should specify whether or not there is a vacancy for the car in the parking lot. If there is no parking space, the car will leave without enter the parking lot. When a car leaves the parking lot, message should include the number of times the car has been manoeuvred out of the parking lot to allow other cars out.
The push and pop functions seem to be working normally, but when I assign the push function to another one (maneuver for example), I cannot save to the stack. Follows 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;
}
maneuvering:
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, thank you.
Describe your problem better, it’s very abstract.
– Maniero
I tested the push function normally through main(), only when I use the push function inside another function in the maneuver() case, and try to recover the data, the function returns me "junk".
– dan
It will be a little difficult for us to look at everything this way. Take a look at this: http://answall.com/help/mcve
– Maniero
Not if it helps, but
tamanho
seems not to be set. At the beginning#define 10
shouldn’t be#define tamanho 10
?– krystalgamer
At first glance the error is in the way you invoke the push function within the maneuver function. The maneuver function takes as parameter, among others, a pointer to a variable of type 'struct stack'. What is happening is that you are passing to the push function, not the pEstacionation variable, but the address of this variable (the address of the pointer, and not the address of the variable. So I suggest you replace the summoning of this push(&pEstacionation, &car, board, maneuverAux); to push(pEstacionation, &car, board, maneuverAux);. Already, the car parameter is not used
– bruno
It’s the kind of bad question for Stack Overflow, basically it’s asking someone to solve an exercise, the answer will probably be useless for anyone who goes looking for the OS history after.
– epx
I agree with @Bruno. It is worth registering as an answer, no?
– lpacheco