addition help

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;
}
 
M

Mike Wahler

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.
else {
//CODE TO ADD TWO NUMBERS WITH DIFFERENT SIGNS

You can do it in terms of subtracting two positive
values.

All you need do is find the difference between their
absolute values, and give the result the sign of
the larger addend. I.e. temporarily make the negative
number positive, subtract, and apply the sign of the
larger addend to the result.

-Mike
 
R

rossum

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;
}
}

[snip]

As you can see you code arrived in my newsreader without indentation.
Replacing tabs with spaces may help.

You can use two principal functions: absolute_sum() and
absolute_difference() with a front end add() function to drive things.

The two absolute_xxx functions ignore the signs of the numbers and
just work on their magnitudes. When you get two numbers to add, look
at their signs. If they are the same then you need absolute_sum(), if
the signs are different then use absolute_difference(). Work out the
sign of the answer separately, combine it with the magnitude from
whichever absolute_xxx() you used and there is your answer.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top