From 3b3470ed35e74d0a66eae234e90828ee0e37d3b2 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Wed, 11 Dec 2024 15:38:35 -0500 Subject: [PATCH] added modulus and realized more operations are trivial --- integer.cpp | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/integer.cpp b/integer.cpp index 74fe8a0..7408694 100644 --- a/integer.cpp +++ b/integer.cpp @@ -9,22 +9,6 @@ // "Use rvalue references to reuse the resources of temporaries when possible, // 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 { using byte = unsigned char; // bytes are litle endian @@ -100,6 +84,12 @@ public: } 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) { Integer ret{lhs}; ret += rhs; @@ -120,23 +110,33 @@ public: ret /= rhs; return ret; } + friend Integer operator%(Integer const &lhs, Integer const &rhs) { + Integer ret{lhs}; + ret %= rhs; + return ret; + } friend Integer operator-(Integer const &val) { Integer ret = val; ret.isNegative = !ret.isNegative; return ret; } 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) { - todo("operator>"); + Integer tmp = (rhs - lhs); + return tmp.isNegative && tmp != 0; } 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) { - todo("ostream impl"); + Integer discard = rhs; + return lhs; } explicit operator unsigned long() {