0

I have to write own class for operation on very big numbers. Already have adding:

 char index1 = liczba1.length();
    char index2 = liczba2.length();



stack<char> wyniki;
    while (index1 > 0 || index2 > 0) {
        index1--;
        index2--;
        k = 0;
        o = 0;
        k = index1 < 0 ? 0 : (liczba1[index1] - 48);
        o = index2 < 0 ? 0 : (liczba2[index2] - 48);

    wynik = k + o + f;
    if (wynik > 9) {
        wynik -= 10;
        f = 1;
    } else {
        f = 0;
    }
    wyniki.push(wynik+48); //
}
short i=0;
if (f > 0){
    fin[i++] = f;
}

I throw a stack to reverse the sequence.

do {
        fin[i++]= wyniki.top();
        wyniki.pop(); 
    } while (!wyniki.empty()); 
    fin[i]=0;
    string res(fin);
    return DuzaLiczba(res);
}

Now trying to make substraction and comparison of the two numbers. Have any idea how to make substraction?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Is this homework ? Otherwise, you can use GMP. – Alexandre C. Jan 21 '11 at 09:27
  • 2
    Maybe you just need to implement a XyczeFuzba and make sure to get the waryfuczlys and winitykis right? – Johan Kotlinski Jan 21 '11 at 09:29
  • What's fin? You seem to be zero-terminating it. But that makes no sense if it can contain zero bytes as data. – TonyK Jan 21 '11 at 10:21
  • fin is a character array that holds the end result of the addition. The funny variable names are almost certainly because the programmer is not a native english speaker. – levis501 Jan 21 '11 at 15:48
  • @levis501: I got all that, thank you. Perhaps you should have read my comment before answering it. – TonyK Jan 21 '11 at 19:59

2 Answers2

0

Instead of adding o to k, subtract it. if wynik < 0 after that, add ten to wynik and set the carry (f) to -1.

levis501
  • 4,117
  • 23
  • 25
0

Man, I own u beer. Works perfectly.

Got prob with returning.

In operator overloading I have:

do {
    fin[i++]= wyniki.top();
    wyniki.pop(); //zdejmujemy z stosu
} while (!wyniki.empty()); //az nie bedzie empty
fin[i]=0;
string res(fin);
cout << res(fin);
return DuzaLiczba(res);

In main:

DuzaLiczba dl1(liczba1);
DuzaLiczba dl2(liczba2);
DuzaLiczba dl5 = dl1-dl2;
DuzaLiczba dl3 = dl1+dl2;

temp1 = dl3.getData();
cout<<temp1;

Error: Segmentation fault on dl3.getData()

VoonArt
  • 884
  • 1
  • 7
  • 21