Skip to content

Commit b45e90e

Browse files
authored
Add files via upload
1 parent ecb3b97 commit b45e90e

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

AdvDiv.dart

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
String times10(String nstring) {
2+
if(nstring.contains(".") && nstring.indexOf(".")==nstring.length - 2)
3+
return nstring.replaceFirst(".", "");
4+
if(nstring.contains("."))
5+
return nstring.split(".")[0]+nstring.split(".")[1][0]+"."+nstring.split(".")[1].substring(1);
6+
return nstring+"0";
7+
}
8+
String? advdiv(double n1d, double n2d, int ri, [final String rstr1 = "[", final String rstr2 = "]"]) {
9+
bool over = false;
10+
int carry = 0, newcarry = 0, x, d;
11+
int? rcount = null;
12+
String n1string, n2string, r, res = "";
13+
List<int> carries;
14+
List<String> n1s;
15+
late final int n1, n2, dotx;
16+
late final String sign;
17+
late final List<String> n1s1;
18+
19+
if(n2d==0)
20+
return null;
21+
sign = (n1d < 0 ? n2d >= 0 : n2d < 0) ? "-" : "";
22+
n1d = n1d.abs();
23+
n2d = n2d.abs();
24+
ri = ri.abs();
25+
r = ri.toString();
26+
n1string = n1d.toString();
27+
n2string = n2d.toString();
28+
29+
while(n1string.contains(".") || n2string.contains(".")) {
30+
if(!n1string.contains(".")) {
31+
n1string+= r[0];
32+
if(r.length > 1)
33+
r = r.substring(1)+r[0];
34+
}
35+
else {
36+
n1string = times10(n1string);
37+
if(n1string.contains(".")) {
38+
n1string+= r[0];
39+
if(r.length > 1)
40+
r = r.substring(1)+r[0];
41+
};
42+
};
43+
n2string = times10(n2string);
44+
};
45+
46+
n1 = int.parse(n1string);
47+
n2 = int.parse(n2string);
48+
n1s = n1string.split("");
49+
n1s1 = n1string.split(".")[0].split("");
50+
51+
for(x = 0; x < n1s1.length; x++) {
52+
d = (int.parse(times10(carry.toString())) + int.parse(n1s1[x])) ~/ n2;
53+
res+= d.toString();
54+
carry = int.parse(times10(carry.toString())) + int.parse(n1s1[x]) - n2 * d;
55+
};
56+
57+
if(res.length==0) {
58+
res = "0";
59+
x++;
60+
};
61+
62+
dotx = x;
63+
res+= ".";
64+
carries = [carry];
65+
66+
while(n1s.length <= x)
67+
n1s.add("");
68+
69+
while(true) {
70+
x++;
71+
if(x >= n1s.length) {
72+
if(rcount==null)
73+
rcount = 0;
74+
else
75+
rcount++;
76+
over = true;
77+
n1s.add(r[rcount % r.length]);
78+
};
79+
80+
newcarry = (int.parse(times10(carry.toString())) + int.parse(n1s[x])) - n2 * ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2);
81+
82+
if((newcarry==0 && r=="0") || List.generate(carries.length, (int y) => y).any((y) => carries[y]==newcarry && (y % r.length)==((x - dotx) % r.length)))
83+
res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2).toString();
84+
if(newcarry==0 && r=="0")
85+
return sign+res.replaceAll(RegExp(r'^0+|0$'), "").replaceFirst(RegExp(r'^\.'), "0.").replaceFirst(RegExp(r'\.$'), "");
86+
if(List.generate(carries.length, (int y) => y).any((y) => carries[y]==newcarry && (y % r.length)==((x - dotx) % r.length)))
87+
return sign+(res.substring(0, dotx + List.generate(carries.length, (int y) => y).firstWhere((y) => carries[y]==newcarry && (y % r.length)==((x - dotx) % r.length)) + 1)+rstr1+res.substring(dotx + List.generate(carries.length, (int y) => y).firstWhere((y) => carries[y]==newcarry && (y % r.length)==((x - dotx) % r.length)) + 1)+rstr2).replaceFirst(RegExp(r'^0+'), "").replaceFirst(RegExp(r'^\.'), "0.");
88+
89+
res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2).toString();
90+
carry = newcarry;
91+
if(over)
92+
carries.add(carry);
93+
};
94+
}
95+
96+
void main(List<String> args) {
97+
int r;
98+
double n1, n2;
99+
100+
if(args.length < 2 || !RegExp(r'^\d*\.?\d+$').hasMatch(args[0]) || !RegExp(r'^\d*\.?\d+$').hasMatch(args[1]) || (args.length > 2 && !RegExp(r'^\d+$').hasMatch(args[2]))) {
101+
print("Usage: advdiv n1 n2[ r]");
102+
return;
103+
};
104+
105+
n1 = double.parse(args[0]);
106+
n2 = double.parse(args[1]);
107+
r = args.length < 3 ? 0 : int.parse(args[2]);
108+
109+
print(advdiv(n1, n2, r) ?? "Error");
110+
}

0 commit comments

Comments
 (0)