From f085080c7dc682d02c42b973c45317a7e403ad99 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 31 Jan 2026 18:43:12 +0000 Subject: [PATCH] dfsdfsdfsdf# --- ex00/Main.cpp | 24 ++++++++++++++++ ex00/Makefile | 36 +++++++++++++++++++++++ ex00/ScalarConverter.cpp | 62 +++++++++++++++++++++++++++++++++++++++- ex00/ScalarConverter.hpp | 13 +++++---- 4 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 ex00/Makefile diff --git a/ex00/Main.cpp b/ex00/Main.cpp index e69de29..5dfa0c0 100644 --- a/ex00/Main.cpp +++ b/ex00/Main.cpp @@ -0,0 +1,24 @@ +#include "ScalarConverter.hpp" + +int main(int argc, char **argv) +{ + std::cout << std::endl; + + if (argc < 2) + std::cout << "Usage: ./convert ..." << std::endl; + + int i = 1; + + while (argv[i]) + { + std::cout << "############## OUTPUT " << i << " ##############" << std::endl; + std::cout << "user input:\t\"" << argv[i] << "\"" << std::endl; + + if (argv[i][0]) + ScalarConverter::convert(argv[i++]); + else + std::cout << "Argument " << i++ << " is empty." << std::endl; + std::cout << std::endl; + } + return (0); +} \ No newline at end of file diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..839085e --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,36 @@ +CXX = c++ +CXXFLAGS = -Wall -Wextra -Werror -std=c++98 +OBJDIR = obj +SOURCES = Main.cpp ScalarConverter.cpp +OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o)) +NAME = Bureaucrat + +all: $(NAME) + +$(OBJDIR): + @echo "๐Ÿ“ Creating obj directory..." + @mkdir -p $(OBJDIR) + +$(OBJDIR)/%.o: %.cpp | $(OBJDIR) + @echo "๐Ÿง  Compiling $< ..." + @$(CXX) $(CXXFLAGS) -c $< -o $@ + @echo "โœ… $@ ready!" + +$(NAME): $(OBJECTS) + @echo "๐Ÿ”— Linking $(NAME) ..." + @$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME) + @echo "๐ŸŽ‰ $(NAME) is ready!" + +clean: + @echo "๐Ÿงน Cleaning object files..." + @rm -rf $(OBJDIR) + @echo "โœจ Objects cleaned!" + +fclean: clean + @echo "๐Ÿ—‘๏ธ Removing $(NAME)..." + @rm -f $(NAME) + @echo "๐Ÿ’€ Full clean complete!" + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex00/ScalarConverter.cpp b/ex00/ScalarConverter.cpp index ffa14f4..a7c0311 100644 --- a/ex00/ScalarConverter.cpp +++ b/ex00/ScalarConverter.cpp @@ -113,7 +113,7 @@ void ScalarConverter::toChar(const std::string &input) double d = static_cast(c); if (input.size() == 3) - if (input[0] == '\'' && input[2] == '\''); + if (input[0] == '\'' && input[2] == '\'') c = input[1]; if (isprint(c)) @@ -125,6 +125,66 @@ void ScalarConverter::toChar(const std::string &input) std::cout << "double: \t" << d << std::endl; } +void ScalarConverter::toInt(const std::string &input) +{ + int i = atoi(input.c_str()); + if (i >= std::numeric_limits::min() && i <= std::numeric_limits::max()) + { + char c = static_cast(i); + if (isprint(c)) + std::cout << "char: \t'" << c << "'" << std::endl; + else + std::cout << "char: \tnon displayable" << std::endl; + } + else + std::cout << "char: \timpossible" << std::endl; + std::cout << "int: \t" << i << std::endl; + std::cout << "float: \t" << std::fixed << std::setprecision(1) << static_cast(i) << "f" << std::endl; + std::cout << "double: \t" << static_cast(i) << std::endl; +} + +void ScalarConverter::toFloat(const std::string &input) +{ + float f = static_cast(atof(input.c_str())); + if (f >= std::numeric_limits::min() && f <= std::numeric_limits::max()) + { + char c = static_cast(f); + if (isprint(c)) + std::cout << "char: \t'" << c << "'" << std::endl; + else + std::cout << "char: \tnon displayable" << std::endl; + } + else + std::cout << "char: \timpossible" << std::endl; + if (f >= std::numeric_limits::min() && f <= std::numeric_limits::max()) + std::cout << "int: \t" << static_cast(f) << std::endl; + else + std::cout << "int: \timpossible" << std::endl; + std::cout << "float: \t" << std::fixed << std::setprecision(1) << f << "f" << std::endl; + std::cout << "double: \t" << static_cast(f) << std::endl; +} + +void ScalarConverter::toDouble(const std::string &input) +{ + double d = atof(input.c_str()); + if (d >= std::numeric_limits::min() && d <= std::numeric_limits::max()) + { + char c = static_cast(d); + if (isprint(c)) + std::cout << "char: \t'" << c << "'" << std::endl; + else + std::cout << "char: \tnon displayable" << std::endl; + } + else + std::cout << "char: \timpossible" << std::endl; + if (d >= std::numeric_limits::min() && d <= std::numeric_limits::max()) + std::cout << "int: \t" << static_cast(d) << std::endl; + else + std::cout << "int: \timpossible" << std::endl; + std::cout << "float: \t" << std::fixed << std::setprecision(1) << static_cast(d) << "f" << std::endl; + std::cout << "double: \t" << d << std::endl; +} + void ScalarConverter::toOther(const std::string &input) { std::cout << "char: \t" << "impossible" << std::endl; diff --git a/ex00/ScalarConverter.hpp b/ex00/ScalarConverter.hpp index d4a44bc..87c0c33 100644 --- a/ex00/ScalarConverter.hpp +++ b/ex00/ScalarConverter.hpp @@ -5,6 +5,7 @@ #include #include #include +#include class ScalarConverter { @@ -15,7 +16,7 @@ class ScalarConverter ScalarConverter& operator=(ScalarConverter const &rhs); public: - void convert(const std::string& input); + static void convert(const std::string& input); static bool isChar(const std::string& input); static bool isInt(const std::string& input); @@ -23,11 +24,11 @@ class ScalarConverter static bool isDouble(const std::string& input); static bool isOther(const std::string &input); - void ScalarConverter::toChar(const std::string &input); - void ScalarConverter::toOther(const std::string &input); - void ScalarConverter::toInt(const std::string &input); - void ScalarConverter::toFloat(const std::string &input); - void ScalarConverter::toDouble(const std::string &input); + static void toChar(const std::string &input); + static void toOther(const std::string &input); + static void toInt(const std::string &input); + static void toFloat(const std::string &input); + static void toDouble(const std::string &input); };