comparison operator functions

 avatar
unknown
c_cpp
17 days ago
3.2 kB
4
Indexable
bool bigNum::operator<(const bigNum &rhs) const {
    if (this->sign && !rhs.sign) {
        return false;
    } else if (!this->sign && rhs.sign) {
        return true;
    } else if (this->digits < rhs.digits) {
        return true;
    } else if (this->digits > rhs.digits) {
        return false;
    } else {
        if (this->sign == rhs.sign) {
            if (this->sign == false) {
                int i = 0;
                while (i != this->digits) {
                    if (this->number[i] > rhs.number[i]) {
                        return false;
                    }
                    if (this->number[i] < rhs.number[i]) {
                        return true;
                    }
                    i++;
                }
            } else {
                int i = 0;
                while (i != this->digits) {
                    if (this->number[i] > rhs.number[i]) {
                        return true;
                    }
                    if (this->number[i] < rhs.number[i]) {
                        return false;
                    }
                    i++;
                }
            }
        }
        return false;
    }
}

bool bigNum::operator<(long long rhs) const {
    //call constructor and put rhs into it
    bigNum num(rhs);
    return *this < num;
}

bool bigNum::operator>(const bigNum &rhs) const {
    if (!this->sign && rhs.sign) {
        return false;
    } else if (this->sign && !rhs.sign) {
        return true;
    } else if (this->digits < rhs.digits) {
        return false;
    } else if (this->digits > rhs.digits) {
        return true;
    } else {
        if (this->sign == rhs.sign) {
            if (this->sign == false) {
                int i = 0;
                while (i != this->digits) {
                    if (this->number[i] > rhs.number[i]) {
                        return true;
                    }
                    if (this->number[i] < rhs.number[i]) {
                        return false;
                    }
                    i++;
                }
            } else {
                int i = 0;
                while (i != this->digits) {
                    if (this->number[i] > rhs.number[i]) {
                        return true;
                    }
                    if (this->number[i] < rhs.number[i]) {
                        return false;
                    }
                    i++;
                }
            }
            return false;
        }
    }
    return false;
}

bool bigNum::operator>(long long rhs) const {
    bigNum num(rhs);
    return *this > num;
}

bool bigNum::operator==(const bigNum &rhs) const {
    if (!this->sign && rhs.sign) {
        return false;
    } else if (this->sign && !rhs.sign) {
        return false;
    } else if (this->digits != rhs.digits) {
        return false;
    } else if (this->sign == rhs.sign) {
        int i = 0;
        while (i <= this->digits) {
            if (this->number[i] != rhs.number[i]) {
                return false;
            }
            i++;
        }
        return true;
    }
    return false;
}

bool bigNum::operator==(long long rhs) const {
    bigNum num(rhs);
    return *this == num;
}
Leave a Comment