Created Money structure and implementation and also made some modification on the doxygen template

This commit is contained in:
Gustavo Henrique Santos Souza de Miranda 2024-04-24 02:48:45 -03:00
parent 5d080d092d
commit 1695e9dc44
4 changed files with 220 additions and 7 deletions

View File

@ -89,7 +89,7 @@ ALLOW_UNICODE_NAMES = NO
# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish,
# Ukrainian and Vietnamese.
# The default value is: English.
@PROJECT_SOURCE_DIR@/
OUTPUT_TEXT_DIRECTION = None
# If the BRIEF_MEMBER_DESC tag is set to YES, doxygen will include brief member
@ -134,7 +134,7 @@ ABBREVIATE_BRIEF = "The $name class" \
# description.
# The default value is: NO.
ALWAYS_DETAILED_SEC = NO
ALWAYS_DETAILED_SEC = YES
# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all
# inherited members of a class in the documentation of that class as if those
@ -855,7 +855,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.
INPUT = "@CMAKE_CURRENT_SOURCE_DIR@/../src/" "@CMAKE_CURRENT_SOURCE_DIR@/../docs"
INPUT = "@CMAKE_CURRENT_SOURCE_DIR@/../src/" "@CMAKE_CURRENT_SOURCE_DIR@/../include" "@CMAKE_CURRENT_SOURCE_DIR@/../docs"
# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
@ -1065,7 +1065,7 @@ USE_MDFILE_AS_MAINPAGE =
# also VERBATIM_HEADERS is set to NO.
# The default value is: NO.
SOURCE_BROWSER = NO
SOURCE_BROWSER = YES
# Setting the INLINE_SOURCES tag to YES will include the body of functions,
# classes and enums directly into the documentation.
@ -1084,13 +1084,13 @@ STRIP_CODE_COMMENTS = YES
# entity all documented functions referencing it will be listed.
# The default value is: NO.
REFERENCED_BY_RELATION = NO
REFERENCED_BY_RELATION = YES
# If the REFERENCES_RELATION tag is set to YES then for each documented function
# all documented entities called/used by that function will be listed.
# The default value is: NO.
REFERENCES_RELATION = NO
REFERENCES_RELATION = YES
# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set
# to YES then the hyperlinks from functions in REFERENCES_RELATION and

27
include/money.h Normal file
View File

@ -0,0 +1,27 @@
/** @file money.h
* Money structure header file used in the program
*/
#ifndef MONEY_H
#define MONEY_H
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/**
* @struct money
* @brief Struct Holding the Monetary value in a separate form
*/
typedef struct money {
int full; ///< The Full part of the monetary value
int cents; ///< The Cents parts of the monetary value
} Money;
Money *InitializeMoney(int full, int cents);
void addMoneyToMoney(Money *dest, Money *src);
void subtractMoneyToMoney(Money *dest, Money *src);
#endif

View File

@ -1,4 +1,4 @@
set(SOURCE_FILES main.c)
set(SOURCE_FILES main.c money.c)
add_executable(Contarius ${SOURCE_FILES})

186
src/money.c Normal file
View File

@ -0,0 +1,186 @@
/**
* @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
*
*/
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;
};
/**
* @deprecated
* @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;
};
/**
* @deprecated
* @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);
}
}