|
25 | 25 | #include <cctype> |
26 | 26 | #include <limits> |
27 | 27 |
|
28 | | -MathLib::biguint MathLib::toULongNumber(const std::string & str) { |
29 | | - // hexadecimal numbers: |
30 | | - if (isHex(str)) { |
31 | | - if (str[0] == '-') { |
32 | | - biguint ret = 0; |
33 | | - std::istringstream istr(str); |
34 | | - istr >> std::hex >> ret; |
35 | | - return ret; |
36 | | - } else { |
37 | | - unsigned long long ret = 0; |
38 | | - std::istringstream istr(str); |
39 | | - istr >> std::hex >> ret; |
40 | | - return (biguint)ret; |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - // octal numbers: |
45 | | - if (isOct(str)) { |
| 28 | +MathLib::biguint MathLib::toULongNumber(const std::string & str) |
| 29 | +{ |
| 30 | + // hexadecimal numbers: |
| 31 | + if (isHex(str)) { |
| 32 | + if (str[0] == '-') { |
46 | 33 | biguint ret = 0; |
47 | 34 | std::istringstream istr(str); |
48 | | - istr >> std::oct >> ret; |
49 | | - return ret; |
50 | | - } |
51 | | - |
52 | | - // binary numbers: |
53 | | - if (isBin(str)) { |
54 | | - biguint ret = 0; |
55 | | - for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { |
56 | | - ret <<= 1; |
57 | | - if (str[i] == '1') |
58 | | - ret |= 1; |
59 | | - } |
60 | | - /* if (str[0] == '-') |
61 | | - ret = -ret; */ |
| 35 | + istr >> std::hex >> ret; |
62 | 36 | return ret; |
| 37 | + } else { |
| 38 | + unsigned long long ret = 0; |
| 39 | + std::istringstream istr(str); |
| 40 | + istr >> std::hex >> ret; |
| 41 | + return (biguint)ret; |
63 | 42 | } |
| 43 | + } |
64 | 44 |
|
65 | | - if (isFloat(str)) { |
66 | | - return static_cast<biguint>(std::atof(str.c_str())); |
67 | | - } |
68 | | - |
| 45 | + // octal numbers: |
| 46 | + if (isOct(str)) { |
69 | 47 | biguint ret = 0; |
70 | 48 | std::istringstream istr(str); |
71 | | - istr >> ret; |
| 49 | + istr >> std::oct >> ret; |
72 | 50 | return ret; |
73 | | -} |
74 | | - |
75 | | -MathLib::bigint MathLib::toLongNumber(const std::string & str) { |
76 | | - // hexadecimal numbers: |
77 | | - if (isHex(str)) { |
78 | | - if (str[0] == '-') { |
79 | | - bigint ret = 0; |
80 | | - std::istringstream istr(str); |
81 | | - istr >> std::hex >> ret; |
82 | | - return ret; |
83 | | - } else { |
84 | | - unsigned long long ret = 0; |
85 | | - std::istringstream istr(str); |
86 | | - istr >> std::hex >> ret; |
87 | | - return (bigint)ret; |
88 | | - } |
89 | | - } |
| 51 | + } |
90 | 52 |
|
91 | | - // octal numbers: |
92 | | - if (isOct(str)) { |
93 | | - bigint ret = 0; |
94 | | - std::istringstream istr(str); |
95 | | - istr >> std::oct >> ret; |
96 | | - return ret; |
| 53 | + // binary numbers: |
| 54 | + if (isBin(str)) { |
| 55 | + biguint ret = 0; |
| 56 | + for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { |
| 57 | + ret <<= 1; |
| 58 | + if (str[i] == '1') |
| 59 | + ret |= 1; |
97 | 60 | } |
| 61 | + /* if (str[0] == '-') |
| 62 | + ret = -ret; */ |
| 63 | + return ret; |
| 64 | + } |
| 65 | + |
| 66 | + if (isFloat(str)) { |
| 67 | + return static_cast<biguint>(std::atof(str.c_str())); |
| 68 | + } |
| 69 | + |
| 70 | + biguint ret = 0; |
| 71 | + std::istringstream istr(str); |
| 72 | + istr >> ret; |
| 73 | + return ret; |
| 74 | +} |
98 | 75 |
|
99 | | - // binary numbers: |
100 | | - if (isBin(str)) { |
| 76 | +MathLib::bigint MathLib::toLongNumber(const std::string & str) |
| 77 | +{ |
| 78 | + // hexadecimal numbers: |
| 79 | + if (isHex(str)) { |
| 80 | + if (str[0] == '-') { |
101 | 81 | bigint ret = 0; |
102 | | - for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { |
103 | | - ret <<= 1; |
104 | | - if (str[i] == '1') |
105 | | - ret |= 1; |
106 | | - } |
107 | | - if (str[0] == '-') |
108 | | - ret = -ret; |
| 82 | + std::istringstream istr(str); |
| 83 | + istr >> std::hex >> ret; |
109 | 84 | return ret; |
| 85 | + } else { |
| 86 | + unsigned long long ret = 0; |
| 87 | + std::istringstream istr(str); |
| 88 | + istr >> std::hex >> ret; |
| 89 | + return (bigint)ret; |
110 | 90 | } |
| 91 | + } |
111 | 92 |
|
112 | | - if (isFloat(str)) { |
113 | | - return static_cast<bigint>(std::atof(str.c_str())); |
114 | | - } |
115 | | - |
| 93 | + // octal numbers: |
| 94 | + if (isOct(str)) { |
116 | 95 | bigint ret = 0; |
117 | 96 | std::istringstream istr(str); |
118 | | - istr >> ret; |
| 97 | + istr >> std::oct >> ret; |
119 | 98 | return ret; |
| 99 | + } |
| 100 | + |
| 101 | + // binary numbers: |
| 102 | + if (isBin(str)) { |
| 103 | + bigint ret = 0; |
| 104 | + for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) { |
| 105 | + ret <<= 1; |
| 106 | + if (str[i] == '1') |
| 107 | + ret |= 1; |
| 108 | + } |
| 109 | + if (str[0] == '-') |
| 110 | + ret = -ret; |
| 111 | + return ret; |
| 112 | + } |
| 113 | + |
| 114 | + if (isFloat(str)) { |
| 115 | + return static_cast<bigint>(std::atof(str.c_str())); |
| 116 | + } |
| 117 | + |
| 118 | + bigint ret = 0; |
| 119 | + std::istringstream istr(str); |
| 120 | + istr >> ret; |
| 121 | + return ret; |
120 | 122 | } |
121 | 123 |
|
122 | 124 |
|
|
0 commit comments