dfsdfsdfsdf#
This commit is contained in:
parent
ac048446e5
commit
f085080c7d
|
|
@ -0,0 +1,24 @@
|
||||||
|
#include "ScalarConverter.hpp"
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
std::cout << "Usage: ./convert <value_to_cast1> <value_to_cast2> ..." << 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);
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -113,7 +113,7 @@ void ScalarConverter::toChar(const std::string &input)
|
||||||
double d = static_cast<double>(c);
|
double d = static_cast<double>(c);
|
||||||
|
|
||||||
if (input.size() == 3)
|
if (input.size() == 3)
|
||||||
if (input[0] == '\'' && input[2] == '\'');
|
if (input[0] == '\'' && input[2] == '\'')
|
||||||
c = input[1];
|
c = input[1];
|
||||||
|
|
||||||
if (isprint(c))
|
if (isprint(c))
|
||||||
|
|
@ -125,6 +125,66 @@ void ScalarConverter::toChar(const std::string &input)
|
||||||
std::cout << "double: \t" << d << std::endl;
|
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<char>::min() && i <= std::numeric_limits<char>::max())
|
||||||
|
{
|
||||||
|
char c = static_cast<char>(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<float>(i) << "f" << std::endl;
|
||||||
|
std::cout << "double: \t" << static_cast<double>(i) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScalarConverter::toFloat(const std::string &input)
|
||||||
|
{
|
||||||
|
float f = static_cast<float>(atof(input.c_str()));
|
||||||
|
if (f >= std::numeric_limits<char>::min() && f <= std::numeric_limits<char>::max())
|
||||||
|
{
|
||||||
|
char c = static_cast<char>(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<int>::min() && f <= std::numeric_limits<int>::max())
|
||||||
|
std::cout << "int: \t" << static_cast<int>(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<double>(f) << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScalarConverter::toDouble(const std::string &input)
|
||||||
|
{
|
||||||
|
double d = atof(input.c_str());
|
||||||
|
if (d >= std::numeric_limits<char>::min() && d <= std::numeric_limits<char>::max())
|
||||||
|
{
|
||||||
|
char c = static_cast<char>(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<int>::min() && d <= std::numeric_limits<int>::max())
|
||||||
|
std::cout << "int: \t" << static_cast<int>(d) << std::endl;
|
||||||
|
else
|
||||||
|
std::cout << "int: \timpossible" << std::endl;
|
||||||
|
std::cout << "float: \t" << std::fixed << std::setprecision(1) << static_cast<float>(d) << "f" << std::endl;
|
||||||
|
std::cout << "double: \t" << d << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
void ScalarConverter::toOther(const std::string &input)
|
void ScalarConverter::toOther(const std::string &input)
|
||||||
{
|
{
|
||||||
std::cout << "char: \t" << "impossible" << std::endl;
|
std::cout << "char: \t" << "impossible" << std::endl;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
class ScalarConverter
|
class ScalarConverter
|
||||||
{
|
{
|
||||||
|
|
@ -15,7 +16,7 @@ class ScalarConverter
|
||||||
ScalarConverter& operator=(ScalarConverter const &rhs);
|
ScalarConverter& operator=(ScalarConverter const &rhs);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void convert(const std::string& input);
|
static void convert(const std::string& input);
|
||||||
|
|
||||||
static bool isChar(const std::string& input);
|
static bool isChar(const std::string& input);
|
||||||
static bool isInt(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 isDouble(const std::string& input);
|
||||||
static bool isOther(const std::string &input);
|
static bool isOther(const std::string &input);
|
||||||
|
|
||||||
void ScalarConverter::toChar(const std::string &input);
|
static void toChar(const std::string &input);
|
||||||
void ScalarConverter::toOther(const std::string &input);
|
static void toOther(const std::string &input);
|
||||||
void ScalarConverter::toInt(const std::string &input);
|
static void toInt(const std::string &input);
|
||||||
void ScalarConverter::toFloat(const std::string &input);
|
static void toFloat(const std::string &input);
|
||||||
void ScalarConverter::toDouble(const std::string &input);
|
static void toDouble(const std::string &input);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue