public static Huge operator *(Huge left, Huge right) { Huge sum = new Huge(); uint p = 0; for (int i = 0; i < left.Digits; i++) { sum = sum + (right * left.Data[i]).MultiplyBy10(p); p++; } return(sum); }
public static Huge operator *(Huge left, byte right) { byte carry = 0; byte[] product = new byte[left.Digits]; for (int i = 0; i < left.Digits; i++) { product[i] = (byte)((left.Data[i] * right + carry) % 10); carry = (byte)((left.Data[i] * right + carry) / 10); } if (carry > 0) { Array.Resize(ref product, product.Length + 1); product[product.Length - 1] = carry; } Huge rez = new Huge(string.Join("", product.Reverse())); return(rez); }
/** * Creati un TAD cu numele Huge, care implementeaza conceptul de numar natural mare. * * * */ static void Main(string[] args) { Huge h1 = new Huge(123456789); Huge h2 = new Huge("231231231231231231231231231231232"); Huge h3 = new Huge("349249023849023849023849023842903482930482934823904823904823094823094"); Console.WriteLine(h1); Console.WriteLine(h2); Console.WriteLine(h3); Huge a = new Huge("234234234234234234"); Huge b = new Huge(1); Console.WriteLine(a + b); Console.WriteLine(a * b); Console.WriteLine(a.Power(10)); Console.ReadKey(); }
public static Huge operator +(Huge left, Huge right) { Huge result = null; byte[] sum = new byte[Math.Max(left.Digits, right.Digits)]; int carry = 0, i, j, contor = 0; for (i = 0; i < Math.Min(left.Digits, right.Digits); i++) { sum[i] = (byte)((left.Data[i] + right.Data[i] + carry) % 10); carry = (left.Data[i] + right.Data[i] + carry) / 10; contor++; } for (j = i; j < left.Digits; j++) { contor++; sum[j] = (byte)((left.Data[j] + carry) % 10); carry = (left.Data[j] + carry) / 10; } for (j = i; j < right.Digits; j++) { contor++; sum[j] = (byte)((right.Data[j] + carry) % 10); carry = (right.Data[j] + carry) / 10; } if (carry > 0) { Array.Resize(ref sum, sum.Length + 1); sum[sum.Length - 1] = (byte)carry; contor++; } result = new Huge(string.Join("", sum)); result.data = sum; return(result); }
public static Huge operator +(Huge left, int right) { Huge rightH = new Huge(right); return(left + rightH); }
public static Huge operator -(Huge left, Huge right) { Huge result = null; byte[] sum = new byte[Math.Max(left.Digits, right.Digits)]; int carry = 0, i, j, contor = 0; for (i = 0; i < Math.Min(left.Digits, right.Digits); i++) { if (right.Data[i] == 0) { right.Data[i] = 9; } byte dif = (byte)((10 - right.Data[i] - carry + left.Data[i]) % 10); sum[i] = Modul(dif); if (left.Data[i] - carry < right.Data[i]) { carry = 1; } else { carry = 0; } contor++; } for (j = i; j < left.Digits; j++) { byte dif = (byte)(left.Data[i] - (byte)(carry)); sum[i] = Modul(dif); if (dif < 0) { carry = 1; } else { carry = 0; } contor++; } /* * for (j = i; j < right.Digits; j++) * { * contor++; * byte dif = (byte)(left.Data[i] - (byte)(carry) - right.Data[i]); * sum[j] = (byte)((right.Data[j] + carry) % 10); * carry = (right.Data[j] + carry) / 10; * } */ if (carry > 0) { Array.Resize(ref sum, sum.Length + 1); sum[sum.Length - 1] = (byte)carry; contor++; } result = new Huge(string.Join("", sum)); result.data = sum; return(result); }