diff --git a/cpp09/ex01/Makefile b/cpp09/ex01/Makefile new file mode 100644 index 0000000..1357b18 --- /dev/null +++ b/cpp09/ex01/Makefile @@ -0,0 +1,61 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: fgras-ca +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/12/27 20:57:42 by fgras-ca #+# #+# # +# Updated: 2024/03/06 14:40:11 by fgras-ca ### ########.fr # +# # +# **************************************************************************** # + +LOGO = @echo "🅻🅰🅳🅴🅱🅴🆉🅴";\ + +DEF_COLOR = \033[0m +GRAY = \033[0;90m +RED = \033[0;91m +GREEN = \033[0;92m +YELLOW = \033[0;93m +BLUE = \033[0;94m +MAGENTA = \033[0;95m +CYAN = \033[0;96m +WHITE = \033[0;97m +ORANGE = \033[38;5;214m + +NAME = rpn + +SRC = main.cpp \ + RPN.cpp \ + +CC = c++ +CFLAGS = -Wall -Werror -Wextra -std=c++98 +RM = rm -f +OBJS = ${SRC:.cpp=.o} + +all : $(NAME) + +%.o: %.cpp + $(CC) $(CFLAGS) -c $< -o $@ + +$(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)" + $(LOGO) + +clean : + @echo "$(RED)Deleating files objects... $(DEF_COLOR)" + $(RM) $(OBJS) + @echo "$(GREEN)files deleted!! $(DEF_COLOR)" + $(LOGO) + +fclean : clean + @echo "$(RED)Delete program name... $(DEF_COLOR)" + $(RM) $(NAME) + @echo "$(GREEN)File program deleted!! $(DEF_COLOR)" + $(LOGO) + +re : fclean all + +.PHONY : all clean fclean re \ No newline at end of file diff --git a/cpp09/ex01/RPN.cpp b/cpp09/ex01/RPN.cpp new file mode 100644 index 0000000..2f2a587 --- /dev/null +++ b/cpp09/ex01/RPN.cpp @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RPN.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/06 15:04:53 by fgras-ca #+# #+# */ +/* Updated: 2024/03/06 15:40:51 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RPN.hpp" + + +RPNCalculator::RPNCalculator() +{ + // Le constructeur par défaut reste inchangé. +} + +RPNCalculator::~RPNCalculator() +{ + // Le destructeur reste inchangé. +} + +RPNCalculator::RPNCalculator(const RPNCalculator& other) : stack(other.stack) +{ + // Le constructeur de copie reste inchangé. +} + +RPNCalculator& RPNCalculator::operator=(const RPNCalculator& other) +{ + if (this != &other) + { + this->stack = other.stack; + } + return (*this); +} + +float 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 + + while (iss >> token) + { + processToken(token); + } + + if (stack.size() != 1) + { + throw std::runtime_error("Error: Invalid RPN expression."); + } + float 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); +} + +void RPNCalculator::processToken(const std::string& token) +{ + if (isdigit(token[0]) || (token.size() > 1 && token[0] == '-')) + { + std::istringstream iss(token); + float number; + iss >> number; + + // Vérifie si le nombre est strictement inférieur à 10 + if (std::abs(number) >= 10) + { + throw std::runtime_error("Error: Numbers used must always be less than 10."); + } + + stack.push(number); + } + 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(); + + 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 new file mode 100644 index 0000000..2a8220f --- /dev/null +++ b/cpp09/ex01/RPN.hpp @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RPN.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/06 14:49:08 by fgras-ca #+# #+# */ +/* Updated: 2024/03/06 15:42:36 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RPN_HPP +#define RPN_HPP + +#include +#include +#include +#include +#include +#include + +#define RESET "\033[0m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" +#define ORANGE "\033[38;5;214m" + +class RPNCalculator +{ +public: + RPNCalculator(); + ~RPNCalculator(); + RPNCalculator(const RPNCalculator& other); + RPNCalculator& operator=(const RPNCalculator& other); + + float evaluateExpression(const std::string& expression); + +private: + std::stack stack; + void processToken(const std::string& token); +}; + +#endif \ No newline at end of file diff --git a/cpp09/ex01/main.cpp b/cpp09/ex01/main.cpp new file mode 100644 index 0000000..bd6e83d --- /dev/null +++ b/cpp09/ex01/main.cpp @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/06 15:13:10 by fgras-ca #+# #+# */ +/* Updated: 2024/03/06 15:44:07 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RPN.hpp" + +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' + } + + try + { + float 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; + } + } + return (0); +}