Write A C++ library integer.h that defines a numerical class implementing arbitr
ID: 3813769 • Letter: W
Question
Write A C++ library integer.h that defines a numerical class implementing arbitrary precision integers and operators?
NOTE:
- Your class should support creation from int or string.
- Any valid int literal or variable should be correctly converted into an Integer.
- Valid string literals or variables should be converted. The base of the string literal is always 10, and the value may have a leading - sign to indicate negative values.
NOTE:
Your class should overload the following operators:
- addition, multiplication, subtraction, division, remainder (+*-/%)
- stream insertion and extraction (<< and >>)
- increment, decrement (post- and pre-) (++,--)
- negation (unary -)
- equal, not equal, less than, greater than, less-than-or-equal-to,greater-than- or-equal-to (==,!=,<,>,<=,>=)
- compound operators: +=,-=,*=,/=,%=
THIS IS WHAT I HAVE SO FAR:
#ifndef INTEGER_H
#define INTEGER_H
#include
#include
using std::string;
using std::cout;
using std::cin;
// w7integer_basics.cpp
class Integer {
public:
Integer();
Integer(int val);
Integer(string s);
} ;
#endif
#include
#include "integer.h"
int main() {
Integer i("1234");
Integer j("-321");
Integer k;
return 0;
}
// w7integer_addition.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer i("1234");
Integer j(2847);
i + j;
j + j + j + j + j + j;
i + 14;
14 + j;
return 0;
}
// w7integer_output.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer i("1234");
Integer j(123);
Integer k("-1230001");
Integer z;
std::cout << i << ' ' << j << ' ' << k << ' ' << z << ' ';
return 0;
}
// w7integer_input.cpp
#include
using std::cin;
using std::cout;
#include "w7integer.h"
int main() {
Integer i;
Integer total;
while (cin >> i) {
cout << i << ' ';
total = total + i;
}
cout << total << ' ';
return 0;
}
// w7integer_subtract.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer i("1234");
Integer j(2847);
cout << i - j << ' ' << j - i << ' ';
i = 10000;
j = 9912;
cout << i - j << ' ' << j - i << ' ';
i = 0;
j = 1990;
cout << i - j << ' ' << j - i << ' ';
i = -689;
j = 1232;
cout << i - j << ' ' << j - i << ' ';
i = -689;
j = -1232;
cout << i - j << ' ' << j - i << ' ';
i = 689;
j = -1232;
cout << i - j << ' ' << j - i << ' ';
i = 689;
j = 1232;
cout << i - j << ' ' << j - i << ' ';
return 0;
}
// w7integer_increment.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer i("1234");
Integer j, k;
j = i++;
k = ++i;
cout << i << ' ' << j << ' ' << k << ' ';
i = string("1000000");
j = i--;
k = --i;
cout << i << ' ' << j << ' ' << k << ' ';
i = string("-1000000");
j = i--;
k = --i;
cout << i << ' ' << j << ' ' << k << ' ';
i = string("9999999999");
j = i++;
k = ++i;
cout << i << ' ' << j << ' ' << k << ' ';
return 0;
}
// w7integer_logic.cpp
#include
#include "w7integer.h"
using std::cout;
void compare(string i, string j) {
Integer ii(i);
Integer jj(j);
std::cout << ( (ii > jj) ? (i + ">" + j) : ("not " + i + ">" + j)) << ' ';
std::cout << ( (ii < jj) ? (i + "<" + j) : ("not " + i + "<" + j)) << ' ';
std::cout << ( (ii == jj) ? (i + "==" + j) : ("not " + i + "==" + j)) << ' ';
std::cout << ( (ii != jj) ? (i + "!=" + j) : ("not " + i + "!=" + j)) << ' ';
std::cout << ( (ii >= jj) ? (i + ">=" + j) : ("not " + i + ">=" + j)) << ' ';
std::cout << ( (ii <= jj) ? (i + "<=" + j) : ("not " + i + "<=" + j)) << ' ';
}
void testbool(string s) {
std::cout << ( Integer(s) ? s + " is true" : s + " is false") << ' ';
}
int main() {
string val1[] = {"6291021234", "100", "-100", "1099", "-200", "-400", "-301"};
string val2[] = {"6291021234", "99", "100", "911", "-100", "300", "-301"};
for (int i = 0; i < 7; i++)
compare(val1[i], val2[i]);
string s[] = {"2", "0", "-3", "100", "-0"};
for (int i = 0; i < 5; i++)
testbool(s[i]);
}
// w7integer_zero.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer z1("0000");
Integer z2("-0");
Integer large("1000000000000000");
std::cout << "zero: " << z1 << ' ';
std::cout << "zero: " << z2 << ' ';
std::cout << "zero: " << Integer(100) + Integer(-100) << ' ';
std::cout << "zero: " << Integer(100) - 100 << ' ';
std::cout << "zero: " << 1000 - Integer(1000) << ' ';
std::cout << "zero: " << large - large << ' ';
z1 = large - large;
std::cout << "zero: " << z1 << ' ';
std::cout << "zero: " << (Integer("1000000000000000") - Integer("1000000000000000")) << ' ';
z2 = Integer(300) - Integer(400) + Integer(100);
std::cout << "zero: " << z2 << ' ';
return 0;
}
// w7integer_multiply.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer i(12);
cout << i * 2 << ' ' << 3 * i << ' ' << i*i + 4 << ' ';
string val1[] = {"6291021234", "100", "-100", "1099", "-200", "-400", "-301", "12", "0", "-12"};
string val2[] = {"6291021234", "99", "100", "911", "-100", "300", "-301", "0", "122", "0"};
for (int i = 0; i < 10; i++) {
Integer ii(val1[i]);
Integer jj(val2[i]);
cout << ii*jj << ' ';
}
return 0;
}
// w7integer_divide.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer i("1234");
Integer j("12");
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = -1234;
j = -12;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = -1234;
j = 12;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = 1234;
j = -12;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = 200;
j = 25;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = 25;
j = 200;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
Integer k(12);
cout << k / 2 << ' ' << 30 / k << ' ' << 30 % k << ' ' << k % 7 << ' ';
return 0;
}
// w7integer_compound.cpp
#include
#include "w7integer.h"
using std::cout;
int main() {
Integer i(999);
Integer j(1002);
Integer k(100000);
i += Integer(2);
j *= Integer(3);
k -= Integer("122");
cout << i << ' ' << j << ' ' << k << ' ';
i /= Integer(2);
k = 18;
k /= Integer(6);
j %= Integer(6);
cout << i << ' ' << j << ' ' << k << ' ';
return 0;
}
// Combining these files:
#include
#include "w7integer.h"
using std::cout;
using std::cin;
void basics() {
Integer i("1234");
Integer j(-321);
Integer k;
}
void addition() {
Integer i("1234");
Integer j(2847);
i + j;
j + j + j + j + j + j;
i + 14;
14 + j;
}
void multiply() {
Integer i(12);
cout << i * 2 << ' ' << 3 * i << ' ' << i*i + 4 << ' ';
string val1[] = {"6291021234", "100", "-100", "1099", "-200", "-400", "-301", "12", "0", "-12"};
string val2[] = {"6291021234", "99", "100", "911", "-100", "300", "-301", "0", "122", "0"};
for (int i = 0; i < 10; i++) {
Integer ii(val1[i]);
Integer jj(val2[i]);
cout << ii*jj << ' ';
}
}
void output() {
Integer i("1234");
Integer j(123);
Integer k("-1230001");
Integer z;
std::cout << i << ' ' << j << ' ' << k << ' ' << z << ' ';
}
void compare(string i, string j) {
Integer ii(i);
Integer jj(j);
std::cout << ( (ii > jj) ? (i + ">" + j) : ("not " + i + ">" + j)) << ' ';
std::cout << ( (ii < jj) ? (i + "<" + j) : ("not " + i + "<" + j)) << ' ';
std::cout << ( (ii == jj) ? (i + "==" + j) : ("not " + i + "==" + j)) << ' ';
std::cout << ( (ii != jj) ? (i + "!=" + j) : ("not " + i + "!=" + j)) << ' ';
std::cout << ( (ii >= jj) ? (i + ">=" + j) : ("not " + i + ">=" + j)) << ' ';
std::cout << ( (ii <= jj) ? (i + "<=" + j) : ("not " + i + "<=" + j)) << ' ';
}
void testbool(string s) {
std::cout << ( Integer(s) ? s + " is true" : s + " is false") << ' ';
}
void logic() {
string val1[] = {"6291021234", "100", "-100", "1099", "-200", "-400", "-301"};
string val2[] = {"6291021234", "99", "100", "911", "-100", "300", "-301"};
for (int i = 0; i < 7; i++)
compare(val1[i], val2[i]);
string s[] = {"2", "0", "-3", "100", "-0"};
for (int i = 0; i < 5; i++)
testbool(s[i]);
}
void subtract() {
Integer i(1),j;
cout << i << ' ';
while (cin >> j){
i = i * j;
cout << i << ' ';
}
}
void limits() {
Integer i(1),j;
cout << i << ' ';
while (cin >> j){
i = i * j;
cout << i << ' ';
}
}
void input() {
Integer i;
Integer total;
while (cin >> i) {
cout << i << ' ';
total = total + i;
}
cout << total << ' ';
}
void increment() {
Integer i("1234");
Integer j, k;
j = i++;
k = ++i;
cout << i << ' ' << j << ' ' << k << ' ';
i = string("1000000");
j = i--;
k = --i;
cout << i << ' ' << j << ' ' << k << ' ';
i = string("-1000000");
j = i--;
k = --i;
cout << i << ' ' << j << ' ' << k << ' ';
i = string("9999999999");
j = i++;
k = ++i;
cout << i << ' ' << j << ' ' << k << ' ';
}
void compound() {
Integer i(999);
Integer j(1002);
Integer k(100000);
i += Integer(2);
j *= Integer(3);
k -= Integer("122");
cout << i << ' ' << j << ' ' << k << ' ';
i /= Integer(2);
k = 18;
k /= Integer(6);
j %= Integer(6);
cout << i << ' ' << j << ' ' << k << ' ';
}
void divide() {
Integer i("1234");
Integer j("12");
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = -1234;
j = -12;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = -1234;
j = 12;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = 1234;
j = -12;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = 200;
j = 25;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
i = 25;
j = 200;
cout << i << '/' << j << '=' << (i / j) << 'r' << i % j << '=' << (i / j)*j + i % j << ' ';
Integer k(12);
cout << k / 2 << ' ' << 30 / k << ' ' << 30 % k << ' ' << k % 7 << ' ';
}
void zero() {
Integer z1("0000");
Integer z2("-0");
Integer large("1000000000000000");
std::cout << "zero: " << z1 << ' ';
std::cout << "zero: " << z2 << ' ';
std::cout << "zero: " << Integer(100) + Integer(-100) << ' ';
std::cout << "zero: " << Integer(100) - 100 << ' ';
std::cout << "zero: " << 1000 - Integer(1000) << ' ';
std::cout << "zero: " << large - large << ' ';
z1 = large - large;
std::cout << "zero: " << z1 << ' ';
std::cout << "zero: " << (Integer("1000000000000000") - Integer("1000000000000000")) << ' ';
z2 = Integer(300) - Integer(400) + Integer(100);
std::cout << "zero: " << z2 << ' ';
}
int main() {
basics();
addition();
output();
input();
subtract();
increment();
logic();
zero();
multiply();
divide();
compound();
return 0;
}
Explanation / Answer
#include
#include "w7integer.h"
using std::cout;
int main()
cout << total << ' ';
return 0;
}
// w7integer_subtract.cpp
#include
#include "w7integer.h"
using std::cout;
int main() ;
string val2[] = ;
for (int i = 0; i < 7; i++)
compare(val1[i], val2[i]);
string s[] = ;
for (int i = 0; i < 5; i++)
testbool(s[i]);
}
// w7integer_zero.cpp
#include
#include "w7integer.h"
using std::cout;
int main() a pair of << ' ' << three * i << ' ' << i*i + four << ' ';
string val1[] = ;
string val2[] = ;
for (int i = 0; i < 10; i++)
return 0;
}
// w7integer_divide.cpp
#include
#include "w7integer.h"
using std::cout;
int main() > i) {
cout
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.