Skip to content

Instantly share code, notes, and snippets.

@tashima42
Created February 14, 2022 14:26
Show Gist options
  • Save tashima42/dd42ccaa26536ff599f257c61f5dce43 to your computer and use it in GitHub Desktop.
Save tashima42/dd42ccaa26536ff599f257c61f5dce43 to your computer and use it in GitHub Desktop.
Exame de suficiencia disciplina Laboratório de Informática
// Autor: Pedro Henrique Mateus Tashima
// RA: 2145200
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#define name_length 1000
#define email_length 100
#define phone_length 13
#define agenda_length 100
// (a) definir estruturas
struct Date
{
int day;
int month;
int year;
};
struct Contact
{
char name[name_length];
char email[email_length];
char phone[phone_length];
struct Date birth_date;
};
// (b) validar email
int validate_email(char *email)
{
char email_copy1[email_length], email_copy2[email_length];
strcpy(email_copy1, email);
strcpy(email_copy2, email);
char *first_section = strtok(email_copy1, "@");
if (strlen(first_section) <= 2)
return 0;
if (strlen(email) == strlen(first_section))
return 0;
char *middle_section = strtok(email_copy2, ".");
if (strlen(email) == strlen(middle_section))
return 0; // email has "."
char *final_section = strtok(NULL, ".");
if (strlen(final_section) != 3)
return 0;
int first_section_length = strlen(first_section), middle_section_length = strlen(middle_section);
if ((middle_section_length - (first_section_length + 1)) < 3)
return 0;
return 1;
}
// (c) validar telefone
int validate_phone(char *phone)
{
const size_t length = strlen(phone);
if (length < 11 || length > 12)
return 0;
if (phone[0] != '0')
return 0;
for (int i = 0; i < length; i++)
{
if (!isdigit(phone[i]))
return 0;
}
return 1;
}
// (d) validar data
int validate_date(struct Date date)
{
if (date.day < 1 || date.day > 30)
return 0;
if (date.month < 1 || date.month > 12)
return 0;
if (date.year < 1900 || date.year > 2021)
return 0;
return 1;
}
void print_contact(struct Contact contact)
{
printf("Nome: %s\n", contact.name);
printf("Email: %s\n", contact.email);
printf("Telefone: %s\n", contact.phone);
printf("Data de nascimento: %d/%d/%d\n", contact.birth_date.day, contact.birth_date.month, contact.birth_date.year);
}
unsigned int register_contact(struct Contact agenda[], unsigned int length, struct Contact contact)
{
if (validate_email(contact.email) != 1 ||
validate_phone(contact.phone) != 1 ||
validate_date(contact.birth_date) != 1 ||
length > agenda_length)
return 0;
agenda[length] = contact;
length++;
return length;
}
void find_by_phone(struct Contact contacts[], int length, char number[])
{
for (int i = 0; i < length; i++)
{
if (strcmp(number, contacts[i].phone) == 0)
{
print_contact(contacts[i]);
}
}
}
int find_ddd(struct Contact agenda[], int length, char ddd[])
{
int same_ddd = 0, i = 0;
for (i = 0; i < length; i++)
{
char sub[3];
strncpy(sub, agenda[i].phone, 3);
if (strcmp(sub, ddd) == 0)
{
same_ddd++;
}
}
return same_ddd;
}
void sort_by_name(struct Contact agenda[], unsigned int length)
{
struct Contact temp;
for (int i = 0; i < length; i++)
{
for (int j = i + 1; j < length; j++)
{
if (strcmp(agenda[i].name, agenda[j].name) > 0)
{
temp = agenda[i];
agenda[i] = agenda[j];
agenda[j] = temp;
}
}
}
printf("| nome | email | telefone |\n");
for (int k = 0; k < length; k++)
{
printf("| %s | %s | %s |\n", agenda[k].name, agenda[k].email, agenda[k].phone);
}
}
int main()
{
struct Contact agenda[agenda_length];
unsigned int agenda_size = 0;
struct Date dates[20] = {
{2, 11, 2001},
{3, 12, 2002},
{4, 1, 2003},
{5, 2, 2004},
{6, 3, 2005},
{7, 4, 2006},
{8, 5, 2007},
{9, 6, 2008},
{1, 7, 2009},
{2, 8, 2010},
{3, 9, 2011},
{4, 10, 2012},
{5, 11, 2013},
{6, 12, 2014},
{7, 1, 2015},
{8, 4, 2016},
{9, 5, 2017},
{1, 6, 2018},
{2, 7, 2019},
{3, 8, 2020},
};
char names[20][name_length] = {
"Aaran",
"Corbin",
"Darryn",
"Darwyn",
"Daryl",
"Fawaz",
"Fawkes",
"Faysal",
"Fearghus",
"Karsyn",
"Karthikeya",
"Kasey",
"Kash",
"Kashif",
"Marco",
"Marcos",
"Marcous",
"Marcquis",
"Stewarty",
"Strachan",
};
char emails[20][email_length] = {
"asf00@example.com",
"asf01@example.com",
"asf02@example.com",
"asf03@example.com",
"asf04@example.com",
"asf05@example.com",
"asf06@example.com",
"asf07@example.com",
"asf08@example.com",
"asf09@example.com",
"asf11@example.com",
"asf12@example.com",
"asf13@example.com",
"asf14@example.com",
"asf15@example.com",
"asf16@example.com",
"asf17@example.com",
"asf18@example.com",
"asf19@example.com",
};
char phones[20][phone_length] = {
"011111111111",
"011111111112",
"011111111113",
"011111351114",
"021111212311",
"021112112112",
"021111111113",
"021112123114",
"031111212111",
"031111121112",
"011111111111",
"011111131112",
"011113111113",
"011132111114",
"021112111211",
"021111111112",
"021112111113",
"021111111114",
"041111131111",
"051111211112",
};
struct Contact generator;
srand(time(NULL));
for (int i = 0; i < 20; i++)
{
strcpy(generator.name, names[rand() % 20]);
strcpy(generator.email, emails[rand() % 20]);
strcpy(generator.phone, phones[rand() % 20]);
generator.birth_date = dates[rand() % 20];
agenda_size = register_contact(agenda, agenda_size, generator);
}
// form variables
unsigned int exit = 0, option, add_size = 0, valid = 0;
struct Contact add;
struct Date form_date;
char form_phone[phone_length], ddd_search[4], email_validate[email_length];
while (exit != 1)
{
printf("1 - Adiconar contato\n");
printf("2 - Buscar contato por telefone\n");
printf("3 - Numero de contatos com DDD\n");
printf("4 - Listar contatos por ordem alfabetica\n");
printf("5 - Validar email\n");
printf("6 - Validar telefone\n");
printf("7 - Validar data\n");
printf("Escolha uma opcao: ");
scanf("%d", &option);
switch (option)
{
case 1:
while (add_size == 0)
{
printf("nome: ");
scanf("%s", add.name);
printf("email: ");
scanf("%s", add.email);
printf("telefone: ");
scanf("%s", add.phone);
printf("data de nascimento\n");
printf("dia: ");
scanf("%d", &form_date.day);
printf("mes: ");
scanf("%d", &form_date.month);
printf("ano: ");
scanf("%d", &form_date.year);
add.birth_date = form_date;
add_size = register_contact(agenda, agenda_size, add);
if (add_size == 0)
{
printf("Informacoes invalidas, tente novamente\n");
}
else
{
agenda_size = add_size;
printf("\nContato adicionado: \n");
print_contact(agenda[agenda_size - 1]);
}
}
break;
case 2:
printf("Telefone: ");
scanf("%s", form_phone);
find_by_phone(agenda, agenda_size, form_phone);
break;
case 3:
printf("DDD: ");
scanf("%s", ddd_search);
printf("Numero de contatos com DDD %s: %d\n", ddd_search, find_ddd(agenda, agenda_size, ddd_search));
break;
case 4:
sort_by_name(agenda, agenda_size);
break;
case 5:
printf("Email: ");
scanf("%s", email_validate);
valid = validate_email(email_validate);
if (valid == 1)
{
printf("Email valido\n");
}
else
{
printf("Email invalido\n");
}
break;
case 6:
printf("Telefone: ");
scanf("%s", form_phone);
valid = validate_phone(form_phone);
if (valid == 1)
{
printf("Telefone valido\n");
}
else
{
printf("Telefone invalido\n");
}
break;
case 7:
printf("dia: ");
scanf("%d", &form_date.day);
printf("mes: ");
scanf("%d", &form_date.month);
printf("ano: ");
scanf("%d", &form_date.year);
valid = validate_date(form_date);
if (valid == 1)
{
printf("Data valida\n");
}
else
{
printf("Data invalida\n");
}
break;
default:
printf("Opcao invalida\n");
}
printf("0 - continuar | 1 - sair\n");
scanf("%d", &exit);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment