From 72612a10079e68e96ddaeffaa9d5dbb8ed6d2090 Mon Sep 17 00:00:00 2001 From: "Luna Mira Lage (Desoroxxx)" Date: Wed, 23 Sep 2026 18:02:52 +0200 Subject: [PATCH] ex01 Signed-off-by: Luna Mira Lage (Desoroxxx) --- ex01/Makefile | 4 +++ ex01/src/RPN.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++ ex01/src/RPN.hpp | 15 +++++++++ ex01/src/main.cpp | 20 +++++++++++ 4 files changed, 124 insertions(+) create mode 100644 ex01/Makefile create mode 100644 ex01/src/RPN.cpp create mode 100644 ex01/src/RPN.hpp create mode 100644 ex01/src/main.cpp diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..e99f4bf --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,4 @@ +override NAME = RPN +override SRC_FILES = main.cpp RPN.cpp + +include ../Makefile diff --git a/ex01/src/RPN.cpp b/ex01/src/RPN.cpp new file mode 100644 index 0000000..9ab2a54 --- /dev/null +++ b/ex01/src/RPN.cpp @@ -0,0 +1,85 @@ +#include + +#include +#include +#include + +RPN::RPN() {} + +RPN::RPN(const RPN& other) { (void)other; } + +enum Operator { PLUS, MINUS, MULTIPLY, DIVIDE }; + +static bool isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } + +static Operator getOperator(char c) { + switch (c) { + case '+': + return PLUS; + case '-': + return MINUS; + case '*': + return MULTIPLY; + case '/': + return DIVIDE; + default: + throw std::runtime_error(""); + } +} + +static long operate(const Operator& op, const long& a, const long& b) { + switch (op) { + case PLUS: + return a + b; + case MINUS: + return a - b; + case MULTIPLY: + return a * b; + case DIVIDE: + return a / b; + } +} + +long RPN::evaluate(const std::string& expression) const { + std::stack stack; + + for (size_t i = 0; i < expression.size(); ++i) { + if (expression[i] == ' ') + continue; + + if (isOperator(expression[i])) { + if (stack.size() < 2) + throw std::runtime_error(""); + + const Operator op = getOperator(expression[i]); + + const long b = stack.top(); + stack.pop(); + const long a = stack.top(); + stack.pop(); + + if (op == DIVIDE && b == 0) + throw std::runtime_error(""); + + stack.push(operate(op, a, b)); + } else if (std::isdigit(static_cast(expression[i]))) { + stack.push(expression[i] - '0'); + } else { + throw std::runtime_error(""); + } + } + + if (stack.size() != 1) + throw std::runtime_error(""); + + return stack.top(); +} + +RPN& RPN::operator=(const RPN& other) { + if (this == &other) + return *this; + + return *this; +} + +RPN::~RPN() {} diff --git a/ex01/src/RPN.hpp b/ex01/src/RPN.hpp new file mode 100644 index 0000000..f92dba0 --- /dev/null +++ b/ex01/src/RPN.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +class RPN { + public: + RPN(); + RPN(const RPN& other); + + long evaluate(const std::string& expression) const; + + RPN& operator=(const RPN& other); + + ~RPN(); +}; diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp new file mode 100644 index 0000000..74512a4 --- /dev/null +++ b/ex01/src/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +int main(int argc, char* argv[]) { + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + + try { + const RPN rpn; + + std::cout << rpn.evaluate(argv[1]) << std::endl; + } catch (const std::exception& exception) { + std::cerr << "Error" << std::endl; + std::cerr << exception.what() << std::endl; + return EXIT_FAILURE; + } +} -- 2.51.2