Last active
June 17, 2016 08:49
-
-
Save celsojr/566920403eeca6ba5025a465d69a501f to your computer and use it in GitHub Desktop.
Código criado para ajudar os amigos da faculdade de Engenharia de Software da UniCesumar que estão no outro módulo mais avançado na atividade MAPA. Link para a explicação em vídeo: https://youtu.be/JilpPJsmaOU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ////////////////////////// MAPA FILA BANCO ////////////////////////////// | |
* Atividade MAPA desenvolvida para ajudar os amigos da faculdade UniCesumar | |
* Trata-se de uma simples implementação de um atendimento de uma fila | |
* de banco utilizando os recursos da fila encadeada da linguagem c | |
* | |
* Copyright(c) 2016 Celso Junior, [email protected] | |
* | |
* Licensed under MIT-style license: | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
// Dependências | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <locale.h> | |
#include <time.h> | |
#ifdef WIN32 | |
#include <windows.h> | |
//#include <math.h> | |
#define LIMPA_TELA system("cls") | |
#define AGUARDAR getch(); | |
#else | |
#include <cmath> | |
#include <unistd.h> | |
#define LIMPA_TELA system("/usr/bin/clear") | |
#define AGUARDAR getchar(); | |
#endif | |
typedef struct no { | |
int senha; | |
char categoria; | |
struct no *prox; | |
} jfilabanco; | |
enum menu { SAIR, INSERIR, RETIRAR, EXIBIR }; | |
enum CAIXAS { CAIXA_1 = 1, CAIXA_2, CAIXA_3, CAIXA_4, CAIXA_5, CAIXA_6, CAIXA_7, CAIXA_8 }; | |
// Prototipação | |
int Menu(void); | |
int RetornaCaixaLivre(); | |
int EstaVazia(jfilabanco *LISTA); | |
void Opcao(jfilabanco *LISTA, int op); | |
void Inserir(jfilabanco *LISTA); | |
void Retirar(jfilabanco *LISTA); | |
void Exibir(jfilabanco *LISTA); | |
void Liberar(jfilabanco *LISTA); | |
void Atender(jfilabanco *LISTA, char categoria, int caixaLivre); | |
// Variáveis globais | |
int atendimentos = 1; | |
jfilabanco *LISTA = NULL; | |
int main(void) | |
{ | |
// Define o idioma como português devido às acentuações | |
setlocale(LC_ALL, "Portuguese"); | |
LISTA = (jfilabanco *)malloc(sizeof(jfilabanco)); | |
// Se não houver espaço suficiente em memória, fecha imediatamente | |
if (!LISTA) { | |
printf("Sem memória disponível!\n"); | |
exit(1); | |
} | |
LISTA->prox = NULL; | |
int opt; | |
do { | |
opt = Menu(); | |
Opcao(LISTA, opt); | |
} while (opt); | |
// Limpa a memória | |
free(LISTA); | |
return 0; | |
} | |
int Menu(void) | |
{ | |
LIMPA_TELA; | |
int opt; | |
printf("Escolha a opção\n"); | |
printf("%d. Sair\n", SAIR); | |
printf("%d. Adicionar cliente na fila\n", INSERIR); | |
printf("%d. Atender próximo cliente da fila\n", RETIRAR); | |
printf("%d. Exibir lista\n", EXIBIR); | |
printf("Opção: "); scanf("%d", &opt); | |
return opt; | |
} | |
void Opcao(jfilabanco *LISTA, int op) | |
{ | |
switch (op) | |
{ | |
case SAIR: | |
Liberar(LISTA); | |
break; | |
case INSERIR: | |
Inserir(LISTA); | |
break; | |
case RETIRAR: | |
Retirar(LISTA); | |
break; | |
case EXIBIR: | |
Exibir(LISTA); | |
break; | |
default: | |
printf("\nComando inválido!\n"); | |
AGUARDAR; | |
} | |
} | |
void Inserir(jfilabanco *LISTA) | |
{ | |
jfilabanco *novo = (jfilabanco *)malloc(sizeof(jfilabanco)); | |
if (!novo) { | |
printf("Sem memória disponível!\n"); | |
exit(1); | |
} | |
printf("\nC - Correntista\n"); | |
printf("N - Não-correntista\n"); | |
printf("P - Prioridade (Idosos, Gestantes, etc.)\n"); | |
printf("R - Atendimento rápido (Operações simples)\n"); | |
printf("A - Amigos do André Noel\n\n"); | |
printf("Categoria: "); | |
char cat; | |
scanf(" %c", &cat); | |
switch (cat) | |
{ | |
case 'C': case 'c': | |
novo->categoria = 'C'; | |
break; | |
case 'N': case 'n': | |
novo->categoria = 'N'; | |
break; | |
case 'P': case 'p': | |
novo->categoria = 'P'; | |
break; | |
case 'R': case 'r': | |
novo->categoria = 'R'; | |
break; | |
case 'A': case 'a': | |
novo->categoria = 'A'; | |
break; | |
default: | |
printf("\n**Por favor escolha uma opção válida.**\n\n"); | |
AGUARDAR; | |
return; | |
} | |
novo->prox = NULL; | |
if (EstaVazia(LISTA)) | |
{ | |
novo->senha = 1; | |
LISTA->prox = novo; | |
} | |
else | |
{ | |
jfilabanco *tmp = LISTA->prox; | |
while (tmp->prox != NULL) | |
{ | |
tmp = tmp->prox; | |
} | |
novo->senha = tmp->senha + 1; | |
tmp->prox = novo; | |
} | |
printf("\nIncluído!\n\n"); | |
AGUARDAR; | |
} | |
void Exibir(jfilabanco *LISTA) | |
{ | |
LIMPA_TELA; | |
if (EstaVazia(LISTA)) { | |
printf("Lista vazia!\n\n"); | |
AGUARDAR; | |
return; | |
} | |
jfilabanco *tmp; | |
tmp = LISTA->prox; | |
printf("Senhas retiradas:\n\n"); | |
while (tmp) | |
{ | |
printf("%c-%02d\n", tmp->categoria, tmp->senha); | |
tmp = tmp->prox; | |
} | |
AGUARDAR; | |
} | |
void Retirar(jfilabanco *LISTA) | |
{ | |
// Simula um caixa livre aleatório | |
int caixaLivre = RetornaCaixaLivre(); | |
switch (caixaLivre) | |
{ | |
case CAIXA_1: case CAIXA_2: | |
Atender(LISTA, 'P', caixaLivre); | |
break; | |
case CAIXA_3: case CAIXA_4: | |
Atender(LISTA, 'N', caixaLivre); | |
break; | |
case CAIXA_5: | |
Atender(LISTA, '.', caixaLivre); | |
break; | |
case CAIXA_6: case CAIXA_7: | |
Atender(LISTA, 'C', caixaLivre); | |
break; | |
case CAIXA_8: | |
Atender(LISTA, 'R', caixaLivre); | |
break; | |
} | |
} | |
int EstaVazia(jfilabanco *LISTA) | |
{ | |
// if ternário | |
return LISTA->prox == NULL ? 1 : 0; | |
} | |
// Libera a memória | |
void Liberar(jfilabanco *LISTA) | |
{ | |
if (!EstaVazia(LISTA)) { | |
jfilabanco *proxNode, | |
*atual; | |
atual = LISTA->prox; | |
while (atual != NULL) { | |
proxNode = atual->prox; | |
free(atual); | |
atual = proxNode; | |
} | |
} | |
} | |
// Retorna um caixa aleatório entre 1 e 8 | |
int RetornaCaixaLivre() | |
{ | |
time_t t; | |
srand((unsigned)time(&t)); | |
return rand() % 8 + 1; | |
} | |
// Realiza os atendimentos da fila | |
void Atender(jfilabanco *LISTA, char categoria, int caixaLivre) | |
{ | |
if (EstaVazia(LISTA)) { | |
printf("\nLista vazia!\n\n"); | |
AGUARDAR; | |
return; | |
} | |
int achou = 0; | |
jfilabanco *aux, *nova = LISTA->prox; | |
// Para o caixa 5 procurar pelas categorias N e C | |
if (caixaLivre == 5) | |
{ | |
for (; nova != NULL; nova = nova->prox) | |
{ | |
if ('N' == nova->categoria || 'C' == nova->categoria) | |
{ | |
categoria = nova->categoria; | |
achou = 1; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
for (; nova != NULL; nova = nova->prox) | |
{ | |
if (categoria == nova->categoria) | |
{ | |
achou = 1; | |
break; | |
} | |
} | |
} | |
// Retorna com o ponteiro para o início da fila encadeada | |
nova = LISTA->prox; | |
// Atendimento da categoria A é prioritário | |
for (; nova != NULL; nova = nova->prox) | |
{ | |
if ('A' == nova->categoria) | |
{ | |
categoria = 'A'; | |
achou = 1; | |
break; | |
} | |
} | |
//Se encontrou a busca | |
if (achou == 1) | |
{ | |
int ind = 0; | |
//Apaga o primeiro registro | |
if (categoria == LISTA->prox->categoria) | |
{ | |
aux = LISTA->prox; | |
LISTA->prox = LISTA->prox->prox; | |
free(aux); | |
} | |
//Senão, procura até encontrar | |
else | |
{ | |
while (categoria != LISTA->prox->categoria) | |
{ | |
aux = LISTA; | |
LISTA = LISTA->prox; | |
} | |
if (LISTA->prox->categoria == categoria) | |
LISTA->prox = LISTA->prox->prox; | |
} | |
} | |
else | |
{ | |
// Atende o primeiro da fila caso não encontre | |
// a categoria passada por parâmetro | |
aux = LISTA->prox; | |
LISTA->prox = LISTA->prox->prox; | |
free(aux); | |
} | |
printf("\nAtendido!\n"); | |
AGUARDAR; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment