Beispiel #1
0
//string
big_integer::big_integer(std::string const& str) {
    if (str == "" || str == "0") {
        *this = big_integer();
    }
    else {
        std::string temp = str;
        int cut = 0;
        if (temp.at(0) == '-')
            cut = 1;
        while (temp.at(cut) == '0' && cut < temp.size() - 1)
            temp.erase(cut, cut);
        if (temp == "0" || temp == "-0" || temp == "") {
            *this = big_integer();
            return;
        }
        std::reverse(temp.begin(), temp.end());
        for (int i = 0; i < temp.size() - 1; ++i) {
            if (!isdigit(temp[i])) {
                throw parse_exception("Illegal character");
            }
            this->digits.push_back(temp[i] - 48);
        }
        if (temp[temp.size() - 1] == '-') {
            sign = -1;
        }
        else {
            sign = 1;
            this->digits.push_back(temp[temp.size() - 1] - 48);
        }
    }
};
Beispiel #2
0
big_integer& big_integer::operator/=(big_integer const& rhs)
{
    if (*this < rhs)
    {
        *this = big_integer(0);
        return *this;
    }
    big_integer m(1);
    big_integer a(-1), b(*this + 1);
    while (b - a > big_integer(1))
    {
        m = div2(b + a + big_integer(1));
        big_integer bb = m * rhs;
        if (bb > *this) {
            b = m;
        }
        else
        {
            a = m;
        }
    }
    
    if (a * rhs <= *this) {
        *this = a;
    } else {
        *this = b;
    }
    while (!data[data.size() - 1] and data.size() != 1) data.pop_back();
    return *this;
}
Beispiel #3
0
std::string to_string(big_integer const& a)
{
    std::string res = "";
    big_integer aa (a);
    big_integer bb (0);
    big_integer cc (0);
    big_integer dd (0);
    if (a.flag == 1) {
        aa.flag = false;
    }
    if (a == big_integer(0)) return "0";
    while ((aa) > big_integer(0))
    {
        bb = aa / big_integer(1000000000);
        cc = bb * big_integer(1000000000);
        dd = aa - cc;
        aa = bb;
        for (int i = 0; i < 9; i++)
        {
            res += dd.data[0] % 10 + 48;
            dd.data[0] /= 10;
        }
    }
    while (res[res.size() - 1] == '0') res.pop_back();
    if (a.flag == 1) {
        res+='-';
    }
    for (int  i = 0; i < res.size()/2; ++i)
    {
        std::swap(res[i], res[res.size()-1-i]);
    }
    return res;
}
Beispiel #4
0
big_integer::big_integer(std::string str)
{
    if (str[0] == '-') flag = true; else flag = false;
    for (int i = 0; i < str.size(); ++i)
    {
        *this *= big_integer(10);
        *this += big_integer(str[i] - 48);
    }
}
Beispiel #5
0
//subtract
big_integer& big_integer::operator-=(big_integer const& rhs) {
    if (sign == rhs.sign) {
        big_integer temp;
        switch (absolute_comparator(*this, rhs)) {
            case 0:
                *this = big_integer();
                return *this;
            case 1:
            	break;
            case -1:
                temp = rhs - *this;
                temp.sign = -temp.sign;
                *this = temp;
                return *this;
            default:
            	break;
        }
        int carry = 0;
        for (int i = 0; i < rhs.digits.size() || carry; ++i) {
            digits[i] -= carry + (i < rhs.digits.size() ? rhs.digits[i] : 0);
            carry = digits[i] < 0;
            if (carry) digits[i] += 10;
        }
        while (digits.size() > 1 && digits.back() == 0)
            digits.pop_back();
        return *this;
    }
    else {
            *this += -rhs;
            return *this;
    }
};
Beispiel #6
0
//add
big_integer& big_integer::operator+=(big_integer const& rhs) {
    if (sign == 0) {
        *this = rhs;
        return *this;
    }
    if (rhs.sign == 0)
        return *this;
    if (sign == rhs.sign) {
        int carry = 0;
        for (int i = 0; i < std::max(digits.size(), rhs.digits.size()) || carry; ++i) {
            if (i == digits.size())
                digits.push_back(0);
            digits[i] += carry + (i < rhs.digits.size() ? rhs.digits[i] : 0);
            carry = digits[i] >= 10;
            if (carry) digits[i] -= 10;
        }
        return *this;
    }
    else {
        big_integer temp;
        switch (absolute_comparator(*this, rhs)) {
            case 0:
                *this = big_integer();
                return *this;
            case 1:
                if (sign == 1) {
                    *this -= -rhs;
                }
                else {
                    sign = 1;
                    *this -= rhs;
                    sign = -1;
                }
                return *this;
            case -1:
                sign = -sign;
                temp = rhs - *this;
                *this = temp;
                return *this;
            default:
            	break;
        }
        return *this;
    }

};