Add files via upload

gabaritos para a classe.
This commit is contained in:
Felipe Domingos 2023-10-16 17:39:10 -03:00 committed by GitHub
parent b22aecd30d
commit ba50db8fa5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 566 additions and 0 deletions

357
tps/gabaritos/Player.c Normal file
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;
}

209
tps/gabaritos/Player.java Normal file
View File

@ -0,0 +1,209 @@
/**
* @path TP02Q01 - Classe em Java/Player.java
* @description Java class of a player from the NBA database
* @author Pedro Lopes - github.com/httpspedroh
* @version 1.0
* @update 2023-09-27
*/
// ---------------------------------------------------------------------------------------------------- //
// Imports
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
// ---------------------------------------------------------------------------------------------------- //
public class Player {
// Global variables
public static final String FILE_PATH = "/tmp/players.csv";
public static ArrayList<Player> allPlayers = new ArrayList<Player>();
// -------------------------- //
// Attributes
private int id;
private String name;
private int height;
private int weight;
private String college;
private int yearOfBirth;
private String birthCity;
private String birthState;
// Empty constructor
public Player() {
this.id = 0;
this.name = "";
this.height = 0;
this.weight = 0;
this.college = "";
this.yearOfBirth = 0;
this.birthCity = "";
this.birthState = "";
}
// Constructor
public Player(int id, String name, int height, int weight, String college, int yearOfBirth, String birthCity, String birthState) {
this.id = id;
this.name = name;
this.height = height;
this.weight = weight;
this.college = college;
this.yearOfBirth = yearOfBirth;
this.birthCity = birthCity;
this.birthState = birthState;
}
// Gets
public int getId() { return this.id; }
public String getName() { return this.name; }
public int getHeight() { return this.height; }
public int getWeight() { return this.weight; }
public String getCollege() { return this.college; }
public int getYearOfBirth() { return this.yearOfBirth; }
public String getBirthCity() { return this.birthCity; }
public String getBirthState() { return this.birthState; }
// Sets
public void setId(int id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setHeight(int height) { this.height = height; }
public void setWeight(int weight) { this.weight = weight; }
public void setCollege(String college) { this.college = college; }
public void setYearOfBirth(int yearOfBirth) { this.yearOfBirth = yearOfBirth; }
public void setBirthCity(String birthCity) { this.birthCity = birthCity; }
public void setBirthState(String birthState) { this.birthState = birthState; }
// Clone
public Player clone() { return new Player(this.id, this.name, this.height, this.weight, this.college, this.yearOfBirth, this.birthCity, this.birthState); }
// Print
public void print() {
System.out.printf("[%d ## %s ## %d ## %d ## %d ## %s ## %s ## %s]\n",
this.id, this.name, this.height, this.weight, this.yearOfBirth, this.college, this.birthCity, this.birthState);
}
// Read
public void read(String line) {
// Split line by ","
String[] splitted = line.split(",", -1);
// Fill empty attributes
for(int i = 0; i < splitted.length; i++) {
if(splitted[i].equals("")) splitted[i] = "nao informado";
}
// Set attributes
this.id = Integer.parseInt(splitted[0]);
this.name = splitted[1];
this.height = Integer.parseInt(splitted[2]);
this.weight = Integer.parseInt(splitted[3]);
this.college = splitted[4];
this.yearOfBirth = Integer.parseInt(splitted[5]);
this.birthCity = splitted[6];
this.birthState = splitted[7];
}
// ---------------------------------------------------------------------------------------------------- //
// Read all players function
public static void startPlayers() {
// 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 player
Player player = new Player();
// Read line
player.read(line);
// Add player to array
allPlayers.add(player);
}
// Close CSV file
fstream.close();
}
catch(IOException e) { e.printStackTrace(); }
}
// ---------------------------------------------------------------------------------------------------- //
// Search by id function
public static Player searchById(int id, ArrayList<Player> players) {
// Search for player
for(int i = 0; i < players.size(); i++) {
if(players.get(i).getId() == id) return players.get(i);
}
return null;
}
// ---------------------------------------------------------------------------------------------------- //
public static void main(String[] args) {
// ----------------------------------------------------------------- //
// #1 - Start - Read all players in CSV file
startPlayers();
// ----------------------------------------------------------------- //
// #2 - Read input and print players from pub.in id entries
// Initialize scanner
Scanner inScanner = new Scanner(System.in);
// Initialize player
Player player = new Player();
// Read first line
String line = inScanner.nextLine();
// While line is not "FIM"
while(!line.equals("FIM")) {
// Get id
int id = Integer.parseInt(line);
// Search for player
player = searchById(id, allPlayers);
// Print player
if(player != null) player.print();
else System.out.println("x Player not found!");
// Read line
line = inScanner.nextLine();
}
// Close scanner
inScanner.close();
}
}
// ---------------------------------------------------------------------------------------------------- //