need help with large int multiplication and division

A

akickdoe22

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 > larger.digits) { // 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 = larger.digits + 10;
}
result.digits = larger.digits - smaller.digits;
}
}
}
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 > larger.digits) { // 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 = larger.digits + 10;
}
result.digits = larger.digits - smaller.digits;
}
}
}
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;
}
 
T

Thomas Matthews

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
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top