first commits
This commit is contained in:
commit
3503abc649
|
|
@ -0,0 +1,90 @@
|
|||
#include "ScalarConverter.hpp"
|
||||
|
||||
bool ischar(const std::string& input)
|
||||
{
|
||||
return (input.length() == 1);
|
||||
}
|
||||
|
||||
bool isInt(const std::string& input)
|
||||
{
|
||||
if (input.empty())
|
||||
return false;
|
||||
size_t i = 0;
|
||||
if (input[0] == '+' || input[0] == '-')
|
||||
i = 1;
|
||||
if (i >= input.length())
|
||||
return false;
|
||||
while (i < input.length())
|
||||
{
|
||||
if (!std::isdigit(input[i]))
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isFloat(const std::string& input)
|
||||
{
|
||||
if (input.empty())
|
||||
return false;
|
||||
size_t i = 0;
|
||||
bool hasPoint = false;
|
||||
bool hasDigit = false;
|
||||
if (input[0] == '+' || input[0] == '-')
|
||||
i = 1;
|
||||
while (i < input.length())
|
||||
{
|
||||
if (std::isdigit(input[i]))
|
||||
hasDigit = true;
|
||||
else if (input[i] == '.' && !hasPoint)
|
||||
hasPoint = true;
|
||||
else if (input[i] == 'f' && i == input.length() - 1 && hasDigit)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
return (hasDigit && hasPoint);
|
||||
}
|
||||
|
||||
bool isDouble(const std::string& input)
|
||||
{
|
||||
if (input.empty())
|
||||
return false;
|
||||
size_t i = 0;
|
||||
bool hasPoint = false;
|
||||
bool hasDigit = false;
|
||||
if (input[0] == '+' || input[0] == '-')
|
||||
i = 1;
|
||||
while (i < input.length())
|
||||
{
|
||||
if (std::isdigit(input[i]))
|
||||
hasDigit = true;
|
||||
else if (input[i] == '.' && !hasPoint)
|
||||
hasPoint = true;
|
||||
else
|
||||
return false;
|
||||
i++;
|
||||
}
|
||||
return (hasDigit && hasPoint);
|
||||
}
|
||||
|
||||
void ScalarConverter::toChar(const std::string &input)
|
||||
{
|
||||
char c = input[0];
|
||||
int i = static_cast<int>(c);
|
||||
float f = static_cast<float>(c);
|
||||
double d = static_cast<double>(c);
|
||||
|
||||
if (input.size() == 3)
|
||||
if (input[0] == '\'' && input[2] == '\'');
|
||||
c = input[1];
|
||||
|
||||
if (isprint(c))
|
||||
std::cout << "char: \t'" << c << "'" << std::endl;
|
||||
else
|
||||
std::cout << "char: \t" << "non displayable" << std::endl;
|
||||
std::cout << "int: \t" << i << std::endl;
|
||||
std::cout << "float: \t" << std::fixed << std::setprecision(1) << f << "f" << std::endl;
|
||||
std::cout << "double: \t" << d << std::endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef SCALARCONVERTER_HPP
|
||||
#define SCALARCONVERTER_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
|
||||
class ScalarConverter
|
||||
{
|
||||
private:
|
||||
ScalarConverter(void);
|
||||
ScalarConverter(ScalarConverter const &src);
|
||||
~ScalarConverter(void);
|
||||
ScalarConverter &operator=(ScalarConverter const &rhs);
|
||||
|
||||
public:
|
||||
void convert(const std::string& input);
|
||||
|
||||
static bool isChar(const std::string& input);
|
||||
static bool isInt(const std::string& input);
|
||||
static bool isFloat(const std::string& input);
|
||||
static bool isDouble(const std::string& input);
|
||||
void ScalarConverter::toChar(const std::string &input);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue