Merge pull request #30 from httpspedroh/master

add: tp02 solutions
This commit is contained in:
Felipe Domingos 2024-05-09 15:13:05 -03:00 committed by GitHub
commit a2e99430f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
87 changed files with 12656 additions and 0 deletions

View File

@ -0,0 +1,27 @@
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

Binary file not shown.

View File

@ -0,0 +1,27 @@
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 28ms 448

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,511 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with sequential search algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
Character character = searchById(id, allCharacters);
// Add character to mainCharacters array
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Read input and search for characters from pub.in name entries in mainCharacters array
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// Read first line
line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
Character foundCharacter = null;
// Search for character
for(int i = 0; i < mainCharacters.size(); i++) {
// Increment comparisons
comparisons++;
// If character is found
if(mainCharacters.get(i).getName().equals(line)) {
foundCharacter = mainCharacters.get(i);
break;
}
}
if(foundCharacter != null) System.out.println("SIM");
else System.out.println("NAO");
// ----------------- //
// Read line
line = inScanner.nextLine();
}
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_sequencial.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,19 @@
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
NAO
NAO
SIM

View File

@ -0,0 +1,48 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM
Lord Voldemort
Bathilda Bagshot
Terry Boot
Fat Friar
Seamus Finnigan
Ellie Cattermole
Stubby Boardman
Newt Scamander
Rubeus Hagrid
Gibbon
James Potter
Mr Mason
Tom Riddle
Madam Puddifoot
Ginny Weasley
Eileen Prince
Lucian Bole
Gilderoy Lockhart
Lord Voldemort
FIM

View File

@ -0,0 +1 @@
753045 0ms 517

View File

@ -0,0 +1,737 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with binary search algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
// ------------------------- //
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Read input and search for characters from pub.in name entries in mainCharacters array
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// Order mainCharacters array by name asc
for(int i = 0; i < m; i++) {
for(int j = i + 1; j < m; j++) {
comparisons++;
if(strcmp(character_getName(&mainCharacters[i]), character_getName(&mainCharacters[j])) > 0) {
Character aux = mainCharacters[i];
mainCharacters[i] = mainCharacters[j];
mainCharacters[j] = aux;
}
}
}
// ----------------- //
// Read input
char name[MAX_NAME_SIZE];
scanf(" %[^\n]s", name);
while(true) {
// Clean \n from the end of the string
if(id[strlen(name) - 1] == '\n' || name[strlen(name) - 1] == '\r') name[strlen(name) - 1] = '\0';
// ------------------------- //
if(isEnd(name)) break;
else {
// Search for character
Character *foundCharacter = NULL;
// Binary search
int left = 0;
int right = m - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
comparisons += 2;
if(strcmp(character_getName(&mainCharacters[mid]), name) == 0) {
foundCharacter = &mainCharacters[mid];
break;
}
else if(strcmp(character_getName(&mainCharacters[mid]), name) < 0) left = mid + 1;
else right = mid - 1;
}
// Print result
printf("%s\n", foundCharacter == NULL ? "NAO" : "SIM");
// ------------------------- //
scanf(" %[^\n]s", name);
}
}
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_binaria.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,19 @@
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
SIM
NAO
NAO
NAO
NAO
NAO
SIM

View File

@ -0,0 +1,48 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM
Lord Voldemort
Bathilda Bagshot
Terry Boot
Fat Friar
Seamus Finnigan
Ellie Cattermole
Stubby Boardman
Newt Scamander
Rubeus Hagrid
Gibbon
James Potter
Mr Mason
Tom Riddle
Madam Puddifoot
Ginny Weasley
Eileen Prince
Lucian Bole
Gilderoy Lockhart
Lord Voldemort
FIM

View File

@ -0,0 +1 @@
753045 24ms 351

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,510 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with selection sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Add character to array
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacter array by key "name" using selection sort
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// Selection sort
for(int i = 0; i < mainCharacters.size() - 1; i++) {
// Initialize min
int min = i;
// Search for min
for(int j = i + 1; j < mainCharacters.size(); j++) {
// Compare
if(mainCharacters.get(j).getName().compareTo(mainCharacters.get(min).getName()) < 0) min = j;
// Increment comparisons
comparisons++;
}
// Swap
Character temp = mainCharacters.get(i);
mainCharacters.set(i, mainCharacters.get(min));
mainCharacters.set(min, temp);
}
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_selecao.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,27 @@
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1,696 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with recursive selection sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
// ------------------------- //
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "name" using recursive selection sort
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// Recursive selection sort
for(int i = 0; i < m - 1; i++) {
int min = i;
for(int j = i + 1; j < m; j++) {
comparisons++;
if(strcmp(character_getName(&mainCharacters[j]), character_getName(&mainCharacters[min])) < 0) min = j;
}
Character aux = mainCharacters[i];
mainCharacters[i] = mainCharacters[min];
mainCharacters[min] = aux;
}
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_selecaoRecursiva.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < m; i++) character_print(&mainCharacters[i]);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,27 @@
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 22ms 167

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,513 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with insertion sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Add character to mainCharacters array
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "dateOfBirth" using insertion sort, in draw case, order by key "name"
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// Insertion sort
for(int i = 1; i < mainCharacters.size(); i++) {
Character current = mainCharacters.get(i);
int j = i - 1;
// Compare based on "dateOfBirth"
while(j >= 0 && current.getBirthDate().compareTo(mainCharacters.get(j).getBirthDate()) < 0) {
mainCharacters.set(j + 1, mainCharacters.get(j));
j--;
comparisons++;
}
// In case of a tie in "dateOfBirth" compare based on "name"
while(j >= 0 && current.getBirthDate().compareTo(mainCharacters.get(j).getBirthDate()) == 0 && current.getName().compareTo(mainCharacters.get(j).getName()) < 0) {
mainCharacters.set(j + 1, mainCharacters.get(j));
j--;
comparisons++;
}
mainCharacters.set(j + 1, current);
}
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_insercao.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,27 @@
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 0ms 87

Binary file not shown.

View File

