Esempio n. 1
0
    def substitution(self, number, base, dest_base, no_digits_fractional_part):
        operations = Operations()
        if base < dest_base:
            if ',' in number:
                parts = number.split(',')
                integer_part = parts[0]
                decimal_part = parts[1]
            else:
                integer_part = number
                decimal_part = ''
            #transform integer part
            p = 0
            integer_result = ''
            result = ''
            while len(integer_part) > 0:
                if p == 0:
                    result = integer_part[-1]
                    integer_result = result
                else:
                    result = integer_part[-1]
                    for i in list(range(p)):
                        operations.mul(result,
                                       self.get_numbers_inv()[base], dest_base)
                        result = operations.get_result()
                    operations.add(str(integer_result), str(result), dest_base)
                    integer_result = operations.get_result()
                p = p + 1
                integer_part = integer_part[:-1]

            #transforms decimal part
            p = 1
            decimal_result = '0'
            result = ''
            while len(decimal_part) > 0:
                result = decimal_part[0]
                for i in range(no_digits_fractional_part):
                    result = result + '0'
                for i in range(p):
                    operations.div(result,
                                   self.get_numbers_inv()[base], dest_base)
                    result = operations.get_result()
                operations.add(decimal_result, result, dest_base)
                decimal_result = operations.get_result()
                p = p + 1
                decimal_part = decimal_part[1:]
            if decimal_result == '0':
                self.set_result(str(integer_result))
            elif len(decimal_result) == no_digits_fractional_part:
                #if decimal_result[0]=='0':
                self.set_result(str(integer_result + ',' + decimal_result))
            else:
                current_decimal_result = decimal_result[
                    -no_digits_fractional_part:]
                decimal_result = decimal_result[:-no_digits_fractional_part]
                operations.add(decimal_result, integer_result, dest_base)
                integer_result = operations.get_result()
                self.set_result(
                    str(integer_result + ',' + current_decimal_result[1:]))
Esempio n. 2
0
 def test_addition(self):
     test_operations=Operations()
     test_operations.add('2', '2', 3)
     assert(test_operations.get_result()=='11')
Esempio n. 3
0
 def test_addition3(self):
     test_operations=Operations()
     test_operations.add('AB0','F01', 16)
     assert(test_operations.get_result()=='19B1')
Esempio n. 4
0
 def test_addition2(self):
     test_operations=Operations()
     test_operations.add('11', '11', 2)
     assert(test_operations.get_result()=='110')