# Thread: My own BigInteger class. Need help.

1. Member
Join Date
Jan 2010
Posts
2
Rep Power
0

## My own BigInteger class. Need help.

First of all I want to say hello to all of you. It is my first post here. I am 19-year old student from Poland. My name is Artur.

So, my uni teacher gave me a project to made, which is not so hard as i thought, but still gives me some trouble.
I have to write a class, which can make operations on numbers, which are much longer than any type of variables. The only solution to make operations on such numbers is, save them as a String. It should look like BigInteger class, but when i started to read this class, i recognised i can not just copy from the documentation. I have to writte it myself.

Firstly i thought about :
User gives me String number one.
User gives me String number two.

Convert both Strings to CharArray
Make a simple adding from CharArrayOne[0] and CharArrayTwo[0] and save it to CharArrayThree[0] .

But then, when i add 2 chars, and the result is > 10, the char takes a form of ( ":" ; ";" ; "<" etc. ).
Which made me to take a other way. In the time my teacher gave me some advices, and i made a code.

public class BigInt{
char[] tab;
}
public BigInt(String a) {
tab = toCharArray();
return;
}
public BigInt add(BigInt number) {
String a = number.toString();

int memory= 0;

String RESULT= "";
int result;
for (int i = tablica.length - 1; i >= 0; i--) {

result= tab[i] - '0' + tabAdd[i] - '0' + memory;

if (memory > 9) {
reuslt= result - 10;
memory = 1;

} else
memory = 0;
RESULT= result + result;
}
if (pamiec == 1)
WYNIK ="11"+WYNIK;
return new DuzaLiczba(WYNIK);
And on the main class :
public class Main {

public static void main(String[] args) {
BigInt x = new BigInt("33333333");
System.out.println(x);

BigInt result = x.add(new BigInt("33333333"));

System.out.println(wynik);
}
}
And the result of that is :
33333333
66666666
If the result is greater than 10 in any charArraySegment it works also good :
BigInt x = new BigInt("33333333");
System.out.println(x);
BigInt result = x.add(new BigInt("33333333"));
System.out.println(wynik);
Result :
33333333
111111110
BUT :D it starts to break when x have different lenght than result. My teacher gave me some thoughts, but i really do not know what i have to do now.

Can you help me in?
Make it work with different lengths of arrays.
And write a subtraction method? I made a method by copying a add method and just change :
wynik = tablica[i] - '0' + tablicaDodawania[i] - '0' + pamiec;
to :
wynik = tablica[i] - '0' - tablicaDodawania[i] - '0' + pamiec;
But it was for sure bad idea :).
Thanks for the thoughts. If you have any questions, ask here, i will track this thread.
Last edited by Fubarable; 01-23-2010 at 01:20 AM.

2. Member
Join Date
Jan 2010
Posts
2
Rep Power
0
Thanks -_-'.

Help need no more.
Last edited by Fubarable; 01-27-2010 at 11:46 PM. Reason: signature removed

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•