Professional Documents
Culture Documents
PR3 - Lenguaje C - Archivos C
PR3 - Lenguaje C - Archivos C
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "city.h"
#include <stdbool.h>
#include <limits.h>
#include "error.h"
city->last_update->day = date->day;
city->last_update->month = date->month;
city->last_update->year = date->year;
city->population = population;
city->cases = cases;
city->critical_cases = critical_cases;
city->deaths = deaths;
city->recovered = recovered;
city->medical_beds = medical_beds;
return OK;
}
// Free a city
void city_free(tCity * city){
assert(city != NULL);
if (city->last_update != NULL) {
free(city->last_update);
city->last_update = NULL;
}
if (strcmp(city1->name, city2->name) != 0) {
// cities are different
return false;
}
return true;
cities->first = NULL;
}
if (index == 0) {
// no previous element
newCity->next = cities->first;
cities->first = newCity;
}
else{
if (index > cityList_size(cities)) {
city_free(newCity->city);
free(newCity->city);
free(newCity);
return ERR_INVALID;
}
if (prev != NULL){
// standard case
newCity->next = prev->next;
prev->next = newCity;
}
else{
city_free(newCity->city);
free(newCity->city);
free(newCity);
return ERR_INVALID;
}
}
}
return OK;
}
// Out of range
num_elements = cityList_size(cities);
if (num_elements == 0 || index > num_elements) return false;
return true;
}
// Out of range
num_elements = cityList_size(cities);
if (num_elements == 0 || index > num_elements) return NULL;
// Out of range
num_elements = cityList_size(cities);
if (num_elements == 0) return NULL;
if (findCity)
return ptr->city;
else return NULL;
}
if (city == NULL)
return NULL;
city->last_update->day = date->day;
city->last_update->month = date->month;
city->last_update->year = date->year;
city->cases += cases;
city->critical_cases += critical_cases;
city->deaths += deaths;
city->recovered += recovered;
city->population -= deaths;
return city;
}
ptrCity = cities->first;
city_free(ptrDeleteCity->city);
free(ptrDeleteCity->city);
free(ptrDeleteCity);
}
//free(ptr);
cities->first = NULL;
}
ptr = cities->first;
return num_elements;
}
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "country.h"
#include "city.h"
return OK;
}
// Copy the data of a Country to another Country
tError country_cpy(tCountry* dst, tCountry* src){
tError err;
tCityNode* ptr = NULL;
// Verify pre conditions
assert(src != NULL);
ptr = src->cities->first;
while (ptr != NULL) {
err = country_addCity(dst, ptr->city);
if (err != OK){
free(dst);
return err;
}
ptr = ptr->next;
}
return OK;
}
return error;
}
if (strcmp(country1->name, country2->name) != 0) {
// countries are different
return false;
}
// All fields have the same value
return true;
if (country->cities != NULL) {
cityList_free(country->cities);
free(country->cities);
country->cities = NULL;
}
return OK;
}
#include "error.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "commons.h"
#include "infection.h"
#include <stdio.h>
// Allocate the memory for the date field. We use the malloc
command.
object->date = (tDate*)malloc(sizeof(tDate));
object->infectiousAgent =
(tInfectiousAgent*)malloc(sizeof(tInfectiousAgent));
infectiousAgent_cpy(object->infectiousAgent,
infectiousAgent);
object->totalCases = 0;
object->totalCriticalCases = 0;
object->totalDeaths = 0;
object->totalRecovered = 0;
return OK;
}
if (object->infectiousAgent != NULL) {
infectiousAgent_free(object->infectiousAgent);
free(object->infectiousAgent);
object->infectiousAgent = NULL;
}
infection->totalCases += cases;
infection->totalDeaths += deaths;
infection->totalCriticalCases += criticalCases;
infection->totalRecovered += recovered;
}
if (strcmp(infection1->infectiousAgent->name, infection2-
>infectiousAgent->name) != 0) {
// infectious Agents are different
return false;
}
if (!country_equal(infection1->country, infection2-
>country)) {
// countries are different
return false;
}
int i;
if (infectionTable1->size != infectionTable2->size){
return false;
}
return true;
}
return OK;
}
tInfection* infectionTable_getMaxInfection(tInfectionTable*
table, const char* infectiousAgentName){
int maxCases = 0;
int maxDeaths = 0;
int i;
tInfection * infection = NULL;
}
}
}
return infection;
}
}
return mortalityRate;
}
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include "infectiousAgent.h"
// Allocate the memory for all the string fields, using the
length of the provided text plus 1 space for the "end of string"
char '\0'.
// To allocate memory we use the malloc command.
object->name = (char*)malloc((strlen(name) + 1) *
sizeof(char));
object->medium = (char*)malloc((strlen(medium) + 1) *
sizeof(char));
object->city = (char *)malloc((strlen(city) + 1) *
sizeof(char));
// Check that memory has been allocated for all fields.
Pointer must be different from NULL.
if (object->name == NULL || object->medium == NULL ||
object->city == NULL) {
// Some of the string fields have a NULL value, what
means that we found some problem allocating the memory
return ERR_MEMORY_ERROR;
}
// Allocate the memory for the date field. We use the malloc
command.
object->date = (tDate*)malloc(sizeof(tDate));
return OK;
}
object->r0 = 0;
if (object->name != NULL) {
free(object->name);
object->name = NULL;
}
if (object->medium != NULL) {
free(object->medium);
object->medium = NULL;
}
if (object->date != NULL) {
free(object->date);
object->date = NULL;
}
if (object->city != NULL) {
free(object->city);
object->city = NULL;
}
if (object->reservoirList != NULL) {
reservoirTable_free(object->reservoirList);
free(object->reservoirList);
object->reservoirList = NULL;
}
return object->reservoirList;
}
if (strcmp(infectiousAgent1->name, infectiousAgent2->name)
!= 0) {
// names are different
return false;
}
if (infectiousAgent1->r0 != infectiousAgent2->r0) {
// basic reproductive ratio r0 are different
return false;
}
if (strcmp(infectiousAgent1->medium, infectiousAgent2-
>medium) != 0) {
// transmission medium
return false;
}
if (infectiousAgent1->date->day != infectiousAgent2->date-
>day ||
infectiousAgent1->date->month != infectiousAgent2->date-
>month ||
infectiousAgent1->date->year != infectiousAgent2->date-
>year) {
// date of first infection
return false;
}
if (strcmp(infectiousAgent1->city, infectiousAgent2->city)
!= 0) {
// city of first infection
return false;
}
if (!reservoirTable_equals(infectiousAgent1->reservoirList,
infectiousAgent2->reservoirList)) {
// infectious agent reservoir list
return false;
}
return true;
}
return OK;
}
return OK;
}
}
else if (strcmp(table->elements[i].name,
infectiousAgent->name) == 0) {
// The current element is the element we want to
remove. Set found flag to true to start element movement.
found = true;
}
}
return OK;
return table->size;
}
}
printf("\n ");
}
#include <stdbool.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h>
#include <stdlib.h>
#include "research.h"
#include "infection.h"
#include "country.h"
#include "commons.h"
#include "error.h"
if (object->country == NULL) {
// Some of the fields have a NULL value, what means that
we found some problem allocating the memory
return ERR_MEMORY_ERROR;
}
ifs.Infectivity = country_totalCases(object->country);
ifs.Severity = country_totalCriticalCases(object->country);
ifs.Lethality = country_totalDeaths(object->country);
object->stats = ifs;
return OK;
}
if (object->country != NULL) {
country_free(object->country);
free(object->country);
object->country = NULL;
}
// Check preconditions
assert(list != NULL);
assert(index > 0);
tResearchListNode *tmp;
tResearchListNode *prev; // Stores the node allocated on
index-1
// Out of range
if (index > list->size+1)
return ERR_INVALID_INDEX;
if (tmp == NULL) {
return ERR_MEMORY_ERROR;
} else {
// Create new research
tmp->e = (tResearch*) malloc(sizeof(tResearch));
if (tmp->e == NULL)
{
free(tmp);
return ERR_MEMORY_ERROR;
}
research_init(tmp->e, research->country);
if (index == 1) {
// add to first position
tmp->next = list->first;
tmp->prev = NULL;
list->first = tmp;
} else {
// Goes in between. Gets current node from position
index-1, that will we "prev" from new node
prev = researchList_get(list, index-1);
if (prev != NULL) {
// Fill "prev" and "next" from new node
tmp->prev = prev;
tmp->next = prev->next; // Could be null if
index==size
}
}
return OK;
}
}
// Check preconditions
assert(list != NULL);
assert(index > 0);
tResearchListNode *tmp;
tResearchListNode *prev;
if (index == 1) {
// Deletes first position
tmp = list->first;
if (tmp == NULL) {
return ERR_EMPTY_LIST;
} else {
// Updates first position
list->first = tmp->next;
// Free memory
research_free(tmp->e);
free(tmp->e);
free(tmp);
return OK;
}
// Check preconditions
assert(list != NULL);
assert(index > 0);
int i;
tResearchListNode *prev;
return prev;
}
int i;
list->first = NULL;
list->last = NULL;
}
// Check preconditions
if (first == NULL) return -1;
if (country_equal(first->e->country, country)){
return pos;
}
return researchList_getPosByCountryRecursive(first->next,
country, ++pos);
// Check preconditions
assert(list != NULL);
prev = list->first;
// check if is an empty list
if (researchList_empty(list)) {
position = -1;
}
else{
// recursive until find country position
position =
researchList_getPosByCountryRecursive(prev,country,1);
}
return position;
}
tError err;
tResearch research_src, research_dst;
tResearchListNode * researchNode_src, * researchNode_dst;
// get source node, store its elem / don't delete (do not
modify list len)
researchNode_src = researchList_get(list, index_src);
research_init(&research_src,researchNode_src->e->country);
//e_src = pNode_src->e;
// get dest node, store / don't delete (do not modify list
len)
researchNode_dst = researchList_get(list, index_dst);
research_init(&research_dst,researchNode_dst->e->country);
//e_dst = pNode_dst->e;
}
}
}
research_free(&research_src);
research_free(&research_dst);
return err;
}
int i, j;
tError err;
tResearchListNode *j_node, *jj_node;
if (research_compare(j_node->e->stats, jj_node->e-
>stats) == -1) {
}
}
return OK;
}
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "reservoir.h"
#include <stdio.h>
// Allocate the memory for all the fields, using the length
of the provided text plus 1 space for the "end of string" char
'\0'. To allocate memory we use the malloc command.
object->name = (char*)malloc((strlen(name) + 1) *
sizeof(char));
object->species = (char*)malloc((strlen(species) + 1) *
sizeof(char));
return OK;
}
if (strcmp(reservoir1->name, reservoir2->name) != 0) {
// names are different
return false;
}
if (strcmp(reservoir1->species, reservoir2->species) != 0) {
// species are different
return false;
}
int i;
if (reservoirTable1->size != reservoirTable2->size){
return false;
}
return true;
}
return OK;
}
}
// As the table is now empty, assign the size to 0.
object->size = 0;
}
return OK;
}
}
else if (strcmp(table->elements[i].name, reservoir-
>name) == 0) {
// The current element is the element we want to
remove. Set found flag to true to start element movement.
found = true;
}
}
return OK;
}
printf("\n ");
}