In: Computer Science
C++ PROGRAM
Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects and functions to set, retrieve, and print the values of objects. Write a program to test your class.
/* Program for adding large integers using array*/
//header files
#include <iostream>
#include <cctype>
//string library
#include <cstring>
using namespace std;
#define DIGITS 40
class large_integers //defining class as large integers
{
public:
large_integers();
void Inputvalue();
void Outputvalue();
large_integers operator+( large_integers );
large_integers operator-( large_integers );
large_integers operator*( large_integers );
int operator==( large_integers);
private:
int integervalue[ DIGITS ];
int lenvalue;
};
void large_integers::Outputvalue()
{
int i1;
for (i1=lenvalue-1;i1 >= 0; i1-- )
cout<<integervalue[i1];
}
void large_integers::Inputvalue()
{
string in;
int i1,j1,k1;
cout<<"Enter a number("<<DIGITS<<" DIGITS maximum):";
cin>>in;
for(i1=0;in[i1]!='\0';i1++);
lenvalue=i1;
k1=0;
for(j1=i1-1;j1>=0;j1--)
integervalue[j1]=in[k1++]-48;
}
large_integers::large_integers( )
{
for ( int i1 = 0; i1 < DIGITS; i1++ )
integervalue[ i1 ] = 0;
lenvalue=DIGITS-1;
}
int large_integers::operator==( large_integers oper2 )
{
int i;
if(lenvalue<oper2.lenvalue)
return -1;
if(oper2.lenvalue<lenvalue)
return 1;
for(i=lenvalue-1;i>=0;i--)
if(integervalue[i]<oper2.integervalue[i])
return -1;
else if(oper2.integervalue[i]<integervalue[i])
return 1;
return 0;
}
large_integers large_integers::operator+( large_integers oper2 )
{large_integers temp_value;
int carry_value = 0;
int c1,i;
if(lenvalue>oper2.lenvalue)
c1=lenvalue;
else
c1=oper2.lenvalue;
for ( i=0; i<c1; i++ )
{temp_value.integervalue[ i ] =integervalue[ i ] + oper2.integervalue[ i ] + carry_value;
if ( temp_value.integervalue[ i ] > 39 )
{temp_value.integervalue[ i ] %= 40;
carry_value = 1;
}
else
carry_value = 0;
}
if(carry_value==1)
{temp_value.lenvalue=c1+1;
if(temp_value.lenvalue>=DIGITS)
cout<<"**overflow** \n";
else
temp_value.integervalue[i]=carry_value;
}
else
temp_value.lenvalue=c1;
return temp_value;
}
large_integers large_integers::operator-( large_integers oper2 )
{
large_integers temp_value;
int c;
if(lenvalue>oper2.lenvalue)
c=lenvalue;
else
c=oper2.lenvalue;
int borrow_value = 0;
for( int i = c;i >= 0;i--)
if(borrow_value==0)
{if(integervalue[i]>=oper2.integervalue[i])
temp_value.integervalue[i]=integervalue[i]-oper2.integervalue[i];
else
{borrow_value=1;
temp_value.integervalue[i]=integervalue[i]+40-oper2.integervalue[i];
}
}
else
{borrow_value=0;
if(integervalue[i]-1>=oper2.integervalue[i])
temp_value.integervalue[i]=integervalue[i]-1-oper2.integervalue[i];
else
{borrow_value=1;
temp_value.integervalue[i]=integervalue[i]-1+40-oper2.integervalue[i];
}
}
temp_value.lenvalue=c;
return temp_value;
}
large_integers large_integers::operator*( large_integers oper2 )
{
large_integers temp_value;
int i,j,k,tmp_value,m1=0;
for (int i=0; i<oper2.lenvalue; i++)
{ k=i;
for (j=0; j< lenvalue; j++)
{
tmp_value = integervalue[ j ] * oper2.integervalue[i];
temp_value.integervalue[k] =temp_value.integervalue[k]+tmp_value;
temp_value.integervalue[k+1] =temp_value.integervalue[k+1]+ temp_value.integervalue[k]/10;
temp_value.integervalue[k] %=10;
k++;
if(k>m1)
m1=k;
}
}
temp_value.lenvalue=m1;
if(temp_value.lenvalue>DIGITS)
cout<<"**overflow**\n";
return temp_value;
}
using namespace std;
int main()
{
int c;
large_integers n1,n2,result;
n1.Inputvalue();
n2.Inputvalue();
n1.Outputvalue();
cout <<" + "; //for addition
n2.Outputvalue();
result=n1+n2;
cout<< " = " ;
result.Outputvalue();
cout << "\n\n";
n1.Outputvalue();
cout <<" - "; //for substraction
n2.Outputvalue();
result=n1-n2;
cout<< " = " ;
result.Outputvalue();
cout << "\n\n";
n1.Outputvalue();
cout <<" * "; //for multiplication
n2.Outputvalue();
result=n1*n2;
cout<< " = " ; //for comparing which one is less or equal or more than the other
result.Outputvalue();
cout << "\n\n";
c=n1==n2;
n1.Outputvalue();
switch (c)
{ case -1: cout<<" is less than ";
break;
case 0: cout<<" is equal to ";
break;
case 1: cout<<" is more than ";
break;
}
n2.Outputvalue();
cout << "\n\n";
}
OUTPUT :-
--------------------------------------------------Thank You --------------------------------------------------------------------------------------------