Expand|Select|Wrap|Line Numbers
- #include <iomanip>
- #include <iostream>
- using namespace std;
- class LongInt
- {
- public:
- LongInt();
- LongInt(int[], int);
- void print();
- void read();
- LongInt add(LongInt);
- void setLength(int);
- void createIntArray(int);
- int getLength();
- private:
- int *a;
- int length;
- };
- LongInt::LongInt()
- {
- }
- LongInt LongInt::add(LongInt other)
- {
- int firstIntLength = length;
- int secondIntLength = other.length;
- int resultIntLength;
- if(firstIntLength> secondIntLength)
- resultIntLength = firstIntLength + 1;
- else resultIntLength = secondIntLength + 1;
- LongInt result;
- result.a = new int[resultIntLength];
- result.length = resultIntLength;
- int carry = 0;
- int p1 = firstIntLength -1;
- int p2 = secondIntLength -1;
- int p3 = resultIntLength -1;
- for( int i = resultIntLength - 3 ; i>0 ; i-- )
- {
- result.a[p3] = a[p3] + carry;
- if( p1 >= 0 )
- {
- result.a[p3] = result.a[p3] + a[p1];
- p1--;
- }
- if(p2>=0)
- {
- result.a[p3] = result.a[p3] + other.a[p2];
- p2--;
- }
- carry = (result.a[p3]/10);
- result.a[p3] = (result.a[p3])%10;
- p3--;
- }
- result.a[p3] = carry;
- return result;
- }
- void LongInt::setLength(int intLength)
- {
- length = intLength;
- }
- int LongInt::getLength()
- {
- return length;
- }
- void LongInt::read()
- {
- unsigned long int number;
- int remainder;
- int lengthcount = 0;
- cin >> number;
- unsigned long int newnumber = number;
- while( newnumber >= 1 )
- {
- lengthcount++;
- newnumber = newnumber/10;
- cout << newnumber << endl;
- }
- cout << lengthcount << endl;
- a = new int(lengthcount);
- for( int i = lengthcount-1 ; i >= 0 ; i-- )
- {
- remainder = number%10;
- number = number/10;
- a[i] = remainder;
- cout << a[i] << endl;
- }
- setLength(lengthcount);
- }
- void LongInt::print()
- {
- for( int i = 0 ; i < getLength() ; i++)
- cout << a[i];
- cout << endl;
- }
- int main()
- {
- LongInt I1, I2, I3;
- I1.read();
- //I2.read();
- //I3 = I2.add(I1);
- I1.print();
- //I2.print();
- //I3.print();
- return 0;
- }
if I put in 1111111111 it will put out 1111111111
if I put in 11111111111 it will put out 3086772644
it will always put out that number. if i change it to an unsigned long int. it puts out the same number. long int does that same number also.