Could you complete this without adding any header file? ------------------------
ID: 3591280 • Letter: C
Question
Could you complete this without adding any header file?
-------------------------------------------------------------------------------
You'll be working with big numbers. These numbers are too big to fit into an int type, or even into a long long! We'll be working with integers that are up to 100 digits in length. To store these, it will take a special class with some interesting methods. Here is an outline of the BigInt class you should use:
#include <iostream>
#include <string>
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) {
}
// this constructor should save the num array into arr verbatim
BigInt(int num[100]) {
}
// this function gets the digit at 'index' from arr
int get(int index) {
}
// this function should add the argument "num"
// to the private array and return the sum
BigInt add(BigInt num) {
}
// this function should subtract the argument "num"
// from the private array and return the difference
BigInt sub(BigInt num) {
}
// this function will print the BigInt with NO leading zeroes
void print() {
}
};
In main, please get input from the user in the form of two strings and use those strings to construct two BigInt's. You may assume that the first input is larger than the second (so that you don't have to worry about negative numbers from subtraction). Next, echo the numbers that you input, so that you know you did the input correctly. Then, output the sum and difference of these two numbers. You will need to use the other constructor and 'get' function along the way. This is more challenging than it may first appear.
Explanation / Answer
#include <iostream>
#include <string>
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<str.length();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<min(len1,len2))
{
ans.arr[i]=(num.arr[i]+arr[i]+carry)%10;
carry=(num.arr[i]+arr[i]+carry)/10;
i++;
}
while(i<len1)
{
ans.arr[i]=(num.arr[i]+carry)%10;
carry=(num.arr[i]+carry)/10;
i++;
}
while(i<len2)
{
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 <len; 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<len2)
{
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<<arr[i];
cout<<' ';
}
};
int main(int argc, char* argv[])
{
string n1,n2;
cin>>n1;//Input number1
cin>>n2; //input number2
BigInt a(n1),b(n2),c;
a.print();b.print();
c=a.sub(b);
c.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.