From 1458548d67805e4e0811070d8852a4b302314567 Mon Sep 17 00:00:00 2001 From: yosyo Date: Wed, 27 Aug 2025 15:58:23 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat(ex01):=20RPN?= =?UTF-8?q?=20is=20working=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex00/BitcoinExchange.hpp | 2 ++ ex01/Makefile | 58 ++++++++++++++++++++++++++++++++++++++++ ex01/RPN.cpp | 55 +++++++++++++++++++++++++++++++++++++ ex01/RPN.hpp | 9 +++++++ ex01/main.cpp | 21 +++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 ex01/Makefile create mode 100644 ex01/RPN.cpp create mode 100644 ex01/RPN.hpp create mode 100644 ex01/main.cpp diff --git a/ex00/BitcoinExchange.hpp b/ex00/BitcoinExchange.hpp index 6ac2700..8a9f2b5 100644 --- a/ex00/BitcoinExchange.hpp +++ b/ex00/BitcoinExchange.hpp @@ -1,3 +1,5 @@ +#pragma once + #include #include #include diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..0c23ed6 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou stack; + + std::string value; + + while (ss_input >> value) + { + if (value.size() != 1) + throw std::invalid_argument("invalid value"); + + if (isdigit(value[0])) + { + stack.push(value[0] - '0'); + continue ; + } + + if (!calc[(int) value[0]]) + throw std::invalid_argument("invalid character"); + + if (stack.size() < 2) + throw std::range_error("not enough operands"); + + int a = stack.top(); stack.pop(); + int b = stack.top(); stack.pop(); + stack.push(calc[(int) value[0]](b, a)); + } + + if (stack.size() != 1) + throw std::range_error("there are too many operands"); + + std::cout << stack.top() << std::endl; +} + diff --git a/ex01/RPN.hpp b/ex01/RPN.hpp new file mode 100644 index 0000000..1031e5d --- /dev/null +++ b/ex01/RPN.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include +#include +#include + +void rpn(char *); diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..68348b2 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,21 @@ +#include "RPN.hpp" + +int main(int ac, char **av) +{ + if (ac == 2) + { + try + { + rpn(av[1]); + } + catch(std::exception &e) + { + std::cerr << e.what() << std::endl; + } + } + else + { + std::cerr << "Error: wrong usage." << std::endl; + } +} +