add: TP02Q02 solved

This commit is contained in:
Pedro Lopes 2023-10-24 08:43:23 -03:00
parent 23f0bcd709
commit c5309df2e4
No known key found for this signature in database
GPG Key ID: 1B416E26DD161BE9
5 changed files with 478 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,357 @@
/**
* @path TP02Q02 - Classe em C/Player.c
* @description C file that implements the Player class.
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2023-09-27
*/
// ---------------------------------------------------------------------------------------------------- //
// Includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// ---------------------------------------------------------------------------------------------------- //
// Constants
#define MAX_PLAYERS 4000
#define FILE_PATH "/tmp/players.csv"
#define MAX_NAME_SIZE 40
#define MAX_COLLEGE_SIZE 60
#define MAX_BIRTH_CITY_SIZE 40
#define MAX_BIRTH_STATE_SIZE 40
#define MAX_LINE_SIZE 300
#define MAX_ATTRIBUTE_SIZE 100
// ---------------------------------------------------------------------------------------------------- //
// Structs
typedef struct Player {
int id;
char *name;
int height;
int weight;
int birthYear;
char *birthCity;
char *birthState;
char *college;
} Player;
// ---------------------------------------------------------------------------------------------------- //
// Global variables
Player players[MAX_PLAYERS];
int playersLength = 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) {
// Skip first comma
if(!isFirstAttribute) {
if(*substringEnd != NULL) *substringStart = *substringEnd + 1;
else *substringStart = *substringEnd;
}
// Get next comma
*substringEnd = strchr(*substringStart, ',');
// Get substring
if(*substringEnd) substring(attribute, *substringStart, *substringEnd - *substringStart);
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, "nao informado");
// Clean \n from the end of the string
if(attribute[strlen(attribute) - 1] == '\n') attribute[strlen(attribute) - 1] = '\0';
}
// ---------------------------------------------------------------------------------------------------- //
// Methods signatures
// Class
Player player_newBlank();
Player player_new(int id, char *name, int height, int weight, int birthYear, char *birthCity, char *birthState, char *college);
Player *player_clone(Player *player);
void player_print(Player *player);
Player player_read(char *line);
Player *player_searchById(int id);
// Gets
int player_getId(Player *player);
char *player_getName(Player *player);
int player_getHeight(Player *player);
int player_getWeight(Player *player);
char *player_getCollege(Player *player);
int player_getBirthYear(Player *player);
char *player_getBirthCity(Player *player);
char *player_getBirthState(Player *player);
// Sets
void player_setId(Player *player, int id);
void player_setName(Player *player, char *name);
void player_setHeight(Player *player, int height);
void player_setWeight(Player *player, int weight);
void player_setCollege(Player *player, char *college);
void player_setBirthYear(Player *player, int birthYear);
void player_setBirthCity(Player *player, char *birthCity);
void player_setBirthState(Player *player, char *birthState);
// General
void startPlayers();
// ---------------------------------------------------------------------------------------------------- //
// Methods implementations
// Class
Player player_newBlank() {
Player player;
player.id = -1;
player.height = -1;
player.weight = -1;
player.birthYear = -1;
player.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(player.name, "");
player.birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char));
strcpy(player.birthCity, "");
player.birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char));
strcpy(player.birthState, "");
player.college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char));
strcpy(player.college, "");
return player;
}
Player player_new(int id, char *name, int height, int weight, int birthYear, char *birthCity, char *birthState, char *college) {
Player player;
player.id = id;
player.height = height;
player.weight = weight;
player.birthYear = birthYear;
player.name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(player.name, name);
player.birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char));
strcpy(player.birthCity, birthCity);
player.birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char));
strcpy(player.birthState, birthState);
player.college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char));
strcpy(player.college, college);
return player;
}
Player *player_clone(Player *player) {
Player *clone = (Player *) malloc(sizeof(Player));
clone -> id = player -> id;
clone -> height = player -> height;
clone -> weight = player -> weight;
clone -> name = (char *) calloc(MAX_NAME_SIZE, sizeof(char));
strcpy(clone -> name, player -> name);
clone -> birthCity = (char *) calloc(MAX_BIRTH_CITY_SIZE, sizeof(char));
strcpy(clone -> birthCity, player -> birthCity);
clone -> birthState = (char *) calloc(MAX_BIRTH_STATE_SIZE, sizeof(char));
strcpy(clone -> birthState, player -> birthState);
clone -> college = (char *) calloc(MAX_COLLEGE_SIZE, sizeof(char));
strcpy(clone -> college, player -> college);
return clone;
}
void player_print(Player *player) {
printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n",
player -> id,
player -> name,
player -> height,
player -> weight,
player -> birthYear,
player -> college,
player -> birthCity,
player -> birthState
);
}
Player player_read(char *line) {
Player player = player_newBlank();
char *substringStart = line;
char *substringEnd = NULL;
char attribute[MAX_ATTRIBUTE_SIZE];
// Get id
proccess_attribute(attribute, &substringStart, &substringEnd, true);
player_setId(&player, atoi(attribute));
// Get name
proccess_attribute(attribute, &substringStart, &substringEnd, false);
player_setName(&player, attribute);
// Get height
proccess_attribute(attribute, &substringStart, &substringEnd, false);
player_setHeight(&player, atoi(attribute));
// Get weight
proccess_attribute(attribute, &substringStart, &substringEnd, false);
player_setWeight(&player, atoi(attribute));
// Get college
proccess_attribute(attribute, &substringStart, &substringEnd, false);
player_setCollege(&player, attribute);
// Get birth year
proccess_attribute(attribute, &substringStart, &substringEnd, false);
player_setBirthYear(&player, atoi(attribute));
// Get birth city
proccess_attribute(attribute, &substringStart, &substringEnd, false);
player_setBirthCity(&player, attribute);
// Get birth state
proccess_attribute(attribute, &substringStart, &substringEnd, false);
player_setBirthState(&player, attribute);
return player;
}
Player *player_searchById(int id) {
for(int i = 0; i < playersLength; i++) {
if(player_getId(&players[i]) == id) return &players[i];
}
return NULL;
}
// Gets
int player_getId(Player *player) { return player -> id; }
char *player_getName(Player *player) { return player -> name; }
int player_getHeight(Player *player) { return player -> height; }
int player_getWeight(Player *player) { return player -> weight; }
char *player_getCollege(Player *player) { return player -> college; }
int player_getBirthYear(Player *player) { return player -> birthYear; }
char *player_getBirthCity(Player *player) { return player -> birthCity; }
char *player_getBirthState(Player *player) { return player -> birthState; }
// Sets
void player_setId(Player *player, int id) { player -> id = id; }
void player_setName(Player *player, char *name) { strcpy(player -> name, name); }
void player_setHeight(Player *player, int height) { player -> height = height; }
void player_setWeight(Player *player, int weight) { player -> weight = weight; }
void player_setBirthYear(Player *player, int birthYear) { player -> birthYear = birthYear; }
void player_setBirthCity(Player *player, char *birthCity) { strcpy(player -> birthCity, birthCity); }
void player_setBirthState(Player *player, char *birthState) { strcpy(player -> birthState, birthState); }
void player_setCollege(Player *player, char *college) { strcpy(player -> college, college); }
// General
void startPlayers() {
// 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 player from line
Player player = player_read(line);
players[playersLength++] = player;
if(playersLength >= MAX_PLAYERS) {
perror("x Max players reached");
exit(EXIT_FAILURE);
}
}
// Close file and free memory
fclose(fp);
if(line) free(line);
}
// ---------------------------------------------------------------------------------------------------- //
// Main
int main() {
// ----------------------------------------------------------------- //
// #1 - Start - Read all players in CSV file
startPlayers();
// ----------------------------------------------------------------- //
// #2 - Read input and print players from pub.in id entries
char in[5];
scanf(" %[^\n]s", in);
while(true) {
if(isEnd(in)) break;
else {
int id = atoi(in);
Player *player = player_searchById(id);
if(player) player_print(player);
else printf("x Player not found!\n");
// ------------------------- //
scanf(" %[^\n]s", in);
}
}
return 0;
}

