Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you explain this code?(Like leave a comment on each loop and statement) And this program should get input from the user in the form

Could you explain this code?(Like leave a comment on each loop and statement)

And this program should get input from the user in the form of two strings and use those strings to construct two BigInt's but this program doesn't get input from the user.

I think there's something wrong in the main function?

Please don't add any header file when editing the source code to let this program get input from the user.

-------------------------------------------------------------

#include

#include

using namespace std;

class BigInt {

private:

int arr[100];

public:

BigInt() { }

// this constructor should convert str into an array of ints,

// storing the values on the RIGHT SIDE of the array :)

BigInt(string str) {

int l=str.length()-1;

for(int i=0;i

arr[i]=str[l--]-'0';

arr[str.length()]=-1;//to mark end of number

}

// this constructor should save the num array into arr verbatim

BigInt(int num[100]) {

for(int i=0;i<100;i++)

{

if(num[i]==-1)break;

arr[i]=num[i];

}

}

// this function gets the digit at 'index' from arr

int get(int index) {

return arr[index];

}

// this function should add the argument "num"

// to the private array and return the sum

BigInt add(BigInt num) {

BigInt ans;

int len1=100,len2=100;

for(int j=0;j<100;j++)

if(num.arr[j]==-1)

{

len1=j;break;

}

for(int j=0;j<100;j++)

if(arr[j]==-1)

{

len2=j;break;

}

int i=0,carry=0;

while(i

{

ans.arr[i]=(num.arr[i]+arr[i]+carry)%10;

carry=(num.arr[i]+arr[i]+carry)/10;

i++;

}

while(i

{

ans.arr[i]=(num.arr[i]+carry)%10;

carry=(num.arr[i]+carry)/10;

i++;

}

while(i

{

ans.arr[i]=(arr[i]+carry)%10;

carry=(arr[i]+carry)/10;

i++;

}

if(carry)

ans.arr[i++]=carry;

ans.arr[i]=-1;//end of digits of ans

return ans;

}

// this function should subtract the argument "num"

// from the private array and return the difference

BigInt sub(BigInt num) {

BigInt ans;

int len=100,len2=100;

for(int i=0;i<100;i++)

if(num.arr[i]==-1)

{

len=i;break;

}

for(int i=0;i<100;i++)

if(arr[i]==-1)

{

len2=i;break;

}

int carry=0;

int i=0;

for( i = 0; i

{

int newDigit = arr[i] - num.arr[i];

newDigit += carry;

if (newDigit < 0) {

carry = -1;

newDigit += 10;

} else {

carry = 0;

}

ans.arr[i] = newDigit;

}

while(carry==-1)

{

int newDigit = arr[i] - 1;

if (newDigit < 0) {

carry = -1;

newDigit += 10;

} else {

carry = 0;

}

ans.arr[i] = newDigit;i++;

}

while(i

{

ans.arr[i]=arr[i];i++;

}

ans.arr[i]=-1;

return ans;

}

// this function will print the BigInt with NO leading zeroes

void print() {

int len=100;

for(int i=0;i<100;i++)

if(arr[i]==-1)

{

len=i;break;

}

for(int i=len-1;i>=0;i--)

cout<

cout<<' ';

}

};

int main(int argc, char* argv[])

{

BigInt a("1234"),b("14"),c;

a.print();b.print();

c=a.sub(b);

c.print();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions