-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformations.cpp
68 lines (53 loc) · 1.24 KB
/
transformations.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
string IntToString(int a) {
if(a == 0)
return "0";
string res = "";
for (int d = a; d > 0; d /= 10)
res += d % 10 + '0';
reverse(res.begin(), res.end());
return res;
}
int StringToInt(const string& a) {
int res = 0;
for (char i : a)
res = res * 10 + (i - '0');
return res;
}
double StringToDouble(const string& a) {
double res = 0;
double afterDot = 1;
bool dotBegins = 0;
for(char i : a){
if(dotBegins)
afterDot *= 10;
if(i == '.')
dotBegins = 1;
else
res = (res * 10) + (i - '0');
}
return res / afterDot;
}
string IntToBinary(int a) {
const unsigned int kRequiredLength = 32;
string res = "";
if (a == 0)
res = "0";
for (int d = a; d > 0; d >>= 1){
if ((d & 1) == 1)
res += "1";
else
res += "0";
}
while (res.length() < requiredLength)
res += "0";
reverse(res.begin(), res.end());
return res;
}
int BinaryToInt(const string& a){
int res = 0;
int curPow = 0;
reverse(a.begin(), a.end());
for(char i : a)
res += (i == '1') * (1 << curPow++);
return res;
}