473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

need help with large int multiplication and division

Please help me finish this program. i have completed the addition and
the subtraction parts, but i am stuck on the multiplication and
division. any suggestions, hints, code, anyhting. it's not a true large
int calculator, the numbers entered are at max 100 intergers.
CODE SO FAR:
#include<iostream> //libraries limited to
#include<string>
using namespace std;
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 INT operator-(const INT&, const INT&); // new
friend INT operator*(const INT&, const INT&); // new
friend INT operator/(const INT&, const INT&); // new
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, larger, smaller;
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 {

int a=0;
bool flag=false;
while (a<100 && flag==false) { // figure out which one is largest
if (x.digits[a] < y.digits[a])
{ larger = y; smaller = x; result.sign = y.sign; flag = true; }
if (x.digits[a] > y.digits[a])
{ larger = x; smaller = y; result.sign = x.sign; flag = true; }
a++;
}
if (flag) { // add them unless flag is false (the two were equal)
for (int b=99; b>=0; b--) {
if (smaller.digits[b] > larger.digits[b]) { // need to borrow
int c = b-1;
while (larger.digits[c] == 0) {
larger.digits[c] = 9;
c = c - 1;
}
larger.digits[c] = larger.digits[c] - 1;
larger.digits[b] = larger.digits[b] + 10;
}
result.digits[b] = larger.digits[b] - smaller.digits[b];
}
}
}
return result;
}
INT operator-(const INT& x, const INT& y) {
INT result, larger, smaller;
if (x.sign != y.sign) {
int a=0;
bool flag=false;
while (a<100 && flag==false) { // figure out which one is largest
if (x.digits[a] < y.digits[a])
{ larger = y; smaller = x; result.sign = y.sign; flag = true; }
if (x.digits[a] > y.digits[a])
if(x.digits[a] == y.digits[a]){ result.sign = x.sign; }//my sign
config.
else
{ larger = x; smaller = y; result.sign = x.sign; flag = true; }
a++;
}
int 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 {

int a=0;
bool flag=false;
while (a<100 && flag==false) { // figure out which one is largest
if (x.digits[a] < y.digits[a])
{ larger = y; smaller = x; result.sign = y.sign; flag = true; }
if (x.digits[a] > y.digits[a])
{ larger = x; smaller = y; result.sign = x.sign; flag = true; }
a++;
}
if (flag) { // add them unless flag is false (the two were equal)
for (int b=99; b>=0; b--) {
if (smaller.digits[b] > larger.digits[b]) { // need to borrow
int c = b-1;
while (larger.digits[c] == 0) {
larger.digits[c] = 9;
c = c - 1;
}
larger.digits[c] = larger.digits[c] - 1;
larger.digits[b] = larger.digits[b] + 10;
}
result.digits[b] = larger.digits[b] - smaller.digits[b];
}
}
}
return result;
}
INT operator*(const INT& x, const INT& y) {
INT result;
int mul_top[100];
int mul_bottom[100];
// int ans[200];
for (int a=99; a>=0; a--)
mul_top[a] = x.digits[a]; //want to copy arrays
for (int a=99; a>=0; a--)
mul_bottom[a] = y.digits[a];
//for (int c=0; c<200; c++) //want 2 find lenghts of #s
// ans[c] = 0; //4 for loops. set e,d = to #s
int c=200, carry=0, total=0; //don't know if .digits or
..ans should b returned
for (int d=99; d>=mul_bottom.lenght(); d--){
for(int e=99; e>=mul_top.lenght(); e--){
total=mul_top[e]*mul_bottom[d]+carry;
if (total > 9) carry = total / 10;
else carry = 0;
result.digits[c] = total % 10;
c--;
}
}
return result;

}


int main( ) {
INT a, b, c;
cout << "Enter two large integers : ";
cin >> a >> b;
cout << "Addition : " << endl;
c = a + b; cout << c << endl;
cout << "Subtraction : " << endl;
c = a - b; cout << c << endl;
cout << "Multiplication : " << endl;
c = a * b; cout << c << endl;
//cout << "Division : " << endl;
//c = a / b; cout << c << endl;
return 0;
}

Jul 22 '05 #1
1 2800
ak********@hotmail.com wrote:
Please help me finish this program. i have completed the addition and
the subtraction parts, but i am stuck on the multiplication and
division. any suggestions, hints, code, anyhting. it's not a true large
int calculator, the numbers entered are at max 100 intergers.


Save yourself from reinventing the wheel.
Use your favorite search engine and search for "Big Number Library".
Some people recommend the GNU GMP, but I recommend the BN library
in the crypto section of the OpenSSL:
http://www.openssl.org/docs/crypto/bn.html#

One big feature about the libraries, is that they have already
been tested by many people.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #2

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

Similar topics

3
by: Mark Dickinson | last post by:
Can anyone either reproduce or explain the following apparently inconsistent behaviours of __add__ and __mul__? The class Gaussian provides a sub-bare-bones implementation of Gaussian integers (a...
4
by: jimh | last post by:
I'm not a SQL expert. I want to be able to write a stored procedure that will return 'people who bought this product also bought this...'. I have a user table that links to a transaction table...
5
by: Witless | last post by:
This is 'supposed' to be a simple problem and shouldn't need too much time to solve :S Below is a function that works like this: int fn(int input) { int x=(input/8);
6
by: Marlene Stebbins | last post by:
My hobby project is a library of routines in C that do big integer arithmetic. I've been working on it for several weeks and it now consists of about 1200 lines of code. I've got addition,...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
3
by: dee vee bee | last post by:
this is my project i need a code that will show like this.. "first number __" "second number ___" "choose operation: addition..subtraction..multiplication..division" "the answer is ___" the...
5
by: emre esirik(hacettepe computer science and enginee | last post by:
/////////////////////// DIVISION FUNCTION //////////////////// void div ( int pol1 , int pol1p , int pol2 , int pol2p) { int r=5,z,i,g,j,k,l,dividend,rising={0}; for (i=0 ; i<6 ; i++)...
3
by: Radrily | last post by:
I'm trying to create a small program for a beginner's C++ class. Basically, we have to take user input of 2 numbers and then output the sum, difference, product, and quotient of each. I'm trying to...
8
by: jac130 | last post by:
I need to create a calculator using combo boxes/procedures/functions etc... and some other stuff. most of it works properly, but, i need to create a user defined function that checks if both input...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.