-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivision.cpp
More file actions
116 lines (89 loc) · 1.79 KB
/
Copy pathdivision.cpp
File metadata and controls
116 lines (89 loc) · 1.79 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//division.cpp : Defines the entry point for the console
// application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
const int BASE = 100000;
struct longNumber {
int pre = 0;
int su = 0;
int zap = 0;
};
longNumber increase(longNumber a);
longNumber create_ln(std::string a, std::string b) {
int zap = 0;
while (b[zap] == '0' && (zap < 5)) {
zap++;
}
while (b.size() != 5) {
b += '0';
}
return longNumber{std::stoi(a), std::stoi(b), zap};
}
void print(longNumber a) {
std::cout << a.pre << ".";
while (a.zap--) {
std::cout << "0";
}
std::cout << a.su << std::endl;
}
bool ge(longNumber a, longNumber b) {
if (a.pre > b.pre) {
return true;
} else if (a.pre == b.pre) {
return a.su >= b.su;
}
return false;
}
longNumber sub(longNumber a, longNumber b);
longNumber division(longNumber a, longNumber b) {
longNumber res;
int ecz = 5;
while (ge(a, b)) {
a = sub(a, b);
res.pre++;
}
while (ecz) {
int times = 0;
a = increase(a);
while (ge(a, b)) {
a = sub(a, b);
times++;
}
res.su += times;
res.su *= 10;
ecz--;
}
res.su /= 10;
return res;
}
longNumber increase(longNumber a) {
a.pre = a.pre * 10;
a.su *= 10;
if (a.zap == 0) {
a.pre += a.su / BASE;
a.su %= BASE;
} else {
a.zap--;
}
return a;
}
longNumber sub(longNumber a, longNumber b) {
if (b.su > a.su) {
a.pre--;
a.su += BASE;
}
a.su = a.su - b.su;
a.pre = a.pre - b.pre;
return a;
}
int main() {
longNumber a = create_ln("0", "00103");
longNumber b = create_ln("0", "00007");
longNumber c = division(a, b);
print(a);
print(b);
print(c);
return 0;
}