-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringConvertor.cpp
154 lines (110 loc) · 3.57 KB
/
StringConvertor.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
// std::string to QString
#include <iostream>
#include <QString>
#include <cmath>
#define MAXPOWERALLOWED 5
inline std::string removeLineChange(const std::string &str)
{
if (!str.size())
return str;
std::string temp = "";
for (int i = 0; i <= str.size()-1; i++)
if (str[i] != '\n')
temp += str[i];
return temp;
}
inline QString QRemoveLineChange(QString _str)
{
if (!_str.size())
return _str;
QString temp = "";
for (int i = 0; i <= _str.size()-1; i++)
if (_str[i] != '\n')
temp += _str[i];
return temp;
}
inline QString stringConvert(std::string str)
{
str = removeLineChange(str);
if (!str.size())
return "USER_NOT_INPUT";
QString _str = "";
for (long long int i=0; i<=str.size()-1; i++)
_str += str[i];
return _str;
}
inline std::string QStringConvert(QString _str)
{
_str = QRemoveLineChange(_str);
if (!_str.size())
return "USER_NOT_INPUT";
return _str.toStdString();
}
inline QString unsignedIntConvert(unsigned long long int i)
{
QString _str = "";
if (i == 0) {
_str = "0";
return _str;
}
int digits = 0;
for (long int j = i; j; j /= 10) {
digits++;
}
for (long int j = digits; j >= 1; j--) {
_str += (char)((unsigned int)(i % (long int)pow(10,j)) / (long int)pow(10, j - 1) + 0x30);
}
return _str;
}
inline double stringFractionToDouble(std::string &str)
{
double d = 0;
unsigned long int divisionPosition = -1;
for (int i = 0; i <= str.size() - 1; i++)
if (str[i] == '/')
{
divisionPosition = i;
break;
}
long long int strNumerator = 0;
long long int strDenominator = 0;
for (long int i = divisionPosition - 1; i >= (str[0] == '-') ? 1 : 0; i--)
strNumerator += (int)(str[i] - 0x30) * (long long int)pow(10,
(divisionPosition - ((str[0] == '-') ? 1 : 0)) - // digits of numerator
(i - ((str[0] == '-') ? 1 : 0) + 1) );
for (int i = str.size() - 1; i >= divisionPosition + 1; i--)
strDenominator += (int)(str[i] - 0x30) * (long long int)pow(10,
(str.size() - 1 - divisionPosition) - // digits of denominator
(i - divisionPosition) );
d = (str[0] == '-') ? (- (double)strNumerator / (double)strDenominator) : ((double)strNumerator / (double)strDenominator);
return d;
}
inline QString doubleConvert(double d) // The double parameter here must be a 'real' decimal
{
QString _str = "";
if (d < 0) _str += '-';
unsigned long long int intPart = (d < 0) ? (long long int)(- d) : (long long int)d;
_str += unsignedIntConvert(intPart);
double doublePart = ((d < 0) ? (- d) : d) - (double)intPart; // a decimal that is in [0,1).
unsigned int powRequired = 0;
unsigned int moreZeroRequired = 0;
for (powRequired = 1; powRequired <= MAXPOWERALLOWED; powRequired++)
{
if (doublePart * pow(10, powRequired) < 1) {
moreZeroRequired++;
}
if ((doublePart * pow(10, powRequired) - (double)((long long int)(doublePart * pow(10, powRequired)))) < 1e-10) {
// 1e-10 to prevent the 'remainder' from type conversion.
break;
}
}
_str += '.';
if (moreZeroRequired) {
for (int i = 1; i <= moreZeroRequired; i++) {
_str += '0';
}
}
unsigned int intFormOfDoublePart = (unsigned long long int)(doublePart * pow(10, powRequired));
_str += unsignedIntConvert(intFormOfDoublePart);
return _str;
}