Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write a program to perform addition and subtraction of numbers up to 100

ID: 3625308 • Letter: P

Question

Please write a program to perform addition and subtraction of numbers up to 100 digits long You can assume that the number being subtracted would always be smaller than the other number. I want you to create a class for the long integers called Biginteger with: a constructor that takes in an integer as a string Parses the string and stores the information in an appropriate ADT overload the + and - operators to perform the addition with another Biginteger a print() method to point the value of the Biginteger run at least ten test cases of addition and ten of subtraction to feel confident that your program is working fine provide documentation on how to compile and test the code, along with the code itself. and the test run output would be graded on aspects of software development including style, efficient use of memory robustness, and readability provides lots of comments

Explanation / Answer

please rate - thanks

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
#define DIGITS 100
class BigInteger
{
public:
BigInteger();
void Input();
void Output();
BigInteger operator+( BigInteger );
BigInteger operator-( BigInteger );

private:
int integer[ DIGITS ];
int len;
};
void BigInteger::Output()
{
int i;
for (i=len-1;i >= 0; i-- )
cout<<integer[i];
}
void BigInteger::Input()
{string in;
int i,j,k;
cout<<"Enter a number("<<DIGITS<<" digits max):";
cin>>in;
for(i=0;in[i]!='';i++);
len=i;
k=0;
for(j=i-1;j>=0;j--)
    integer[j]=in[k++]-48;
}
BigInteger::BigInteger( )
{
for ( int i = 0; i <DIGITS; i++ )
      integer[ i ] = 0;
len=DIGITS-1;
}

BigInteger BigInteger::operator+( BigInteger op2 )
{BigInteger temp;
int carry = 0;
int c,i;
if(len>op2.len)
    c=len;
else
    c=op2.len;

for ( i=0; i<c; i++ )
     {temp.integer[ i ] =integer[ i ] + op2.integer[ i ] + carry;
     if ( temp.integer[ i ] > 9 )
         {temp.integer[ i ] %= 10;
          carry = 1;
         }
     else
          carry = 0;
     }
if(carry==1)
    {temp.len=c+1;
    temp.integer[i]=carry;
    }
else    
   temp.len=c;
return temp;
}
BigInteger BigInteger::operator-( BigInteger op2 )
{BigInteger temp;
int c;
if(len>op2.len)
    c=len;
else
    c=op2.len;
int borrow = 0;
for( int i = c;i >= 0;i--)
if(borrow==0)
    {if(integer[i]>=op2.integer[i])
           temp.integer[i]=integer[i]-op2.integer[i];
    else
          {borrow=1;
          temp.integer[i]=integer[i]+10-op2.integer[i];
          }
    }
else
    {borrow=0;
    if(integer[i]-1>=op2.integer[i])
           temp.integer[i]=integer[i]-1-op2.integer[i];
      else
          {borrow=1;
          temp.integer[i]=integer[i]-1+10-op2.integer[i];
          }
      }
temp.len=c;
return temp;
}   
using namespace std;
int main()
{int c;
BigInteger n1,n2,result;
int choice;
char op[]={'+','-'};
do
{cout<<"enter 1 to add, 2 to subtract, 3 to exit: :";
cin>>choice;
if(choice==1||choice==2)
       {cout<<"enter 1st number: ";
        n1.Input();
        cout<<"enter 2nd number: ";
        n2.Input();
        n1.Output();
        cout <<" "<<op[choice-1]<<" ";
        n2.Output();
        cout<< " = " ;
        if(choice==1)
            result=n1+n2;
        else
            result=n1-n2;
        result.Output();
        cout << " ";
        }
}while(choice!=3);            

}