-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraction2Decimal.h
76 lines (73 loc) · 1.78 KB
/
fraction2Decimal.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
//
// Created by so_go on 2019/6/19.
//
#ifndef SRC_FRACTION2DECIMAL_H
#define SRC_FRACTION2DECIMAL_H
#include<string>
#include<vector>
#include<set>
#include<map>
#include"printVector.h"
using namespace std;
class Fraction2Decimal {
public:
/*
* long long everywhere
*
*/
string fractionToDecimal(int numerator, int denominator) {
long long p = numerator, q = denominator, r, i;
bool symbol = true;
if(numerator < 0){
p = -p;
symbol = not symbol;
}
if(denominator < 0){
q = -q;
symbol = not symbol;
}
bool isRepeat = false;
int repeatPos;
vector<long long> vec;
int pos = 1;
map<int, int> rpos;
do{
i = p / q;
vec.push_back(i);
r = p % q;
if(rpos.find(r) == rpos.end()){
rpos.insert({r, pos});
}
else{
isRepeat = true;
repeatPos = rpos[r];
break;
}
cout << i << ' ' << r << endl;
p = r * 10;
pos++;
}
while(r != 0);
printVector(vec);
string res;
cout << symbol << ' ' << p << endl;
if(not symbol and numerator != 0){
res.push_back('-');
}
res.append(to_string(vec[0]));
if(vec.size() > 1){
res.push_back('.');
for(int i = 1; i < vec.size(); i++){
if(isRepeat and repeatPos == i){
res.push_back('(');
}
res.append(to_string(vec[i]));
}
if(isRepeat){
res.push_back(')');
}
}
return res;
}
};
#endif //SRC_FRACTION2DECIMAL_H