「✨」 feat: finished ex01
This commit is contained in:
60
ex01/Makefile
Normal file
60
ex01/Makefile
Normal file
@ -0,0 +1,60 @@
|
||||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
|
||||
# Updated: 2025/06/03 11:58:38 by adjoly ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
SHELL = bash
|
||||
|
||||
NAME = RPN
|
||||
|
||||
CC = clang++
|
||||
|
||||
OBJSDIR = obj/
|
||||
|
||||
SRCS = $(shell find . -name '*.cpp')
|
||||
|
||||
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
|
||||
|
||||
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
|
||||
|
||||
RED = \033[0;31m
|
||||
GREEN = \033[0;32m
|
||||
YELLOW = \033[1;33m
|
||||
PURPLE = \e[0;35m
|
||||
NC = \033[0m
|
||||
DELETE = \x1B[2K\r
|
||||
|
||||
ifeq ($(VERBOSE),true)
|
||||
FLAGS += -D VERBOSE
|
||||
endif
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJS)
|
||||
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
|
||||
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
|
||||
|
||||
$(OBJSDIR)%.o: %.cpp
|
||||
@mkdir -p $(@D)
|
||||
@$(CC) $(FLAGS) -I . -c $< -o $@
|
||||
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
|
||||
|
||||
clean:
|
||||
@rm -f $(OBJS)
|
||||
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
|
||||
|
||||
fclean: clean
|
||||
@rm -f $(NAME)
|
||||
@rm -Rf $(OBJSDIR)
|
||||
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: clean fclean all re
|
77
ex01/RPN.cpp
Normal file
77
ex01/RPN.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* RPN.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/03 12:07:59 by adjoly #+# #+# */
|
||||
/* Updated: 2025/06/03 15:56:22 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <RPN.hpp>
|
||||
#include <cctype>
|
||||
#include <exception>
|
||||
#include <sstream>
|
||||
#include <stack>
|
||||
#include <stdexcept>
|
||||
|
||||
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("can't divide by 0 sorry D:");
|
||||
return first / second;
|
||||
}
|
||||
|
||||
void calc(int calc(int, int), std::string::const_iterator ,
|
||||
std::stack<int> &stack) {
|
||||
int first, second;
|
||||
|
||||
if (stack.size() != 2)
|
||||
throw std::range_error("need 2 at least two number");
|
||||
second = stack.top();
|
||||
stack.pop();
|
||||
first = stack.top();
|
||||
stack.pop();
|
||||
if (stack.size() != 0)
|
||||
throw std::range_error("too much number");
|
||||
stack.push(calc(first, second));
|
||||
// if (*(c + 1) != ' ' || *(c + 1) != '\0')
|
||||
// throw std::invalid_argument("");
|
||||
}
|
||||
|
||||
void handle(std::string::const_iterator c, std::stack<int> &stack) {
|
||||
if (*c == ' ')
|
||||
return;
|
||||
else if (std::isdigit(*c))
|
||||
stack.push(*c - '0');
|
||||
else if (*c == '+')
|
||||
calc(add, c, stack);
|
||||
else if (*c == '-')
|
||||
calc(subst, c, stack);
|
||||
else if (*c == '*')
|
||||
calc(mult, c, stack);
|
||||
else if (*c == '/')
|
||||
calc(div, c, stack);
|
||||
else
|
||||
throw std::invalid_argument("invalid caracter detected");
|
||||
}
|
||||
|
||||
void rpn(char *av) {
|
||||
std::string str(av);
|
||||
std::stack<int> stack;
|
||||
|
||||
for (auto it = range(str)) {
|
||||
try {
|
||||
handle(it, stack);
|
||||
} catch (std::exception &e) {
|
||||
std::stringstream str;
|
||||
str << "Error: " << e.what() << " : " << *it;
|
||||
throw std::runtime_error(str.str());
|
||||
}
|
||||
}
|
||||
std::cout << stack.top() << std::endl;
|
||||
}
|
30
ex01/RPN.hpp
Normal file
30
ex01/RPN.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* RPN.hpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/03 12:00:35 by adjoly #+# #+# */
|
||||
/* Updated: 2025/06/03 12:09:55 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
|
||||
#define range(x) \
|
||||
x.begin(); \
|
||||
it != x.end(); \
|
||||
it++
|
||||
|
||||
#define auto __auto_type
|
||||
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned short uint16_t;
|
||||
|
||||
|
||||
void rpn(char *);
|
26
ex01/main.cpp
Normal file
26
ex01/main.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/03 11:58:43 by adjoly #+# #+# */
|
||||
/* Updated: 2025/06/03 15:54:55 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <RPN.hpp>
|
||||
#include <exception>
|
||||
|
||||
int main(int ac, char **av) {
|
||||
if (ac > 1 && ac < 3) {
|
||||
try {
|
||||
rpn(av[1]);
|
||||
} catch(std::exception &e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Error" << std::endl;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user