Created
July 10, 2017 01:20
-
-
Save felipsmartins/7c7a6b2120790aa5d19fc967c980acd6 to your computer and use it in GitHub Desktop.
C - IFCE - crud bandas
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define TRUE 1 | |
#define FALSE 0 | |
#define CHOICE_EXIT 0 | |
#define SIZE_NOME_BANDA 80 | |
#define SIZE_NOME_MUSICO 80 | |
#define SIZE_MUSICO_FUNCAO 30 | |
/** | |
* PROBLEMA: | |
* Crie um arquivo com nome "bandas" e um registro (musico) que conterá as seguintes | |
* informações: nome da banda, nome do musico, nome da musica, função, cachê; | |
* Funções: inserir, remover, consultar (pelo nome da banda), listar. | |
* ---------------------------------------------------------------------------- | |
* Teste em: https://repl.it/languages/c | |
* Compilando no Windows: http://www.cygwin.com/ | |
*/ | |
const char *filepath = "bandas.dat"; | |
typedef struct musico { | |
char nomeBanda[SIZE_NOME_BANDA]; | |
char nomeMusico[SIZE_NOME_MUSICO]; | |
char funcao[SIZE_MUSICO_FUNCAO]; | |
double cache; | |
} musico; | |
/** | |
* Uma vez que fflush() não é confiável/portável e seu comportamento | |
* à respeito do stdin é indefinido. | |
* http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351 | |
* http://c-faq.com/stdio/gets_flush2.html | |
*/ | |
void flushStdin() { | |
char c = getchar(); | |
while (c != '\n' && c != EOF) c = getchar(); | |
} | |
void clearScreen() { | |
system("clear"); | |
} | |
int enterChoice() { | |
printf("===== MENU PRINCIPAL ======\n"); | |
printf("[1] inserir\n"); | |
printf("[2] consultar\n"); | |
printf("[3] remover\n"); | |
printf("[4] listar\n"); | |
printf("[5] Soma total dos cachês\n"); | |
printf("[0] SAIR\n"); | |
int menuChoice; | |
printf("> "); | |
scanf("%d", &menuChoice); | |
return menuChoice; | |
} | |
/** | |
* isso apenas vai criar um arquivo vazio caso ele ainda não exista. | |
* Depois é retornado o arquivo no modo rb+. | |
*/ | |
FILE *getFile(const char *filename) { | |
FILE *fp = fopen(filename, "rb+"); | |
// se arquivo não existe ainda, tentamos criar. | |
if (fp) { | |
return fp; | |
} | |
// cria arquivo se não existir. | |
FILE *wfile = fopen(filename, "w"); | |
fclose(wfile); | |
return fopen(filename, "rb+"); | |
} | |
void info(const musico data) { | |
printf("Nome do músico: %s\nBanda: %s\nFunção: %s\nCachê: %0.2f\n", | |
data.nomeMusico, | |
data.nomeBanda, | |
data.funcao, | |
data.cache | |
); | |
} | |
void inserir(FILE *fp) { | |
clearScreen(); | |
printf("[ Novo cadastro ]\n"); | |
musico data; | |
flushStdin(); | |
printf("Nome da banda\n> "); | |
scanf("%80[^\n]", data.nomeBanda); | |
flushStdin(); | |
printf("Nome do músico \n> "); | |
scanf("%80[^\n]", data.nomeMusico); | |
flushStdin(); | |
printf("Função \n> "); | |
scanf("%30s", data.funcao); | |
flushStdin(); | |
printf("Cachê \n> "); | |
scanf("%lf", &data.cache); | |
fseek(fp, 0, SEEK_END); | |
fwrite(&data, sizeof(struct musico), 1, fp); | |
printf("Registro inserido, retornando ao menu principal...\n"); | |
} | |
void consultar(FILE *fp) { | |
char search[SIZE_NOME_BANDA]; | |
musico data; | |
printf("Digite nome da banda (ou parte) para pesquisar \n> "); | |
flushStdin(); | |
scanf("%80[^\n]", search); | |
rewind(fp); | |
while (!feof(fp)) { | |
if (!(fread(&data, sizeof(struct musico), 1, fp))) { | |
continue; | |
} | |
if (strstr(data.nomeBanda, search)) { | |
info(data); | |
printf("-------------------\n"); | |
return; | |
} | |
} | |
printf("*Nada encontrado!\n"); | |
} | |
void remover(FILE *fp) { | |
size_t buffersize = sizeof(struct musico); | |
char search[SIZE_NOME_BANDA]; | |
musico data; | |
musico blankData = {"", "", "", 0.0}; | |
printf("Digite o exato nome da banda para remover \n> "); | |
flushStdin(); | |
scanf("%80[^\n]", search); | |
rewind(fp); | |
while (!feof(fp)) { | |
if (!fread(&data, buffersize, 1, fp)) { | |
continue; | |
} | |
if (strcmp(data.nomeBanda, search) == 0) { | |
/* Calculamos o início do bloco de bytes ao qual queremos | |
sobrescrever, voltando para o início do registro atual. */ | |
fseek(fp, (ftell(fp) - buffersize), SEEK_SET); | |
// finalmente sobrescrevemos o local | |
fwrite(&blankData, buffersize, 1, fp); | |
printf("*Registro apagado!\n\n"); | |
return; | |
} | |
} | |
printf("*Nada encontrado, então nada foi apagado.\n\n"); | |
} | |
void listar(FILE *fp) { | |
musico data; | |
rewind(fp); | |
while (!feof(fp)) { | |
if (!fread(&data, sizeof(struct musico), 1, fp) || | |
!strcmp(data.nomeMusico, "")) { | |
continue; | |
} | |
info(data); | |
puts("------------------------------------------------"); | |
} | |
printf("*Fim de listagem, retornando ao menu principal...\n\n"); | |
} | |
void somarCacheTotal(FILE *fp) { | |
musico data; | |
double total; | |
rewind(fp); | |
while (!feof(fp)) { | |
if (!fread(&data, sizeof(struct musico), 1, fp) || | |
!strcmp(data.nomeMusico, "")) { | |
continue; | |
} | |
total += data.cache; | |
} | |
printf("*Soma total dos cachês: %0.2f\n\n", total); | |
} | |
int main() { | |
FILE *file = getFile(filepath); | |
int menuChoice; | |
while ((menuChoice = enterChoice()) != CHOICE_EXIT) { | |
switch (menuChoice) { | |
case 1: | |
inserir(file); | |
break; | |
case 2: | |
consultar(file); | |
break; | |
case 3: | |
remover(file); | |
break; | |
case 4: | |
listar(file); | |
break; | |
case 5: | |
somarCacheTotal(file); | |
break; | |
default: | |
printf("Essa opção não existe!\n"); | |
break; | |
} | |
} | |
fclose(file); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment