ex01 done
This commit is contained in:
43
ex01/Makefile
Normal file
43
ex01/Makefile
Normal file
@@ -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
|
||||||
108
ex01/RPN.cpp
Normal file
108
ex01/RPN.cpp
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* RPN.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 == "/");
|
||||||
|
}
|
||||||
36
ex01/RPN.hpp
Normal file
36
ex01/RPN.hpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* RPN.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <iostream>
|
||||||
|
# include <stack>
|
||||||
|
# include <sstream>
|
||||||
|
# include <cstdlib>
|
||||||
|
|
||||||
|
class RPN
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::stack<int> numbers;
|
||||||
|
public:
|
||||||
|
RPN();
|
||||||
|
RPN(const RPN &other);
|
||||||
|
RPN& operator=(const RPN &other);
|
||||||
|
~RPN();
|
||||||
|
|
||||||
|
bool isToken(std::string token);
|
||||||
|
void math(std::string data);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
27
ex01/main.cpp
Normal file
27
ex01/main.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/04/24 12:37:45 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/04/24 12:57:54 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "RPN.hpp"
|
||||||
|
using std::cout;
|
||||||
|
using std::endl;
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
cout << "Error." << endl;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
RPN calc;
|
||||||
|
calc.math(argv[1]);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user