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

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 */
/* */
/* ************************************************************************** */
@@ -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);

BIN
cpp09/ex01/rpn Executable file

Binary file not shown.