Hello everybody !
I am writing code to create a BigNum class and being able to add it with others and I also have to write it so I can multiply it with others, but I can't seem to get that working... Here is my code :
My results for the add method works good, but I can't seem to get the multiply method going... Somehow in the second cycle for the multiply I get 256 and not 1024... Also I don't know how to get the program to add a 0 at the right whenever a new cycle begins...public class BigNum { public static boolean showZero = true; // I am not sure if that is relevant public static final int MAX_DIGITS = 40; private int[] digits = new int [MAX_DIGITS]; public BigNum(){ for (int i=0; i < MAX_DIGITS; i++) digits[i]=0; } public BigNum(int a) { for (int i=0; i<MAX_DIGITS; i++){ int stock = (int) (a %10); digits[i] = stock; a = a/10; } } public String show(){ String s =""; for (int i = MAX_DIGITS-1; i>=0; i--){ if (digits[i] != 0) { s = s + digits[i]; } } System.out.println("BigNum = "+s); return s; } public BigNum add(BigNum t){ BigNum sum = new BigNum (); int a = 0; for (int i = 0; i< MAX_DIGITS; i++){ sum.digits[i] = (this.digits[i]+ t.digits[i]+a)%10; a = (this.digits[i] + t.digits[i] + a)/10; } return sum; } public BigNum multiply(BigNum u){ BigNum e = new BigNum (); for (int i = 0; i< 2; i++){ // the condition i<2 is wrong, but I don't know what to put in else BigNum mult = new BigNum (); int a = 0; for (int k = 0; k<MAX_DIGITS; k++){ mult.digits[k] = (this.digits[i] * u.digits[k] + a)%10; a = (this.digits[i] * u.digits[k] + a)/10; } e = e.somme(prod); System.out.println("e="+e.affiche()); } return e; } public static void main(String[] args) { BigNum a = new BigNum(123); BigNum b = new BigNum(512); a.show(); b.show(); BigNum c = a.add(b); c.show(); BigNum d = a.multiply(b); d.show(); } }
Thank you for helping me in advance !!!