Contarius/src/money.c

195 lines
4.8 KiB
C

/**
* @file money.c
* Implementation file to use the money structure
*/
#include "money.h"
/**
*
* @brief Initialize a money structure, sets the value according to the parameters and return a pointer to the structure created
* @param full the full value for the structure that will be created.
* @param cents the cents value the structure that will be created.
* @return pointer to the created structure.
*
*/
Money *initalizeMoney(int full, int cents) {
Money *m = malloc(sizeof(Money));
m->full = full;
m->cents = cents;
return m;
}
/**
* @brief Check if the value of the cents is bigger than 100
* @param *m the money structure that will be checked
* @retval true if the value is bigger than 100
* @retval false if the falue is not bigger than 100
*/
bool checkOverflow(Money *m) {
if (m->cents >= 100)
return true;
return false;
};
/**
* @brief Check if the value of the cents is less than 0
* @param *m the money structure that will be checked
* @retval true if the value is less than 0
* @retval false if the falue is less than 0
*/
bool checkUnderflow(Money *m) {
if (m->cents < 0)
return true;
return false;
};
/**
* @brief Handles the case of the cents being bigger than 100.
*
* It adds or subtracks to the full part and do a modulus operation on cents
* @param *m The money structure that will be operated on
*/
void handleOverflow(Money *m) {
if (m->full > 0) {
m->full += m->cents / 100;
} else {
m->full -= m->cents / 100;
}
m->cents = m->cents % 100;
};
/**
*
* @brief Handles the case of the cents being less than 0.
*
* It adds or subtracks to the full part and do a modulus operation on cents
* @param *m The money structure that will be operated on
*/
void handleUnderflow(Money *m) {
m->cents = 100 + m->cents;
if (m->full > 0) {
m->full -= 1;
} else {
m->full += 1;
}
};
/**
* @brief Set the value for a money structure
*
* @param *m The money structure that will be set the value
* @param full the full value that money structure will set to
* @param cents the cent value that money structure will set to
*/
void setMoneyValue(Money *m, int full, int cents) {
m->full = full;
m->cents = cents;
if (checkOverflow(m)) {
handleOverflow(m);
}
}
/**
* @brief Return the value of a money structure as a string
* @param *m The money structure that string will be generated from
* @returns the values of the money structure as a string
*/
char *getMoneyString(Money *m) {
char *str = malloc(sizeof(char) * 20);
char *str_cents = malloc(sizeof(char) * 4);
char *str_full = malloc(sizeof(char) * 100);
if (m->cents < 10) {
sprintf(str_cents, "0%d", m->cents);
} else {
sprintf(str_cents, "%d", m->cents);
}
sprintf(str, "%2i,%s", m->full, str_cents);
return str;
}
/**
* @brief Sums two money structures.
*
*
* Executes the summing of two money strucutures,
* it sums the destination money structure to the source money structure,
* altering the the destination money structure.
*
* @param *dest The money structure that will be altered
* @param *src The money srtruture that will used to alter
*
*/
void addMoneyToMoney(Money *dest, Money *src) {
if (dest->full < 0) {
if (src->full > 0) {
dest->cents = (-1 * dest->cents) + src->cents;
dest->full = dest->full + src->full;
} else {
dest->cents = ((-1 * dest->cents) + (-1 * src->cents)) * -1;
dest->full = dest->full + src->full;
}
} else {
if (src->full > 0) {
dest->cents += src->cents;
dest->full += src->full;
} else {
dest->cents = (dest->cents + (-1 * src->cents)) * -1;
dest->full = dest->full + src->full;
}
}
if (checkOverflow(dest)) {
handleOverflow(dest);
}
}
/**
* @brief Subtracts two money structures
*
*
* Executes the subtraction of two money strucutures,
* it subtracts the destination money structure to the source money structure,
* altering the the destination money structure.
*
* @param *dest The money structure that will be altered
* @param *src The money srtruture that will used to alter
*
*
*/
void subtractMoneyToMoney(Money *dest, Money *src) {
if (dest->full > 0) {
if (src->full > 0) {
dest->cents = dest->cents - src->cents;
dest->full = dest->full - src->full;
} else {
dest->cents = dest->cents - (-1 * src->cents);
dest->full = dest->full - src->full;
}
} else {
if (src->full > 0) {
dest->cents = -1 * ((-1 * dest->cents) - src->cents);
dest->full = dest->full - src->full;
} else {
dest->cents = dest->cents - src->cents;
dest->full = dest->full - src->full;
}
}
if (checkUnderflow(dest)) {
handleUnderflow(dest);
}
}
/**
* @brief Frees the Memory used by a money structure
* @param *money The money structure that will be freed
*/
void destroyMoney(Money *money){
free(money);
}