C ordening

This commit is contained in:
Rens Pastoor
2025-05-27 23:26:28 +02:00
parent 39269a71a7
commit 517087ccc1
207 changed files with 0 additions and 4278 deletions

View File

@@ -0,0 +1,59 @@
#include "administration.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "animal.h"
int addAnimal(const Animal* animalPtr, Animal* animalArray, size_t animalArrayLength, size_t numberOfAnimalsPresent, size_t* newNumberOfAnimalsPresent){
int fault = 0;
if (animalArray == NULL || animalPtr == NULL || numberOfAnimalsPresent >= animalArrayLength) {
fault = -1; // Invalid
} else {
animalArray[numberOfAnimalsPresent] = *animalPtr;
++numberOfAnimalsPresent;
}
*newNumberOfAnimalsPresent = numberOfAnimalsPresent;
return fault;
}
int removeAnimal(int animalId, Animal* animalArray, size_t numberOfAnimalsPresent, size_t* newNumberOfAnimalsPresent){
int fault = -1;
if (animalArray == NULL || numberOfAnimalsPresent == 0) {
fault = -1; // Invalid
} else {
for (size_t i = 0; i < numberOfAnimalsPresent; ++i) {
if (animalArray[i].Id == animalId) {
// move the array elements to the left to remove the animal
for (size_t j = i; j < numberOfAnimalsPresent - 1; ++j) {
animalArray[j] = animalArray[j + 1];
}
--numberOfAnimalsPresent;
fault = 0;
break;
}
}
}
*newNumberOfAnimalsPresent = numberOfAnimalsPresent;
return fault;
}
int findAnimalById(int animalId, const Animal* animalArray, size_t numberOfAnimalsPresent, Animal* foundAnimal) {
int fault = -1;
if (animalArray == NULL || numberOfAnimalsPresent == 0 || foundAnimal == NULL) {
fault = -1;
} else {
for (size_t i = 0; i < numberOfAnimalsPresent; i++) {
if (animalArray[i].Id == animalId) {
*foundAnimal = animalArray[i];
fault = 0;
break; // Exit loop after finding the animal
}
}
if (fault == -1) { // Only zero out if animal wasn't found
memset(foundAnimal, 0, sizeof(Animal));
}
}
return fault;
}

View File

@@ -0,0 +1,56 @@
#ifndef ADMINISTRATION_H
#define ADMINISTRATION_H
#include <stddef.h>
#include "animal.h"
/* pre : animalArrayLength must be greater than numberOfAnimalsPresent
* post : animalArray is updated with the information from animalPtr, the
* updated number of animals is passed in newNumberOfAnimalsPresent returns: 0
* on success or -1 if an error occurs
*/
int addAnimal(
const Animal* animalPtr, Animal* animalArray,
size_t animalArrayLength, size_t numberOfAnimalsPresent,
size_t* newNumberOfAnimalsPresent);
/* pre :
* post : All animals with id 'animalId' are removed from AnimalArray, the
* updated number of animals is passed in newNumberOfAnimalsPresent returns: The
* number of removed animals, 0 if no animals removed or -1 if an error occurs
*/
int removeAnimal(
int animalId, Animal* animalArray,
size_t numberOfAnimalsPresent,
size_t* newNumberOfAnimalsPresent);
/* pre :
* post : The first animal from animalArray with id 'animalId' is copied into
* animalPtr returns: 1 on success, 0 if not found or -1 if an error occurs
*/
int findAnimalById(
int animalId, const Animal* animalArray,
size_t numberOfAnimalsPresent, Animal* animalPtr);
/* pre :
* post : All animals in animalArray are sorted by age
* returns: 0 on success or -1 if an error occurs
*/
int sortAnimalsByAge(Animal* animalArray, size_t numberOfAnimalsPresent);
/* pre :
* post : All animals in animalArray are sorted by the year in which they were
* found returns: 0 on success or -1 if an error occurs
*/
int sortAnimalsByYearFound(
Animal* animalArray, size_t numberOfAnimalsPresent);
/* pre :
* post : All animals in animalArray are sorted: first female animals and then
* male animals returns: 0 on success or -1 if an error occurs
*/
int sortAnimalsBySex(Animal* animalArray, size_t numberOfAnimalsPresent);
#endif

