Last active
August 29, 2015 14:07
-
-
Save marcoonroad/e93fe7bbac6e76ecc057 to your computer and use it in GitHub Desktop.
Trabalho de AL2, simulando um sistema de mensagens.
This file contains 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> | |
// max & min size of strings | |
#define MIN 50 | |
#define MAX 500 | |
// limit of messages | |
#define MSGS 100 | |
typedef struct date { | |
int Day, Month, Year; | |
} TDate; | |
typedef struct message { | |
int ID; | |
char Title[ MIN ], Addr[ MIN ], Cont[ MAX ]; // Title, Address, Content | |
TDate Rcvd; // Received: date | |
int Acsd, Rmvd; // Accessed? Removed? | |
} TMsg; | |
int fill(TMsg box[ ], char *name) { | |
FILE *backup; | |
backup = fopen(name, "r"); | |
if (backup == NULL) return -1; | |
int i; | |
for (i = 0; !feof(backup); i += 1) { | |
fscanf(backup, "%d\n", &box[ i ].ID); | |
fscanf(backup, "%[^\n]\n", box[ i ].Title); | |
fscanf(backup, "%[^\n]\n", box[ i ].Addr); | |
fscanf(backup, "%d %d %d\n", &box[ i ].Rcvd.Day, | |
&box[ i ].Rcvd.Month, | |
&box[ i ].Rcvd.Year); | |
fscanf(backup, "%[^\n]\n", box[ i ].Cont); | |
fscanf(backup, "%d %d\n\n", &box[ i ].Acsd, &box[ i ].Rmvd); | |
} | |
fclose(backup); | |
box[ i ].ID = 0; // sentinel | |
return 1; | |
} | |
int menu(void) { | |
int option; | |
printf(" =&= Bem-vindo ao sistema de mensagens! Favor, escolha\n"); | |
printf("uma das opcoes abaixo, de sua preferencia: =&= \n"); | |
printf("\t 1 -> Mensagens nao lidas.\n"); | |
printf("\t 2 -> Filtrar mensagens por remetente.\n"); | |
printf("\t 3 -> Buscar mensagens por assunto.\n"); | |
printf("\t 4 -> Buscar mensagens por conteudo.\n"); | |
printf("\t 5 -> Buscar mensagens por período (de data inicial a data final).\n"); | |
printf("\t 6 -> Sair do sistema.\n"); | |
printf("(Opcao?) >> "); | |
scanf("%d", &option); | |
fflush(stdin); | |
if ((option < 1) || (option > 6)) { | |
printf("Erro! Entrada invalida, tente novamente...\n"); | |
return menu( ); | |
} | |
return option; | |
} | |
int ask(void) { | |
char answer; | |
printf("Voce quer apagar a mensagem? (S / N): "); | |
scanf(" %c", &answer); | |
fflush(stdin); | |
switch (answer) { | |
case 'S': | |
return 1; | |
case 'N': | |
return 0; | |
default: | |
printf("Erro! Opcao invalida, tente novamente...\n"); | |
return ask( ); | |
} | |
} | |
int access(TMsg m) { | |
printf("(Recebida de '%s' em %d/%d/%d.)\n", m.Addr, | |
m.Rcvd.Day, | |
m.Rcvd.Month, | |
m.Rcvd.Year); | |
printf(" --- %s --- \n", m.Title); | |
printf("> %s.\n", m.Cont); | |
return 1; | |
} | |
int process(int option, TMsg box[ ]) { | |
int i; | |
int __id__; | |
char __temp__[ MAX ]; | |
TDate __initial__, __final__; // datas | |
switch (option) { | |
case 1: | |
for (i = 0; box[ i ].ID; i++) { | |
if (box[ i ].Acsd == 0) { | |
printf("Encontrou uma! ID = %d, Titulo = %s...\n", box[ i ].ID, | |
box[ i ].Title); | |
} | |
} | |
break; | |
case 2: | |
printf("Digite algumas palavras que o endereco do remetente contenha.\n"); | |
printf(">> "); | |
scanf("%s", __temp__); | |
fflush(stdin); | |
for (i = 0; box[ i ].ID; i++) { | |
if (strstr(box[ i ].Addr, __temp__) != NULL) { | |
printf("Encontrou uma! ID = %d, Titulo = %s...\n", box[ i ].ID, | |
box[ i ].Title); | |
} | |
} | |
break; | |
case 3: | |
printf("Digite algumas palavras que o titulo contenha.\n"); | |
printf (">> "); | |
scanf ("%s", __temp__); | |
fflush(stdin); | |
for (i = 0; box[ i ].ID; i++) { | |
if (strstr (box[ i ].Title, __temp__) != NULL) { | |
printf ("Encontrou uma! ID = %d, Titulo = %s...\n", box[ i ].ID, | |
box[ i ].Title); | |
} | |
} | |
break; | |
case 4: | |
puts ("Digite algumas palavras que o conteudo contenha."); | |
printf (">> "); | |
scanf ("%s", __temp__); | |
fflush(stdin); | |
for (i = 0; box[ i ].ID; i++) { | |
if (strstr (box[ i ].Cont, __temp__) != NULL) { | |
printf ("Encontrou uma! ID = %d, Titulo = %s...\n", box[ i ].ID, | |
box[ i ].Title); | |
} | |
} | |
break; | |
case 5: | |
puts ("Digite o periodo inicial separado por ' '."); | |
printf (">> "); | |
scanf (" %d %d %d", &__initial__.Day, &__initial__.Month, &__initial__.Year); | |
fflush (stdin); | |
puts ("Agora digite o periodo final separado por ' '."); | |
printf (">> "); | |
scanf (" %d %d %d", &__final__.Day, &__final__.Month, &__final__.Year); | |
fflush(stdin); | |
for (i = 0; box[ i ].ID; i++) { | |
if ( | |
( | |
((__initial__.Year == __final__.Year) && | |
(__initial__.Month == __final__.Month) && | |
(__initial__.Day == __final__.Day)) && | |
((__initial__.Day == box[ i ].Rcvd.Day) && | |
(__initial__.Month == box[ i ].Rcvd.Month) && | |
(__initial__.Year == box[ i ].Rcvd.Year)) | |
) || | |
( | |
((__initial__.Year == __final__.Year) && | |
(__initial__.Month == __final__.Month)) && | |
((__initial__.Day <= box[ i ].Rcvd.Day) && | |
(box[ i ].Rcvd.Day <= __final__.Day)) | |
) || | |
( | |
((__initial__.Year == __final__.Year)) && | |
((__initial__.Day <= box[ i ].Rcvd.Day) && | |
(box[ i ].Rcvd.Day <= __final__.Day) || | |
(__initial__.Month <= box[ i ].Rcvd.Month) && | |
(box[ i ].Rcvd.Month <= __final__.Month)) | |
) || | |
( | |
(__initial__.Day <= box[ i ].Rcvd.Day) && | |
(box[ i ].Rcvd.Day <= __final__.Day) || | |
(__initial__.Month <= box[ i ].Rcvd.Month) && | |
(box[ i ].Rcvd.Month <= __final__.Month) || | |
(__initial__.Year <= box[ i ].Rcvd.Year) && | |
(box[ i ].Rcvd.Year <= __final__.Year) | |
) | |
) { | |
printf("Encontrou uma! ID = %d, Titulo = %s...\n", box[ i ].ID, | |
box[ i ].Title); | |
} | |
} | |
break; | |
case 6: | |
return 0; | |
break; | |
} | |
printf("Digite o ID da mensagem que voce quer ler: "); | |
scanf("%d", &__id__); | |
fflush(stdin); | |
for (i = 0; box[ i ].ID; i++) { | |
if (box[ i ].ID == __id__) { | |
box[ i ].Acsd = access(box[ i ]); | |
box[ i ].Rmvd = ask( ); | |
} | |
} | |
return 1; | |
} | |
int save(char *file, TMsg box[ ]) { | |
FILE *backup; | |
backup = fopen(file, "w"); | |
if (backup == NULL) return -1; | |
int i; | |
for (i = 0; box[ i ].ID != 0; i++) { | |
if (! box[ i ].Rmvd) { | |
fprintf(backup, "%d\n", box[ i ].ID); | |
fprintf(backup, "%s\n", box[ i ].Title); | |
fprintf(backup, "%s\n", box[ i ].Addr); | |
fprintf(backup, "%d %d %d\n", box[ i ].Rcvd.Day, | |
box[ i ].Rcvd.Month, | |
box[ i ].Rcvd.Year); | |
fprintf(backup, "%s\n", box[ i ].Cont); | |
fprintf(backup, "%d %d\n\n", box[ i ].Acsd, box[ i ].Rmvd); | |
} | |
} | |
fclose(backup); | |
return 1; | |
} | |
int main (int argc, char *argv[ ]) { | |
TMsg input[ MSGS ]; | |
if (strcmp(argv[ 1 ], "") == 0) { | |
printf("Erro! Nenhum arquivo foi passado como argumento...\n"); | |
return -1; | |
} | |
if (fill(input, argv[ 1 ]) == -1) { | |
printf("Erro! Favor, verifique se o arquivo existe...\n"); | |
return -1; | |
} | |
int flag = 1; | |
while (flag) { | |
flag = process(menu( ), input); | |
} | |
if (save(argv[ 1 ], input) == -1) { | |
printf("Erro! Verifique as permissões do usuário para escrita.\n"); | |
return -1; | |
} | |
return 0; | |
} | |
// end of code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: Remover os bugs do programa.
FIXME: Fazer uma parada séria.
DON'TOUCH: Gambiarra da data pelo período inicial e final.
HOW?: Nem eu sei...