implemented all the trivial operations for integer.cpp

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

View file

@ -1,3 +1,4 @@
#include <algorithm>
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
#include <iostream> #include <iostream>
@ -25,19 +26,51 @@
// conversion of Integer to an unsigned long, so that operations like bitwise // conversion of Integer to an unsigned long, so that operations like bitwise
// AND will work: Integer_object^123. // AND will work: Integer_object^123.
class Integer { class Integer {
unsigned char *bytes; using byte = unsigned char;
// bytes are litle endian
byte *bytes;
bool isNegative;
std::size_t size; std::size_t size;
public: public:
explicit Integer(long num = 0) {} Integer(long long num = 0)
Integer(Integer const &rhs) { todo("copy constructor"); } : bytes{new byte[sizeof(long long)]}, isNegative{num < 0},
Integer(Integer &&rhs) { todo("move constructor"); } size{sizeof(long long)} {
for (std::size_t i = 0; i < size; i++) {
bytes[i] = num % 256;
num /= 256;
}
}
Integer(Integer const &rhs)
: bytes{new byte[rhs.size]}, isNegative{rhs.isNegative}, size{rhs.size} {
for (std::size_t i = 0; i < size; i++) {
bytes[i] = rhs.bytes[i];
}
}
Integer(Integer &&rhs)
: bytes{rhs.bytes}, isNegative{rhs.isNegative}, size{rhs.size} {
rhs.bytes = nullptr;
}
~Integer() {
delete[] bytes;
bytes = nullptr;
}
Integer &operator=(Integer const &rhs) { Integer &operator=(Integer const &rhs) {
todo("copy assignment"); delete[] bytes;
bytes = new byte[rhs.size];
size = rhs.size;
isNegative = rhs.isNegative;
for (std::size_t i = 0; i < size; i++) {
bytes[i] = rhs.bytes[i];
}
return *this; return *this;
} }
Integer &operator=(Integer &&rhs) { Integer &operator=(Integer &&rhs) {
todo("copy assignment"); bytes = rhs.bytes;
rhs.bytes = nullptr;
size = rhs.size;
isNegative = rhs.isNegative;
return *this; return *this;
} }
friend Integer &operator+=(Integer &lhs, Integer const &rhs) { friend Integer &operator+=(Integer &lhs, Integer const &rhs) {
@ -49,11 +82,22 @@ public:
return lhs; return lhs;
} }
friend Integer &operator*=(Integer &lhs, Integer const &rhs) { friend Integer &operator*=(Integer &lhs, Integer const &rhs) {
todo("*="); Integer tmp = std::move(lhs);
Integer tmp_r = rhs;
lhs = 0;
while (tmp_r > 0) {
lhs += tmp;
tmp_r -= 1;
}
return lhs; return lhs;
} }
friend Integer &operator/=(Integer &lhs, Integer const &rhs) { friend Integer &operator/=(Integer &lhs, Integer const &rhs) {
todo("+="); Integer tmp = std::move(lhs);
lhs = 0;
while (tmp > rhs) {
tmp -= rhs;
lhs += 1;
}
return lhs; return lhs;
} }
friend Integer operator+(Integer const &lhs, Integer const &rhs) { friend Integer operator+(Integer const &lhs, Integer const &rhs) {
@ -76,19 +120,31 @@ public:
ret /= rhs; ret /= rhs;
return ret; return ret;
} }
friend Integer operator-(Integer const &val) { todo("unary minus"); } friend Integer operator-(Integer const &val) {
friend bool operator<(Integer const &lhs, Integer const &rhs) {} Integer ret = val;
friend bool operator>(Integer const &lhs, Integer const &rhs) {} ret.isNegative = !ret.isNegative;
friend bool operator==(Integer const &lhs, Integer const &rhs) {} return ret;
}
friend bool operator<(Integer const &lhs, Integer const &rhs) {
todo("operator<");
}
friend bool operator>(Integer const &lhs, Integer const &rhs) {
todo("operator>");
}
friend bool operator==(Integer const &lhs, Integer const &rhs) {
todo("operator==");
}
friend std::ostream &operator<<(std::ostream &lhs, Integer const &rhs) { friend std::ostream &operator<<(std::ostream &lhs, Integer const &rhs) {
todo("ostream impl"); todo("ostream impl");
return lhs; return lhs;
} }
explicit operator unsigned long() { explicit operator unsigned long() {
// TODO: check for big or little endianess of unsigned long or see if unsigned long ret = 0;
// there's an stdlib function that does what we need for us for (std::size_t i = 0; i < std::min(size, sizeof(unsigned long)); i++) {
todo("Convert from Integer to unsigned long"); ret += (static_cast<unsigned long>(this->bytes[i]) << (i * 8));
}
return ret;
} }
}; };