fork download
  1. #include <cstdint>
  2. #include <cmath>
  3. #include <string>
  4. #include <cstdio>
  5. #include <cassert>
  6.  
  7. struct MyCurrency
  8. {
  9. uint64_t m_significand;
  10. int32_t m_exp10;
  11. bool m_sign;
  12.  
  13. MyCurrency()
  14. : m_significand(0)
  15. , m_exp10(0)
  16. , m_sign(false)
  17. {
  18. }
  19.  
  20. void normalize()
  21. {
  22. if (m_significand == 0)
  23. {
  24. m_exp10 = 0;
  25. m_sign = false;
  26. return;
  27. }
  28. while (m_significand % 10 == 0)
  29. {
  30. m_significand /= 10;
  31. ++m_exp10;
  32. }
  33. }
  34.  
  35. MyCurrency(int64_t significand, int32_t exp10 = 0)
  36. : m_significand(std::abs(significand))
  37. , m_exp10(exp10)
  38. , m_sign(significand < 0)
  39. {
  40. normalize();
  41. }
  42.  
  43. MyCurrency(const char *str)
  44. : m_significand(0)
  45. , m_exp10(0)
  46. , m_sign(false)
  47. {
  48. while (*str == ' ')
  49. ++str;
  50.  
  51. if (*str == '-')
  52. {
  53. m_sign = true;
  54. ++str;
  55. }
  56. else if (*str == '+')
  57. {
  58. ++str;
  59. }
  60.  
  61. while (*str == '0')
  62. ++str;
  63.  
  64. for (bool found_dot = false; *str; ++str)
  65. {
  66. if (*str == '.')
  67. {
  68. found_dot = true;
  69. continue;
  70. }
  71.  
  72. if (found_dot)
  73. {
  74. --m_exp10;
  75. }
  76.  
  77. if ('0' <= *str && *str <= '9')
  78. {
  79. m_significand *= 10;
  80. m_significand += *str - '0';
  81. }
  82. }
  83.  
  84. normalize();
  85. }
  86.  
  87. MyCurrency(const std::string& str)
  88. : MyCurrency(str.c_str())
  89. {
  90. }
  91.  
  92. MyCurrency operator+() const
  93. {
  94. return *this;
  95. }
  96. MyCurrency operator-() const
  97. {
  98. MyCurrency ret = *this;
  99. ret.m_sign = !ret.m_sign;
  100. return ret;
  101. }
  102.  
  103. bool is_zero() const
  104. {
  105. return m_significand == 0;
  106. }
  107. bool is_positive() const
  108. {
  109. return !m_sign;
  110. }
  111. bool is_negative() const
  112. {
  113. return m_sign;
  114. }
  115.  
  116. std::string to_string() const
  117. {
  118. if (is_zero())
  119. return "0";
  120.  
  121. if (m_sign)
  122. {
  123. std::string ret("-");
  124. ret += (-*this).to_string();
  125. return ret;
  126. }
  127.  
  128. std::string ret = std::to_string(m_significand);
  129. if (m_exp10 == 0)
  130. return ret; // just a integer
  131.  
  132. if (m_exp10 > 0)
  133. {
  134. std::string zeros(m_exp10, '0');
  135. ret += zeros;
  136. return ret; // 123456000...0
  137. }
  138. else if (-m_exp10 < ret.size())
  139. {
  140. // 1234.567...
  141. ret.insert(ret.size() + m_exp10, ".");
  142. return ret;
  143. }
  144. else
  145. {
  146. // 0.0123456...
  147. std::string str = "0.";
  148. std::string zeros(-m_exp10 - ret.size(), '0');
  149. str += zeros + ret;
  150. return str;
  151. }
  152. }
  153.  
  154. void print() const
  155. {
  156. std::printf("%s\n", to_string().c_str());
  157. }
  158.  
  159. void round(int32_t e10 = 0)
  160. {
  161. // TODO:
  162. }
  163. void round_up(int32_t e10 = 0)
  164. {
  165. // TODO:
  166. }
  167. void round_down(int32_t e10 = 0)
  168. {
  169. // TODO:
  170. }
  171. void bankers_rounding(int32_t e10 = 0)
  172. {
  173. // TODO:
  174. }
  175.  
  176. MyCurrency get_round(int32_t e10 = 0)
  177. {
  178. MyCurrency ret(*this);
  179. ret.round(e10);
  180. return ret;
  181. }
  182. MyCurrency get_round_up(int32_t e10 = 0)
  183. {
  184. MyCurrency ret(*this);
  185. ret.round_up(e10);
  186. return ret;
  187. }
  188. MyCurrency get_round_down(int32_t e10 = 0)
  189. {
  190. MyCurrency ret(*this);
  191. ret.round_down(e10);
  192. return ret;
  193. }
  194. MyCurrency get_bankers_rounding(int32_t e10)
  195. {
  196. MyCurrency ret(*this);
  197. ret.get_bankers_rounding(e10);
  198. return ret;
  199. }
  200. };
  201.  
  202. int main(void)
  203. {
  204. MyCurrency(0, 0).print();
  205. MyCurrency(1, 2).print();
  206. MyCurrency(10, -2).print();
  207. MyCurrency(10, -3).print();
  208. MyCurrency(12345, 0).print();
  209. MyCurrency(12345, 10).print();
  210. MyCurrency(12345, 2).print();
  211. MyCurrency(12345, -2).print();
  212. MyCurrency(-12345, -10).print();
  213. MyCurrency("0").print();
  214. MyCurrency("100").print();
  215. MyCurrency("0.1").print();
  216. MyCurrency("0.01").print();
  217. MyCurrency("12345").print();
  218. MyCurrency("123450000000000").print();
  219. MyCurrency("1234500").print();
  220. MyCurrency("123.45").print();
  221. MyCurrency("-0.0000012345").print();
  222. }
  223.  
Success #stdin #stdout 0s 4452KB
stdin
Standard input is empty
stdout
0
100
0.1
0.01
12345
123450000000000
1234500
123.45
-0.0000012345
0
100
0.1
0.01
12345
123450000000000
1234500
123.45
-0.0000012345