-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrational_decimal.h
112 lines (90 loc) · 2.19 KB
/
rational_decimal.h
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
#pragma once
#include <stdint.h>
#include <algorithm>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
class rational_decimal_calculator_t
{
static int32_t gcd(int64_t a, int64_t b)
{
int32_t c = 0;
do
{
if (a < b)
std::swap(a, b);
c = a % b;
a = b;
b = c;
} while (c);
return a;
}
public:
static std::string calc(int64_t numerator, int64_t denominator)
{
if (numerator == 0)
return "0";
//sign handling
bool neg_num = numerator < 0;
bool neg_den = denominator < 0;
bool neg_ret = neg_den != neg_num;
if (neg_num)
numerator = -numerator;
if (neg_den)
denominator = -denominator;
//gcd
int32_t divisor = gcd(numerator, denominator);
numerator /= divisor;
denominator /= divisor;
//first step
int64_t quocient = numerator / denominator;
numerator %= denominator;
//loop
unordered_map<int32_t, int32_t> remain2pos;
vector<int8_t> decimal_vec;
decimal_vec.reserve(1024);
int32_t pos = 0;
int64_t r = numerator;
while (r)
{
if (remain2pos.find(r) != remain2pos.end())
break;
remain2pos[r] = pos++;
r *= 10;
decimal_vec.push_back(r / denominator);
r %= denominator;
}
char buf[64];
if (neg_ret)
sprintf_s(buf, "-%lld\0", quocient);
else
sprintf_s(buf, "%lld\0", quocient);
if (!decimal_vec.size())
return buf;
int32_t buf_str_len = strlen(buf);
vector<char> str_builder;
str_builder.reserve(sizeof(buf) + decimal_vec.size() + 16);
str_builder.resize(buf_str_len);
std::copy(buf, buf + buf_str_len, str_builder.begin());
str_builder.push_back('.');
int32_t loop_start = r ? remain2pos[r] : 0;
for (int32_t i = 0; i < loop_start; ++i)
str_builder.push_back('0' + decimal_vec[i]);
if (r)
str_builder.push_back('(');
for (int32_t i = loop_start; i < decimal_vec.size(); ++i)
str_builder.push_back('0' + decimal_vec[i]);
if (r)
str_builder.push_back(')');
str_builder.push_back('\0');
return &str_builder[0];
}
};
#if 0
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
}
};
#endif