-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert_Fraction_to_Decimal.cpp
More file actions
90 lines (79 loc) · 1.25 KB
/
Copy pathConvert_Fraction_to_Decimal.cpp
File metadata and controls
90 lines (79 loc) · 1.25 KB
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
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "string"
#include "map"
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <sstream>
#include <iomanip>
#include <random>
#include <assert.h>
#include <iterator>
#include <queue>
using namespace std;
string toPoint(int divid, int divisor)
{
if(divisor==0)return "";
if(divid==0)return "0";
int intPart=divid/divisor;
divid=divid%divisor;
map<int, int> rest;
vector<int> result;
int start=-1, index=0;
while(divid!=0)
{
map<int, int>::iterator it=rest.find(divid);
if(it!=rest.end())
{
start=it->second;
break;
}
else
{
rest[divid]=index;
}
divid*=10;
if(divid>=divisor)
{
result.push_back(divid/divisor);
divid%=divisor;
}
else
{
result.push_back(0);
}
index++;
}
string s="";
for(int i=0;i<result.size();i++)
{
if(i==start)
{
s+="(";
}
s.push_back(result[i]+'0');
}
if(start!=-1)
{
s.push_back(')');
}
if(intPart)
{
stringstream ss;
ss<<intPart;
s=ss.str()+"."+s;
}
else
{
s="0."+s;
}
return s;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<<toPoint(112,11)<<endl;
return 0;
}