Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define max 10
- struct stack{
- int vet[max];
- int top;
- int qtd[max];
- }typedef node;
- struct row{
- int vet[20];
- int begin;
- int end;
- }typedef fila;
- void iniciar(node *p) {
- p->top=-1;
- }
- void inicia(fila *f){
- f->begin=0;
- f->end=0;
- }
- void push(node *p,int x){
- p->top++;
- p->vet[p->top]=x;
- }
- int pop(node *p){
- int x;
- x=p->vet[p->top];
- p->top--;
- return x;
- }
- void add(fila *f,int x){
- f->vet[f->end]=x;
- f->end++;
- }
- int remover(fila *f){
- int x=f->vet[f->begin];
- for(int i=0;i<f->end;i++) f->vet[i]=f->vet[i+1];
- f->end--;
- return x;
- }
- void chegada(node *p,fila *f,int x,int vet[]){
- node aux;
- int NaoAchou=1;
- iniciar(&aux);
- while(p->top!=-1 && aux.vet[aux.top]!=x){
- if(p->vet[p->top]==x) NaoAchou=0;
- push(&aux,pop(p));
- }
- while(aux.top!=-1) push(p,pop(&aux));
- if(NaoAchou){
- if((p->top==max-1)) {
- if(f->end==20) printf("Fila de espera ta cheia.\n");
- else{
- printf("Fila de espera estacionamento cheio.\n");
- vet[f->end]=x;
- add(f,x);
- }
- }
- else {
- printf("Carro estacionado com sucesso.\n");
- push(p,x);
- p->qtd[p->top]=0;
- }
- }
- else printf("Carro ja esta estacionado.\n");
- }
- void mostrar1(fila *f){
- fila aux;
- int x;
- inicia(&aux);
- printf(" _________________________________________\n\n");
- while(f->end!=f->begin){
- x=remover(f);
- add(&aux,x);
- printf(" Placa: %d\n",x);
- }
- inicia(f);
- while(aux.end!=aux.begin) add(f,remover(&aux));
- printf(" __________________________________________\n");
- }
- void mostrar(node *p){
- node aux;
- iniciar(&aux);
- printf(" _________________________________________\n\n");
- while(p->top!=-1){
- printf(" Placa: %d Quantidade de movimento: %d\n",p->vet[p->top],p->qtd[p->top]);
- push(&aux,pop(p));
- }
- while(aux.top!=-1)push(p,pop(&aux));
- printf(" _________________________________________\n");
- }
- void partida(node *p,fila *f,int x,int vet[]){
- node aux;
- int achou=0,c;
- iniciar(&aux);
- while(p->top!=-1&&p->vet[p->top]!=x){
- c=p->qtd[p->top];
- push(&aux,pop(p));
- aux.qtd[aux.top]=c;
- if(p->vet[p->top]==x) achou=1;
- }
- if(p->vet[p->top]==x) achou=1;
- if(achou){
- c=p->qtd[p->top];
- printf("Placa: %d\nQuantidade de Movimento: %d\n",pop(p),c);
- while(aux.top!=-1){
- c=aux.qtd[aux.top];
- push(p,pop(&aux));
- p->qtd[p->top]=c;
- p->qtd[p->top]++;
- }
- }
- else while(aux.top!=-1) push(p,pop(&aux));
- achou=0;
- fila aux1;
- inicia(&aux1);
- for(int i=0;i<20;i++) if(vet[i]==x)achou=1;
- if(achou){
- while(f->vet[f->begin]!=x) add(&aux1,remover(f));
- printf("Placa: %d\n",remover(f));
- printf("Removido da fila de espera.\n");
- while(f->end!=f->begin)add(&aux1,remover(f));
- while(aux1.end!=aux1.begin){
- vet[f->end]=aux1.vet[aux1.begin];
- add(f,remover(&aux1));
- }
- }
- if(achou==0){
- printf("Carro nao encontrado no estacionamento.\n");
- }
- if(f->end!=f->begin)
- if(p->top<max-1) push(p,remover(f));
- }
- int menu(){
- char opc;
- printf(" _________________________________\n");
- printf("|_________Estacionamento__________|\n");
- printf("| |\n");
- printf("|S-Sair |\n");
- printf("|C-Chegar |\n");
- printf("|P-Partir |\n");
- printf("|M-Mostrar |\n");
- printf("|_________________________________|\n");
- printf("Digite uma opcao: ");
- fflush(stdin);
- scanf("%c",&opc);
- opc = toupper(opc);
- return opc;
- }
- int main(){
- int x,opc,vet[20];
- node pilha;
- fila fil;
- inicia(&fil);
- iniciar(&pilha);
- do{
- opc=menu();
- switch(opc){
- case 'S':
- printf("....Obrigado por usar o programa....\n");
- break;
- case 'C':
- printf("Digite o numero da placa: ");
- scanf("%d",&x);
- chegada(&pilha,&fil,x,vet);
- break;
- case 'P':
- printf("Digite o numero da placa: ");
- scanf("%d",&x);
- partida(&pilha,&fil,x,vet);
- break;
- case 'M':
- mostrar(&pilha);
- if(fil.end!=fil.begin)mostrar1(&fil);
- break;
- default:
- printf("Opcao invalida\n");
- break;
- }
- system("pause");
- system("cls");
- }while(opc!='S');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement