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

There is one problems, which you have to solve and submit the solution as separa

ID: 3829110 • Letter: T

Question

There is one problems, which you have to solve and submit the solution as separate C++ source code, header and driver program source files, a hpp file containing class, a separate file for function and a main file. This problem asks you to create a custom data type to handle large integer arithmetic, by creating a custom C++ class called BigInt, C++ has a number of types to handle integer data, including ints, short ints and long ints, and their signed and unsigned variants. However, the maximum positive number that one can store in the largest of these, which is an unsigned long int, is 18416, 744.073 709, 551, 615. This number, as you can The program will run as follows: see, has 20 digits. In many scientific and engineering applications, this does not suffice 1. Ask the user to input two BigInts. to hold values required for complex computations. Anything about 1020 would be too big 2. Output the results of addition, subtraction, and multiplication of these two to hold in the unsigned long int type. So your task in this assignment is to create a C- 3. Then, ask the user to input a BigInt and an integers class called BigInt which will allow basic arithmetic operations on positive integer 4. Output the results of addition, subtraction, and multiplication of the BigInt and int values of up to 300 digits, storing integers from 0 to 9999. 9 (300 9s). The three operations you have to do are addition, subtraction and multiplication. Specific tasks are

Explanation / Answer

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stdio.h>
using namespace std;


class BigInt
{
private:
         vector<int> digits;
public:
         BigInt()
         {}
         BigInt(string num);

     friend ostream& operator<<(ostream &out,BigInt& b)
    {
       for(auto it=b.digits.crbegin();it!=b.digits.crend();it++)
         out<<*it;
       out<<endl;
       return out;
     }

   friend istream& operator>>(istream &in,BigInt& b)
   {
  
            string line;
            int n;
            getline(cin,line);
            for(int i=0;i<line.length();i++)
            {
               n=line[i]-'0';
               b.digits.push_back(n);
            }
            reverse(b.digits.begin(),b.digits.end());
   }

        BigInt operator+(BigInt& b);
        BigInt operator+(int& a) ;
        BigInt operator-(BigInt& a);
        BigInt operator-(int& a) ;
        BigInt operator*(BigInt& a);
        BigInt operator*(int& a) ;
};

BigInt::BigInt(string num)
{
   int n;

   for(int i=num.length()-1;i>=0;i--)
   {
     n=num[i]-'0';
     digits.push_back(n);
   }

}       


BigInt BigInt::operator+(BigInt& b)
{
   if(digits.empty())
     return b;
   if(b.digits.empty())
     return *this;
   BigInt c;
   int num, carry = 0;

// index 0 contains least significant digit.

   for(unsigned int i=0; i<this->digits.size()&& i<b.digits.size();++i)
{
        num=carry + this->digits[i] + b.digits[i];
          if(num >= 10)
         {
           num = num - 10;
           carry = 1;
   }
         c.digits.push_back(num);
      
}
    if (carry)
       c.digits.push_back(carry);
  
    return c;
}
  
BigInt BigInt::operator+(int& a)
{
BigInt c;
if(a==0)
   return *this;
if(digits.empty())
   return c;


int carry=0;
int n;

n=carry+digits[0]+a;
if(n >= 10)
    {
           n = n - 10;
           carry = 1;
       
    }
c.digits.push_back(n);
for(unsigned int i=1; i<this->digits.size();i++)
{
    n=carry+digits[i];
    if(n >= 10)
    {
           n = n - 10;
           carry = 1;
       
    }
      c.digits.push_back(n);
}

if (carry)
       c.digits.push_back(carry);
return c;
}

int main()
{
BigInt a,b,c;
int num;
cout<<"Enter the first number: ";
cin>>a;
cout<<"Enter the second number: ";
cin>>b;
cout<<"Enter the small integer: ";
cin>>num;

c=a+b;
cout<<"After addition: ";
cout<<c<<endl;

c=a+num;
cout<<"After addition with small integer : ";
cout<<c<<endl;

/* c=a-b;
cout<<"After subtraction: ";
cout<<c<<endl;

c=a-num;
cout<<"After subtraction with small integer : ";
cout<<c<<endl;

   c=a*b;
cout<<"After multiplication: ";
cout<<c<<endl;

c=a*num;
cout<<"After multiplication with small integer : ";
cout<<c<<endl; */

return 0;
}       

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote