Should have a header (BigInt.h) and a source file (BigInt.cpp) Write a class cal
ID: 3595706 • Letter: S
Question
Should have a header (BigInt.h) and a source file (BigInt.cpp)
Write a class called Biglnt. The Bigint class is going to be able to hold and perform arithmetic on integers of arbitrary size (numbers with thousands, millions, or billions of digits). For this daily you are only doing a small portion of this class. Yoss must contain a string internally to store the number since the Bigint can have millions of digits in it. You must also have a private member variable of type bool that remembers if the number is positive or negative. class BigInt public: BigInt) I/ Initializes the BigInt to zero BigInt(int x); //Initializes the BigInt to have the same value as x explicit BigInt(string x)://Initalizes the BigInt to have the value of the given string BUT //must check that the string is valid or exit (1) otherwise friend ostream& operatorExplanation / Answer
//BigInt.h
#include<iostream>
#include<string>
using namespace std;
class BigInt
{
public:
BigInt(); //Initializes the BigInt to zero
BigInt(int x); //Initializes the BigInt to have the same value as x
explicit BigInt(string x);
friend ostream& operator<<(ostream& out, const BigInt& right);
private:
string data;
bool isNegative;
};
--------------------------------------------------------------------------------------------------
//BigInt.cpp
#include"BigInt.h"
BigInt::BigInt()
{
data = "";
isNegative = false;
}
BigInt::BigInt(int x)
{
int rem,i=0;
char tmp[10];
if (x < 0)
isNegative = true;
else
isNegative = false;
do
{
rem = x % 10;
tmp[i++] = rem;
x /= 10;
} while (x > 0);
tmp[i] = '';
for (int j = i-1; j >=0; j--)
data.push_back(tmp[j]+ 48);
}
BigInt::BigInt(string x)
{
for (int i = 0; i < x.length(); i++)
{
if (x[i] != 32) //space ignore
{
if (x[i] == '+')
isNegative = false;
if (x[i] == '-')
isNegative = true;
if (x[i] == '+' || x[i] == '-')
{
//only allowed character before +/- is space
if (i != 0 )
{
if (x[i - 1] == 32)
{
if (isdigit(x[i + 1])) //if it starts with digits after sign digit then copy the digits to data
{
data.push_back(x[i + 1]);
}
}
else
cout << "Invalid number" << endl;
}
else
{
if (x[i] != 32 && isdigit(x[i + 1]) && isdigit(x[i - 1]))
{
data.push_back(x[i + 1]);
}
else
{
cout << "invalid characters in string" << endl;
break;
}
}
}
else
{
if (x[i] != 32 && isdigit(x[i + 1]) )
{
data.push_back(x[i + 1]);
}
else
{
break;
}
}
}
}
}
ostream& operator<<(ostream& out, const BigInt& right)
{
if (right.data != "")
{
if (right.isNegative)
out << "value negative ";
else
out << "value positive ";
out << right.data << endl;
}
return out;
}
---------------------------------------------------------------------------------------
//main.cpp
#include"BigInt.h"
#include<iostream>
using namespace std;
int main()
{
BigInt x;
cout << x << endl;
x = BigInt(42);
cout << x << endl;
x = BigInt(" -12345678901234567890 and more stuff");
cout << x << endl;
x = BigInt("+.48899890890890");
cout << x << endl;
x = BigInt(" +123 +123 + 123 123123123");
cout << x << endl;
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------
/*output
value positive 42
value negative 12345678901234567890
invalid characters in string
value positive 123*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.