aboutsummaryrefslogtreecommitdiff
path: root/cpp06
diff options
context:
space:
mode:
Diffstat (limited to 'cpp06')
-rw-r--r--cpp06/ex00/Makefile4
-rw-r--r--cpp06/ex00/main.cpp139
-rwxr-xr-xcpp06/ex00/test.sh83
3 files changed, 209 insertions, 17 deletions
diff --git a/cpp06/ex00/Makefile b/cpp06/ex00/Makefile
index a5cff07..e0de1f1 100644
--- a/cpp06/ex00/Makefile
+++ b/cpp06/ex00/Makefile
@@ -6,11 +6,11 @@
# By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/04/15 09:59:30 by charles #+# #+# #
-# Updated: 2020/04/16 10:53:22 by charles ### ########.fr #
+# Updated: 2020/12/13 16:00:15 by charles ### ########.fr #
# #
# **************************************************************************** #
-NAME = scalar_conversion
+NAME = convert
CXX = clang++
CXXFLAGS = -std=c++98 -Wall -Wextra -Werror
diff --git a/cpp06/ex00/main.cpp b/cpp06/ex00/main.cpp
index b82c106..a0ef7f8 100644
--- a/cpp06/ex00/main.cpp
+++ b/cpp06/ex00/main.cpp
@@ -6,31 +6,140 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/16 10:54:10 by charles #+# #+# */
-/* Updated: 2020/11/18 15:53:33 by cacharle ### ########.fr */
+/* Updated: 2020/12/13 17:11:08 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
+#include <iomanip>
#include <sstream>
#include <string>
+#include <cctype>
+#include <climits>
+#include <cmath>
+
+bool is_char(std::string input)
+{
+ return input.length() == 3 && input[0] == '\'' && input[2] == '\'';
+}
+
+bool is_int(std::string input)
+{
+ if (input[0] == '-' || input[0] == '+')
+ input = input.substr(1);
+ for (size_t i = 0; i < input.length(); i++)
+ if (!isdigit(input[i]))
+ return false;
+ return true;
+}
+
+bool is_double(std::string input)
+{
+ if (input == "nan" || input == "+inf" || input == "-inf")
+ return true;
+ if (input[0] == '-' || input[0] == '+')
+ input = input.substr(1);
+ if (input.find('.') == std::string::npos ||
+ input.find('.') != input.rfind('.') ||
+ input.find('.') == 0 ||
+ input.find('.') == input.length() - 1)
+ return false;
+ for (size_t i = 0; i < input.length(); i++)
+ {
+ if (input[i] == '.')
+ continue;
+ if (!isdigit(input[i]))
+ return false;
+ }
+ return true;
+}
+
+bool is_float(std::string input)
+{
+ if (input[input.length() - 1] != 'f')
+ return false;
+ if (input.find('.') == std::string::npos)
+ input.insert(input.length() - 1, ".0");
+ return is_double(input.substr(0, input.length() - 1));
+}
int main(int argc, char **argv)
{
- // if (argc != 2)
- // {
- // std::cerr << "Usage: " << argv[0] << " litteral" << std::endl;
- // return 1;
- // }
- //
- //
- // std::string s(argv[1]);
- //
- // if (s.empty())
- // {
- // std::cerr << "Cannot be empty" << std::endl;
- // return 1;
- // }
+ if (argc != 2)
+ {
+ std::cerr << "Usage: " << argv[0] << " litteral" << std::endl;
+ return 1;
+ }
+ std::string s(argv[1]);
+ if (s.empty())
+ {
+ std::cerr << "Litteral cannot be empty" << std::endl;
+ return 1;
+ }
+ std::cout << std::setprecision(1) << std::fixed;
+
+ double x;
+ if (is_char(s))
+ x = s[1];
+ else if (is_int(s))
+ {
+ std::stringstream ss(s);
+ ss >> x;
+ }
+ else if (is_float(s))
+ {
+ if (s == "nanf")
+ x = NAN;
+ else if (s == "+inff")
+ x = INFINITY;
+ else if (s == "-inff")
+ x = -INFINITY;
+ else
+ {
+ std::stringstream ss(s.substr(0, s.length() - 1));
+ ss >> x;
+ }
+ }
+ else if (is_double(s))
+ {
+ if (s == "nan")
+ x = NAN;
+ else if (s == "+inf")
+ x = INFINITY;
+ else if (s == "-inf")
+ x = -INFINITY;
+ else
+ {
+ std::stringstream ss(s);
+ ss >> x;
+ }
+ }
+ else
+ {
+ std::cerr << "Litteral `" << s << "` is not valid" << std::endl;
+ return 1;
+ }
+
+
+ if (CHAR_MIN <= x && x <= CHAR_MAX && !std::isnan(x) && !std::isinf(x))
+ {
+ char c = static_cast<char>(x);
+ if (std::isprint(c))
+ std::cout << "char: '" << c << "'" << std::endl;
+ else
+ std::cout << "char: Non displayable" << std::endl;
+ }
+ else
+ std::cout << "char: impossible" << std::endl;
+
+
+ if (INT_MIN <= x && x <= INT_MAX && !std::isnan(x) && !std::isinf(x))
+ std::cout << "int: " << static_cast<int>(x) << std::endl;
+ else
+ std::cout << "int: impossible" << std::endl;
+ std::cout << "float: " << static_cast<float>(x) << 'f' << std::endl;
+ std::cout << "double: " << x << std::endl;
return 0;
}
diff --git a/cpp06/ex00/test.sh b/cpp06/ex00/test.sh
new file mode 100755
index 0000000..2ed386d
--- /dev/null
+++ b/cpp06/ex00/test.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+convert_test () {
+ echo "$ ./convert $1"
+ ./convert "$1"
+ echo '-------------------------------------------------'
+}
+
+convert_test ''
+convert_test ' '
+convert_test ' 1'
+convert_test '1 '
+convert_test ' 1 '
+convert_test '- 1'
+
+echo '=================================== CHAR ==================='
+convert_test "'a'"
+convert_test "'A'"
+convert_test "' '"
+convert_test "'~'"
+convert_test "'.'"
+convert_test "'"
+convert_test "''"
+
+echo '=================================== INT ==================='
+convert_test 0
+convert_test 5
+convert_test 70
+convert_test -1
+convert_test -0
+convert_test -70
+convert_test +70
+convert_test 2147483647
+convert_test -2147483648
+convert_test 2147483648
+convert_test -2147483649
+
+echo '=================================== FLOAT =================='
+convert_test '0.0f'
+convert_test '1.0f'
+convert_test '0.1f'
+convert_test '3333.1f'
+convert_test '1.3333f'
+convert_test '.f'
+convert_test '.1f'
+convert_test '1.f'
+convert_test '1..1f'
+convert_test 'f'
+convert_test '0f'
+convert_test '1f'
+convert_test '33f'
+convert_test '-33f'
+convert_test '-33.45f'
+convert_test '+33f'
+convert_test '+33.45f'
+convert_test 'inff'
+convert_test '+inff'
+convert_test '-inff'
+convert_test 'nanf'
+convert_test '-nanf'
+
+echo '=================================== DOUBLE =================='
+convert_test '0.0'
+convert_test '1.0'
+convert_test '0.1'
+convert_test '3333.1'
+convert_test '1.3333'
+convert_test '.'
+convert_test '.1'
+convert_test '1.'
+convert_test '1..1'
+convert_test '0'
+convert_test '1'
+convert_test '33'
+convert_test '-33'
+convert_test '-33.45'
+convert_test '+33'
+convert_test '+33.45'
+convert_test 'inf'
+convert_test '+inf'
+convert_test '-inf'
+convert_test 'nan'
+convert_test '-nan'