I’m stuck on this error. I’m wondering if I can get a second set of eyes.TIA
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <limits>
#include <climits>
using namespace std;
class BigInt {
private:
vector<char> v;
BigInt fiboHelper(BigInt n, BigInt a = 0, BigInt b = 1) {
if (n.size() == 0)
return a;
else if (n.size() == 1)
return b;
else
return fiboHelper(n - 1, b, a + b);
}
public:
BigInt() {}
BigInt(int num) {
while (num > 0) {
v.insert(v.begin(), num % 10);
num /= 10;
}
}
BigInt(string num) {
for (char digit : num) {
if (isdigit(digit))
v.push_back(digit - '0');
}
}
BigInt operator+(const BigInt& bigInt) const{
BigInt result;
int carry = 0;
int i = v.size() - 1;
int j = bigInt.v.size() - 1;
while (i >= 0 || j >= 0 || carry) {
int sum = carry;
if (i >= 0) sum += v[i--];
if (j >= 0) sum += bigInt.v[j--];
result.v.insert(result.v.begin(), sum % 10);
carry = sum / 10;
}
return result;
}
BigInt operator+(int num) {
return *this + BigInt(num);
}
BigInt operator-(BigInt other) {
BigInt result;
int borrow = 0;
int i = v.size() - 1;
int j = other.v.size() - 1;
while (i >= 0 || j >= 0) {
int diff = borrow;
if (i >= 0) diff += v[i--];
if (j >= 0) diff -= other.v[j--];
if (diff < 0) {
diff += 10;
borrow = -1;
} else {
borrow = 0;
}
result.v.insert(result.v.begin(), diff % 10);
}
while (!result.v.empty() && result.v.front() == 0)
result.v.erase(result.v.begin());
return result;
}
bool operator>=(const BigInt& other) const {
if (v.size() != other.v.size())
return v.size() > other.v.size();
for (int i = 0; i < v.size(); ++i) {
if (v[i] != other.v[i])
return v[i] > other.v[i];
}
return true;
}
bool operator==(const BigInt& other) const {
if (v.size() != other.v.size())
return false;
for (int i = 0; i < v.size(); ++i) {
if (v[i] != other.v[i])
return false;
}
return true;
}
BigInt operator*(BigInt other) {
BigInt result;
result.v.resize(v.size() + other.v.size(), 0);
for (int i = v.size() - 1; i >= 0; --i) {
int carry = 0;
for (int j = other.v.size() - 1; j >= 0; --j) {
int product = v[i] * other.v[j] + result.v[i + j + 1] + carry;
result.v[i + j + 1] = product % 10;
carry = product / 10;
}
result.v[i] += carry;
}
while (!result.v.empty() && result.v.front() == 0)
result.v.erase(result.v.begin());
return result;
}
BigInt operator/(BigInt other) {
BigInt quotient;
BigInt remainder = *this;
BigInt divisor = other;
BigInt divisorShifted;
BigInt subtrahend;
BigInt temp;
while (remainder >= divisor) {
int shift = remainder.size() - divisor.size();
divisorShifted = divisor;
divisorShifted = divisorShifted.shiftLeft(shift);
temp = 1;
temp = temp.shiftLeft(shift);
while (remainder >= divisorShifted) {
remainder = remainder - divisorShifted;
quotient = quotient + temp;
}
}
return quotient;
}
BigInt operator%(BigInt other) {
BigInt quotient;
BigInt remainder = *this;
BigInt divisor = other;
BigInt divisorShifted;
BigInt subtrahend;
BigInt temp;
while (remainder >= divisor) {
int shift = remainder.size() - divisor.size();
divisorShifted = divisor;
divisorShifted = divisorShifted.shiftLeft(shift);
temp = 1;
temp = temp.shiftLeft(shift);
while (remainder >= divisorShifted) {
remainder = remainder - divisorShifted;
quotient = quotient + temp;
}
}
return remainder;
}
BigInt operator++(int) {
*this = *this + 1;
return *this - 1;
}
BigInt operator++() {
*this = *this + 1;
return *this;
}
BigInt operator[](int index) {
if (index >= 0 && index < v.size()) {
return v[index];
} else {
return BigInt();
}
}
void print() {
for (char digit : v)
cout << static_cast<int>(digit);
}
int size() {
return v.size();
}
BigInt fibo() {
return fiboHelper(*this);
}
bool operator>(const BigInt& other) const {
if (v.size() != other.v.size())
return v.size() > other.v.size();
for (int i = 0; i < v.size(); ++i) {
if (v[i] != other.v[i])
return v[i] > other.v[i];
}
return false;
}
BigInt fact() {
BigInt result = 1;
BigInt current = *this;
while (current > 1) {
result = result * current;
current = current - 1;
}
return result;
}
friend ostream& operator<<(ostream& os, const BigInt& num) {
if (num.v.size() <= 12) {
for (char digit : num.v)
os << static_cast<int>(digit);
} else {
os << fixed << setprecision(7) << num.toExponential();
}
return os;
}
BigInt shiftLeft(int n) const {
BigInt result = *this;
for (int i = 0; i < n; ++i)
result.v.push_back(0);
return result;
}
private:
string toExponential() const {
string str;
str.push_back(v[0] + '0');
str.push_back('.');
for (size_t i = 1; i < 7 && i < v.size(); ++i)
str.push_back(v[i] + '0');
str.push_back('e');
str += to_string(v.size() - 1);
return str;
}
};
void testUnit()
{
int space = 10;
cout << "anTestUnit:n"<<flush;
system("whoami");
system("date");
BigInt n1(25);
BigInt s1("25");
BigInt n2(1234);
BigInt s2("1234");
BigInt n3(n2);
BigInt fibo(12345);
BigInt fact(50);
BigInt imax = INT_MAX;
BigInt big("9223372036854775807");
cout << "n1(int) :"<<setw(space)<<n1<<endl;
cout << "s1(str) :"<<setw(space)<<s1<<endl;
cout << "n2(int) :"<<setw(space)<<n2<<endl;
cout << "s2(str) :"<<setw(space)<<s2<<endl;
cout << "n3(n2) :"<<setw(space)<<n3<<endl;
cout << "fibo(12345):"<<setw(space)<<fibo<<endl;
cout << "fact(50) :"<<setw(space)<<fact<<endl;
cout << "imax :"<<setw(space)<<imax<<endl;
cout << "big :"<<setw(space)<<big<<endl;
cout << "big.print(): "; big.print(); cout << endl;
cout << n2 << "/"<< n1<< " = "<< n2/n1 <<" rem "<<n2%n1<<endl;
cout << "fibo("<<fibo<<") = "<<fibo.fibo() << endl;
cout << "fact("<<fact<<") = "<<fact.fact() << endl;
cout << "10 + n1 = " << 10 + n1 << endl;
cout << "n1 + 10 = " << n1 + 10 << endl;
cout << "(n1 == s1)? --> "<<((n1 == s1)?"true":"false")<<endl;
cout << "n1++ = ? --> before:"<<n1++<<" after:"<<n1<<endl;
cout << "++s1 = ? --> before:"<<++s1<<" after:"<<s1<<endl;
cout << "s2 * big = ? --> "<< s2 * big<<endl;
cout << "big * s2 = ? --> "<< big * s2<<endl;
}
int main() {
testUnit();
return 0;
}
I tried to run the code but I keep getting this error. I attempeted to change the BigInt operator+. I am also unable to change the testUnit function.(https://i.sstatic.net/yrmptM90.jpg) The goal is to overload the different operators and print the results as shown in the testUnit function.
New contributor
GraveGhost1 GamingVlogs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.