blob: a0ef7f8e93121bd885ad493d8efe0d71ceed4939 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/16 10:54:10 by charles #+# #+# */
/* 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 << "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;
}
|