A
akickdoe22
i could really use help finishing this addition program. I'm stuck on
the part that allows you to add any two large integers,up to 100
digits,(pos+pos, neg+neg, and pos+neg). could use hints ideas code
anything for neg+pos addition.
CODE SO FAR:
class INT {
int digits[100];
char sign;
public:
INT( ) {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT(int num) {
int a;
for (a=0; a<100; a++) digits[a] = 0;
if (num < 0) { sign = 'n'; num = num * -1; }
else sign = 'p';
a = 99;
while (num > 0) {
digits[a] = num % 10;
num = num / 10;
a = a - 1;
}
}
friend INT operator+(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a<number.length(); a++)
num.digits[loc++] = number.at(a) - '0';
return is;
}
INT operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
else {
//CODE TO ADD TWO NUMBERS WITH DIFFERENT SIGNS
}
return result;
}
Here's a sample main routine to test it:
int main( ) {
INT a, b, c;
cout << "Enter two large integers : ";
cin >> a >> b;
c = a + b;
cout << "The result is : " << endl;
cout << c << endl;
return 0;
}
the part that allows you to add any two large integers,up to 100
digits,(pos+pos, neg+neg, and pos+neg). could use hints ideas code
anything for neg+pos addition.
CODE SO FAR:
class INT {
int digits[100];
char sign;
public:
INT( ) {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT(int num) {
int a;
for (a=0; a<100; a++) digits[a] = 0;
if (num < 0) { sign = 'n'; num = num * -1; }
else sign = 'p';
a = 99;
while (num > 0) {
digits[a] = num % 10;
num = num / 10;
a = a - 1;
}
}
friend INT operator+(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a<number.length(); a++)
num.digits[loc++] = number.at(a) - '0';
return is;
}
INT operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
else {
//CODE TO ADD TWO NUMBERS WITH DIFFERENT SIGNS
}
return result;
}
Here's a sample main routine to test it:
int main( ) {
INT a, b, c;
cout << "Enter two large integers : ";
cin >> a >> b;
c = a + b;
cout << "The result is : " << endl;
cout << c << endl;
return 0;
}