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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 :

View File

@ -6,13 +6,12 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<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)
{
@ -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<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.");
}
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.");

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <string>
#include <sstream>
#include <sstream> // Pour std::istringstream
#include <iostream>
#include <stdexcept>
#include <cmath>
#include <stdexcept> // Pour std::runtime_error
#include <cmath> // 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<float> stack;
std::stack<int> stack;
void processToken(const std::string& token);
};

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@ -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);

BIN
cpp09/ex01/rpn Executable file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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::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&);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iterator> // Pour std::distance et std::advance
#include <cstdlib> // Pour std::atoi
#include <climits>
#include <cerrno>
#define RESET "\033[0m"
#define BLACK "\033[30m"

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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::list<int> 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<int>(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;
pmerge.sortAndDisplay(vectorInput);
// Traitement et affichage pour std::list
std::cout << ORANGE << "\nProcessing with std::list<int>..." << RESET << std::endl;
pmerge.sortAndDisplay(listInput);