Tuesday, July 14, 2009

C++ question?

I'm trying to create a class called BigInt that is just like an int but with a much larger range...this is for a project so no shortcuts...anyway, my plan is to take the number in as a string, which is an array of chars, and convert each char which will be an ascii number to an integer (i.e. if the string is 356, I'll convert the ascii decimal representation of 3 which is 51 back to 3 by subtracting 48 from it, same with all of the other numbers). So then by performing a quick atoi (ascii to int) conversion of all of the chars in the input string, it will in sense just be one long number in an array.





So anyway, my problem is how to define a number as a BigInt...i.e. how would I make it possible to say:





BigInt x = 523;





And have the 523 taken in as a string? Once I have the string I can do pretty much anything else with it, but that is my difficulty right now.

C++ question?
Have a look the link..and just download the header file.


http://www.codeproject.com/useritems/lin...





Other Resources


http://plasimo.phys.tue.nl/TBCI/online-d... ...good web for Numerical C++Library.





http://doc.ddart.net/msdn/header/include...





http://www.freeprogrammingresources.com/...





hope...it will help.
Reply:For BIgInt x = 523; to work, you could try overloading the "=" operator. This is off the top of my head, and may not work exactly, but inside the BigInt class try this:





BigInt%26amp; operator=(int i)


{


//use int to ascii (itoa) to get the int into into a string,


// and feed that to the char* member variable of


// this class





//lastly:


return *this;


}





It also looks like you'll have to overload the * operator for between a BigInt and an int, as well as the %26lt;%26lt; operator for between a ostream and a BigInt. (cout is an object of a class called ostream).





Hopefully this sends you in the right direction.





For more info:


http://en.wikibooks.org/wiki/C++_Program...


No comments:

Post a Comment