@ -0,0 +1,709 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with shell sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "eyeColour", in draw case, order by key "name"
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// ----------------- //
// Shell sort
for(int gap = m / 2; gap > 0; gap /= 2) {
for(int i = gap; i < m; i++) {
Character temp = mainCharacters[i];
int j;
for(j = i; j >= gap; j -= gap) {
if(strcmp(character_getEyeColour(&mainCharacters[j - gap]), character_getEyeColour(&temp)) > 0) {
comparisons++;
mainCharacters[j] = mainCharacters[j - gap];
}
else if(strcmp(character_getEyeColour(&mainCharacters[j - gap]), character_getEyeColour(&temp)) == 0) {
// In case of a draw in eyeColour, compare by name
comparisons++;
if(strcmp(character_getName(&mainCharacters[j - gap]), character_getName(&temp)) > 0) mainCharacters[j] = mainCharacters[j - gap];
else break;
}
else break;
}
mainCharacters[j] = temp;
}
}
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_shellsort.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < m; i++) character_print(&mainCharacters[i]);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,27 @@
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 25ms 0

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,538 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with heap sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// -------------------------- //
// Compare method
public int compareTo(Character other) {
// Compare by hair colour
int hairComparison = this.hairColour.compareTo(other.hairColour);
// If hair colour is the same, compare by name
if(hairComparison != 0) return hairComparison;
else return this.name.compareTo(other.name);
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// Helper method to heapify
static void heapify(ArrayList<Character> characters, int n, int root, int comparisons) {
int largest = root;
int left = 2 * root + 1;
int right = 2 * root + 2;
comparisons++;
if(left < n && characters.get(left).compareTo(characters.get(largest)) > 0) largest = left;
comparisons++;
if(right < n && characters.get(right).compareTo(characters.get(largest)) > 0) largest = right;
if(largest != root) {
Character swap = characters.get(root);
characters.set(root, characters.get(largest));
characters.set(largest, swap);
heapify(characters, n, largest, comparisons);
}
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Print character
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "hairColour" using insertion sort, in draw case, order by key "name"
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// Heap sort
for(int i = mainCharacters.size() / 2 - 1; i >= 0; i--) heapify(mainCharacters, mainCharacters.size(), i, comparisons);
for(int i = mainCharacters.size() - 1; i > 0; i--) {
// Swap the root (maximum element) with the last character
Character temp = mainCharacters.get(0);
mainCharacters.set(0, mainCharacters.get(i));
mainCharacters.set(i, temp);
// Heapify the reduced heap
heapify(mainCharacters, i, 0, comparisons);
}
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_heapsort.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,27 @@
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 0ms 521

Binary file not shown.

View File

@ -0,0 +1,706 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with quicksort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "house", in draw case, order by key "name"
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// Quick sort
int i = 0, j = 0;
for(i = 0; i < m; i++) {
for(j = i + 1; j < m; j++) {
comparisons++;
if(strcmp(character_getHouse(&mainCharacters[i]), character_getHouse(&mainCharacters[j])) > 0) {
Character temp = mainCharacters[i];
mainCharacters[i] = mainCharacters[j];
mainCharacters[j] = temp;
}
else if(strcmp(character_getHouse(&mainCharacters[i]), character_getHouse(&mainCharacters[j])) == 0) {
comparisons++;
if(strcmp(character_getName(&mainCharacters[i]), character_getName(&mainCharacters[j])) > 0) {
Character temp = mainCharacters[i];
mainCharacters[i] = mainCharacters[j];
mainCharacters[j] = temp;
}
}
}
}
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_quicksort.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < m; i++) character_print(&mainCharacters[i]);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,27 @@
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 33ms 60

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,565 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with counting sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// -------------------------- //
// Compare method
public int compareTo(Character other) {
// Compare by yearOfBirth
int yearOfBirthComparison = Integer.compare(this.yearOfBirth, other.yearOfBirth);
// If yearOfBirth is the same, compare by name
if(yearOfBirthComparison != 0) return yearOfBirthComparison;
else return this.name.compareTo(other.name);
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
// Helper method to find the maximum yearOfBirth in the mainCharacters array
private static int getMaxHeight(ArrayList<Character> characters) {
int max = Integer.MIN_VALUE;
for(Character character : characters) {
if(character.getYearOfBirth() > max) max = character.getYearOfBirth();
}
return max;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Print character
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "yearOfBirth" using counting sort, in draw case, order by key "name"
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// ----------------- //
// Counting sort
// Create an array to store the counts
int maxHeight = getMaxHeight(mainCharacters);
int[] count = new int[maxHeight + 1];
// Initialize the count array
for(int i = 0; i < mainCharacters.size(); i++) count[mainCharacters.get(i).getYearOfBirth()]++;
// Modify the count array to store the position of elements
for(int i = 1; i <= maxHeight; i++) count[i] += count[i - 1];
// Create a temporary array to store the sorted output
Character[] sortedCharacters = new Character[mainCharacters.size()];
// Build the sorted array
for(int i = mainCharacters.size() - 1; i >= 0; i--) {
int yob = mainCharacters.get(i).getYearOfBirth();
sortedCharacters[count[yob] - 1] = mainCharacters.get(i);
count[yob]--;
}
// Copy the sorted array back to the mainCharacters array
for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.set(i, sortedCharacters[i]);
// Use insertion sort to order by key "yearOfBirth" and "name" in draw case
for(int i = 1; i < mainCharacters.size(); i++) {
Character key = mainCharacters.get(i);
int j = i - 1;
while(j >= 0 && mainCharacters.get(j).getYearOfBirth() == key.getYearOfBirth()) {
// While and if comparisons
comparisons += 2;
if(mainCharacters.get(j).getName().compareTo(key.getName()) > 0) {
mainCharacters.set(j + 1, mainCharacters.get(j));
j--;
}
else break;
}
mainCharacters.set(j + 1, key);
}
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_countingsort.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,27 @@
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 0ms 493

Binary file not shown.

View File

@ -0,0 +1,706 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with bubble sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "hairColour", in draw case, order by key "name"
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// ----------------- //
// Bubble sort
for(int i = 0; i < m - 1; i++) {
for(int j = 0; j < m - i - 1; j++) {
comparisons++;
if(strcmp(character_getHairColour(&mainCharacters[j]), character_getHairColour(&mainCharacters[j + 1])) > 0) {
Character aux = mainCharacters[j];
mainCharacters[j] = mainCharacters[j + 1];
mainCharacters[j + 1] = aux;
}
else if(strcmp(character_getHairColour(&mainCharacters[j]), character_getHairColour(&mainCharacters[j + 1])) == 0) {
comparisons++;
if(strcmp(character_getName(&mainCharacters[j]), character_getName(&mainCharacters[j + 1])) > 0) {
Character aux = mainCharacters[j];
mainCharacters[j] = mainCharacters[j + 1];
mainCharacters[j + 1] = aux;
}
}
}
}
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_bolha.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < m; i++) character_print(&mainCharacters[i]);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,27 @@
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 24ms 98

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,575 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with merge sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// -------------------------- //
// Compare method
public int compareTo(Character other) {
// Compare by actorName
int actorNameComparison = this.actorName.compareTo(other.actorName);
// If actorName is the same, compare by name
if(actorNameComparison != 0) return actorNameComparison;
else return this.name.compareTo(other.name);
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
// Merge sort helper function
public static int mergeSort(ArrayList<Character> arr, int l, int r) {
if(l < r) {
int mid = (l + r) / 2;
int leftComparisons = mergeSort(arr, l, mid);
int rightComparisons = mergeSort(arr, mid + 1, r);
int mergeComparisons = merge(arr, l, mid, r);
return leftComparisons + rightComparisons + mergeComparisons;
}
return 0;
}
// Merge function
public static int merge(ArrayList<Character> arr, int l, int mid, int r) {
int comparisons = 0;
int n1 = mid - l + 1;
int n2 = r - mid;
ArrayList<Character> leftArr = new ArrayList<Character>();
ArrayList<Character> rightArr = new ArrayList<Character>();
for(int i = 0; i < n1; i++) leftArr.add(arr.get(l + i));
for(int i = 0; i < n2; i++) rightArr.add(arr.get(mid + 1 + i));
int i = 0, j = 0, k = l;
while(i < n1 && j < n2) {
// Compare characters and increment comparisons
comparisons++;
if(leftArr.get(i).getActorName().compareTo(rightArr.get(j).getActorName()) < 0 ||
(leftArr.get(i).getActorName().equals(rightArr.get(j).getActorName()) &&
leftArr.get(i).getName().compareTo(rightArr.get(j).getName()) <= 0)) {
arr.set(k, leftArr.get(i));
i++;
}
else {
arr.set(k, rightArr.get(j));
j++;
}
k++;
}
while (i < n1) {
arr.set(k, leftArr.get(i));
i++;
k++;
}
while (j < n2) {
arr.set(k, rightArr.get(j));
j++;
k++;
}
return comparisons;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Print character
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "actorName" using counting sort, in draw case, order by key "name"
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// ----------------- //
// Merge sort
comparisons = mergeSort(mainCharacters, 0, mainCharacters.size() - 1);
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_mergesort.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < mainCharacters.size(); i++) mainCharacters.get(i).print();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,27 @@
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 0ms 9522

Binary file not shown.

View File

@ -0,0 +1,765 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with radix sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Function to get the character at a specific position in a string
char getCharAtIndex(char *str, int index) {
if(index >= strlen(str)) return '\0';
else return str[strlen(str) - index - 1]; // Read characters from right to left
}
// Radix Sort for string keys
void radixSort(Character arr[], int n, int *comparisons) {
const int NUM_CHARS = 128; // Assuming ASCII characters
// Initialize buckets
Character output[n];
int *count = (int *)calloc(NUM_CHARS, sizeof(int));
if(count == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// Find the maximum length of the id string
int maxLen = 0;
for(int i = 0; i < n; i++) {
(*comparisons) += 2; // For and if
int len = strlen(character_getId(&arr[i]));
if(len > maxLen) maxLen = len;
}
// Perform counting sort for each character position
for(int pos = 0; pos < maxLen; pos++) {
(*comparisons)++; // For
// Count occurrences of characters
for(int i = 0; i < n; i++) {
(*comparisons) += 2; // For and getCharAtIndex
char ch = getCharAtIndex(character_getId(&arr[i]), pos);
count[ch]++;
}
// Accumulate counts to get positions
for(int i = 1; i < NUM_CHARS; i++) {
(*comparisons)++; // For
count[i] += count[i - 1];
}
// Build the output array
for(int i = n - 1; i >= 0; i--) {
(*comparisons) += 2; // For and getCharAtIndex
char ch = getCharAtIndex(character_getId(&arr[i]), pos);
output[count[ch] - 1] = arr[i];
count[ch]--;
}
// Copy output array back to the original array
for(int i = 0; i < n; i++) {
(*comparisons)++; // For
arr[i] = output[i];
}
// Reset count array
memset(count, 0, NUM_CHARS * sizeof(int));
}
free(count);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "id"
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// ----------------- //
// Radix sort
radixSort(mainCharacters, m, &comparisons);
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_radixsort.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < m; i++) character_print(&mainCharacters[i]);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,27 @@
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[3a0fe4df-2e40-4541-8d7f-13586f0b9294 ## Tom Riddle ## {Tom Riddle Senior} ## Ravenclaw ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1980 ## brown ## male ## dark ## false]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[861c4cde-2f0f-4796-8d8f-9492e74b2573 ## Luna Lovegood ## {Loony Lovegood} ## Ravenclaw ## pure-blood ## human ## hare ## false ## true ## Evanna Lynch ## true ## 13-02-1981 ## 1981 ## silver ## female ## blonde ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[eaea5eb3-48a3-41c6-9ea5-c695299bab16 ## Lisa Turpin ## {} ## Ravenclaw ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 27-03-1960 ## 1951 ## amber ## female ## sandy ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,526 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with partial selection sort
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// -------------------------- //
// Compare method
public int compareTo(Character other) {
// Compare by actorName
int actorNameComparison = this.actorName.compareTo(other.actorName);
// If actorName is the same, compare by name
if(actorNameComparison != 0) return actorNameComparison;
else return this.name.compareTo(other.name);
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Print character
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "name" using parcial selection sort
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// ----------------- //
// Parcial selection sort with k = 10
int k = 10;
for(int i = 0; i < Math.min(k, mainCharacters.size() - 1); i++) {
// Initialize min
int min = i;
// Find min among remaining unsorted elements
for(int j = i + 1; j < mainCharacters.size(); j++) {
// Compare
if(mainCharacters.get(j).getName().compareTo(mainCharacters.get(min).getName()) < 0) min = j;
comparisons++;
}
// Swap
Character temp = mainCharacters.get(i);
mainCharacters.set(i, mainCharacters.get(min));
mainCharacters.set(min, temp);
}
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_selecaoParcial.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < k; i++) mainCharacters.get(i).print();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,10 @@
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[6b59be3f-e527-422d-959d-79fcdb3b24eb ## Hedwig ## {} ## Hufflepuff ## muggle ## owl ## N/A ## false ## false ## Gizmo ## false ## 07-04-1979 ## 1954 ## amber ## female ## brown ## false]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1,719 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with partial insertion sort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "dateOfBirth" using partial insertion sort
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// ----------------- //
// Partial insertion sort with k = 10, order by dateOfBirth, including day, month and year, and name as secondary key
int k = 10;
for(int i = 1; i < m; i++) {
Character key = mainCharacters[i];
int j = i - 1;
while(j >= 0 && mainCharacters[j].birthDate.year > key.birthDate.year) {
mainCharacters[j + 1] = mainCharacters[j];
j--;
comparisons += 2;
}
while(j >= 0 && mainCharacters[j].birthDate.year == key.birthDate.year && mainCharacters[j].birthDate.month > key.birthDate.month) {
mainCharacters[j + 1] = mainCharacters[j];
j--;
comparisons += 2;
}
while(j >= 0 && mainCharacters[j].birthDate.year == key.birthDate.year && mainCharacters[j].birthDate.month == key.birthDate.month && mainCharacters[j].birthDate.day > key.birthDate.day) {
mainCharacters[j + 1] = mainCharacters[j];
j--;
comparisons += 2;
}
// In case of a tie, order by name
while(j >= 0 && mainCharacters[j].birthDate.year == key.birthDate.year && mainCharacters[j].birthDate.month == key.birthDate.month && mainCharacters[j].birthDate.day == key.birthDate.day && strcmp(character_getName(&mainCharacters[j]), character_getName(&key)) > 0) {
mainCharacters[j + 1] = mainCharacters[j];
j--;
comparisons += 2;
}
mainCharacters[j + 1] = key;
}
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_insercaoParcial.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < k; i++) character_print(&mainCharacters[i]);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,10 @@
[d59691a4-f830-4eb0-a819-a0fb00b7e80f ## Garrick Ollivander ## {} ## Hufflepuff ## muggle ## human ## N/A ## false ## false ## John Hurt ## true ## 04-10-1925 ## 1954 ## pale, silvery ## male ## brown ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[d58e7249-19d1-40bd-a43f-1da0497fe8aa ## Dolores Umbridge ## {} ## Slytherin ## half-blood ## human ## persian cat ## true ## false ## Imelda Staunton ## true ## 31-12-1926 ## 1926 ## brown ## female ## grey ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[b0620914-858d-46fc-8e6d-033c565e138b ## Mrs Norris ## {} ## Slytherin ## pure-blood ## cat ## N/A ## true ## false ## Maxime, Alanis and Tommy the cats ## true ## 31-12-1926 ## 1954 ## yellow ## female ## brown ## false]
[20354d7a-e4fe-47af-8ff6-187bca92f3f9 ## Pandora Lovegood ## {} ## Ravenclaw ## quarter-veela ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1959 ## dark ## female ## white ## true]
[fed624df-56d9-495e-9ad4-ea77000957e8 ## Petunia Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Fiona Shaw ## true ## 31-12-1926 ## 1954 ## yellow ## female ## blonde ## false]
[1413e1b3-2903-4a47-a2d5-e8abc5ce8014 ## Seamus Finnigan ## {O Flaherty, Seamus Finnegan} ## Gryffindor ## half-blood ## human ## N/A ## false ## true ## Devon Murray ## true ## 31-12-1926 ## 1960 ## amber ## male ## sandy ## true]
[57fe29d4-312a-4711-bd9a-c320253d9176 ## Victoire Weasley ## {} ## Slytherin ## pure-blood ## human ## N/A ## false ## true ## N/A ## true ## 31-12-1926 ## 1926 ## grey ## female ## blonde ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 0ms 746

View File

@ -0,0 +1,741 @@
/**
* @path TP02Q02 - Classe em C/Character.c
* @description C file that implements the Character class with partial heapsort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_CHARACTERS 405
#define FILE_PATH "/tmp/characters.csv"
#define MAX_UUID_SIZE 37
#define MAX_NAME_SIZE 30
#define MAX_ALTERNATE_NAMES 10
#define MAX_ALTERNATE_NAME_SIZE 35
#define MAX_HOUSE_SIZE 15
#define MAX_ANCESTRY_SIZE 15
#define MAX_SPECIES_SIZE 20
#define MAX_PATRONUS_SIZE 25
#define MAX_ACTOR_NAME_SIZE 35
#define MAX_EYE_COLOUR_SIZE 10
#define MAX_GENDER_SIZE 10
#define MAX_HAIR_COLOUR_SIZE 10
#define MAX_LINE_SIZE 300
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Date {
int day;
int month;
int year;
} Date;
typedef struct Character {
char *id;
char *name;
char *alternateNames[MAX_ALTERNATE_NAMES];
char *house;
char *ancestry;
char *species;
char *patronus;
bool hogwartsStaff;
bool hogwartsStudent;
char *actorName;
bool alive;
Date birthDate;
int yearOfBirth;
char *eyeColour;
char *gender;
char *hairColour;
bool wizard;
} Character;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Character characters[MAX_CHARACTERS];
int charactersLength = 0;
// ---------------------------------------------------------------------------------------------------- //
// Functions
bool isEnd(char* line) { return line[0] == 'F' && line[1] == 'I' && line[2] == 'M'; }
void substring(char *string, char *stringStart, int length) {
strncpy(string, stringStart, length);
string[length] = '\0';
}
void proccess_attribute(char *attribute, char **substringStart, char **substringEnd, bool isFirstAttribute, bool isStringArray) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
if(!isStringArray) {
if((*substringStart)[0] == '"') {
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '"');
}
else *substringEnd = strchr(*substringStart, ';');
// Get substring
if(*substringEnd) {
substring(attribute, *substringStart, *substringEnd - *substringStart);
if(*substringEnd[0] == '"') *substringEnd = *substringEnd + 1;
}
else strcpy(attribute, *substringStart);
// Set default value if attribute is empty
if(strcmp(attribute, "") == 0 || attribute[0] == '\n' || attribute[0] == '\r' || attribute[0] == '\0') strcpy(attribute, "N/A");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
}
else {
// Check if the first character is a [
if((*substringStart)[0] == '[') {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ']') strcpy(attribute, ""); // Case: []
else {
char *tempConcat = (char *) calloc(MAX_LINE_SIZE, sizeof(char));
*substringStart = *substringStart - 1;
while(1) {
*substringStart = *substringStart + 1;
if((*substringStart)[0] == ';') break;
else if((*substringStart)[0] == '\'') { // Case: "['example', 'example']"
*substringStart = *substringStart + 1;
*substringEnd = strchr(*substringStart, '\'');
// Get substring
if(*substringEnd) {
// Create tmp name
char tmp[MAX_LINE_SIZE];
substring(tmp, *substringStart, *substringEnd - *substringStart);
// Concat tempConcat with tmp
strcat(tempConcat, tmp);
strcat(tempConcat, ", ");
*substringStart = *substringEnd + 1;
}
}
}
// Get substring
strcpy(attribute, tempConcat);
// Clean "attribute" removing last 2 characters
attribute[strlen(attribute) - 2] = '\0';
}
}
*substringEnd = strchr(*substringStart, ';');
}
}
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Gets
char *character_getId(Character *character) { return character -> id; }
char *character_getName(Character *character) { return character -> name; }
char *character_getHouse(Character *character) { return character -> house; }
char *character_getAncestry(Character *character) { return character -> ancestry; }
char *character_getSpecies(Character *character) { return character -> species; }
char *character_getPatronus(Character *character) { return character -> patronus; }
bool character_getHogwartsStaff(Character *character) { return character -> hogwartsStaff; }
bool character_getHogwartsStudent(Character *character) { return character -> hogwartsStudent; }
char *character_getActorName(Character *character) { return character -> actorName; }
bool character_getAlive(Character *character) { return character -> alive; }
char *character_getEyeColour(Character *character) { return character -> eyeColour; }
char *character_getGender(Character *character) { return character -> gender; }
char *character_getHairColour(Character *character) { return character -> hairColour; }
bool character_getWizard(Character *character) { return character -> wizard; }
char *character_getYearOfBirth(Character *character) {
// "N/A" if yearOfBirth is -1
char *yearOfBirth = (char *) calloc(15, sizeof(char));
strcpy(yearOfBirth, "N/A");
if(character -> yearOfBirth != -1) sprintf(yearOfBirth, "%d", character -> yearOfBirth);
return yearOfBirth;
}
char *character_getBirthDate(Character *character) {
// "N/A" if day, month and year are -1
char *birthDate = (char *) calloc(15, sizeof(char));
strcpy(birthDate, "N/A");
if(character -> birthDate.day != -1 && character -> birthDate.month != -1 && character -> birthDate.year != -1) {
sprintf(birthDate, "%02d-%02d-%04d", character -> birthDate.day, character -> birthDate.month, character -> birthDate.year);
}
return birthDate;
}
char *character_getAlternateNames(Character *character) {
// Concatenate all alternate names with a comma
char *alternateNames = (char *) calloc(MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES, sizeof(char));
alternateNames[0] = '{';
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
if(strcmp(character -> alternateNames[i], "") != 0) {
strcat(alternateNames, character -> alternateNames[i]);
if(strcmp(character -> alternateNames[i + 1], "") != 0) strcat(alternateNames, ", ");
}
}
strcat(alternateNames, "}");
return alternateNames;
}
// Sets
void character_setId(Character *character, char *id) { strcpy(character -> id, id); }
void character_setName(Character *character, char *name) { strcpy(character -> name, name); }
void character_setHouse(Character *character, char *house) { strcpy(character -> house, house); }
void character_setAncestry(Character *character, char *ancestry) { strcpy(character -> ancestry, ancestry); }
void character_setSpecies(Character *character, char *species) { strcpy(character -> species, species); }
void character_setPatronus(Character *character, char *patronus) { strcpy(character -> patronus, patronus); }
void character_setHogwartsStaff(Character *character, bool hogwartsStaff) { character -> hogwartsStaff = hogwartsStaff; }
void character_setHogwartsStudent(Character *character, bool hogwartsStudent) { character -> hogwartsStudent = hogwartsStudent; }
void character_setActorName(Character *character, char *actorName) { strcpy(character -> actorName, actorName); }
void character_setAlive(Character *character, bool alive) { character -> alive = alive; }
void character_setYearOfBirth(Character *character, int yearOfBirth) { character -> yearOfBirth = yearOfBirth; }
void character_setEyeColour(Character *character, char *eyeColour) { strcpy(character -> eyeColour, eyeColour); }
void character_setGender(Character *character, char *gender) { strcpy(character -> gender, gender); }
void character_setHairColour(Character *character, char *hairColour) { strcpy(character -> hairColour, hairColour); }
void character_setWizard(Character *character, bool wizard) { character -> wizard = wizard; }
void character_setBirthDate(Character *character, char *birthDate) {
// Explode birthDate in format DD-MM-YYYY if in format DD-MM-YYYY
if(strlen(birthDate) >= 8 && strlen(birthDate) <= 10) {
char *token = strtok(birthDate, "-");
character -> birthDate.day = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.month = atoi(token);
token = strtok(NULL, "-");
character -> birthDate.year = atoi(token);
}
}
void character_setAlternateNames(Character *character, char *alternateNames) {
// Copy names to a temporary variable
char tempNames[MAX_ALTERNATE_NAME_SIZE * MAX_ALTERNATE_NAMES];
strcpy(tempNames, alternateNames);
// Separate names by comma
char *token = strtok(tempNames, ",");
int i = 0;
// Copy names to the character
while (token != NULL && i < MAX_ALTERNATE_NAMES) {
while (*token == ' ') token++;
int len = strlen(token);
while (len > 0 && token[len - 1] == ' ') {
token[len - 1] = '\0';
len--;
}
strcpy(character -> alternateNames[i++], token);
token = strtok(NULL, ",");
}
}
// Class
Character character_newBlank() {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, "");
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, "");
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, "");
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, "");
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, "");
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, "");
character.hogwartsStaff = false;
character.hogwartsStudent = false;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, "");
character.alive = false;
character.birthDate.day = -1;
character.birthDate.month = -1;
character.birthDate.year = -1;
character.yearOfBirth = -1;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, "");
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, "");
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, "");
character.wizard = false;
return character;
}
Character character_new(char *id, char *name, char *house, char *ancestry, char *species, char *patronus, bool hogwartsStaff, bool hogwartsStudent, char *actorName, bool alive, Date birthDate, int yearOfBirth, char *eyeColour, char *gender, char *hairColour, bool wizard) {
Character character;
character.id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(character.id, id);
character.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(character.name, name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
character.alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(character.alternateNames[i], "");
}
character.house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(character.house, house);
character.ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(character.ancestry, ancestry);
character.species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(character.species, species);
character.patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(character.patronus, patronus);
character.hogwartsStaff = hogwartsStaff;
character.hogwartsStudent = hogwartsStudent;
character.actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(character.actorName, actorName);
character.alive = alive;
character.birthDate = birthDate;
character.yearOfBirth = yearOfBirth;
character.eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(character.eyeColour, eyeColour);
character.gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(character.gender, gender);
character.hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(character.hairColour, hairColour);
character.wizard = wizard;
return character;
}
Character *character_clone(Character *character) {
Character *clone = (Character *) malloc(sizeof(Character));
clone -> id = (char *) calloc(MAX_UUID_SIZE, sizeof(char));
strcpy(clone -> id, character -> id);
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, character -> name);
for(int i = 0; i < MAX_ALTERNATE_NAMES; i++) {
clone -> alternateNames[i] = (char *) calloc(MAX_ALTERNATE_NAME_SIZE, sizeof(char));
strcpy(clone -> alternateNames[i], character -> alternateNames[i]);
}
clone -> house = (char *) calloc(MAX_HOUSE_SIZE, sizeof(char));
strcpy(clone -> house, character -> house);
clone -> ancestry = (char *) calloc(MAX_ANCESTRY_SIZE, sizeof(char));
strcpy(clone -> ancestry, character -> ancestry);
clone -> species = (char *) calloc(MAX_SPECIES_SIZE, sizeof(char));
strcpy(clone -> species, character -> species);
clone -> patronus = (char *) calloc(MAX_PATRONUS_SIZE, sizeof(char));
strcpy(clone -> patronus, character -> patronus);
clone -> hogwartsStaff = character -> hogwartsStaff;
clone -> hogwartsStudent = character -> hogwartsStudent;
clone -> actorName = (char *) calloc(MAX_ACTOR_NAME_SIZE, sizeof(char));
strcpy(clone -> actorName, character -> actorName);
clone -> alive = character -> alive;
clone -> birthDate = character -> birthDate;
clone -> yearOfBirth = character -> yearOfBirth;
clone -> eyeColour = (char *) calloc(MAX_EYE_COLOUR_SIZE, sizeof(char));
strcpy(clone -> eyeColour, character -> eyeColour);
clone -> gender = (char *) calloc(MAX_GENDER_SIZE, sizeof(char));
strcpy(clone -> gender, character -> gender);
clone -> hairColour = (char *) calloc(MAX_HAIR_COLOUR_SIZE, sizeof(char));
strcpy(clone -> hairColour, character -> hairColour);
clone -> wizard = character -> wizard;
return clone;
}
void character_print(Character *character) {
printf("[%s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s ## %s]\n",
character_getId(character),
character_getName(character),
character_getAlternateNames(character),
character_getHouse(character),
character_getAncestry(character),
character_getSpecies(character),
character_getPatronus(character),
character_getHogwartsStaff(character) ? "true" : "false",
character_getHogwartsStudent(character) ? "true" : "false",
character_getActorName(character),
character_getAlive(character) ? "true" : "false",
character_getBirthDate(character),
character_getYearOfBirth(character),
character_getEyeColour(character),
character_getGender(character),
character_getHairColour(character),
character_getWizard(character) ? "true" : "false"
);
}
Character character_read(char *line) {
Character character = character_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_LINE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true, false);
character_setId(&character, attribute);
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setName(&character, attribute);
// Get alternate names
proccess_attribute(attribute, &substringStart, &substringEnd, false, true);
character_setAlternateNames(&character, attribute);
// Get house
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHouse(&character, attribute);
// Get ancestry
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAncestry(&character, attribute);
// Get species
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setSpecies(&character, attribute);
// Get patronus
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setPatronus(&character, attribute);
// Get hogwarts staff
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStaff(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get hogwarts student
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHogwartsStudent(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get actor name
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setActorName(&character, attribute);
// Get alive
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setAlive(&character, strcmp(attribute, "VERDADEIRO") == 0);
// Get birth date
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setBirthDate(&character, attribute);
// Get year of birth
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setYearOfBirth(&character, atoi(attribute) == 0 ? -1 : atoi(attribute));
// Get eye colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setEyeColour(&character, attribute);
// Get gender
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setGender(&character, attribute);
// Get hair colour
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
character_setHairColour(&character, attribute);
// Get wizard
proccess_attribute(attribute, &substringStart, &substringEnd, false, false);
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n' || attribute[strlen(attribute) - 1] == '\r') attribute[strlen(attribute) - 1] = '\0';
character_setWizard(&character, strcmp(attribute, "VERDADEIRO") == 0);
return character;
}
Character *character_searchById(char *id) {
for(int i = 0; i < charactersLength; i++) {
if(!strcmp(characters[i].id, id)) return &characters[i];
}
return NULL;
}
// General
void startCharacters() {
// Open file
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(FILE_PATH, "r");
if(fp == NULL) {
perror("x Error opening file");
exit(EXIT_FAILURE);
}
// Skip first line
getline(&line, &len, fp);
// Read all lines
while((read = getline(&line, &len, fp)) != -1) {
// Read character from line
Character character = character_read(line);
characters[charactersLength++] = character;
if(charactersLength >= MAX_CHARACTERS) {
perror("x Max characters reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// Heapify function
void heapify(Character *characters, int n, int i, int *comparisons) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if(l < n) {
// Compare hairColour of characters
int comparison = strcmp(character_getHairColour(&characters[l]), character_getHairColour(&characters[largest]));
// In draw case, compare names
if(comparison == 0) comparison = strcmp(character_getName(&characters[l]), character_getName(&characters[largest]));
if(comparison > 0) largest = l;
(*comparisons) += 2;
}
if(r < n) {
// Compare hairColour of characters
int comparison = strcmp(character_getHairColour(&characters[r]), character_getHairColour(&characters[largest]));
// In draw case, compare names
if(comparison == 0) comparison = strcmp(character_getName(&characters[r]), character_getName(&characters[largest]));
if(comparison > 0) largest = r;
(*comparisons) += 2;
}
if(largest != i) {
Character aux = characters[i];
characters[i] = characters[largest];
characters[largest] = aux;
heapify(characters, n, largest, comparisons);
}
(*comparisons) += 3;
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
Character mainCharacters[MAX_CHARACTERS];
int m = 0;
char id[MAX_UUID_SIZE];
scanf(" %[^\n]s", id);
while(true) {
// Clean \n from the end of the string
if(id[strlen(id) - 1] == '\n' || id[strlen(id) - 1] == '\r') id[strlen(id) - 1] = '\0';
if(isEnd(id)) break;
else {
Character *character = character_searchById(id);
if(character) mainCharacters[m++] = *character;
// ------------------------- //
scanf(" %[^\n]s", id);
}
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "id"
// Start benchmark
clock_t startTime = clock();
int comparisons = 0;
// ----------------- //
// Heapsort with k = 10
int k = 10;
for(int i = m / 2 - 1; i >= 0; i--) {
comparisons++;
heapify(mainCharacters, m, i, &comparisons);
}
for(int i = m - 1; i >= 0; i--) {
comparisons++;
Character aux = mainCharacters[0];
mainCharacters[0] = mainCharacters[i];
mainCharacters[i] = aux;
heapify(mainCharacters, i, 0, &comparisons);
}
// ----------------- //
// Save benchmark in file
FILE *fp = fopen("753045_heapsortParcial.txt", "w");
fprintf(fp, "753045\t%.0fms\t%d", (double)(clock() - startTime) / CLOCKS_PER_SEC * 1000.0, comparisons);
fclose(fp);
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < k; i++) character_print(&mainCharacters[i]);
// ----------------- //
return 0;
}

View File

@ -0,0 +1,10 @@
[efa802c8-ae18-4ae1-a524-49df21d05939 ## Lord Voldemort ## {Tom Riddle, Tom Marvolo Riddle, You-Know-Who, He Who Must Not Be Named, the Dark Lord, the Heir of Slytherin} ## Slytherin ## half-blood ## human ## N/A ## false ## false ## Ralph Fiennes ## false ## 31-12-1926 ## 1926 ## Scarlet ## male ## bald ## true]
[6afb1960-febd-418d-b604-e50c1b59459b ## Bellatrix Lestrange ## {Bella} ## Slytherin ## pure-blood ## human ## N/A ## false ## false ## Helena Bonham Carter ## false ## 06-02-1950 ## 1951 ## brown ## female ## black ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[5a4c95db-947d-4914-a631-41e8d466328e ## Kendra Dumbledore ## {} ## Slytherin ## muggleborn ## human ## N/A ## false ## false ## N/A ## false ## 31-12-1926 ## 1925 ## dark ## female ## black ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]
[2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f ## Sirius Black ## {Padfoot, Snuffles} ## Gryffindor ## pure-blood ## human ## hare ## false ## false ## Gary Oldman ## false ## 03-11-1959 ## 1959 ## grey ## male ## black ## true]
[0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3 ## Dobby ## {} ## Slytherin ## pure-blood ## house-elf ## N/A ## false ## false ## Toby Jones ## false ## 06-02-1950 ## 1950 ## green ## male ## blond ## false]
[7614cf6e-689e-47ac-a976-b1e9997637e9 ## Dudley Dursley ## {} ## Slytherin ## muggle ## human ## N/A ## false ## false ## Harry Melling ## true ## 23-06-1980 ## 1980 ## blue ## male ## blond ## false]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM

View File

@ -0,0 +1 @@
753045 31ms 325

View File

@ -0,0 +1,206 @@
import java.io.*;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.nio.charset.*;
public class Arq
{
private static String nomeArquivo = "";
private static String charsetArquivo = "ISO-8859-1";
private static boolean write = false, read = false;
private static Formatter saida = null;
private static Scanner entrada = null;
public static boolean openWrite(String nomeArq, String charset) {
boolean resp = false;
close();
try{
saida = new Formatter(nomeArq, charset);
nomeArquivo = nomeArq;
resp = write = true;
} catch (Exception e) {}
return resp;
}
public static boolean openWrite(String nomeArq) {
return openWrite(nomeArq, charsetArquivo);
}
public static boolean openWriteClose(String nomeArq, String charset, String conteudo) {
boolean resp = openWrite(nomeArq, charset);
if(resp == true){
println(conteudo);
close();
}
return resp;
}
public static boolean openWriteClose(String nomeArq, String conteudo) {
return openWriteClose(nomeArq, charsetArquivo, conteudo);
}
public static boolean openRead(String nomeArq) {
return openRead(nomeArq, charsetArquivo);
}
public static boolean openRead(String nomeArq, String charset) {
boolean resp = false;
close();
try{
entrada = new Scanner(new File(nomeArq), charset);
nomeArquivo = nomeArq;
resp = read = true;
} catch (Exception e) {}
return resp;
}
public static String openReadClose(String nomeArq){
openRead(nomeArq);
String resp = readAll();
close();
return resp;
}
public static void close() {
if(write == true){
saida.close();
}
if(read == true){
entrada.close();
}
write = read = false;
nomeArquivo = "";
charsetArquivo = "ISO-8859-1";
}
public static long length(){
long resp = -1;
if(read != write){
File file = new File(nomeArquivo);
resp = file.length();
}
return resp;
}
public static void print(int x){
if(write == true){
saida.format( "%d", x);
}
}
public static void print(double x){
if(write == true){
saida.format( "%f", x);
}
}
public static void print(String x){
if(write == true){
saida.format( "%s", x);
}
}
public static void print(boolean x){
if(write == true){
saida.format( "%s", ((x) ? "true" : "false"));
}
}
public static void print(char x){
if(write == true){
saida.format( "%c", x);
}
}
public static void println(int x){
if(write == true){
saida.format( "%d\n", x);
}
}
public static void println(double x){
if(write == true){
saida.format( "%f\n", x);
}
}
public static void println(String x){
if(write == true){
saida.format( "%s\n", x);
}
}
public static void println(boolean x){
if(write == true){
saida.format( "%s\n", ((x) ? "true" : "false"));
}
}
public static void println(char x){
if(write == true){
saida.format( "%c\n", x);
}
}
public static int readInt(){
int resp = -1;
try{
resp = entrada.nextInt();
} catch (Exception e) {}
return resp;
}
public static char readChar(){
char resp = ' ';
try{
resp = (char)entrada.nextByte();
} catch (Exception e) {}
return resp;
}
public static double readDouble(){
double resp = -1;
try{
resp = Double.parseDouble(readString().replace(",","."));
} catch (Exception e) {}
return resp;
}
public static String readString(){
String resp = "";
try{
resp = entrada.next();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean readBoolean(){
boolean resp = false;
try{
resp = (entrada.next().equals("true")) ? true : false;
} catch (Exception e) {}
return resp;
}
public static String readLine(){
String resp = "";
try{
resp = entrada.nextLine();
} catch (Exception e) { System.out.println(e.getMessage()); }
return resp;
}
public static boolean hasNext(){
return entrada.hasNext();
}
public static String readAll(){
String resp = "";
while(hasNext()){
resp += (readLine() + "\n");
}
return resp;
}
}

View File

@ -0,0 +1,529 @@
/**
* @path TP02Q01 - Classe em Java/Characters.java
* @description Java class of all characters from Harry Potter's saga with partial quicksort algorithm
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2024-04-09
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.util.UUID;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
// ---------------------------------------------------------------------------------------------------- //
public class Character {
static SimpleDateFormat ddf = new SimpleDateFormat("dd-MM-yyyy");
// Global variables
public static final String FILE_PATH = "/tmp/characters.csv";
public static ArrayList<Character> allCharacters = new ArrayList<Character>();
// -------------------------- //
// Attributes
private UUID id;
private String name;
private ArrayList<String> alternateNames;
private String house;
private String ancestry;
private String species;
private String patronus;
private boolean hogwartsStaff;
private boolean hogwartsStudent;
private String actorName;
private boolean alive;
private Date birthDate;
private int yearOfBirth;
private String eyeColour;
private String gender;
private String hairColour;
private boolean wizard;
// -------------------------- //
// Empty constructor
public Character() {
this.id = UUID.randomUUID();
this.name = "";
this.alternateNames = new ArrayList<String>();
this.house = "";
this.ancestry = "";
this.species = "";
this.patronus = "";
this.hogwartsStaff = false;
this.hogwartsStudent = false;
this.actorName = "";
this.alive = false;
this.birthDate = null;
this.yearOfBirth = 0;
this.eyeColour = "";
this.gender = "";
this.hairColour = "";
this.wizard = false;
}
// Constructor
public Character(UUID id, String name, ArrayList<String> alternateNames, String house, String ancestry, String species, String patronus, boolean hogwartsStaff, boolean hogwartsStudent, String actorName, boolean alive, Date birthDate, int yearOfBirth, String eyeColour, String gender, String hairColour, boolean wizard) {
this.id = id;
this.name = name;
this.alternateNames = alternateNames;
this.house = house;
this.ancestry = ancestry;
this.species = species;
this.patronus = patronus;
this.hogwartsStaff = hogwartsStaff;
this.hogwartsStudent = hogwartsStudent;
this.actorName = actorName;
this.alive = alive;
this.birthDate = birthDate;
this.yearOfBirth = yearOfBirth;
this.eyeColour = eyeColour;
this.gender = gender;
this.hairColour = hairColour;
this.wizard = wizard;
}
// -------------------------- //
// Gets
public UUID getId() { return this.id; }
public String getName() { return this.name; }
public String getHouse() { return this.house; }
public String getAncestry() { return this.ancestry; }
public String getSpecies() { return this.species; }
public String getPatronus() { return this.patronus; }
public boolean getHogwartsStaff() { return this.hogwartsStaff; }
public boolean getHogwartsStudent() { return this.hogwartsStudent; }
public String getActorName() { return this.actorName; }
public boolean getAlive() { return this.alive; }
public Date getBirthDate() { return this.birthDate; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getEyeColour() { return this.eyeColour; }
public String getGender() { return this.gender; }
public String getHairColour() { return this.hairColour; }
public boolean getWizard() { return this.wizard; }
public String getAlternateNames() {
// Construct string e.g. {item1, item2, item3}
String alternateNames = "{";
for(int i = 0; i < this.alternateNames.size(); i++) {
alternateNames += this.alternateNames.get(i);
if(i < this.alternateNames.size() - 1) alternateNames += ", ";
}
alternateNames += "}";
return alternateNames;
}
// Sets
public void setId(UUID id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAlternateNames(ArrayList<String> alternateNames) { this.alternateNames = alternateNames; }
public void setHouse(String house) { this.house = house; }
public void setAncestry(String ancestry) { this.ancestry = ancestry; }
public void setSpecies(String species) { this.species = species; }
public void setPatronus(String patronus) { this.patronus = patronus; }
public void setHogwartsStaff(boolean hogwartsStaff) { this.hogwartsStaff = hogwartsStaff; }
public void setHogwartsStudent(boolean hogwartsStudent) { this.hogwartsStudent = hogwartsStudent; }
public void setActorNane(String actorName) { this.actorName = actorName; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setBirthDate(Date birthDate) { this.birthDate = birthDate; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setEyeColour(String eyeColour) { this.eyeColour = eyeColour; }
public void setGender(String gender) { this.gender = gender; }
public void setHairColour(String hairColour) { this.hairColour = hairColour; }
public void setWizard(boolean wizard) { this.wizard = wizard; }
// -------------------------- //
// Clone
public Character clone() { return new Character(this.id, this.name, this.alternateNames, this.house, this.ancestry, this.species, this.patronus, this.hogwartsStaff, this.hogwartsStudent, this.actorName, this.alive, this.birthDate, this.yearOfBirth, this.eyeColour, this.gender, this.hairColour, this.wizard); }
// -------------------------- //
// Print
public void print() {
System.out.println("["
+ this.getId() + " ## "
+ this.getName() + " ## "
+ this.getAlternateNames() + " ## "
+ (this.getHouse() == "" ? "N/A" : this.getHouse()) + " ## "
+ (this.getAncestry() == "" ? "N/A" : this.getAncestry()) + " ## "
+ (this.getSpecies() == "" ? "N/A" : this.getSpecies()) + " ## "
+ (this.getPatronus() == "" ? "N/A" : this.getPatronus()) + " ## "
+ (this.getHogwartsStaff() ? "true" : "false") + " ## "
+ (this.getHogwartsStudent() ? "true" : "false") + " ## "
+ (this.getActorName() == "" ? "N/A" : this.getActorName()) + " ## "
+ (this.getAlive() ? "true" : "false") + " ## "
+ (this.getBirthDate() == null ? "N/A" : ddf.format(this.getBirthDate())) + " ## "
+ (this.getYearOfBirth() == 0 ? "N/A" : this.getYearOfBirth()) + " ## "
+ (this.getEyeColour() == "" ? "N/A" : this.getEyeColour()) + " ## "
+ (this.getGender() == "" ? "N/A" : this.getGender()) + " ## "
+ (this.getHairColour() == "" ? "N/A" : this.getHairColour()) + " ## "
+ (this.getWizard() ? "true" : "false") + "]");
}
// -------------------------- //
public static ArrayList<String> extractNames(String input) {
ArrayList<String> names = new ArrayList<>();
// Clean input
String cleanedInput = input.substring(1, input.length() - 1);
// Check if input contains double quotes
if (cleanedInput.contains("\"\"")) {
String[] parts = cleanedInput.split("\", ");
for (String part : parts) names.add(part.replace("\"", "").replace("'", ""));
}
else {
// Split input by comma and space
String[] parts = cleanedInput.split(", ");
for (String part : parts) names.add(part.replace("'", ""));
}
return names;
}
// -------------------------- //
// Read
public void read(String line) {
// ---------------------- //
// Start position
int positionStart = 0;
int positionEnd = 0;
// ---------------------- //
// Set id
positionEnd = line.indexOf(";", positionStart);
this.setId(UUID.fromString(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setName(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alternate names
positionStart = positionEnd + 1;
if(line.charAt(positionStart) == '[') { // If first character is '[', has one or no names
if(line.charAt(positionStart + 1) == ']') {
positionEnd = positionStart + 2;
this.setAlternateNames(new ArrayList<String>());
}
else {
positionEnd = line.indexOf("];", positionStart);
this.setAlternateNames(extractNames(line.substring(positionStart, positionEnd++)));
}
}
// ---------------------- //
// Set house
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
this.setHouse(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set ancestry
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAncestry(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set species
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setSpecies(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set patronus
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setPatronus(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hogwartsStaff
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStaff("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set hogwartsStudent
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHogwartsStudent("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set actor name
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setActorNane(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set alive
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setAlive("VERDADEIRO".equals(line.substring(positionStart, positionEnd)));
// ---------------------- //
// Set date of birth
positionStart = positionEnd;
positionEnd = line.indexOf(";", ++positionStart);
try { this.setBirthDate(ddf.parse(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setBirthDate(null); }
// ---------------------- //
// Set year of birth
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
try { this.setYearOfBirth(Integer.parseInt(line.substring(positionStart, positionEnd))); }
catch(Exception e) { this.setYearOfBirth(0); }
// ---------------------- //
// Set eye colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setEyeColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set gender
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setGender(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set hair colour
positionStart = positionEnd + 1;
positionEnd = line.indexOf(";", positionStart);
this.setHairColour(line.substring(positionStart, positionEnd));
// ---------------------- //
// Set wizard
positionStart = positionEnd + 1;
this.setWizard("VERDADEIRO".equals(line.substring(positionStart, line.length())));
// ---------------------- //
}
// compareTo
public int compareTo(Character other) {
// Compare by house
int houseComparison = this.house.compareTo(other.house);
if(houseComparison != 0) return houseComparison;
else return this.name.compareTo(other.name);
}
// ---------------------------------------------------------------------------------------------------- //
// Read all characters function
public static void startCharacters() {
// Initialize variables
try {
FileInputStream fstream = new FileInputStream(FILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
// ---------------------- //
// Explode CSV file
String line = br.readLine();
while((line = br.readLine()) != null) {
// Initialize character
Character character = new Character();
// Read line
character.read(line);
// Add character to array
allCharacters.add(character);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Character searchById(UUID id, ArrayList<Character> characters) {
// Search for character
for(int i = 0; i < characters.size(); i++) {
if(characters.get(i).getId().equals(id)) return characters.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all characters in CSV file
startCharacters();
// ----------------------------------------------------------------- //
// #2 - Read input and print characters from pub.in id entries and add to mainCharacters array
// Initialize mainCharacters array
ArrayList<Character> mainCharacters = new ArrayList<Character>();
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize character
Character character = new Character();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
UUID id = UUID.fromString(line);
// Search for character
character = searchById(id, allCharacters);
// Add character to mainCharacters array
if(character != null) mainCharacters.add(character);
// Read line
line = inScanner.nextLine();
}
// ----------------------------------------------------------------- //
// #3 - Order mainCharacters array by key "house" using insertion sort, in draw case, order by key "name"
// Start benchmark
long startTime = System.currentTimeMillis();
int comparisons = 0;
// ----------------- //
// Parcial quicksort with k = 10
int k = 10;
for(int i = 0; i < mainCharacters.size() - 1; i++) {
for(int j = i + 1; j < mainCharacters.size() - 1; j++) {
comparisons++;
if(mainCharacters.get(i).compareTo(mainCharacters.get(j)) > 0) {
Character temp = mainCharacters.get(i);
mainCharacters.set(i, mainCharacters.get(j));
mainCharacters.set(j, temp);
}
else if(mainCharacters.get(i).compareTo(mainCharacters.get(j)) == 0) {
comparisons++;
if(mainCharacters.get(i).getName().compareTo(mainCharacters.get(j).getName()) > 0) {
Character temp = mainCharacters.get(i);
mainCharacters.set(i, mainCharacters.get(j));
mainCharacters.set(j, temp);
}
}
}
}
// ----------------- //
// Save benchmark in file
Arq.openWrite("753045_quicksortParcial.txt");
Arq.print("753045\t" + (System.currentTimeMillis() - startTime) + "ms\t" + comparisons);
Arq.close();
// ----------------- //
// Print mainCharacters array
for(int i = 0; i < k; i++) mainCharacters.get(i).print();
// ----------------------------------------------------------------- //
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //

View File

@ -0,0 +1,10 @@
[94c993f6-a443-4408-b908-6e78e223e8ac ## Aberforth Dumbledore ## {} ## Gryffindor ## half-blood ## human ## goat ## false ## false ## Jim McManus ## true ## 10-03-1960 ## 1960 ## blue ## male ## grey ## true]
[b415c867-1cff-455e-b194-748662ac2cca ## Albus Dumbledore ## {Professor Dumbledore} ## Gryffindor ## half-blood ## human ## Phoenix ## true ## false ## Richard Harris ## false ## 27-03-1960 ## 1881 ## blue ## male ## silver ## true]
[2a0615de-8aa4-41e7-9504-dd875f5f3f01 ## George Weasley ## {} ## Gryffindor ## pure-blood ## human ## N/A ## false ## true ## Oliver Phelps ## true ## 13-02-1981 ## 1954 ## amber ## male ## red ## true]
[9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8 ## Harry Potter ## {The Boy Who Lived, The Chosen One, Undesirable No. 1, Potty} ## Gryffindor ## half-blood ## human ## stag ## false ## true ## Daniel Radcliffe ## true ## 31-07-1980 ## 1980 ## green ## male ## black ## true]
[41cd0bbe-a943-431b-9bde-bb2cad3491a1 ## Lily Potter ## {} ## Gryffindor ## muggleborn ## human ## N/A ## false ## false ## Geraldine Somerville ## false ## 30-01-1960 ## 1960 ## green ## female ## blond ## true]
[ca3827f0-375a-4891-aaa5-f5e8a5bad225 ## Minerva McGonagall ## {} ## Gryffindor ## half-blood ## human ## tabby cat ## true ## false ## Dame Maggie Smith ## true ## 04-10-1925 ## 1925 ## grey ## female ## black ## true]
[933787c2-51e3-4eac-8a85-ab332cac0456 ## Miranda Gaushawk ## {} ## Gryffindor ## muggle ## human ## N/A ## false ## false ## N/A ## false ## 27-03-1960 ## 1960 ## blue ## female ## silver ## true]
[00434cd3-fcc7-44c7-8f98-7368415b4206 ## Miriam Strout ## {} ## Gryffindor ## half-blood ## human ## N/A ## false ## false ## N/A ## true ## 27-03-1960 ## 1954 ## blue ## female ## silver ## true]
[11b5ca88-64ad-41a4-9f36-317b66c290af ## Nearly Headless Nick ## {Sir Nicholas de Mimsy-Porpington, Sir Nicholas, Nick} ## Gryffindor ## pure-blood ## ghost ## N/A ## false ## false ## John Cleese ## false ## 27-03-1960 ## 1926 ## amber ## male ## sandy ## true]
[36bfefd0-e0bb-4d11-be98-d1ef6117a77a ## Rubeus Hagrid ## {Professor Hagrid, Hagger} ## Gryffindor ## half-blood ## half-giant ## N/A ## true ## false ## Robbie Coltrane ## true ## 06-12-1928 ## 1928 ## black ## male ## black ## true]

View File

@ -0,0 +1,28 @@
9e3f7ce4-b9a7-4244-b709-dae5c1f1d4a8
1413e1b3-2903-4a47-a2d5-e8abc5ce8014
ca3827f0-375a-4891-aaa5-f5e8a5bad225
36bfefd0-e0bb-4d11-be98-d1ef6117a77a
20354d7a-e4fe-47af-8ff6-187bca92f3f9
57fe29d4-312a-4711-bd9a-c320253d9176
b415c867-1cff-455e-b194-748662ac2cca
5a4c95db-947d-4914-a631-41e8d466328e
861c4cde-2f0f-4796-8d8f-9492e74b2573
2cfd2d4b-5d1e-4dc5-8837-37a97c7e2f2f
41cd0bbe-a943-431b-9bde-bb2cad3491a1
2a0615de-8aa4-41e7-9504-dd875f5f3f01
11b5ca88-64ad-41a4-9f36-317b66c290af
eaea5eb3-48a3-41c6-9ea5-c695299bab16
0d8ea37f-35c4-4c7d-9dd2-8ccd96b0a2b3
b0620914-858d-46fc-8e6d-033c565e138b
6b59be3f-e527-422d-959d-79fcdb3b24eb
fed624df-56d9-495e-9ad4-ea77000957e8
d58e7249-19d1-40bd-a43f-1da0497fe8aa
3a0fe4df-2e40-4541-8d7f-13586f0b9294
6afb1960-febd-418d-b604-e50c1b59459b
efa802c8-ae18-4ae1-a524-49df21d05939
933787c2-51e3-4eac-8a85-ab332cac0456
94c993f6-a443-4408-b908-6e78e223e8ac
00434cd3-fcc7-44c7-8f98-7368415b4206
7614cf6e-689e-47ac-a976-b1e9997637e9
d59691a4-f830-4eb0-a819-a0fb00b7e80f
FIM