Question
I need to write a class for a big int that can hold large integers up to 20 digit numbers and perform calculations on them. The numbers can be positive or negative . I know I need to take them in as a string and then store them in my int array also checking to see if the number is positive or negative. I have very confused as to how to set up my constructors to take the input. I have my header file and the first few constructors, but not sure if I am going in the right direction. Any help with setting up the constructors would be greatly appreciated. I should be fine to do the rest of the code after the constructors are set up.
// my big.h file
#include<iostream>
using namespace std;
class big
{
private:
int sign; // 1 or -1
char array[41];
public:
big();
big(int other);
big(big& other);
void input();
friend big operator+ (const big& lhs, const big& rhs);
friend big operator- (const big& lhs, const big& rhs);
friend big operator* (const big& lhs, const big& rhs);
friend big operator/ (const big& lhs, const big& rhs);
friend big operator% (const big& lhs, const big& rhs);
friend int operator== (const big& lhs, const big& rhs);
friend int operator!= (const big& lhs, const big& rhs);
friend int operator<= (const big& lhs, const big& rhs);
friend int operator>= (const big& lhs, const big& rhs);
friend int operator< (const big& lhs, const big& rhs);
friend int operator> (const big& lhs, const big& rhs);
friend istream& operator>> (istream& lhs, big& rhs);
friend ostream& operator<< (ostream& lhs, const big& rhs);
big operator++(int); // post
big operator--(int); // post
big& operator++();
big& operator--();
};
// my big.cpp file
#include "big.h"
// initalize the array to 0
big::big()
{
for(int i = 0; i < 42; i++)
{
array[i] = 0;
}
sign = 1;
}
// check to see if positive or negative
big::big(int other)
{
if(array[0] = isnum(array[0]))
{
sign = 1;
}
else
sign = -1;
}
big::big(big& other)
{
}
// takes the input from the user and stores it into the array
void big::input()
{
char inputNum[21];
int i, j, k;
cin >> in;
for(i = 0; inputNum[i] != ''; i++);
k = 0;
for(j = i - 1; j >= 0; j--)
array[j] = inputNum[k++] - 48;
}
Explanation / Answer
header file: #ifndef BIG_H #define BIG_H #includeusing namespace std; class big { private: int *array; int sign; // 1 or -1 public: big(); big(int other); big(big& other); big(const char a[20], int s); //friend big operator+ (const big& lhs, const big& rhs); friend big operator- (const big& lhs, const big& rhs); /*friend big operator* (const big& lhs, const big& rhs); friend big operator/ (const big& lhs, const big& rhs); friend big operator% (const big& lhs, const big& rhs); */ friend big operator + (const big& lhs, const big& rhs); friend int operator== (const big& lhs, const big& rhs); friend int operator!= (const big& lhs, const big& rhs); friend int operator= (const big& lhs, const big& rhs); friend int operator< (const big& lhs, const big& rhs); friend int operator> (const big& lhs, const big& rhs); friend istream& operator>> (istream& lhs, big& rhs); friend ostream& operator 9) { array[i] -= 10; array[i + 1]++; } if(array[i] = 0) sign = 1; else sign = -1; } big::big(big& other) { int i; array = new int[20]; for(i = 0; i < 20; i++) { array[i] = other.array[i]; } sign = other.sign; } big::big(const char a[20], int s) { int i; for(i = 0; i > (istream& lhs, big& rhs) { char ival; int count; char holder[SIZE]; int done; int i; int spot; lhs >> ival; if(ival == '+') { rhs.sign = -1; lhs >> ival; } if(ival == '-') { rhs.sign = -1; lhs >> ival; } count = 0; done = 0; while(done == 0) { holder[count] = ival - '0'; count++; lhs.get(ival); if((ival < '0') || (ival > '9')) { done = 1; } if(count == SIZE) done = 1; } spot = 0; for(i = count - 1; i >= 0; i--) { rhs.array[spot] = holder[i]; spot++; } return lhs; } ostream& operator= 0)) { carry = 0; for(i = 0; i 9) { newbie.array[i] -= 10; carry = 1; } else carry = 0; } } return newbie; } big operator-(const big& lhs, const big& rhs) { big newbie; int i, carry; if((lhs < 0) && (rhs >= 0)) { newbie = operator-(lhs, rhs); } if((rhs < 0) && (lhs >= 0)) { newbie = operator+(lhs, 0-rhs); } if((rhs < 0) && (lhs < 0)) { newbie = operator-(0-lhs, 0-rhs); } if(lhs = 0) && (lhs >= 0)) { carry = 0; for(i=0; i 9) { newbie.array[i] -= 10; carry = 1; } else carry = 0; } } return newbie; } int operator==(const big& lhs, const big& rhs) { int i; if(lhs.sign != rhs.sign) return 1; for(i = 19; i >= 0; i--) { if(lhs.array[i] != rhs.array[i]) { return 1; } } return 0; } int operator!=(const big& lhs, const big& rhs) { return ~(lhs == rhs); } int operator 0 && rhs.sign < 0) { return (lhs > rhs); } for(i = 19; i >= 0; i--) { if((lhs.array[i] = 0; i--) { if((lhs.array[i] > rhs.array[i]) || (lhs.sign > rhs.sign)) { return 1; } } return 0; } int operator rhs); } /* big operator++(int) { } big operator--(int) { } */ #include "big.h" #include #include #include using namespace std; int main() { big a; big b; big c; cout > a; cout > b; errors removed that you mentioned so please rate me