473,466 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

addition help

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

Jul 22 '05 #1
2 1614
<ak********@hotmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
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

Jul 22 '05 #2
On 15 Jan 2005 12:07:59 -0800, ak********@hotmail.com wrote:
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
--

The ultimate truth is that there is no Ultimate Truth
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jeffbernstein | last post by:
Greetings. I'm reading "How to think like a computer scientist: Learning with Python" and there's a question regarding string operations. The question is, "Can you think of a property that...
3
by: Jay | last post by:
I have two strings that instead of adding them together to get the sum the are concatenating together. Does anyone know how I can get these two to add. while not rstemp4.eof vservdate =...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
6
by: Scott Nixon | last post by:
New to Postgres 7.3 from 7.0. Am having some trouble with a query that worked in 7.0 but not in 7.3.....can't seem to figure out the syntax or find info about how to do this anywhere. ...
3
by: snow.carriers | last post by:
Let me first state that I'm using Borland Turbo C++, it's relatively old so the new string methods won't work. Anyways, first I'm trying to collect a line of a string (with numbers, letters,...
2
by: cpbsmw | last post by:
I need help adding to 2 seperate fields. I know this seems as simple as 1+1. However lets say field one has a name of A and has a value of "1" and field two has a name of B and has a value of 2. ...
5
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express...
3
by: srinivas33034 | last post by:
Hi there, my problem is i have to perform addition dyamically My req is i have 3 txt boxes.. and another text box to display total additon.. as i am entering the values in TextBox1 i want...
31
by: Mark Dickinson | last post by:
On SuSE 10.2/Xeon there seems to be a rounding bug for floating-point addition: dickinsm@weyl:~python Python 2.5 (r25:51908, May 25 2007, 16:14:04) on linux2 Type "help", "copyright",...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.