cs440-final/integer.cpp
2024-12-11 12:40:47 -05:00

102 lines
3.6 KiB
C++

#include <cassert>
#include <cstddef>
#include <iostream>
#define todo(msg) \
std::cerr << msg << std::endl; \
assert(false);
// TODO: try to maximize rvalue usage to reuse resources
// "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 {
unsigned char *bytes;
std::size_t size;
public:
explicit Integer(long num = 0) {}
Integer(Integer const &rhs) { todo("copy constructor"); }
Integer(Integer &&rhs) { todo("move constructor"); }
Integer &operator=(Integer const &rhs) {
todo("copy assignment");
return *this;
}
Integer &operator=(Integer &&rhs) {
todo("copy assignment");
return *this;
}
friend Integer &operator+=(Integer &lhs, Integer const &rhs) {
todo("+=");
return lhs;
}
friend Integer &operator-=(Integer &lhs, Integer const &rhs) {
todo("-=");
return lhs;
}
friend Integer &operator*=(Integer &lhs, Integer const &rhs) {
todo("*=");
return lhs;
}
friend Integer &operator/=(Integer &lhs, Integer const &rhs) {
todo("+=");
return lhs;
}
friend Integer operator+(Integer const &lhs, Integer const &rhs) {
Integer ret{lhs};
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 &lhs, Integer const &rhs) {
Integer ret{lhs};
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) { todo("unary minus"); }
friend bool operator<(Integer const &lhs, Integer const &rhs) {}
friend bool operator>(Integer const &lhs, Integer const &rhs) {}
friend bool operator==(Integer const &lhs, Integer const &rhs) {}
friend std::ostream &operator<<(std::ostream &lhs, Integer const &rhs) {
todo("ostream impl");
return lhs;
}
explicit operator unsigned long() {
// TODO: check for big or little endianess of unsigned long or see if
// there's an stdlib function that does what we need for us
todo("Convert from Integer to unsigned long");
}
};
int main(void) {
for (std::size_t i = 0; i <= 8584148901; i++) {
unsigned long n = i * i;
n = i + i;
n = i - i;
n = i / i;
}
}