static void Main(string[] args) { BigInt b1 = new BigInt("00016"); BigInt b2 = new BigInt("9"); Console.WriteLine(b1 * b2); // 16 * 9 = 144 ici, et 16 * 9 = 104 dans FromBigInt... Bistro Bi = new Bistro(); Bi.LoadBase(@"C:\tp\test_files\tp9\bistro"); // Base 16 Console.WriteLine(Bi.FromBigInt(new BigInt("152"))); // Devrait retourner 98 : 9 * 16 + 1 * 8 = 152 }
// FIXME : need implementation of * operator public static BigInt operator *(BigInt a, BigInt b) { int r = 0; int s; BigInt res = new BigInt("0"); string tmp = ""; int al = a.Length(); int bl = b.Length(); for (int bi = 0; bi < bl; bi++) { for (int ai = 0; ai < al; ai++) { s = b[bl - bi] * a[al - ai] + r; if (s >= 10) { r = s / 10; s %= 10; } else { r = 0; } tmp = s.ToString() + tmp; } if (r > 0) { tmp = "1" + tmp; r = 0; } res += new BigInt(tmp); tmp = new String('0', bi + 1); } if (a._sign == b._sign) { return new BigInt(res._value, true); } else { return new BigInt(res._value, false); } }
// methode de normalisation private static BigInt NBigInt(BigInt i) { while (i.Length() > 1 && i._value[0] == '0') { i._value = i._value.Remove(0, 1); } return i; }
public string FromBigInt(BigInt value) { BigInt Base = new BigInt(_base.Length.ToString()); BigInt tvalue = value; BigInt tmp; string r = ""; while (tvalue > Base) { tmp = tvalue / Base; r += _base[Convert.ToInt32(tmp.ToString())]; tvalue = tvalue - (tmp * Base); } return r; }
public BigInt Evaluate(string filename) { // Pour une opération individuelle try { StreamReader sr = new StreamReader(filename); BigInt A; BigInt B; string v = sr.ReadLine(); string a = ""; string o = ""; string b = ""; bool Base10 = true; int i = 0; // Terme 1 while (v[i] > 47 && v[i] < 58 || v[i] > 64 && v[i] < 91 || v[i] > 96 && v[i] < 123) { a += v[i]; if (v[i] > 64) { Base10 = false; } i++; } if (!Base10) { A = ToBigInt(a); Base10 = true; } else { A = new BigInt(a); } // Opérateur o = v[i].ToString(); i++; // Terme 2 while (i < v.Length) { b += v[i]; if (v[i] > 64) { Base10 = false; } i++; } if (!Base10) { B = ToBigInt(b); } else { B = new BigInt(b); } switch (o) { case "+": return A + B; case "-": return A - B; case "*": return A * B; case "/": return A / B; default: return new BigInt(); // Ne devrait jamais arriver. ("sécurité") } } catch (Exception) { throw new FileLoadException("Wrong path or file"); } }
public BigInt ToBigInt(string value) { /* FIXME convert from base to bigint */ BigInt res = new BigInt(); int p = value.Length - 1; foreach (char c in value) { res += new BigInt((BaseValue(c) * Math.Pow(value.Length, p)).ToString()); // Formule pour convertir d'une base b à base 10 : Symbole * Base^poids } return res; }