added modulus and realized more operations are trivial

This commit is contained in:
Pagwin 2024-12-11 15:38:35 -05:00
parent da22a9e615
commit 3b3470ed35
No known key found for this signature in database
GPG key ID: 81137023740CA260

View file

@ -9,22 +9,6 @@
// "Use rvalue references to reuse the resources of temporaries when possible, // "Use rvalue references to reuse the resources of temporaries when possible,
// particularly for binary operands." // particularly for binary operands."
// [50 pts, 10 commits] Write a class named Integer that can hold
// arbitrary-length, signed integers. Use unsigned chars (i.e., bytes) for the
// internal storage. Put one decimal digit per byte. The ones digit should be
// first in the array, etc. You must use a pointer to unsigned char internally
// to point to the data. The internal array must grow as needed, but you do not
// need to shrink it. The constructor should take a single argument of a long
// type, used to initialize the number. Use a default argument so that a
// constructor with no arguments will initialize the integer to 0. Use rvalue
// references to reuse the resources of temporaries when possible, particularly
// for binary operands. Support the plus, minus, unary minus, multiplication,
// equality, greater/less, copy/move assignment/constructor Support outputting
// to a std::ostream via the << operator. Where it makes sense, allow operands
// to be of class Integer, or any integer type. In particular, you need to
// support operations like 1234 + Integer_obj. You should also support
// conversion of Integer to an unsigned long, so that operations like bitwise
// AND will work: Integer_object^123.
class Integer { class Integer {
using byte = unsigned char; using byte = unsigned char;
// bytes are litle endian // bytes are litle endian
@ -100,6 +84,12 @@ public:
} }
return lhs; return lhs;
} }
friend Integer &operator%=(Integer &lhs, Integer const &rhs) {
while (lhs > rhs) {
lhs -= rhs;
}
return lhs;
}
friend Integer operator+(Integer const &lhs, Integer const &rhs) { friend Integer operator+(Integer const &lhs, Integer const &rhs) {
Integer ret{lhs}; Integer ret{lhs};
ret += rhs; ret += rhs;
@ -120,23 +110,33 @@ public:
ret /= rhs; ret /= rhs;
return ret; return ret;
} }
friend Integer operator%(Integer const &lhs, Integer const &rhs) {
Integer ret{lhs};
ret %= rhs;
return ret;
}
friend Integer operator-(Integer const &val) { friend Integer operator-(Integer const &val) {
Integer ret = val; Integer ret = val;
ret.isNegative = !ret.isNegative; ret.isNegative = !ret.isNegative;
return ret; return ret;
} }
friend bool operator<(Integer const &lhs, Integer const &rhs) { friend bool operator<(Integer const &lhs, Integer const &rhs) {
todo("operator<"); Integer tmp = (lhs - rhs);
return tmp.isNegative && tmp != 0;
} }
friend bool operator>(Integer const &lhs, Integer const &rhs) { friend bool operator>(Integer const &lhs, Integer const &rhs) {
todo("operator>"); Integer tmp = (rhs - lhs);
return tmp.isNegative && tmp != 0;
} }
friend bool operator==(Integer const &lhs, Integer const &rhs) { friend bool operator==(Integer const &lhs, Integer const &rhs) {
todo("operator=="); todo("need to actually do a check for the case that one or both of them is "
"0");
return (rhs - lhs) == 0;
} }
friend std::ostream &operator<<(std::ostream &lhs, Integer const &rhs) { friend std::ostream &operator<<(std::ostream &lhs, Integer const &rhs) {
todo("ostream impl"); Integer discard = rhs;
return lhs; return lhs;
} }
explicit operator unsigned long() { explicit operator unsigned long() {