Big int adding problem
I have a problem in adding the 2 big int that i cant adding the different length integer. can someone help me?
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include "LargeInt.h"
#include <iostream>
#include <string>
ostream& operator<< (std::ostream& out, LargeInt& other)
{
int i = 0;
for (i; i < other.length; i++)
{
out << other.info[i];
if (other.info[i] == '0')
break;
}
return out;
}
istream& operator>> (istream& in, LargeInt& other)
{
int i;
int temp;
char *Str = new char[other.max];
in.getline(Str, other.max);
for (i = 0; Str[i] != '\0'&&i < other.max; i++)
{
other.info[i] = Str[i] - '0';
other.length++;
}
for (int i = other.length; i >=0; i--)
{
temp = other.info[other.length - i - 1];
other.info[other.length - i - 1] = other.info[i];
other.info[i] = temp;
}
delete[]Str;
return in;
}
LargeInt::LargeInt(int size)
{
max = size;
length = 0;
info = new int[max];
for (int i = 0;i < max;i++)
info[i] = 0;
}
LargeInt::~LargeInt()
{
delete[] info;
}
LargeInt::LargeInt(const LargeInt& other)
{
length = other.length;
max = other.max;
info = new int[max];
for (int i = 0; i < length; i++)
info[i] = other.info[i];
}
LargeInt& LargeInt:: operator= (const LargeInt& other)
{
if (this != &other)
{
delete[] info;
max = other.max;
length = other.length;
info = new int[max];
for (int j = 0; j < length; j++)
info[j] = other.info[j];
}
return *this;
}
LargeInt LargeInt ::operator+(LargeInt other)
{
LargeInt temp;
int carry = 0;
int c, i;
if (length>other.length)
c = length;
else
c = other.length;
for (i = 0; i < c;i++)
{
temp.info[i] = info[i] + other.info[i] + carry;
if (temp.info[i] > 9)
{
temp.info[i] %= 10;
carry = 1;
}
else
carry = 0;
}
if (carry == 1)
{
temp.length = c + 1;
temp.info[i] = carry;
}
else
temp.length = c;
return temp;
}
bool LargeInt::operator==(const LargeInt& other)
{
if (length != other.length)
return false;
for (int i = 0;i < max;i++)
{
if (info[i] != other.info[i])
{
return false;
}
}
return true;
}
|
Last edited on
try to use the code tag to quote your code, it will make it look much nicer.
Last edited on
Topic archived. No new replies allowed.