This commit is contained in:
Camille Chauvet 2023-09-15 13:16:49 +00:00
commit 9be606322d
7 changed files with 133 additions and 0 deletions

26
ex00/Makefile Normal file
View File

@ -0,0 +1,26 @@
CXX := c++
CXXFLAGS := -std=c++98 -Wall -Wextra -Werror -g
SRCDIR := src
OBJDIR := obj
NAME := convert
SRCS := $(wildcard $(SRCDIR)/*.cpp)
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCS))
all: $(NAME)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
mkdir -p $(OBJDIR)
$(CXX) $(CXXFLAGS) -c $< -o $@
$(NAME): $(OBJS)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(OBJDIR)
fclean: clean
rm -fr $(NAME)
re: fclean
@make --no-print-directory all

BIN
ex00/convert Executable file

Binary file not shown.

BIN
ex00/obj/ScalarConverter.o Normal file

Binary file not shown.

BIN
ex00/obj/main.o Normal file

Binary file not shown.

View File

@ -0,0 +1,73 @@
#include "ScalarConverter.hpp"
#include <cctype>
#include <cerrno>
#include <cstdlib>
#include <cstddef>
#include <exception>
#include <iostream>
#include <string>
#include <limits.h>
ScalarConverter::ScalarConverter()
{}
ScalarConverter::ScalarConverter(const ScalarConverter& src)
{
(void) src;
}
ScalarConverter::~ScalarConverter()
{}
ScalarConverter& ScalarConverter::operator=(const ScalarConverter& src)
{
if (this != &src)
*this = src;
return *this;
}
double get_value(const std::string& str)
{
char *pos;
double value = std::strtod(str.c_str(), &pos);
if (pos)
std::cout << pos << std::endl;
if (errno == ERANGE)
throw std::exception();
return value;
}
void convert2char(const double value)
{
std::cout << "char: ";
if (isprint(static_cast<char>(value)))
std::cout << static_cast<char>(value);
else
std::cout << "Non displayable";
std::cout << std::endl;
}
void convert2int(const double value)
{
std::cout << "int: ";
if (value > INT_MAX)
std::cout << "imposible";
else
std::cout << static_cast<int>(value);
std::cout << std::endl;
}
void ScalarConverter::convert(const std::string &str)
{
double value;
try {
value = get_value(str);
} catch (std::exception &e) {
std::cout << "convert imposible" << std::endl;
return;
}
convert2char(value);
convert2int(value);
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <string>
#define CHAREX "'.{1}'"
#define INTEX "^[+\\-]*[0-9]+$"
#define FLOATEX "^([+\\-]*(([0-9]+(\\.[0-9]+)?)|inf|nan))f?$"
#define DOUBLEEX "^([+\\-]*(([0-9]+(\\.[0-9]+)?)|inf|nan))f?$"
class ScalarConverter
{
private:
ScalarConverter();
ScalarConverter(const ScalarConverter& src);
~ScalarConverter();
ScalarConverter& operator=(const ScalarConverter& src);
public:
static void convert(const std::string& str);
};

14
ex00/src/main.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "ScalarConverter.hpp"
#include <iostream>
int main(int ac, char **av) {
if (ac != 2) {
std::cerr << "Wrong amount of arguments."
" This program takes one argument: the literal to convert."
<< std::endl;
return 1;
}
ScalarConverter::convert(av[1]);
}