d06 ex00 all checks are ok

This commit is contained in:
hugogogo
2022-03-07 22:30:41 +01:00
parent 142687283e
commit 4ee3ae3e7d
10 changed files with 234 additions and 73 deletions

View File

@@ -1,11 +1,21 @@
#include <iostream>
#include <string>
#include <cctype> // isdigit()
#include "color.h"
#include "convert.h"
//Char toChar(str) {
//
//}
void fromChar(char c) {
int i;
float f;
double d;
i = static_cast<int>(c);
std::cout << std::setw(7) << std::left << "int" << B_CYAN << i << RESET "\n";
f = static_cast<float>(c);
std::cout << std::setw(7) << std::left << "float" << B_CYAN << f << RESET "\n";
d = static_cast<double>(c);
std::cout << std::setw(7) << std::left << "double" << B_CYAN << d << RESET "\n";
}
char toChar(std::string str) {
return str[0];
}
bool isChar(std::string str) {
if (str.length() != 1 || isdigit(str[0]))
@@ -14,17 +24,17 @@ bool isChar(std::string str) {
}
bool checkChar(std::string str) {
// char c;
char c;
std::cout << "char\n";
if (str.length() != 1 || isdigit(str[0]))
return false;
// c = toChar(str);
// std::cout << "char"RESET" equal to : "B_CYAN << c << RESET << "\n";
// fromChar(c);
c = toChar(str);
std::cout << B_YELLOW << str << RESET " is a char"
<< " of value "
<< B_CYAN << c << RESET << "\n";
fromChar(c);
return true;
}

View File

@@ -1,16 +1,50 @@
#include <iostream>
#include <string>
#include <cctype> // isdigit()
#include "color.h"
#include "convert.h"
void fromDouble(double d) {
char c;
int i;
float f;
c = static_cast<char>(d);
std::cout << std::setw(7) << std::left << "char" << B_CYAN << c << RESET "\n";
i = static_cast<int>(d);
std::cout << std::setw(7) << std::left << "int" << B_CYAN << i << RESET "\n";
f = static_cast<float>(d);
std::cout << std::setw(7) << std::left << "float" << B_CYAN << f << RESET "\n";
}
//Char toChar(str) {
//
// char c;
// std::istringstream(str) >> c;
// return c;
//}
bool isDouble(std::string str) {
if (str.length() == 3)
size_t l;
std::string neg = "";
if (str[0] == '-')
neg = "-";
if (str[0] == '+' || str[0] == '-')
str.erase(str.begin());
if (!str.length())
return false;
if (!str.compare("inf") || !str.compare("nan"))
return true;
return false;
l = str.find_first_not_of("0123456789");
if (l == std::string::npos || str[l] != '.')
return false;
str.erase(0,l + 1);
if (str.find_first_not_of("0123456789") != std::string::npos)
return false;
str.insert(0, neg);
strtod(str.c_str(), NULL);
if (errno == ERANGE)
return false;
return true;
}
bool checkDouble(std::string str) {

View File

@@ -1,23 +1,43 @@
#include <iostream>
#include <string>
#include <cctype> // isdigit()
#include "color.h"
#include "convert.h"
//Char toChar(str) {
//
//}
bool isFloat(std::string str) {
if (str.length() == 4)
size_t l;
// std::string float_xtrem = MAX_FLOAT;
if (str[0] == '+' || str[0] == '-')
str.erase(str.begin());
// if (str.length() == 0 || str.length() > FLOAT_MAX_LENGTH)
if (str.length() == 0)
return false;
if (!str.compare("inff") || !str.compare("nanf"))
return true;
return false;
l = str.find_first_not_of("0123456789");
if (l == std::string::npos || str[l] != '.')
return false;
str.erase(0,l + 1);
l = str.find_first_not_of("0123456789");
if (l == std::string::npos || str[l] != 'f')
return false;
// to work, one should check the decimal and the integer part
// str.erase(str.end() - 1);
// if (str.length() < FLOAT_MAX_LENGTH)
// return true;
// if (str.compare(float_xtrem) > 0)
// return false;
return true;
}
bool checkFloat(std::string str) {
// float f;
std::cout << "in char\n";
std::cout << "float\n";
if (!isFloat(str))
return false;

View File

@@ -1,16 +1,26 @@
#include <iostream>
#include <string>
#include <cctype> // isdigit()
#include "color.h"
#include "convert.h"
//Char toInt(str) {
//
//}
bool isInt(std::string str) {
if (str.length() == 2)
std::string int_xtrem = MAX_INT;
if (str[0] == '-')
int_xtrem = MAX_INT_1;
if (str[0] == '+' || str[0] == '-')
str.erase(str.begin());
if (str.length() == 0 || str.length() > INT_MAX_LENGTH)
return false;
if (str.find_first_not_of("0123456789") != std::string::npos)
return false;
if (str.length() < INT_MAX_LENGTH)
return true;
return false;
if (str.compare(int_xtrem) > 0)
return false;
return true;
}
bool checkInt(std::string str) {

View File

@@ -1,14 +1,9 @@
#include <iostream>
#include <string>
#include "color.h"
bool checkChar(std::string str);
bool checkInt(std::string str);
bool checkFloat(std::string str);
bool checkDouble(std::string str);
#include "convert.h"
bool isElse(std::string str) {
std::cout << str << B_RED " is not valid type for this exercise" RESET "\n";
std::cout << B_RED << str << CYAN " is not valid type for this exercise" RESET "\n";
return true;
}
@@ -23,9 +18,11 @@ bool (*checkFunc[])(std::string str) =
void convert(std::string str) {
std::cout << B_BLUE << str << RESET "\n";
int size = sizeof(checkFunc) / sizeof(checkFunc[0]);
for (int it = 0; it < size; it++)
if ((*checkFunc[it])(str))
break;
std::cout << "\n";
}