Created
August 7, 2017 19:57
-
-
Save alissonfpmorais/717ebd9da19d1fbcd6d0caca2e7171f9 to your computer and use it in GitHub Desktop.
Challenge made in "Algoritmos e Estruturas de Dados III" class
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
<?xml version="1.0" encoding="UTF-8"?> | |
<module classpath="CMake" type="CPP_MODULE" version="4" /> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/.idea/challenge_hashing.iml" filepath="$PROJECT_DIR$/.idea/challenge_hashing.iml" /> | |
</modules> | |
</component> | |
</project> |
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
cmake_minimum_required(VERSION 3.8) | |
project(challenge_hashing) | |
set(CMAKE_C_STANDARD 99) | |
set(SOURCE_FILES main.c) | |
add_executable(challenge_hashing ${SOURCE_FILES}) |
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> | |
#define SUCCESS 1 | |
#define ERROR (-1) | |
#define EMPTY_CELL 0 | |
#define USED_CELL 1 | |
#define REMOVED_CELL 2 | |
typedef struct { | |
char name[30]; | |
int age; | |
int key; | |
int status; // 0 -> EMPTY_CELL, 1 -> USED_CELL, 2 -> REMOVED_CELL | |
} Person; | |
int calcHash(int key, int attempt, int size) { | |
return (key + attempt) % size; | |
} | |
int hashInsert(Person *persons, int key, int size) { | |
for (int attempt = 0; attempt < size; ++attempt) { | |
int hash = calcHash(key, attempt, size); | |
Person person = *(persons + hash); | |
if(person.status != USED_CELL) return hash; | |
} | |
return ERROR; | |
} | |
int hashSearch(Person* persons, int key, int size) { | |
for(int attempt = 0; attempt < size; ++attempt) { | |
int hash = calcHash(key, attempt, size); | |
Person person = *(persons + hash); | |
if(person.status != EMPTY_CELL) if(person.key == key) return hash; | |
else return ERROR; | |
} | |
return ERROR; | |
} | |
void printPerson(Person* person) { | |
printf("Nome: %s", person->name); | |
printf("\n"); | |
printf("Idade: %d", person->age); | |
printf("\n"); | |
printf("Chave: %d", person->key); | |
printf("\n"); | |
printf("Status: %d", person->status); | |
printf("\n"); | |
printf("--------------------------------"); | |
printf("\n"); | |
} | |
void getPersonFromDB(Person* persons, int pos) { | |
Person* person = persons + pos; | |
printPerson(person); | |
} | |
int addToDatabase(Person *persons, Person *person, int pos) { | |
Person* location = persons + pos; | |
*location = *person; | |
if(location->key == person->key) return SUCCESS; | |
return ERROR; | |
} | |
Person* getPersonInfo() { | |
Person* person = (Person*) malloc(sizeof(Person)); | |
printf("Digite o nome: "); | |
scanf("%s", person->name); | |
printf("Digite a idade: "); | |
scanf("%d", &person->age); | |
printf("Digite a chave: "); | |
scanf("%d", &person->key); | |
person->status = USED_CELL; | |
return person; | |
} | |
void listAllPersons(Person* persons, int size) { | |
for(int i = 0; i < size; i++) { | |
Person* person = persons + i; | |
printPerson(person); | |
} | |
} | |
void removePerson(Person* persons, int size) { | |
} | |
int askKey() { | |
int key; | |
printf("Digite a chave: "); | |
scanf("%d", &key); | |
return key; | |
} | |
void searchPerson(Person* persons, int size) { | |
printf("\n"); | |
int key = askKey(); | |
int pos = hashSearch(persons, key, size); | |
if(pos != ERROR) getPersonFromDB(persons, pos); | |
else printf("Não foi possível encontrar!\n"); | |
printf("\n"); | |
printf("\n"); | |
} | |
void addPerson(Person* persons, int size) { | |
printf("\n"); | |
Person* person = getPersonInfo(); | |
int pos = hashInsert(persons, person->key, size); | |
if(pos != ERROR) addToDatabase(persons, person, pos); | |
else printf("Não foi possível adicionar!\n"); | |
printf("\n"); | |
printf("\n"); | |
} | |
int getUserOption() { | |
int answer; | |
printf("[1] p/ inserção\n"); | |
printf("[2] p/ busca\n"); | |
printf("[3] p/ remoção\n"); | |
printf("[4] p/ listagem\n"); | |
printf("[0] p/ encerrar\n"); | |
printf("Digite a operação desejada: "); | |
scanf("%d", &answer); | |
return answer; | |
} | |
void menu(Person* persons, int size) { | |
int answer; | |
while((answer = getUserOption()) > 0) { | |
switch(answer) { | |
case 1: | |
addPerson(persons, size); | |
break; | |
case 2: | |
searchPerson(persons, size); | |
break; | |
case 3: | |
removePerson(persons, size); | |
break; | |
case 4: | |
listAllPersons(persons, size); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
int main() { | |
int size = 10; | |
Person* persons = (Person*) malloc(sizeof(Person) * size); | |
menu(persons, size); | |
free(persons); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment