Ejemplo n.º 1
0
integer MOD_ZZ_Z(const integer& a, const integer& b)
{
    integer A = a;
    integer B = b;
    integer result;
    result.isPositive = true;  //всегда положительный
    if (B.module != integer(0))  //если делитель НЕ равен нулю
    {
        result = MOD_NN_N(A.module, B.module);  //просто находим остаток
            if (!(A.isPositive) && (result))  //если делимое положительно и делится с остатком
                result = B.module - MOD_NN_N(A.module, B.module);
                //необычный остаток, пример:(−17)/(−5)=4(ост.3) или (−7)/2=−4(ост.1)
        return result;  //возвращаем ответ
    } else {  //если делитель равен нулю
        throw std::invalid_argument("Da nuuu ti chyo...Na ziro delish"); //ошибочка
    }
}
Ejemplo n.º 2
0
/*
ФИО: Гараев Никита Рустамович
e-mail: [email protected]
Операция: НОК двух натуральных чисел
Принимаемые значения: op1 - объект класса DM_N, op2 - объект класса DM_N
Возвращаемое значение: Наименьшее общее кратное result - объект класса DM_N или -1 в случае ошибки
Алгоритм: НОК двух чисел вычисляется по следующей формуле
                            a*b
                    НОК = -------- , где a, b - натур. числа
                          НОД(a,b)
*/
DM_N LCM_NN_N(DM_N op1, DM_N op2) {
    if(op1.a == NULL || op2.a == NULL)
        return -1;
    DM_N a, b, result;
    a = op1* op2;
    b = MOD_NN_N(op1, op2); // a % b
    result = DIV_NN_N(a, b); // a / b
    return ((result.a != NULL) ? result : -1);
}
Ejemplo n.º 3
0
integer DIV_ZZ_Z(const integer& a, const integer& b)
{
    integer A = a;
    integer B = b;
    integer result;
    if (B.module.order() != 0)  //если делитель НЕ равен нулю
    {
    if (A.isPositive == B.isPositive)  //если знаки равны
        result.isPositive = true;  //частное положительное
    else
        result.isPositive = false;  //частное отрицательное
    result = DIV_NN_N(A.module, B.module);  //обычный остаток, пример: (−17):(−5) = 4 (ост. 3) или 17:5 = 4 (ост. 3)
    if ((((!A.isPositive) && (B.isPositive)) || ((!A.isPositive) && (!B.isPositive))) && (MOD_NN_N(A.module, B.module).order() != 0)) //если первый отрицательный, а второй положительный и не делятся нацело
            result = ADD_1N_N(result.module); //прибавляем единицу      
    return result; //возвращаем ответ
    }
    else //если делитель равен нулю
        throw std::invalid_argument("Da nuuu ti chyo...Na ziro delish"); //ошибочка
}