diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..e2e614e --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,43 @@ +NAME = RPN + +SRC = main.cpp RPN.cpp + + +OBJ = $(SRC:.cpp=.o) + +CC = c++ +CFLAGS = -Wall -Wextra -Werror -std=c++98 + +GREEN = \033[0;32m +RED = \033[0;31m +RESET = \033[0m + +TOTAL := $(words $(SRC)) +COUNT = 0 + +all: $(NAME) + +$(NAME): $(OBJ) + @rm -f .build_start + @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) + @printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)" + +%.o: %.cpp + @if [ ! -f .build_start ]; then printf "$(GREEN)Compilando archivos...\n$(RESET)"; touch .build_start; fi + @$(eval COUNT = $(shell echo $$(($(COUNT)+1)))) + @PERCENT=$$(($(COUNT)*100/$(TOTAL))); \ + BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \ + SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \ + printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL) + @$(CC) $(CFLAGS) -c $< -o $@ + +clean: + @printf "$(RED)Eliminando...\n$(RESET)" + @rm -f $(OBJ) + +fclean: clean + @rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex01/RPN.cpp b/ex01/RPN.cpp new file mode 100644 index 0000000..60a707a --- /dev/null +++ b/ex01/RPN.cpp @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RPN.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/04/24 12:37:09 by aortigos #+# #+# */ +/* Updated: 2026/04/24 12:37:09 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "RPN.hpp" + +////////////////// +// Constructors // +////////////////// + +RPN::RPN() +{ + // std::cout << "RPN default constructor called" << std::endl; +} + +RPN::RPN(const RPN &other) +{ + *this = other; + // std::cout << "RPN copy constructor called" << std::endl; +} + +RPN& RPN::operator=(const RPN &other) +{ + if (this != &other) + { + // Copy attributes here + } + // std::cout << "RPN copy assignment operator called" << std::endl; + return (*this); +} + +RPN::~RPN() +{ + // std::cout << "RPN destructor called" << std::endl; +} + +void RPN::math(std::string data) +{ + std::istringstream ss(data); + std::string token; + int nbr; + + while (ss >> token) + { + if (isToken(token)) + { + + if (numbers.size() < 2) + { + std::cout << "Error." << std::endl; + return ; + } + + int b = numbers.top(); + numbers.pop(); + int a = numbers.top(); + numbers.pop(); + + if (token == "+") + numbers.push(a + b); + if (token == "-") + numbers.push(a - b); + if (token == "*") + numbers.push(a * b); + if (token == "/") + numbers.push(a / b); + } + else + { + char *endptr; + + nbr = std::strtol(token.c_str(), &endptr, 10); + if (*endptr != '\0') + { + std::cout << "Error." << std::endl; + return ; + } + + if (nbr >= 10) + { + std::cout << "Error." << std::endl; + return ; + } + + this->numbers.push(nbr); + } + } + if (numbers.size() != 1) + { + std::cout << "Error." << std::endl; + return ; + } + std::cout << numbers.top() << std::endl; +} + +bool RPN::isToken(std::string token) +{ + return (token == "+" || token == "-" + || token == "*" || token == "/"); +} \ No newline at end of file diff --git a/ex01/RPN.hpp b/ex01/RPN.hpp new file mode 100644 index 0000000..4539ed2 --- /dev/null +++ b/ex01/RPN.hpp @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RPN.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/04/24 12:37:09 by aortigos #+# #+# */ +/* Updated: 2026/04/24 12:37:09 by aortigos ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RPN_HPP + +# define RPN_HPP + +# include +# include +# include +# include + +class RPN +{ + private: + std::stack numbers; + public: + RPN(); + RPN(const RPN &other); + RPN& operator=(const RPN &other); + ~RPN(); + + bool isToken(std::string token); + void math(std::string data); +}; + +#endif diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..e9dae47 --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos