Exemple #1
0
def main() -> None:
    print('\tA * x^2 + B * x + C = 0\n')

    def is_nozero(number: float) -> bool:
        return is_this(number != 0, 'Не может быть нулём')

    coef_a = FloatParameter('A', is_nozero)
    coef_b = FloatParameter('B')
    coef_c = FloatParameter('C')

    def compute_and_print() -> None:
        discr = _discriminant(coef_a.value, coef_b.value, coef_c.value)
        print(f'Дискриминант = {discr}')
        if discr < 0:
            print('Нет корней')
        else:
            root1 = _first_root(discr, coef_a.value, coef_b.value)
            print(f'1-й корень = {root1}')
            if discr > 0:
                root2 = _second_root(discr, coef_a.value, coef_b.value)
                print(f'2-й корень = {root2}')
            print()
            _test_roots(root1, coef_a.value, coef_b.value, coef_c.value)
            if discr > 0:
                _test_roots(root2, coef_a.value, coef_b.value, coef_c.value)

    mainloop((coef_a, coef_b, coef_c), compute_and_print)
Exemple #2
0
def main() -> None:
    def test_number(number: int) -> bool:
        return is_this(number > 1, 'Введите число больше единицы.')

    number = IntParameter('Число', test_number)

    def compute_and_print() -> None:
        prime_numbers = compute_prime_numbers(number.value)
        if number.value in prime_numbers:
            print('Простое число.')
        else:
            quotient: float = number.value
            multipliers = []
            while quotient > 1:
                iterator = filter(lambda x: quotient % x == 0, prime_numbers)
                try:
                    prime_number = next(iterator)
                except StopIteration:
                    break
                else:
                    multipliers.append(prime_number)
                    quotient /= prime_number
            print(', '.join([str(i) for i in multipliers]), '.', sep='')

    mainloop((number, ), compute_and_print)
Exemple #3
0
def main() -> None:
    x1_coor = FloatParameter('X1')
    y1_coor = FloatParameter('Y1')
    x2_coor = FloatParameter('X2')

    def test_y2(value: float) -> bool:
        return is_this(
            x1_coor.value != x2_coor.value or y1_coor.value != value,
            'Введите координаты двух разных точек.')

    y2_coor = FloatParameter('Y2', test_y2)
    print('\tY = k*X + b\n')

    def compute_and_print() -> None:
        if x1_coor.value == x2_coor.value:
            print(f'X = {x1_coor.value:g}')
        elif y1_coor.value == y2_coor.value:
            print(f'Y = {y1_coor.value:g}')
        else:
            coef_k = _eq1_coeficient_k(x1_coor.value, y1_coor.value,
                                       x2_coor.value, y2_coor.value)
            coef_b = _eq1_coeficient_b(x1_coor.value, y1_coor.value, coef_k)
            print(f'Y = {coef_k:g} * X + ({coef_b:g})')
            print(f'Y2 = {coef_k * x2_coor.value + coef_b:g}')

    mainloop((x1_coor, y1_coor, x2_coor, y2_coor), compute_and_print)
Exemple #4
0
def main() -> None:
    number = ArrayFloatParameter('Число')

    def compute_and_print() -> None:
        print(f'Среднее арифметическое: {arith_mean(number.value)}')
        print(f'Среднее геометрическое: {geom_mean(number.value)}')

    mainloop((number, ), compute_and_print)
Exemple #5
0
def main() -> None:
    volume = FloatParameter('Объем цилиндра, л', is_positive, from_l)

    def compute_and_print() -> None:
        diam1 = (4 * volume.value / pi * PHI)**(1 / 3)
        diam2 = (4 * volume.value / pi / PHI)**(1 / 3)
        height1 = _cylinder_height(volume.value, diam1)
        height2 = _cylinder_height(volume.value, diam2)
        print(f'D1 = {diam1 * 1e3:.0f} мм, H1 = {height1 * 1e3:.0f} мм\n'
              f'D2 = {diam2 * 1e3:.0f} мм, H2 = {height2 * 1e3:.0f} мм')

    mainloop((volume, ), compute_and_print)
Exemple #6
0
def main() -> None:
    force = FloatParameter('Осевая сила, Н', is_positive)
    diam = FloatParameter('Диаметр трубы, мм', is_positive, from_mm)

    def test_thickness(val: float) -> bool:
        return (is_positive(val)
                and is_this(val < diam.value / 2,
                            'Значение должно быть меньне половины диаметра'))

    thick = FloatParameter('Толщина стенки, мм', test_thickness, from_mm)
    length = FloatParameter('Длина рабочего участка, мм', is_positive, from_mm)

    def compute_and_print() -> None:
        moment = pi * (diam.value - thick.value)**3 * thick.value / 8
        reserve = moment / force.value * (pi / length.value)**2 * ELAST
        print(f'Запас прочности {reserve}')

    mainloop((force, diam, thick, length), compute_and_print)
Exemple #7
0
def main() -> None:
    torque = FloatParameter('Крутящий момент, Нм', is_positive)
    ext_diam = FloatParameter('Наружный диаметр, мм', is_positive, from_mm)

    def check_inn_diam(val: float) -> bool:
        return is_positive_or_zero(val) and is_this(
            val < ext_diam.value, 'Не может быть меньше наружного диаметра')

    inn_diam = FloatParameter('Внутренний диаметр, мм (0 для вала)',
                              check_inn_diam, from_mm)

    def compute_and_print() -> None:
        alpha = inn_diam.value / ext_diam.value
        polar_moment = pi * ext_diam.value**3 / 16 * (1 - alpha**4)
        stress = torque.value / polar_moment
        print(f'Напряжение {stress / 1e6:.1f} МПа')

    mainloop((torque, ext_diam, inn_diam), compute_and_print)
