Reputation: 139
My problem is that I don't know how to sum huge numbers (like "172839023498234792834798237494" or "-172839023498234792834798237494"). So I made char* m_value
where I will store this kind of objects. Now what I want to do is making some basic arithmetic operations. Should I first convert it or use some like itoa
? In my case there is no other options then char* to store numbers.
Main.cpp
int main(int argc, char *argv[])
{
LargeNumber l1;
LargeNumber l3("172839023498234792834798237494");
LargeNumber l7("-172839023498234792834798237494");
l1 = l3 + l7; //How to do it ?
return 0;
}
LargeNumber.h
class LargeNumber{
public:
LargeNumber(char* value):m_value(value)
{}
LargeNumber operator+(const LargeNumber&);
private:
char* m_value;
}
LargeNumber.cpp
LargeNumber LargeNumber ::operator+(const LargeNumber &b)
{
return LargeNumber ( ... ); //Sum both LargeNumber ???
}
Upvotes: 1
Views: 528
Reputation: 1433
Instead of using a string, I'd do it as an array of int (or long). The algorithms for the operators would be just about what you'd do by hand.
Upvotes: 1
Reputation: 11181
Don't reinvent the whell, you should use a BigInteger library.
And anyway, C strings are not the best way to do that.
Upvotes: 3
Reputation: 29529
You will need to use a large numbers arithmetic library. GMP is a good option. Your processor cannot natively perform operations on numbers of this size, so these libraries take care of all the old-school math for you in software.
Upvotes: 4