View File

@ -0,0 +1,40 @@
[106 ## Walt Lautenbach ## 188 ## 83 ## 1922 ## University of Wisconsin ## nao informado ## nao informado]
[1228 ## Willie Wise ## 196 ## 95 ## 1947 ## Drake University ## San Francisco ## California]
[1307 ## James Hardy ## 203 ## 99 ## 1956 ## University of San Francisco ## Knoxville ## Alabama]
[1397 ## Joe Barry ## 193 ## 88 ## 1969 ## Georgia Institute of Technology ## Oakland ## California]
[1412 ## Darrell Griffith ## 193 ## 86 ## 1958 ## University of Louisville ## Louisville ## Kentucky]
[1554 ## Clark Kellogg ## 201 ## 102 ## 1961 ## Ohio State University ## Cleveland ## Ohio]
[1585 ## Trent Tucker ## 196 ## 87 ## 1959 ## University of Minnesota ## Tarboro ## North Carolina]
[1669 ## Stuart Gray ## 213 ## 106 ## 1963 ## University of California - Los Angeles ## Panama Canal Zone ## Panama]
[1750 ## Terry Porter ## 190 ## 88 ## 1963 ## University of Wisconsin-Stevens Point ## Milwaukee ## Wisconsin]
[1928 ## Andrew Lang ## 211 ## 111 ## 1966 ## University of Arkansas ## Pine Bluff ## Arkansas]
[1961 ## Anthony Taylor ## 193 ## 79 ## 1965 ## University of Oregon ## Los Angeles ## California]
[2107 ## Tony Smith ## 201 ## 95 ## 1968 ## nao informado ## nao informado ## nao informado]
[2108 ## Felton Spencer ## 213 ## 120 ## 1968 ## University of Louisville ## Louisville ## Kentucky]
[2127 ## Terrell Brandon ## 180 ## 81 ## 1970 ## University of Oregon ## Portland ## Oregon]
[2159 ## Dikembe Mutombo* ## 218 ## 111 ## 1966 ## Georgetown University ## Kinshasa ## Democratic Republic of the Congo]
[2317 ## Derrick Alston ## 211 ## 102 ## 1972 ## Duquesne University ## Bronx ## New York]
[2453 ## Bruce Bowen ## 201 ## 83 ## 1971 ## California State University - Fullerton ## Merced ## California]
[2462 ## Tony Delk ## 185 ## 85 ## 1974 ## University of Kentucky ## Covington ## Tennessee]
[2681 ## Jumaine Jones ## 203 ## 98 ## 1979 ## University of Georgia ## Cocoa ## Florida]
[2735 ## Mark Madsen ## 206 ## 108 ## 1976 ## Stanford University ## Walnut Creek ## California]
[2806 ## Antonis Fotsis ## 208 ## 99 ## 1981 ## nao informado ## Athens ## Greece]
[2861 ## Gordan Giricek ## 198 ## 95 ## 1977 ## nao informado ## Zagreb ## Croatia]
[2996 ## Matt Freije ## 208 ## 108 ## 1981 ## Vanderbilt University ## Overland Park ## Kansas]
[3059 ## Noel Felix ## 206 ## 102 ## 1981 ## California State University - Fresno ## Los Angeles ## California]
[3117 ## Hakim Warrick ## 206 ## 99 ## 1982 ## Syracuse University ## Philadelphia ## Pennsylvania]
[3150 ## Randy Foye ## 193 ## 96 ## 1983 ## Villanova University ## Newark ## New Jersey]
[3154 ## Daniel Gibson ## 188 ## 86 ## 1986 ## University of Texas at Austin ## Houston ## Texas]
[3182 ## Chris Quinn ## 188 ## 83 ## 1983 ## University of Notre Dame ## New Orleans ## Louisiana]
[3229 ## Spencer Hawes ## 216 ## 111 ## 1988 ## University of Washington ## Seattle ## Washington]
[3292 ## Steven Hill ## 213 ## 112 ## 1985 ## University of Arkansas ## Chanute ## Kansas]
[3529 ## Jordan Williams ## 188 ## 83 ## 1951 ## New Mexico State University ## New Haven ## Connecticut]
[3564 ## Kris Joseph ## 201 ## 95 ## 1988 ## Syracuse University ## Montreal ## Canada]
[3565 ## Michael Kidd-Gilchrist ## 201 ## 105 ## 1993 ## University of Kentucky ## Philadelphia ## Pennsylvania]
[3670 ## Peyton Siva ## 183 ## 83 ## 1990 ## University of Louisville ## Seattle ## Washington]
[3830 ## Christian Wood ## 211 ## 99 ## 1995 ## University of Nevada - Las Vegas ## Long Beach ## California]
[3869 ## Brandon Ingram ## 206 ## 86 ## 1997 ## Duke University ## Kinston ## North Carolina]
[3879 ## Caris LeVert ## 201 ## 92 ## 1994 ## University of Michigan ## Columbus ## Ohio]
[70 ## Robert Hahn ## 208 ## 108 ## 1925 ## North Carolina State University ## nao informado ## nao informado]
[707 ## Jim Caldwell ## 208 ## 108 ## 1943 ## Georgia Institute of Technology ## Durham ## North Carolina]
[723 ## Dave Lattin ## 198 ## 102 ## 1943 ## University of Texas at El Paso ## Houston ## Texas]

View File

@ -0,0 +1,41 @@
106
1228
1307
1397
1412
1554
1585
1669
1750
1928
1961
2107
2108
2127
2159
2317
2453
2462
2681
2735
2806
2861
2996
3059
3117
3150
3154
3182
3229
3292
3529
3564
3565
3670
3830
3869
3879
70
707
723
FIM

View File

@ -0,0 +1,40 @@
[106 ## Walt Lautenbach ## 188 ## 83 ## 1922 ## University of Wisconsin ## nao informado ## nao informado]
[1228 ## Willie Wise ## 196 ## 95 ## 1947 ## Drake University ## San Francisco ## California]
[1307 ## James Hardy ## 203 ## 99 ## 1956 ## University of San Francisco ## Knoxville ## Alabama]
[1397 ## Joe Barry ## 193 ## 88 ## 1969 ## Georgia Institute of Technology ## Oakland ## California]
[1412 ## Darrell Griffith ## 193 ## 86 ## 1958 ## University of Louisville ## Louisville ## Kentucky]
[1554 ## Clark Kellogg ## 201 ## 102 ## 1961 ## Ohio State University ## Cleveland ## Ohio]
[1585 ## Trent Tucker ## 196 ## 87 ## 1959 ## University of Minnesota ## Tarboro ## North Carolina]
[1669 ## Stuart Gray ## 213 ## 106 ## 1963 ## University of California - Los Angeles ## Panama Canal Zone ## Panama]
[1750 ## Terry Porter ## 190 ## 88 ## 1963 ## University of Wisconsin-Stevens Point ## Milwaukee ## Wisconsin]
[1928 ## Andrew Lang ## 211 ## 111 ## 1966 ## University of Arkansas ## Pine Bluff ## Arkansas]
[1961 ## Anthony Taylor ## 193 ## 79 ## 1965 ## University of Oregon ## Los Angeles ## California]
[2107 ## Tony Smith ## 201 ## 95 ## 1968 ## nao informado ## nao informado ## nao informado]
[2108 ## Felton Spencer ## 213 ## 120 ## 1968 ## University of Louisville ## Louisville ## Kentucky]
[2127 ## Terrell Brandon ## 180 ## 81 ## 1970 ## University of Oregon ## Portland ## Oregon]
[2159 ## Dikembe Mutombo* ## 218 ## 111 ## 1966 ## Georgetown University ## Kinshasa ## Democratic Republic of the Congo]
[2317 ## Derrick Alston ## 211 ## 102 ## 1972 ## Duquesne University ## Bronx ## New York]
[2453 ## Bruce Bowen ## 201 ## 83 ## 1971 ## California State University - Fullerton ## Merced ## California]
[2462 ## Tony Delk ## 185 ## 85 ## 1974 ## University of Kentucky ## Covington ## Tennessee]
[2681 ## Jumaine Jones ## 203 ## 98 ## 1979 ## University of Georgia ## Cocoa ## Florida]
[2735 ## Mark Madsen ## 206 ## 108 ## 1976 ## Stanford University ## Walnut Creek ## California]
[2806 ## Antonis Fotsis ## 208 ## 99 ## 1981 ## nao informado ## Athens ## Greece]
[2861 ## Gordan Giricek ## 198 ## 95 ## 1977 ## nao informado ## Zagreb ## Croatia]
[2996 ## Matt Freije ## 208 ## 108 ## 1981 ## Vanderbilt University ## Overland Park ## Kansas]
[3059 ## Noel Felix ## 206 ## 102 ## 1981 ## California State University - Fresno ## Los Angeles ## California]
[3117 ## Hakim Warrick ## 206 ## 99 ## 1982 ## Syracuse University ## Philadelphia ## Pennsylvania]
[3150 ## Randy Foye ## 193 ## 96 ## 1983 ## Villanova University ## Newark ## New Jersey]
[3154 ## Daniel Gibson ## 188 ## 86 ## 1986 ## University of Texas at Austin ## Houston ## Texas]
[3182 ## Chris Quinn ## 188 ## 83 ## 1983 ## University of Notre Dame ## New Orleans ## Louisiana]
[3229 ## Spencer Hawes ## 216 ## 111 ## 1988 ## University of Washington ## Seattle ## Washington]
[3292 ## Steven Hill ## 213 ## 112 ## 1985 ## University of Arkansas ## Chanute ## Kansas]
[3529 ## Jordan Williams ## 188 ## 83 ## 1951 ## New Mexico State University ## New Haven ## Connecticut]
[3564 ## Kris Joseph ## 201 ## 95 ## 1988 ## Syracuse University ## Montreal ## Canada]
[3565 ## Michael Kidd-Gilchrist ## 201 ## 105 ## 1993 ## University of Kentucky ## Philadelphia ## Pennsylvania]
[3670 ## Peyton Siva ## 183 ## 83 ## 1990 ## University of Louisville ## Seattle ## Washington]
[3830 ## Christian Wood ## 211 ## 99 ## 1995 ## University of Nevada - Las Vegas ## Long Beach ## California]
[3869 ## Brandon Ingram ## 206 ## 86 ## 1997 ## Duke University ## Kinston ## North Carolina]
[3879 ## Caris LeVert ## 201 ## 92 ## 1994 ## University of Michigan ## Columbus ## Ohio]
[70 ## Robert Hahn ## 208 ## 108 ## 1925 ## North Carolina State University ## nao informado ## nao informado]
[707 ## Jim Caldwell ## 208 ## 108 ## 1943 ## Georgia Institute of Technology ## Durham ## North Carolina]
[723 ## Dave Lattin ## 198 ## 102 ## 1943 ## University of Texas at El Paso ## Houston ## Texas]