diff --git a/cpp09/.vscode/c_cpp_properties.json b/cpp09/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/cpp09/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/cpp09/.vscode/launch.json b/cpp09/.vscode/launch.json new file mode 100644 index 0000000..554eb5c --- /dev/null +++ b/cpp09/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/fgras-ca/Bureau/CPP/cpp2/cpp09/ex00", + "program": "/home/fgras-ca/Bureau/CPP/cpp2/cpp09/ex00/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/cpp09/.vscode/settings.json b/cpp09/.vscode/settings.json new file mode 100644 index 0000000..bb879da --- /dev/null +++ b/cpp09/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/cpp09/ex00/BitcoinExchange.cpp b/cpp09/ex00/BitcoinExchange.cpp index e7b1dc8..b5dd972 100644 --- a/cpp09/ex00/BitcoinExchange.cpp +++ b/cpp09/ex00/BitcoinExchange.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/04 14:56:17 by fgras-ca #+# #+# */ -/* Updated: 2024/03/04 16:52:42 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 13:28:08 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ bool BitcoinExchange::loadPriceData(const std::string& filename) std::cerr << "Error: could not open price data file." << std::endl; return false; } - + std::string line; // Ignorer la première ligne (en-têtes) std::getline(file, line); @@ -64,10 +64,10 @@ bool BitcoinExchange::loadPriceData(const std::string& filename) std::cerr << "Warning: Malformed line skipped -> " << line << std::endl; } } - + file.close(); // Optionnel: afficher les données chargées pour vérification - + /*for (std::map::const_iterator it = dataBase.begin(); it != dataBase.end(); ++it) { std::cout << "Date: " << it->first << ", Exchange Rate: " << it->second << std::endl; @@ -80,7 +80,7 @@ bool BitcoinExchange::parseLine(const std::string& line, std::string& date, floa std::istringstream iss(line); std::string valueStr; std::getline(iss, date, '|'); // Extrait la date jusqu'au délimiteur '|'. - + // Supprime les espaces en début et en fin de 'date'. date.erase(0, date.find_first_not_of(" \t")); date.erase(date.find_last_not_of(" \t") + 1); @@ -93,7 +93,7 @@ bool BitcoinExchange::parseLine(const std::string& line, std::string& date, floa { // Vérifie la conversion et s'assure qu'il n'y a pas de caractères supplémentaires. return (false); } - + return (true); } //Calcule la valeur d'échange du Bitcoin pour un montant donné à une date spécifique diff --git a/cpp09/ex00/main.cpp b/cpp09/ex00/main.cpp index f3c6e7f..e62f9a6 100644 --- a/cpp09/ex00/main.cpp +++ b/cpp09/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/04 14:09:27 by fgras-ca #+# #+# */ -/* Updated: 2024/03/04 17:31:07 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 13:37:43 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,11 @@ int main(int argc, char **argv) std::cerr << RED << "Error: could not open " << RESET << inputFile << std::endl; exit(1); } + + // Ignorer la première ligne + std::string header; + getline(input, header); + BitcoinExchange exchange; std::string priceDataFile = "data.csv"; if (!exchange.loadPriceData(priceDataFile)) @@ -43,9 +48,14 @@ int main(int argc, char **argv) if (exchange.parseLine(line, date, amount)) { // Vérifie que la valeur est dans la plage [0, 1000] - if (amount < 0 || amount > 1000) + if (amount < 0) { - std::cout << RED << "Error: Value out of range [0, 1000]." << RESET << std::endl; + std::cout << RED << "Error: not a positive number." << RESET << std::endl; + continue; // Passe à la ligne suivante si la valeur est hors plage + } + else if (amount > 1000) + { + std::cout << RED << "Error: too large a number." << RESET << std::endl; continue; // Passe à la ligne suivante si la valeur est hors plage } // Calcule la valeur d'échange du Bitcoin pour la date et le montant donnés @@ -66,5 +76,3 @@ int main(int argc, char **argv) } return (0); } - - diff --git a/cpp09/ex01/Makefile b/cpp09/ex01/Makefile index 1357b18..d9faa96 100644 --- a/cpp09/ex01/Makefile +++ b/cpp09/ex01/Makefile @@ -6,7 +6,7 @@ # By: fgras-ca +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # -# Updated: 2024/03/06 14:40:11 by fgras-ca ### ########.fr # +# Updated: 2024/03/16 15:36:22 by fgras-ca ### ########.fr # # # # **************************************************************************** # @@ -23,7 +23,7 @@ CYAN = \033[0;96m WHITE = \033[0;97m ORANGE = \033[38;5;214m -NAME = rpn +NAME = RPN SRC = main.cpp \ RPN.cpp \ @@ -41,7 +41,7 @@ all : $(NAME) $(NAME) : $(OBJS) @echo "$(RED)Compilation fixed... $(DEF_COLOR)" $(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) - @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./rpn" to execute the program!!$(DEF_COLOR)" + @echo "$(GREEN)Compilation complete. $(ORANGE)Type "./RPN" to execute the program!!$(DEF_COLOR)" $(LOGO) clean : diff --git a/cpp09/ex01/RPN.cpp b/cpp09/ex01/RPN.cpp index 361e55b..3637626 100644 --- a/cpp09/ex01/RPN.cpp +++ b/cpp09/ex01/RPN.cpp @@ -6,13 +6,12 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/06 15:04:53 by fgras-ca #+# #+# */ -/* Updated: 2024/03/06 17:18:53 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 15:46:23 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "RPN.hpp" - RPNCalculator::RPNCalculator() { std::cout << GREEN << "RPNCalculator default constructor called!" << RESET << std::endl; @@ -38,11 +37,11 @@ RPNCalculator& RPNCalculator::operator=(const RPNCalculator& other) return (*this); } -float RPNCalculator::evaluateExpression(const std::string& expression) +int RPNCalculator::evaluateExpression(const std::string& expression) { std::istringstream iss(expression); std::string token; - stack = std::stack(); // Réinitialisez la pile avant de traiter une nouvelle expression + stack = std::stack(); // Réinitialisez la pile avant de traiter une nouvelle expression while (iss >> token) { @@ -53,7 +52,7 @@ float RPNCalculator::evaluateExpression(const std::string& expression) { throw std::runtime_error("Error: Invalid RPN expression."); } - float result = stack.top(); // Stockez le résultat final avant de vider la pile + int result = stack.top(); // Stockez le résultat final avant de vider la pile stack.pop(); // Assurez-vous que la pile est vide après avoir récupéré le résultat return (result); @@ -61,35 +60,51 @@ float RPNCalculator::evaluateExpression(const std::string& expression) void RPNCalculator::processToken(const std::string& token) { + // Vérifier la présence de parenthèses dans le token + if (token.find('(') != std::string::npos || token.find(')') != std::string::npos) + { + throw std::runtime_error("Error: Parentheses are not handled."); + } + if (isdigit(token[0]) || (token.size() > 1 && token[0] == '-')) { std::istringstream iss(token); - float number; + float number; // Utilisez float pour lire le nombre, cela permet de capturer les décimaux iss >> number; - - // Vérifie si le nombre est strictement inférieur à 10 - if (std::abs(number) >= 10) + // Vérifiez si le nombre est décimal en comparant à sa version arrondie + if (number != std::floor(number)) + { + throw std::runtime_error("Error: Decimal numbers are not handled."); + } + // Convertissez le nombre en int pour les opérations ultérieures + int intNumber = static_cast(number); + // Vérifiez si le nombre est strictement inférieur à 10 + if (std::abs(intNumber) >= 10) { throw std::runtime_error("Error: Numbers used must always be less than 10."); } - - stack.push(number); + stack.push(intNumber); } else { if (stack.size() < 2) + { throw std::runtime_error("Error: Not enough operands."); + } - float operand2 = stack.top(); stack.pop(); - float operand1 = stack.top(); stack.pop(); + int operand2 = stack.top(); stack.pop(); + int operand1 = stack.top(); stack.pop(); - switch (token[0]) { + switch (token[0]) + { case '+': stack.push(operand1 + operand2); break; case '-': stack.push(operand1 - operand2); break; case '*': stack.push(operand1 * operand2); break; case '/': if (operand2 == 0) + { throw std::runtime_error("Error: Division by zero."); + } stack.push(operand1 / operand2); break; default: throw std::runtime_error("Error: Invalid operator."); diff --git a/cpp09/ex01/RPN.hpp b/cpp09/ex01/RPN.hpp index 5bc27c6..7c6fbac 100644 --- a/cpp09/ex01/RPN.hpp +++ b/cpp09/ex01/RPN.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/06 14:49:08 by fgras-ca #+# #+# */ -/* Updated: 2024/03/06 17:18:55 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 15:52:43 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,10 +15,10 @@ #include #include -#include +#include // Pour std::istringstream #include -#include -#include +#include // Pour std::runtime_error +#include // Pour std::floor et std::abs #define RESET "\033[0m" #define BLACK "\033[30m" @@ -39,10 +39,10 @@ public: RPNCalculator(const RPNCalculator& other); RPNCalculator& operator=(const RPNCalculator& other); - float evaluateExpression(const std::string& expression); + int evaluateExpression(const std::string& expression); private: - std::stack stack; + std::stack stack; void processToken(const std::string& token); }; diff --git a/cpp09/ex01/main.cpp b/cpp09/ex01/main.cpp index dec4d34..28174fa 100644 --- a/cpp09/ex01/main.cpp +++ b/cpp09/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/06 15:13:10 by fgras-ca #+# #+# */ -/* Updated: 2024/03/06 17:18:59 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 15:02:24 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,13 @@ int main() RPNCalculator calculator; std::cout << ORANGE << "Enter RPN expressions (or 'exit' to quit):" << RESET << std::endl; - + std::string expression; while (true) { std::cout << "> "; std::getline(std::cin, expression); - + if (expression == "exit") { break; // Sortir de la boucle si l'utilisateur entre 'exit' @@ -31,12 +31,12 @@ int main() try { - float result = calculator.evaluateExpression(expression); + int result = calculator.evaluateExpression(expression); std::cout << GREEN << "Result: " << RESET << result << std::endl; } catch (const std::exception& e) { - std::cout << RED << "Error: " << e.what() << RESET << std::endl; + std::cout << RED << e.what() << RESET << std::endl; } } return (0); diff --git a/cpp09/ex01/rpn b/cpp09/ex01/rpn new file mode 100755 index 0000000..c04fb68 Binary files /dev/null and b/cpp09/ex01/rpn differ diff --git a/cpp09/ex02/PmergeMe.cpp b/cpp09/ex02/PmergeMe.cpp index 88a908f..bd89eda 100644 --- a/cpp09/ex02/PmergeMe.cpp +++ b/cpp09/ex02/PmergeMe.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/07 13:12:20 by fgras-ca #+# #+# */ -/* Updated: 2024/03/08 14:08:06 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 16:28:26 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,6 +102,4 @@ template void PmergeMe::displaySequence >(const std::vector >(const std::list&, const std::string&); template void PmergeMe::measureAndSort >(std::vector&, const std::string&); - -// Spécialisation explicite pour std::list template void PmergeMe::measureAndSort >(std::list&, const std::string&); diff --git a/cpp09/ex02/PmergeMe.hpp b/cpp09/ex02/PmergeMe.hpp index 20c4f1c..8535f75 100644 --- a/cpp09/ex02/PmergeMe.hpp +++ b/cpp09/ex02/PmergeMe.hpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/06 16:17:28 by fgras-ca #+# #+# */ -/* Updated: 2024/03/07 14:00:07 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 17:07:11 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ #include // Pour les conteneurs supportant le tri direct #include // Pour std::distance et std::advance #include // Pour std::atoi +#include +#include #define RESET "\033[0m" #define BLACK "\033[30m" diff --git a/cpp09/ex02/main.cpp b/cpp09/ex02/main.cpp index 5dd60a0..f2a2326 100644 --- a/cpp09/ex02/main.cpp +++ b/cpp09/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/07 13:25:02 by fgras-ca #+# #+# */ -/* Updated: 2024/03/08 13:50:25 by fgras-ca ### ########.fr */ +/* Updated: 2024/03/16 17:10:14 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,24 +24,24 @@ int main(int argc, char **argv) std::vector vectorInput; std::list listInput; - // Remplir les conteneurs avec les entiers fournis en arguments for(int i = 1; i < argc; ++i) { - int value = std::atoi(argv[i]); - if(value < 0) + errno = 0; // Réinitialiser errno avant la conversion + char* endPtr; + long value = std::strtol(argv[i], &endPtr, 10); + // Vérifier si la conversion a réussi et que toute la chaîne a été consommée + if (*endPtr != '\0' || errno == ERANGE || value < 0 || value > INT_MAX) { - std::cerr << RED << "Error: Only positive integers are allowed." << RESET << std::endl; + std::cerr << RED << "Error: Argument \"" << argv[i] << "\" is not a valid positive integer or it is out of the int range." << RESET << std::endl; return (1); } - vectorInput.push_back(value); - listInput.push_back(value); + vectorInput.push_back(static_cast(value)); + listInput.push_back(static_cast(value)); } - // Traitement et affichage pour std::vector std::cout << ORANGE << "Processing with std::vector..." << RESET << std::endl; pmerge.sortAndDisplay(vectorInput); - // Traitement et affichage pour std::list std::cout << ORANGE << "\nProcessing with std::list..." << RESET << std::endl; pmerge.sortAndDisplay(listInput);