「✨」 feat(ex01): RPN is working :D
This commit is contained in:
58
ex01/Makefile
Normal file
58
ex01/Makefile
Normal file
@@ -0,0 +1,58 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
|
||||
# Updated: 2025/08/27 14:42:13 by mmoussou ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SHELL = bash
|
||||
|
||||
RED = \033[0;31m
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[1;33m
|
||||
PURPLE = \e[0;35m
|
||||
NC = \033[0m
|
||||
DELETE = \x1B[2K
|
||||
|
||||
CXX = c++
|
||||
|
||||
FLAGS = -std=c++98 -Wall -Werror -Wextra
|
||||
|
||||
INCLUDES = .
|
||||
|
||||
NAME = RPN
|
||||
|
||||
SRCS = main.cpp RPN.cpp
|
||||
|
||||
OBJSDIR = obj/
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
@$(CXX) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
|
||||
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
|
||||
|
||||
$(OBJSDIR)%.o: %.cpp
|
||||
@mkdir -p $(@D)
|
||||
@$(CXX) $(FLAGS) -I$(INCLUDES) -c $< -o $@
|
||||
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
|
||||
|
||||
clean:
|
||||
@rm -f $(OBJS)
|
||||
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@rm -Rf $(OBJSDIR)
|
||||
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
|
||||
|
||||
re: fclean
|
||||
@$(MAKE) -s all
|
||||
|
||||
.PHONY: clean fclean all re
|
||||
55
ex01/RPN.cpp
Normal file
55
ex01/RPN.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "RPN.hpp"
|
||||
|
||||
int add(int first, int second) { return first + second; }
|
||||
int subst(int first, int second) { return first - second; }
|
||||
int mult(int first, int second) { return first * second; }
|
||||
int div(int first, int second) {
|
||||
if (second == 0)
|
||||
throw std::invalid_argument("division by 0 is not possible");
|
||||
return first / second;
|
||||
}
|
||||
|
||||
|
||||
typedef int (*OperatorFunctions)(int, int);
|
||||
|
||||
void rpn(char *av)
|
||||
{
|
||||
OperatorFunctions calc[128] = { NULL };
|
||||
calc['+'] = &add;
|
||||
calc['-'] = &subst;
|
||||
calc['*'] = &mult;
|
||||
calc['/'] = ÷
|
||||
|
||||
std::istringstream ss_input((std::string(av)));
|
||||
std::stack<int> 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;
|
||||
}
|
||||
|
||||
9
ex01/RPN.hpp
Normal file
9
ex01/RPN.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <stack>
|
||||
|
||||
void rpn(char *);
|
||||
21
ex01/main.cpp
Normal file
21
ex01/main.cpp
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user