Exemple #8
0
def main() -> None:
    speed = FloatParameter('Brut-force speed, paswd/sec', is_positive, None,
                           1e6)

    def compute_and_print() -> None:
        for i in range(1, MAX_LEN_PASSWD + 2):
            variants_number = SYMBOLS_NUMBER**i
            time = variants_number / speed.value
            try:
                units = _optimal_size_units(time)
            except StopIteration:
                text_time = 'less than one second'
            else:
                text_time = _qty_units(time, units)
            entropy = log(variants_number) / log(2)
            print(f'{i:2d} symb. {variants_number:21d} variants, '
                  f'{entropy:2.0f} bits, {text_time}')

    mainloop((speed,), compute_and_print)
Exemple #9
0
def main() -> None:
    print('\tx^3 + A * x^2 + B * x + C = 0\n')

    a = FloatParameter('A')
    b = FloatParameter('B')
    c = FloatParameter('C')

    def compute_and_print() -> None:
        q = compute_q(a.value, b.value)
        r = compute_r(a.value, b.value, c.value)
        s = copmute_s(q, r)

        print(f'Q = {q}, R = {r}, S = {s}')

        for i, root in enumerate(roots(a.value, c.value, q, r, s)):
            res = round(root**3 + a.value * root**2 + b.value * root + c.value,
                        6)
            print(f'X_{i} = {root}, result = {res}')

    mainloop((a, b, c), compute_and_print)
Exemple #10
0
def main() -> None:
    diam1 = FloatParameter('Diameter #1, mm', is_positive)

    def test_diam2(val: float) -> bool:
        return (is_positive(val)
                and is_this(val != diam1.value, "Can't be equal to #1"))

    diam2 = FloatParameter('Diameter #2, mm', test_diam2)

    def compute_and_print() -> None:
        dmin = min(diam1.value, diam2.value)
        dmax = max(diam1.value, diam2.value)
        mid = geom_mean([dmin, dmax])
        rmin = (geom_mean([dmin, mid]) - dmin) / 2
        rmax = (dmax - geom_mean([dmax, mid])) / 2
        print(f'Middle diameter is {mid:%.2f}\n'
              f'Radius near {dmin:g} is {rmin:.2f}\n'
              f'Radius near {dmax:g} is {rmax:.2f}')

    mainloop((diam1, diam2), compute_and_print)
Exemple #11
0
def main() -> None:
    sum_a = FloatParameter('Сумма N1 элементов', is_positive)
    qty_a = IntParameter('N1', is_positive)
    sum_b = FloatParameter('Сумма N2 элементов', is_positive)

    def test_qty_b(value: float) -> bool:
        return is_positive(value) and \
            is_this(value != qty_a.value, 'Не может быть равным N1')

    qty_b = IntParameter('N2', test_qty_b)

    def compute_and_print() -> None:
        step = _arith_seq_step(sum_a.value, qty_a.value, sum_b.value,
                               qty_b.value)
        first = _arith_seq_first(sum_a.value, qty_a.value, step)
        for i in reversed(range(1, max(qty_a.value, qty_b.value) + 2)):
            print(round(_arith_seq_summa(i, first, step)), end='  ')
        print('[{0}{1}]'.format('+' if step > 0 else '', round(step)))

    mainloop((sum_a, qty_a, sum_b, qty_b), compute_and_print)
Exemple #12
0
def main() -> None:
    x1 = FloatParameter('x1')
    y1 = FloatParameter('y1')
    z1 = FloatParameter('z1')
    x2 = FloatParameter('x2')
    y2 = FloatParameter('y2')
    z2 = FloatParameter('z2')
    x3 = FloatParameter('x3')
    y3 = FloatParameter('y3')
    z3 = FloatParameter('z3')
    print('\tA * xy + B * x + C * y = z\n')

    def coeff(coords: Tuple[FloatParameter, ...]) -> float:
        return (coords[0].value * coords[1].value * coords[2].value -
                coords[3].value * coords[4].value * coords[5].value)

    def compute_and_print() -> None:
        k1 = coeff((z1, x2, y2, z2, x1, y1))
        k2 = coeff((y1, x2, y2, y2, x1, y1))
        k3 = coeff((x1, x2, y2, x2, x1, y1))
        k4 = coeff((z3, x2, y2, z2, x3, y3))
        k5 = coeff((y3, x2, y2, y2, x3, y3))
        k6 = coeff((x3, x2, y2, x2, x3, y3))
        try:
            kc = (k4 * k3 - k1 * k6) / (k5 * k3 - k2 * k6)
            if k3 == 0:
                kb = (k4 - k5 * kc) / k6
            else:
                kb = (k1 - k2 * kc) / k3
            ka = ((z1.value - x1.value * kb - y1.value * kc) / x1.value /
                  y1.value)
            print(f'A = {ka}\nB = {kb}\nC = {kc}')
        except ZeroDivisionError:
            print_error('Деление на ноль!')

    mainloop((x1, y1, z1, x2, y2, z2, x3, y3, z3), compute_and_print)