In: Computer Science
I have a BigInt class I created that works correctly. I would like to add a constructor
BigInt(string s, int base)
that creates a BigInt operating in the given base. The newly extended class should work with any base between 2 and 36, using the digits 0, 1, …, 9, A, B, C, …, Z. How do I do this from what I have? (you may add new data members and such of course)
#include <string>
#include <vector>
class BigInt {
private:
std::string m_String = "";
public:
BigInt(std::string s) // convert string to
BigInt
{
for(int i = s.length() - 1; i >=
0; i--) {
m_BigInt.push_back((int)s[i] - 48);
}
}
std::string to_string() // get string
representation
{
m_String = ""; // clear before
print
for (auto it = m_BigInt.rbegin();
it != m_BigInt.rend(); it++)
{
m_String +=
std::to_string(*it);
}
return m_String;
}
void add(BigInt b) // add another BigInt to this one
with carry function
{
for (int digit = 0; digit !=
b.getVect().size(); digit++)
{
int temp = m_BigInt[digit] +
b.getVect()[digit];
if(temp >=
10) {
if(digit + 1 !=
m_BigInt.size()) {
m_BigInt[digit] = temp % 10;
m_BigInt[digit + 1] = m_BigInt[digit + 1] + temp / 10;
} else {
m_BigInt[digit] = temp % 10;
m_BigInt.push_back(temp / 10);
}
} else
m_BigInt[digit] =
temp;
}
}
std::vector<int> getVect() // get BigInt
vector
{
return m_BigInt;
}
};
Program Code Screenshot
Program Sample Input/Output Screenshot
Program Code to Copy
#include <string>
#include <vector>
#include <iostream>
class BigInt
{
private:
std::string m_String = "";
std::vector<int> m_BigInt;
int base;
public:
BigInt(std::string s) // convert string to BigInt
{
for (int i = s.length() - 1; i >= 0; i--)
{
m_BigInt.push_back((int)s[i] - 48);
}
base = 10;
}
BigInt(std::string s, int base)
{
for (int i = s.length() - 1; i >= 0; i--)
{
// handle base above 10
if (s[i] >= 'A' && s[i] <= 'Z')
{
m_BigInt.push_back(s[i] - 'A' + 10); //character A is 10
}
else
{
m_BigInt.push_back((int)s[i] - 48);
}
}
this->base = base;
}
std::string to_string() // get string representation
{
m_String = ""; // clear before print
for (auto it = m_BigInt.rbegin(); it != m_BigInt.rend(); it++)
{
// handle base above 10
if (*it >= 10)
{
m_String += char(*it - 10 + 'A');
}
else
{
m_String += std::to_string(*it);
}
}
return m_String;
}
void add(BigInt b) // add another BigInt to this one with carry function
{
for (int digit = 0; digit != b.getVect().size(); digit++)
{
int temp = m_BigInt[digit] + b.getVect()[digit];
if (temp >= base)
{
if (digit + 1 != m_BigInt.size())
{
m_BigInt[digit] = temp % base;
m_BigInt[digit + 1] = m_BigInt[digit + 1] + temp / base;
}
else
{
m_BigInt[digit] = temp % base;
m_BigInt.push_back(temp / base);
}
}
else
m_BigInt[digit] = temp;
}
}
std::vector<int> getVect() // get BigInt vector
{
return m_BigInt;
}
};
// demo for changes
int main()
{
BigInt a1("1101", 2);
BigInt b1("1101", 2);
a1.add(b1);
std::cout <<"1101 (base 2) + 1111 (base 2) = "<< a1.to_string() << std::endl;
BigInt a2("A20", 16);
BigInt b2("13F", 16);
a2.add(b2);
std::cout <<"A20 (base 16) + 13F (base 16) = "<< a2.to_string() << std::endl;
}