-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumFns.cpp
195 lines (165 loc) · 2.83 KB
/
NumFns.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "NumFns.h"
int intLength(int i)
{
int length = 1;
if(i < 0)
i = -i;
for(;i >= 10; i /= 10)
++length;
return length;
}
int llintLength(long long int i)
{
int length = 1;
if(i < 0)
i = -i;
for(;i >= 10; i /= 10)
++length;
return length;
}
bool isInt(const std::string& str)
{
if(!str.size())
return 0;
if(str.size() && str[0] != '-' && !std::isdigit(str[0]))
return 0;
for(size_t i=1; i<str.size(); i++)
if(!std::isdigit(str[i]))
return 0;
return 1;
}
bool isPosInt(const std::string& str)
{
if(str.size() == 0 || str == "0")
return 0;
for(size_t i=0; i<str.size(); i++)
if(!std::isdigit(str[i]))
return 0;
return 1;
}
bool isNonNegInt(const std::string& str)
{
if(!str.size())
return 0;
for(size_t i=0; i<str.size(); i++)
if(!std::isdigit(str[i]))
return 0;
return 1;
}
bool isDouble(const std::string& str)
{
if(!str.size())
return 0;
bool founde = 0, founddp = 0;
for(size_t i=0; i<str.size(); ++i)
{
if(str[i] == 'e')
{
if(founde)
return 0;
founde = 1;
if(i+1 < str.size() && str[i+1] == '-')
++i;
}
else if(str[i] == '-')
{
if(i > 0 && str[i-1] != 'e')
return 0;
}
else if(str[i] == '+')
{
if(i > 0 && str[i-1] != 'e')
return 0;
}
else if(str[i] == '.')
{
if(founde || founddp)
return 0;
founddp = 1;
}
else if(!std::isdigit(str[i]))
return 0;
}
return 1;
}
/*returns a number specifying type
0 == int
1 == double
2 == string
*/
int getTypeInt(const std::string& str)
{
if(!str.size())
return 2;
int typeInt = 0;
bool founde = 0, founddp = 0;
for(size_t i=0; i<str.size(); ++i)
{
if(str[i] == 'e')
{
if(founde)
return 2;
else if(!typeInt)
typeInt = 1;
founde = 1;
if(i+1 < str.size() && str[i+1] == '-')
++i;
}
else if(str[i] == '-')
{
if(i > 0 && str[i-1] != 'e')
return 2;
}
else if(str[i] == '+')
{
if(i > 0 && str[i-1] != 'e')
return 2;
}
else if(str[i] == '.')
{
if(founde || founddp)
return 2;
else if(!typeInt)
typeInt = 1;
founddp = 1;
}
else if(!std::isdigit(str[i]))
return 2;
}
return typeInt;
}
std::string int_to_timestr(int s)
{
if(s < 0)
return "-" + int_to_timestr(-s);
else if(s < 60)
return std::to_string(s) + "s";
else if(s < 3600)
{
int m = s/60;
s -= 60*m;
return std::to_string(m) + "m" + int_to_timestr(s);
}
else if(s < 86400)
{
int h = s/3600;
s -= 3600*h;
int m = s/60;
return std::to_string(h) + "h" + std::to_string(m) + "m";
}
else //if(s < 604800)
{
int d = s/86400;
s -= 86400*d;
int h = s/3600;
s -= 3600*h;
int m = s/60;
return std::to_string(d) + "d" + std::to_string(h) + "h" + std::to_string(m) + "m";
}
}
std::string double_to_string_prec(const double& d, const int& prec)
{
std::ostringstream oss;
oss << std::setprecision(prec) << d;
return oss.str();
}