Skip to content

Commit

Permalink
Change arguments
Browse files Browse the repository at this point in the history
n1 and n2 are now strings, r1 and r2 are deleted, minstr and decstr are added
  • Loading branch information
ratajs committed Jul 18, 2023
1 parent 924349c commit b25330d
Showing 1 changed file with 78 additions and 58 deletions.
136 changes: 78 additions & 58 deletions lib/AdvDiv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,101 @@ String times10(String nstring) {
return nstring+"0";
}

String? advdiv(double n1, double n2d, [int r1i = 0, int r2i = 0, final String rstr1 = "[", final String rstr2 = "]"]) {
bool over = false;
String? advdiv(String n1, String n2, [final String minstr = "-", final String decstr = ".", final String rstr1 = "[", final String rstr2 = "]"]) {
bool neg = false, over = false;
int carry = 0, newcarry = 0, rcount = -1, x, y, d;
String n1string, n2string, r1, r2, res = "", result;
String r1, r2, res = "", result;
List<int> carries = [];
List<String> n1s;
late final int n2, n1mc, n2mc, m1, m2;
late final int n2i, n1mc, n2mc, m1, m2;
late final String sign, n1m, n2m;
late final List<String> n1s1;
late final RegExp nre;

if(n2d==0 && r2i==0)
if(n1.length==0 || n2.length==0 || RegExp(r'^\d*$').hasMatch(minstr) || RegExp(r'^\d*$').hasMatch(decstr) || RegExp(r'^\d*$').hasMatch(rstr1) || RegExp(r'^\d+$').hasMatch(rstr2))
return null;
sign = (n1 < 0 ? n2d >= 0 : n2d < 0) ? "-" : "";
n1 = n1.abs();
n2d = n2d.abs();
r1i = r1i.abs();
r2i = r2i.abs();
r1 = r1i.toString();
n1string = n1.toString();
n2string = n2d.toString();
if(n1string.endsWith(".0"))
n1string = n1string.replaceFirst(".0", "");
if(n2string.endsWith(".0"))
n2string = n2string.replaceFirst(".0", "");

if(r2i!=0) {
r2 = r2i.toString();

n1m = n1string.replaceFirst(".", "");
n2m = n2string.replaceFirst(".", "");

nre = RegExp(r'^('+RegExp.escape(minstr)+r')?\d*('+RegExp.escape(decstr)+r'\d*('+RegExp.escape(rstr1)+r'\d*'+RegExp.escape(rstr2)+r')?)?$');

if(!nre.hasMatch(n1) || !nre.hasMatch(n2))
return null;

if(n1.startsWith(minstr)) {
n1 = n1.replaceFirst(minstr, "");
neg = true;
};
if(n2.startsWith(minstr)) {
n2 = n2.replaceFirst(minstr, "");
neg = !neg;
};

sign = neg ? minstr : "";

n1 = n1.replaceAll(decstr, ".");
n2 = n2.replaceAll(decstr, ".");

if(n1[0]==".")
n1 = "0"+n1;

if(n2[0]==".")
n2 = "0"+n2;

r1 = RegExp(RegExp.escape(rstr1)+r'(.+)'+RegExp.escape(rstr2)).firstMatch(n1)?[1] ?? "0";
n1 = n1.split(rstr1)[0].replaceFirst(RegExp(r'^0+'), "0").replaceFirst(RegExp(r'\.0*$'), "");
r2 = RegExp(RegExp.escape(rstr1)+r'(.+)'+RegExp.escape(rstr2)).firstMatch(n2)?[1] ?? "0";
n2 = n2.split(rstr1)[0].replaceFirst(RegExp(r'^0+'), "0").replaceFirst(RegExp(r'\.0*$'), "");


if(n2=="0" && RegExp(r'^0+$').hasMatch(r2))
return null;

if(n1=="0" && RegExp(r'^0+$').hasMatch(r1))
return "0";

if(r2!="0") {
n1m = n1.replaceFirst(".", "");
n2m = n2.replaceFirst(".", "");

n1mc = int.parse(n1m+r1) - int.parse(n1m);
n2mc = int.parse(n2m+r2) - int.parse(n2m);

if(n1string.contains("."))
m1 = int.parse("1"+List.filled(n1string.length - n1string.indexOf(".") - 1 + r1.length, "0").join("")) - int.parse("1"+List.filled(n1string.length - n1string.indexOf(".") - 1, "0").join(""));
if(n1.contains("."))
m1 = int.parse("1"+List.filled(n1.length - n1.indexOf(".") - 1 + r1.length, "0").join("")) - int.parse("1"+List.filled(n1.length - n1.indexOf(".") - 1, "0").join(""));
else
m1 = int.parse("1"+List.filled(r1.length, "0").join("")) - 1;

if(n2string.contains("."))
m2 = int.parse("1"+List.filled(n2string.length - n2string.indexOf(".") - 1 + r2.length, "0").join("")) - int.parse("1"+List.filled(n2string.length - n2string.indexOf(".") - 1, "0").join(""));
if(n2.contains("."))
m2 = int.parse("1"+List.filled(n2.length - n2.indexOf(".") - 1 + r2.length, "0").join("")) - int.parse("1"+List.filled(n2.length - n2.indexOf(".") - 1, "0").join(""));
else
m2 = int.parse("1"+List.filled(r2.length, "0").join("")) - 1;

return advdiv(n1mc * m2 * 1.0, m1 * n2mc * 1.0, 0, 0, rstr1, rstr2);
return sign+advdiv((n1mc * m2).toString(), (m1 * n2mc).toString(), "-", ".", rstr1, rstr2)!;
};

while(n2string.contains(".")) {
if(!n1string.contains(".")) {
n1string+= r1[0];
while(n2.contains(".")) {
if(!n1.contains(".")) {
n1+= r1[0];
if(r1.length > 1)
r1 = r1.substring(1)+r1[0];
}
else {
n1string = times10(n1string);
n1 = times10(n1);
};
n2string = times10(n2string);
if(n1string.endsWith(".0"))
n1string = n1string.replaceAll(".0", "");
if(n2string.endsWith(".0"))
n2string = n2string.replaceAll(".0", "");
n2 = times10(n2);
if(n1.endsWith(".0"))
n1 = n1.replaceFirst(".0", "");
if(n2.endsWith(".0"))
n2 = n2.replaceFirst(".0", "");
};

n1 = double.parse(n1string);
n2 = int.parse(n2string);
n1s = n1string.split("");
n1s1 = n1string.split(".")[0].split("");
n2i = int.parse(n2);
n1s = n1.split("");
n1s1 = n1.split(".")[0].split("");

for(x = 0; x < n1s1.length; x++) {
d = (int.parse(times10(carry.toString())) + int.parse(n1s1[x])) ~/ n2;
d = (int.parse(times10(carry.toString())) + int.parse(n1s1[x])) ~/ n2i;
res+= d.toString();
carry = int.parse(times10(carry.toString())) + int.parse(n1s1[x]) - n2 * d;
carry = int.parse(times10(carry.toString())) + int.parse(n1s1[x]) - n2i * d;
};

if(res.length==0) {
Expand All @@ -100,46 +123,43 @@ String? advdiv(double n1, double n2d, [int r1i = 0, int r2i = 0, final String rs
n1s.add(r1[rcount % r1.length]);
};

newcarry = (int.parse(times10(carry.toString())) + int.parse(n1s[x])) - n2 * ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2);
newcarry = (int.parse(times10(carry.toString())) + int.parse(n1s[x])) - n2i * ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2i);

if(over) {
if(newcarry==0 && r1=="0") {
res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2).toString();
return sign+res.replaceAll(RegExp(r'^0+|0$'), "").replaceFirst(RegExp(r'^\.'), "0.").replaceFirst(RegExp(r'\.$'), "");
res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2i).toString();
return sign+res.replaceAll(RegExp(r'^0+|0$'), "").replaceFirst(RegExp(r'^\.'), "0.").replaceFirst(RegExp(r'\.$'), "").replaceFirst(".", decstr);
};
for(y = 0; y < carries.length; y++) {
if(carries[y]==newcarry && (y % r1.length)==((rcount + 1) % r1.length)) {
res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2).toString();
res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2i).toString();
result = sign+(res.substring(0, x - rcount + y)+"["+res.substring(x - rcount + y)+"]").replaceFirst(RegExp(r'^0+'), "").replaceFirst(RegExp(r'^\.'), "0.");
if(result[result.indexOf("[") - 1]==result[result.indexOf("]") - 1])
result = result.substring(0, result.indexOf("[") - 1)+"["+result[result.indexOf("[") - 1]+result.substring(result.indexOf("[") + 1, result.indexOf("]") - 1)+"]";
if(result.indexOf("]")==result.indexOf("[") + 3 && result[result.indexOf("[") + 1]==result[result.indexOf("[") + 2])
result = result.substring(0, result.indexOf("[") + 2)+"]";
return result.replaceFirst("[", rstr1).replaceFirst("]", rstr2);
return result.replaceFirst(".", decstr).replaceFirst("[", rstr1).replaceFirst("]", rstr2);
};
};
};

res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2).toString();
res+= ((int.parse(times10(carry.toString())) + int.parse(n1s[x])) ~/ n2i).toString();
if(over)
carries.add(carry);
carry = newcarry;
};
}

void main(List<String> args) {
int r1, r2;
double n1, n2;
String n1, n2;

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]))) {
print("Usage: advdiv n1 n2[ r1[ r2]]");
if(args.length < 2) {
print("Usage: advdiv n1 n2");
return;
};

n1 = double.parse(args[0]);
n2 = double.parse(args[1]);
r1 = args.length < 3 ? 0 : int.parse(args[2]);
r2 = args.length < 4 ? 0 : int.parse(args[3]);
n1 = args[0];
n2 = args[1];

print(advdiv(n1, n2, r1, r2) ?? "Error");
print(advdiv(n1, n2) ?? "Error");
}

0 comments on commit b25330d

Please sign in to comment.