From 96dcf214a8c40529b251ea31ef037868583dd1da Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Thu, 12 Nov 2020 10:54:47 +0100 Subject: Fixing cpp03, moved more common logic in ClapTrap, Added better main --- cpp06/ex00/main.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 9 deletions(-) (limited to 'cpp06/ex00/main.cpp') diff --git a/cpp06/ex00/main.cpp b/cpp06/ex00/main.cpp index 35d64b9..ec30bb8 100644 --- a/cpp06/ex00/main.cpp +++ b/cpp06/ex00/main.cpp @@ -6,27 +6,102 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/16 10:54:10 by charles #+# #+# */ -/* Updated: 2020/04/16 12:23:06 by charles ### ########.fr */ +/* Updated: 2020/11/11 09:21:08 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include #include +#include int main(int argc, char **argv) { - // if (argc != 2) - // { - // std::cout << "Usage: " << argv[0] << " litteral" << std::endl; - // return 1; - // } + if (argc != 2) + { + std::cerr << "Usage: " << argv[0] << " litteral" << std::endl; + return 1; + } - int i; - std::istringstream("bonjour") >> i; + std::string s(argv[1]); + if (s.empty()) + { + std::cerr << "Cannot be empty" << std::endl; + return 1; + } + + bool negative = false; + if (s[0] == '-' || s[0] == '+') + { + if (s[0] == '-') + negative = true; + s.erase(0, 1); + } + + char c; + int i; + float f; + double d; + + if (s == "nan" || s == "inf") + { + d = s == "nan" ? NaN : inf; + if (negative) + d = -d; + f = static_cast(d); + i = static_cast(d); + c = static_cast(d); + } + if (s == "nanf" || s == "inff") + { + f = s == "nanf" ? NaN : inff; + if (negative) + f = -f; + d = static_cast(f); + i = static_cast(f); + c = static_cast(f); + } + + if (isdigit(s[0])) + { + std::stringstream ss(s); + + if (s.find(".") == -1) + { + ss >> i; + d = static_cast(i); + f = static_cast(i); + c = static_cast(i); + } + else if (s.find("f") != -1) + { + ss >> f; + d = static_cast(f); + i = static_cast(f); + c = static_cast(f); + } + else + { + ss >> d; + f = static_cast(d); + i = static_cast(d); + c = static_cast(d); + } + } + else if (!negative && s.length == 1) + { + c = s[0]; + d = static_cast(c); + f = static_cast(c); + i = static_cast(c); + } + else + { + std::cout << "Parsing error" << std::endl; + return 1; + } - std::cout << i << std::endl; return 0; } -- cgit