1
#include "BigInteger.h"2
#include <iostream>3
using namespace std;4

5
class BigDecimal6


{7
private:8
BigInteger divide;9
BigInteger divided;10
int digit_count;11

12
BigInteger Divide()13

{14
if(divide == BigInteger(0))15
return BigInteger(0);16
BigInteger result;17

18
BigInteger temp(0), index(0), rest(0), zero(0);19
BigInteger TEN(10);20
int end = -1;21
int start = divide.Length() - 1;22
vector<int> quotients;23

24
while(rest != zero || start > end)25

{26
temp = rest * TEN;27
digit_count = start > end ? digit_count : digit_count + 1;28
temp = start > end ? temp + BigInteger((int)(divide[start])) : temp;29
if(start <= end && temp < divided)30

{31
quotients.push_back(0);32
}33
while(temp < divided)34

{35
--start;36
temp = start > end ? temp * TEN + BigInteger((int)(divide[start])) : temp * TEN;37
digit_count = start > end ? digit_count : digit_count + 1;38
if(start <= end && temp < divided)39

{40
quotients.push_back(0);41
}42
}43
int quotient = 0;44
rest = temp;45
while(rest >= divided)46

{47
rest = rest - divided;48
++quotient;49
}50
quotients.push_back(quotient);51
--start;52
}53

54
string r;55
vector<int>::iterator i_end = quotients.end();56
for(vector<int>::iterator ite = quotients.begin(); ite != i_end; ++ite)57

{58
r += (char)((*ite) + '0');59
}60
return BigInteger(r);61
}62

63
public:64
BigDecimal()65

{66
digit_count = 0;67
divide = 0;68
divided = 1;69
}70

71
BigDecimal(BigInteger a, BigInteger b)72

{73
digit_count = 0;74
divide = a;75
divided = b;76
}77

78
friend ostream & operator<<(ostream & os, BigDecimal & decimal)79

{80
BigInteger result = decimal.Divide();81
int length = result.Length();82
int copy_digit_count = decimal.digit_count;83
if(copy_digit_count >= length)84
os << "0.";85
os << result;86
return os;87
}88
};89

90
int _tmain(int argc, _TCHAR* argv[])91


{92
BigInteger a(1), b;93
cout << "Problem 4 by team x" << endl;94
while(cin >> b)95

{96
cout << endl;97
cout << "1 / " << b << " =" << endl;98
BigDecimal bd = BigDecimal(a, b);99
cout << bd << endl;100
}101
cout << "End of problem 4 by team x" << endl;102
return 0;103
}104

105



