main
This commit is contained in:
parent
d8beb0c31c
commit
3aff9cc17b
1 changed files with 135 additions and 0 deletions
135
convertor.py
Normal file
135
convertor.py
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
class Convertor:
|
||||
|
||||
def __init__(self,isinput=False,number="0",precision=0,base_from=0.0,base_to=0.0):
|
||||
self.num_lib = {
|
||||
10: "a", 11: "b",
|
||||
12: "c",13: "d",
|
||||
14: "e",15: "f",
|
||||
16: "g",17: "h",
|
||||
18: "i",19: "j",
|
||||
20: "k",21: "l",
|
||||
22: "m",23: "n",
|
||||
24: "o",25: "p",
|
||||
26: "q",27: "r",
|
||||
28: "s",29: "t",
|
||||
30: "u",31: "v",
|
||||
32: "w",33: "x",
|
||||
34: "y",35: "z"}
|
||||
self.char_lib = {}
|
||||
for x in self.num_lib.keys():
|
||||
self.char_lib[self.num_lib[x]] = x
|
||||
if isinput == False:
|
||||
self.isinput = False
|
||||
self.run()
|
||||
else:
|
||||
self.isinput = True
|
||||
self.number = number
|
||||
self.precision = precision
|
||||
self.base_from = base_from
|
||||
self.base_to = base_to
|
||||
self.runI()
|
||||
|
||||
def run(self):
|
||||
print("Welcome to the Number Base Calculator.\n\n")
|
||||
while True:
|
||||
print("Which number do you wish to convert?\n")
|
||||
self.number = input()
|
||||
if "." in self.number or "," in self.number:
|
||||
print(f"What precision do you want for your decimal point?")
|
||||
self.precision = int(input())
|
||||
print(f"Precision: {self.precision}\n")
|
||||
print(f"Number chosen:{self.number}\nWhich base is this number in?\n")
|
||||
self.base_from = float(input())
|
||||
print(f"Base chosen:{self.base_from}\nWhich base would you like to end up with?\n")
|
||||
self.base_to = float(input())
|
||||
print(f"Base chosen:{self.base_to}")
|
||||
self.base_ten()
|
||||
self.calc()
|
||||
|
||||
def runI(self):
|
||||
self.base_ten()
|
||||
self.calc()
|
||||
|
||||
def base_ten(self):
|
||||
self.number = str(self.number)
|
||||
self.frac = True
|
||||
if "." in self.number:
|
||||
num_int, num_frac = self.number.split('.')
|
||||
elif "," in self.number:
|
||||
num_int, num_frac = self.number.split(',')
|
||||
else:
|
||||
self.frac = False
|
||||
|
||||
int_list = []
|
||||
for x in num_int:
|
||||
if x.isnumeric():
|
||||
int_list.append(x)
|
||||
else:
|
||||
int_list.append(str(self.char_lib[x]))
|
||||
frac_list = []
|
||||
for x in num_frac:
|
||||
if x.isnumeric():
|
||||
frac_list.append(x)
|
||||
else:
|
||||
frac_list.append(str(self.char_lib[x]))
|
||||
print(f"{int_list},{frac_list}")
|
||||
|
||||
int_part = 0
|
||||
for a in range(len(int_list)):
|
||||
b = len(int_list)-1-a
|
||||
int_part += int(int_list[a])*(self.base_from**(b))
|
||||
|
||||
frac_part = 0
|
||||
for a in range(len(frac_list)):
|
||||
b = -(a+1)
|
||||
frac_part += int(frac_list[a])*(self.base_from**(b))
|
||||
frac_part = frac_part / (self.base_from ** len(frac_list))
|
||||
self.number = int_part + frac_part
|
||||
print (self.number)
|
||||
|
||||
def calc(self):
|
||||
x = 0
|
||||
highestDiv = 1
|
||||
while x < self.number:
|
||||
highestDiv *= self.base_to
|
||||
x += (self.base_to-1)*highestDiv
|
||||
print(f"Highest div is {highestDiv}")
|
||||
|
||||
result = ""
|
||||
divrange = [highestDiv]
|
||||
while divrange[-1] >= 1:
|
||||
divrange.append(divrange[-1]/self.base_to)
|
||||
divrange.pop()
|
||||
print(f"Divrange is {divrange}")
|
||||
|
||||
int_part = int(self.number)
|
||||
frac_part = self.number - int_part
|
||||
|
||||
for div in divrange:
|
||||
q = int(int_part/div)
|
||||
sq = str(q)
|
||||
int_part %= div
|
||||
result += sq if q < 10 else self.num_lib[q]
|
||||
|
||||
result += "."
|
||||
|
||||
for _ in range(self.precision):
|
||||
frac_part *= self.base_to
|
||||
q = int(frac_part)
|
||||
sq = str(q)
|
||||
frac_part -= q
|
||||
result += sq if q < 10 else self.num_lib[q]
|
||||
|
||||
print(f"The result is: {result}")
|
||||
|
||||
res_list = [x for x in result]
|
||||
t_result = ""
|
||||
for char in res_list:
|
||||
if char.isnumeric() or char == ".":
|
||||
t_result += char
|
||||
else:
|
||||
t_result += f"({self.char_lib[char]})"
|
||||
print(f"Result with translation: {t_result}")
|
||||
|
||||
if self.isinput:
|
||||
return (result, t_result)
|
||||
Loading…
Add table
Add a link
Reference in a new issue