This commit is contained in:
Ladebeze66 2024-03-16 17:25:56 +01:00
parent 8a57eedbd6
commit 1646901adb
13 changed files with 176 additions and 52 deletions

18
cpp09/.vscode/c_cpp_properties.json vendored Normal file
View File

@ -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
}

24
cpp09/.vscode/launch.json vendored Normal file
View File

@ -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
}
]
}
]
}

59
cpp09/.vscode/settings.json vendored Normal file
View File

@ -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
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/04 14:56:17 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; std::cerr << "Error: could not open price data file." << std::endl;
return false; return false;
} }
std::string line; std::string line;
// Ignorer la première ligne (en-têtes) // Ignorer la première ligne (en-têtes)
std::getline(file, line); std::getline(file, line);
@ -64,10 +64,10 @@ bool BitcoinExchange::loadPriceData(const std::string& filename)
std::cerr << "Warning: Malformed line skipped -> " << line << std::endl; std::cerr << "Warning: Malformed line skipped -> " << line << std::endl;
} }
} }
file.close(); file.close();
// Optionnel: afficher les données chargées pour vérification // Optionnel: afficher les données chargées pour vérification
/*for (std::map<std::string, float>::const_iterator it = dataBase.begin(); it != dataBase.end(); ++it) /*for (std::map<std::string, float>::const_iterator it = dataBase.begin(); it != dataBase.end(); ++it)
{ {
std::cout << "Date: " << it->first << ", Exchange Rate: " << it->second << std::endl; 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::istringstream iss(line);
std::string valueStr; std::string valueStr;
std::getline(iss, date, '|'); // Extrait la date jusqu'au délimiteur '|'. std::getline(iss, date, '|'); // Extrait la date jusqu'au délimiteur '|'.
// Supprime les espaces en début et en fin de 'date'. // Supprime les espaces en début et en fin de 'date'.
date.erase(0, date.find_first_not_of(" \t")); date.erase(0, date.find_first_not_of(" \t"));
date.erase(date.find_last_not_of(" \t") + 1); 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. { // Vérifie la conversion et s'assure qu'il n'y a pas de caractères supplémentaires.
return (false); return (false);
} }
return (true); return (true);
} }
//Calcule la valeur d'échange du Bitcoin pour un montant donné à une date spécifique //Calcule la valeur d'échange du Bitcoin pour un montant donné à une date spécifique

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/04 14:09:27 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; std::cerr << RED << "Error: could not open " << RESET << inputFile << std::endl;
exit(1); exit(1);
} }
// Ignorer la première ligne
std::string header;
getline(input, header);
BitcoinExchange exchange; BitcoinExchange exchange;
std::string priceDataFile = "data.csv"; std::string priceDataFile = "data.csv";
if (!exchange.loadPriceData(priceDataFile)) if (!exchange.loadPriceData(priceDataFile))
@ -43,9 +48,14 @@ int main(int argc, char **argv)
if (exchange.parseLine(line, date, amount)) if (exchange.parseLine(line, date, amount))
{ {
// Vérifie que la valeur est dans la plage [0, 1000] // 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 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 // 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); return (0);
} }

View File

@ -6,7 +6,7 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ # # By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/12/27 20:57:42 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 WHITE = \033[0;97m
ORANGE = \033[38;5;214m ORANGE = \033[38;5;214m
NAME = rpn NAME = RPN
SRC = main.cpp \ SRC = main.cpp \
RPN.cpp \ RPN.cpp \
@ -41,7 +41,7 @@ all : $(NAME)
$(NAME) : $(OBJS) $(NAME) : $(OBJS)
@echo "$(RED)Compilation fixed... $(DEF_COLOR)" @echo "$(RED)Compilation fixed... $(DEF_COLOR)"
$(CC) $(CFLAGS) $(OBJS) -g -o $(NAME) $(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) $(LOGO)
clean : clean :

View File

@ -6,13 +6,12 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 15:04:53 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" #include "RPN.hpp"
RPNCalculator::RPNCalculator() RPNCalculator::RPNCalculator()
{ {
std::cout << GREEN << "RPNCalculator default constructor called!" << RESET << std::endl; std::cout << GREEN << "RPNCalculator default constructor called!" << RESET << std::endl;
@ -38,11 +37,11 @@ RPNCalculator& RPNCalculator::operator=(const RPNCalculator& other)
return (*this); return (*this);
} }
float RPNCalculator::evaluateExpression(const std::string& expression) int RPNCalculator::evaluateExpression(const std::string& expression)
{ {
std::istringstream iss(expression); std::istringstream iss(expression);
std::string token; std::string token;
stack = std::stack<float>(); // Réinitialisez la pile avant de traiter une nouvelle expression stack = std::stack<int>(); // Réinitialisez la pile avant de traiter une nouvelle expression
while (iss >> token) while (iss >> token)
{ {
@ -53,7 +52,7 @@ float RPNCalculator::evaluateExpression(const std::string& expression)
{ {
throw std::runtime_error("Error: Invalid RPN 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 stack.pop(); // Assurez-vous que la pile est vide après avoir récupéré le résultat
return (result); return (result);
@ -61,35 +60,51 @@ float RPNCalculator::evaluateExpression(const std::string& expression)
void RPNCalculator::processToken(const std::string& token) 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] == '-')) if (isdigit(token[0]) || (token.size() > 1 && token[0] == '-'))
{ {
std::istringstream iss(token); std::istringstream iss(token);
float number; float number; // Utilisez float pour lire le nombre, cela permet de capturer les décimaux
iss >> number; iss >> number;
// Vérifiez si le nombre est décimal en comparant à sa version arrondie
// Vérifie si le nombre est strictement inférieur à 10 if (number != std::floor(number))
if (std::abs(number) >= 10) {
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<int>(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."); throw std::runtime_error("Error: Numbers used must always be less than 10.");
} }
stack.push(intNumber);
stack.push(number);
} }
else else
{ {
if (stack.size() < 2) if (stack.size() < 2)
{
throw std::runtime_error("Error: Not enough operands."); throw std::runtime_error("Error: Not enough operands.");
}
float operand2 = stack.top(); stack.pop(); int operand2 = stack.top(); stack.pop();
float operand1 = 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 '-': stack.push(operand1 - operand2); break;
case '*': stack.push(operand1 * operand2); break; case '*': stack.push(operand1 * operand2); break;
case '/': case '/':
if (operand2 == 0) if (operand2 == 0)
{
throw std::runtime_error("Error: Division by zero."); throw std::runtime_error("Error: Division by zero.");
}
stack.push(operand1 / operand2); stack.push(operand1 / operand2);
break; break;
default: throw std::runtime_error("Error: Invalid operator."); default: throw std::runtime_error("Error: Invalid operator.");

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 14:49:08 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 <stack> #include <stack>
#include <string> #include <string>
#include <sstream> #include <sstream> // Pour std::istringstream
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept> // Pour std::runtime_error
#include <cmath> #include <cmath> // Pour std::floor et std::abs
#define RESET "\033[0m" #define RESET "\033[0m"
#define BLACK "\033[30m" #define BLACK "\033[30m"
@ -39,10 +39,10 @@ public:
RPNCalculator(const RPNCalculator& other); RPNCalculator(const RPNCalculator& other);
RPNCalculator& operator=(const RPNCalculator& other); RPNCalculator& operator=(const RPNCalculator& other);
float evaluateExpression(const std::string& expression); int evaluateExpression(const std::string& expression);
private: private:
std::stack<float> stack; std::stack<int> stack;
void processToken(const std::string& token); void processToken(const std::string& token);
}; };

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 15:13:10 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; RPNCalculator calculator;
std::cout << ORANGE << "Enter RPN expressions (or 'exit' to quit):" << RESET << std::endl; std::cout << ORANGE << "Enter RPN expressions (or 'exit' to quit):" << RESET << std::endl;
std::string expression; std::string expression;
while (true) while (true)
{ {
std::cout << "> "; std::cout << "> ";
std::getline(std::cin, expression); std::getline(std::cin, expression);
if (expression == "exit") if (expression == "exit")
{ {
break; // Sortir de la boucle si l'utilisateur entre 'exit' break; // Sortir de la boucle si l'utilisateur entre 'exit'
@ -31,12 +31,12 @@ int main()
try try
{ {
float result = calculator.evaluateExpression(expression); int result = calculator.evaluateExpression(expression);
std::cout << GREEN << "Result: " << RESET << result << std::endl; std::cout << GREEN << "Result: " << RESET << result << std::endl;
} }
catch (const std::exception& e) catch (const std::exception& e)
{ {
std::cout << RED << "Error: " << e.what() << RESET << std::endl; std::cout << RED << e.what() << RESET << std::endl;
} }
} }
return (0); return (0);

BIN
cpp09/ex01/rpn Executable file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 13:12:20 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<std::vector<int> >(const std::vector<int
template void PmergeMe::displaySequence<std::list<int> >(const std::list<int>&, const std::string&); template void PmergeMe::displaySequence<std::list<int> >(const std::list<int>&, const std::string&);
template void PmergeMe::measureAndSort<std::vector<int> >(std::vector<int>&, const std::string&); template void PmergeMe::measureAndSort<std::vector<int> >(std::vector<int>&, const std::string&);
// Spécialisation explicite pour std::list<int>
template void PmergeMe::measureAndSort<std::list<int> >(std::list<int>&, const std::string&); template void PmergeMe::measureAndSort<std::list<int> >(std::list<int>&, const std::string&);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 16:17:28 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 <algorithm> // Pour les conteneurs supportant le tri direct #include <algorithm> // Pour les conteneurs supportant le tri direct
#include <iterator> // Pour std::distance et std::advance #include <iterator> // Pour std::distance et std::advance
#include <cstdlib> // Pour std::atoi #include <cstdlib> // Pour std::atoi
#include <climits>
#include <cerrno>
#define RESET "\033[0m" #define RESET "\033[0m"
#define BLACK "\033[30m" #define BLACK "\033[30m"

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 13:25:02 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<int> vectorInput; std::vector<int> vectorInput;
std::list<int> listInput; std::list<int> listInput;
// Remplir les conteneurs avec les entiers fournis en arguments
for(int i = 1; i < argc; ++i) for(int i = 1; i < argc; ++i)
{ {
int value = std::atoi(argv[i]); errno = 0; // Réinitialiser errno avant la conversion
if(value < 0) 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); return (1);
} }
vectorInput.push_back(value); vectorInput.push_back(static_cast<int>(value));
listInput.push_back(value); listInput.push_back(static_cast<int>(value));
} }
// Traitement et affichage pour std::vector
std::cout << ORANGE << "Processing with std::vector<int>..." << RESET << std::endl; std::cout << ORANGE << "Processing with std::vector<int>..." << RESET << std::endl;
pmerge.sortAndDisplay(vectorInput); pmerge.sortAndDisplay(vectorInput);
// Traitement et affichage pour std::list
std::cout << ORANGE << "\nProcessing with std::list<int>..." << RESET << std::endl; std::cout << ORANGE << "\nProcessing with std::list<int>..." << RESET << std::endl;
pmerge.sortAndDisplay(listInput); pmerge.sortAndDisplay(listInput);