From eea437dc84d2906b615fc243bb44cec57255fbe9 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 5 Mar 2023 18:36:32 -0300 Subject: First Commit --- Uni/agenda.c | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 278 insertions(+) create mode 100644 Uni/agenda.c (limited to 'Uni/agenda.c') diff --git a/Uni/agenda.c b/Uni/agenda.c new file mode 100644 index 0000000..7a625c8 --- /dev/null +++ b/Uni/agenda.c @@ -0,0 +1,278 @@ +#include +#include +#include +#include + +typedef struct{ + unsigned long long int pos; + unsigned int ano, mes, dia; + unsigned int hora, minutos; + char texto[1024]; +}agenda; + +typedef struct Node{ + agenda v; + struct Node* next; +}node; + +node* create(agenda v){ + node* tmp = (node*)malloc(sizeof(node)); + tmp->next = NULL; + tmp->v = v; + return tmp; +} + +agenda ler(){ + agenda v; + do{ + printf("insira no formato\n"); + printf("data [31/12/0000]: "); + scanf("%d/%d/%d", &v.dia, &v.mes, &v.ano); + }while(v.dia > 31 || v.mes > 12 ); + + do{ + printf("insira no formato\n"); + printf("horario [23:59]: "); + scanf("%d:%d", &v.hora, &v.minutos); + }while(v.hora > 23 || v.minutos > 59); + + printf("Compromisso: \n"); + scanf(" %[^\n]", v.texto); + + v.pos = v.minutos + 60*v.hora + 24*60*v.dia + 31*24*60*v.mes + 12*31*24*60*v.ano; + return v; +} + +void print_comp(agenda v){ + printf("\nData: %02u/%02u/%04u\n", v.dia, v.mes, v.ano); + printf("Horario: %02u:%02u\n", v.hora, v.minutos); + printf("%s\n", v.texto); +} + +void add_compromisso(agenda v, node** head){ + node* tmp = create(v); + if(*head == NULL){ + *head = tmp; + return; + } + if((*head)->v.pos > tmp->v.pos){ + tmp->next = *head; + *head = tmp; + return; + } + + node* aux = *head; + node* ant; + while(aux != NULL && aux->v.pos < tmp->v.pos){ + ant = aux; + aux = aux->next; + } + if(aux == NULL){ + ant->next = tmp; + }else{ + tmp->next = ant->next; + ant->next = tmp; + } +} + +void print_agenda(node* head){ + while(head != NULL){ + print_comp(head->v); + head = head->next; + } +} + +void remove_compromisso(unsigned long long int pos, node** head){ + bool ini = false; + while((*head) != NULL && (*head)->v.pos >= pos && (*head)->v.pos <= pos+60*23+59){ + node* tmp = *head; + *head = (*head)->next; + free(tmp); + ini = true; + } + if(ini) return; + + node* tmp = (*head)->next; + node* ant = *head; + while(tmp != NULL){ + bool deletou = false; + node* del = tmp; + if(tmp != NULL && tmp->v.pos >= pos && tmp->v.pos <= pos+60*23+59){ + ant->next = tmp->next; + deletou = true; + } + if(deletou){ + ant = ant; + free(del); + } + else + ant = tmp; + tmp = tmp->next; + } +} + +void consulta_data(unsigned long long int pos, node* head){ + while(head != NULL){ + if(head != NULL && head->v.pos >= pos && head->v.pos <= pos+60*23+59) + print_comp(head->v); + head = head->next; + } +} + +void remove_comp_palavra(char* palavra, node** head){ + if(*head == NULL) return; + + node* tmp = *head; + int cont = 0; + while(tmp != NULL){ + if(strstr(tmp->v.texto, palavra) != NULL){ + printf("---------%d---------", cont+1); + printf("\nData: %02u/%02u/%04u\n", tmp->v.dia, tmp->v.mes, tmp->v.ano); + printf("Horario: %02u:%02u\n", tmp->v.hora, tmp->v.minutos); + printf("%s\n\n", tmp->v.texto); + cont++; + } + tmp = tmp->next; + } + if(cont == 0) return; + + int esc=1; + do{ + if(esc > cont || esc <= 0) printf("Insira um valor valido: "); + else printf("Escolha um compromisso para deletar: "); + scanf("%d", &esc); + }while(esc <= 0 || esc > cont); + + tmp = *head; + node* ant; + cont = 0; + while(tmp != NULL){ + if(strstr(tmp->v.texto, palavra) != NULL){ + cont++; + if(cont == esc){ + if(tmp == *head){ + *head = tmp->next; + free(tmp); + }else{ + ant->next = tmp->next; + free(tmp); + } + return; + } + } + ant = tmp; + tmp = tmp->next; + } +} + +void print_comp_palavra(char* palavra, node** head){ + if(*head == NULL) return; + + node* tmp = *head; + int cont = 0; + while(tmp != NULL){ + if(strstr(tmp->v.texto, palavra) != NULL){ + printf("\nData: %02u/%02u/%04u\n", tmp->v.dia, tmp->v.mes, tmp->v.ano); + printf("Horario: %02u:%02u\n", tmp->v.hora, tmp->v.minutos); + printf("%s\n", tmp->v.texto); + cont++; + } + tmp = tmp->next; + } +} + +void altera_comp_palavra(char* palavra, node** head){ + if(*head == NULL) return; + + node* tmp = *head; + int cont = 0; + while(tmp != NULL){ + if(strstr(tmp->v.texto, palavra) != NULL){ + printf("---------%d---------", cont+1); + printf("\nData: %02u/%02u/%04u\n", tmp->v.dia, tmp->v.mes, tmp->v.ano); + printf("Horario: %02u:%02u\n", tmp->v.hora, tmp->v.minutos); + printf("%s\n\n", tmp->v.texto); + cont++; + } + tmp = tmp->next; + } + if(cont == 0) return; + + int esc=1; + do{ + if(esc > cont || esc <= 0) printf("Insira um valor valido: "); + else printf("Escolha um compromisso para alterar: "); + scanf("%d", &esc); + }while(esc <= 0 || esc > cont); + + tmp = *head; + node* ant; + cont = 0; + while(tmp != NULL){ + if(strstr(tmp->v.texto, palavra) != NULL){ + cont++; + if(cont == esc){ + printf("Novo compromisso: \n"); + scanf(" %[^\n]", tmp->v.texto); + return; + } + } + ant = tmp; + tmp = tmp->next; + } +} + +int main(){ + node* head = NULL; + while(1){ + int esc=0; + scanf("%d", &esc); + if(esc == 1){ + agenda v = ler(); + //print_comp(v); + add_compromisso(v, &head); + } + if(esc == 2){ + print_agenda(head); + } + if(esc == 3){ + char palavra[128]; + printf("Texto para procurar: "); + scanf(" %s", palavra); + remove_comp_palavra(palavra, &head); + } + if(esc == 4){ + unsigned long long int pos; + int dia, mes, ano; + printf("data [31/12/0000]: "); + scanf("%d/%d/%d", &dia, &mes, &ano); + pos = 24*60*dia + 31*24*60*mes + 12*31*24*60*ano; + consulta_data(pos, head); + } + if(esc == 5){ + char palavra[128]; + printf("Texto para procurar: "); + scanf(" %s", palavra); + print_comp_palavra(palavra, &head); + } + if(esc == 6){ + char palavra[128]; + printf("Texto para procurar: "); + scanf(" %s", palavra); + altera_comp_palavra(palavra, &head); + } + if(esc == 9){ + unsigned long long int pos; + int dia, mes, ano; + printf("data [31/12/0000]: "); + scanf("%d/%d/%d", &dia, &mes, &ano); + pos = 24*60*dia + 31*24*60*mes + 12*31*24*60*ano; + remove_compromisso(pos ,&head); + } + if(esc == 10) break; + } + return 1; +} + +// TODO: arrumar os inputs da agenda +// TODO: 3 5 -- cgit v1.2.3