View File

@@ -0,0 +1,37 @@
#ifndef ANIMAL_H
#define ANIMAL_H
typedef enum
{
Cat,
Dog,
GuineaPig,
Parrot
} Species;
typedef enum
{
Male,
Female
} Sex;
typedef struct
{
int Day;
int Month;
int Year;
} Date;
#define MaxNameLength 25
#define MaxLocationLength 100
typedef struct
{
int Id;
Species Species;
Sex Sex;
int Age;
Date DateFound;
} Animal;
#endif

View File

@@ -0,0 +1,35 @@
#include "file_element.h"
int readAnimals(const char* filename, Animal* animalPtr, size_t nrAnimals)
{
return -1;
}
int writeAnimals(const char* filename, const Animal* animalPtr, size_t nrAnimals)
{
return -1;
}
int getNrAnimalsInFile(const char* filename, size_t* nrAnimals)
{
return -1;
}
int readAnimalFromFile(
const char* filename, size_t filePosition, Animal* animalPtr)
{
return -1;
}
int writeAnimalToFile(
const char* filename, size_t filePosition, const Animal* animalPtr)
{
return -1;
}
int renameAnimalInFile(
const char* filename, size_t filePosition, const char* animalSurname)
{
return -1;
}

View File

@@ -0,0 +1,66 @@
#ifndef FILE_ELEMENT_H
#define FILE_ELEMENT_H
#include <stddef.h>
#include "animal.h"
int readAnimals(const char* filename, Animal* animalPtr, size_t nrAnimals);
/* pre : n.a.
* post : If file contains enough Animals, nrAnimals Animals are read into
* animalPtr. If less animals than nrAnimals exist, all animals from
* the file are read into animalPtr.
* returns: Nr of animals written into animalPtr or -1 if an error occurs
*/
int writeAnimals(const char* filename, const Animal* animalPtr, size_t nrAnimals);
/* pre : n.a.
* post : nrAnimals animals are written into a new file with data from
* animalPtr
* returns: On succes: 0, on error (file could not be written, input pointers
* are NULL): -1
*/
int getNrAnimalsInFile(const char* filename, size_t* nrAnimals);
/* pre : n.a.
* post : get number of animals (Animal structures) in the file
* returns: on succes: 0, on error: -1
*
*/
int readAnimalFromFile(
const char* filename, size_t filePosition, Animal* animalPtr);
/* pre : n.a.
* post : read the animal on filePosition (first animal is filePosition 0,
* second animal is filePosition 1, ...) into animalPtr
* returns: On success: 0, on error: -1 (no data available on filePosition,
* file could not be read, ...)
*/
int writeAnimalToFile(
const char* filename, size_t filePosition, const Animal* animalPtr);
/* pre :
* post : write the animal in animalPtr to the file at position 'filePosition'
* returns: On success: 0, on error: -1
*
**** note: do not open the file in append mode (a or a+): in append mode you
**** ALWAYS write to the end of the file. You cannot open the file in
**** write mode either (w or w+), as this will truncate an existing file
**** to 0 bytes. You MUST open the file in "r+" mode (means: r+w) and if
**** that fails (could mean: file does not exist) retry in "w" mode.
*/
int renameAnimalInFile(
const char* filename, size_t filePosition, const char* animalSurname);
/* pre :
* post : change the animal name on the filePosition in this way:
* The new animal name will start with animalSurname, followed
* by a space and followed by the original animal name
* Example : We have animal called "Max" on filePosition and animalSurname
* "Verstappen". The new animal name will be "Verstappen Max".
* The renamed animal will be written back to the file.
* returns : On success: 0, on error: -1
*/
#endif