473,979 Members | 1,781 Online

# need help with large int multiplication and division

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<iostre am> //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<<(ostr eam&, const INT&);
friend istream& operator>>(istr eam&, 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.l enght(); d--){
for(int e=99; e>=mul_top.leng ht(); 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 << "Subtractio n : " << endl;
c = a - b; cout << c << endl;
cout << "Multiplica tion : " << endl;
c = a * b; cout << c << endl;
//cout << "Division : " << endl;
//c = a / b; cout << c << endl;
return 0;
}

Jul 22 '05 #1
1 2842
ak********@hotm ail.com wrote:
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.l earn.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.