Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import *
- from tkinter import *
- from tkinter import messagebox
- from math import *
- def transfer():
- num = inp_window.get()
- bastard = '0123456789ABCDEF-.'
- bastard_1 = '0123456789-.'
- for i in range(len(num)):
- if not(num[i] in bastard_1):
- messagebox.showerror("Ошибка ввода", "Введены символы, которых "
- "нет в десятичной "
- "системе счисления.")
- inp_window.delete(0, END)
- return
- if '.' in num:
- num_1 = ''
- num_2 = ''
- i = 0
- while num[i] != '.':
- num_1 += num[i]
- i += 1
- for j in range(i + 1, len(num)):
- num_2 += num[j]
- num_1 = int(num_1)
- result_1 = ''
- while num_1:
- num_1, residue = divmod(num_1, 16)
- result_1 = bastard[residue] + result_1
- num_2 = '0.' + num_2
- num_2 = float(num_2)
- print(num_2)
- result_2 = ''
- while len(result_2) < 6:
- temp = num_2 * 16
- result_2 = result_2 + bastard[int(temp)]
- num_2 = temp - int(temp)
- result = result_1 + '.' + result_2
- else:
- num = int(num)
- result = ''
- while num:
- num, residue = divmod(num, 16)
- result = bastard[residue] + result
- inp_window.delete(0, END)
- inp_window.insert(END, result)
- def retroversion():
- num = inp_window.get()
- bastard = '0123456789ABCDEF-.'
- thing = 'ABCDEF'
- for i in range(len(num)):
- if not(num[i] in bastard):
- messagebox.showerror("Ошибка ввода", "Введены символы, которых "
- "нет в шестнадцатеричной "
- "системе счисления.")
- inp_window.delete(0, END)
- return
- if '.' in num:
- num_1 = ''
- num_2 = ''
- i = 0
- while num[i] != '.':
- num_1 += num[i]
- i += 1
- for j in range(i + 1, len(num)):
- num_2 += num[j]
- total_1 = 0
- for i in range(len(num_1) - 1, -1, -1):
- if num_1[i] in thing:
- total_1 += (10 + thing.index(num_1[len(num_1) - 1 - i])) * pow(
- 16, i)
- else:
- total_1 += int(num_1[len(num_1) - 1 - i]) * pow(16, i)
- total_2 = 0
- for i in range(len(num_2)):
- if num_2 in thing:
- total_2 += (10 + thing.index(num_2[i])) * pow(16, -(i + 1))
- else:
- total_2 += int(num_2[i]) * pow(16, -(i + 1))
- result = str(int(total_1)) + str(total_2)[1:]
- else:
- total = 0
- for i in range(len(num) - 1, -1, -1):
- if num[i] in thing:
- total += (10 + thing.index(num[len(num) - 1 - i])) * pow(16, i)
- else:
- total += int(num[len(num) - 1 - i]) * pow(16, i)
- result = str(int(total))
- inp_window.delete(0, END)
- inp_window.insert(END, result)
- def information():
- messagebox.showinfo("Информация", "Программа: перевод чисел из "
- "десятичной системы счисления в "
- "шестнадцатеричную и обратно." + "\n" +
- "Разработал: Кишов Гаджи. ИУ7-23Б.")
- def clear():
- global action
- inp_window.delete(0, END)
- action = 'inp_window.delete(0, END)'
- def insert(numeral):
- global action
- inp_window.insert(END, numeral)
- action = 'inp_window.insert(END, ' + str(numeral) + ')'
- def delete_last():
- global action
- inp_window.delete(len(inp_window.get()) - 1, END)
- action = 'inp_window.delete(len(inp_window.get()) - 1, END)'
- def action_repeat():
- exec(action)
- # Поля ввода
- action = ''
- root = Tk()
- root.title('Калькулятор')
- root.geometry('285x300')
- inp = StringVar()
- inp_window = Entry(root, textvariable = inp, justify = LEFT, font = (
- "Arial", 15))
- inp_window.place(x = 15, y = 45, width = 255, height = 25)
- inp_window.focus()
- one = Button(root, text='1', command=lambda: insert('1'))
- one.place(x=15, y=100, width=30, height=30)
- two = Button(root, text='2', command=lambda: insert('2'))
- two.place(x=60, y=100, width=30, height=30)
- three = Button(root, text='3', command=lambda: insert('3'))
- three.place(x=105, y=100, width=30, height=30)
- four = Button(root, text='4', command=lambda: insert('4'))
- four.place(x=150, y=100, width=30, height=30)
- A = Button(root, text='A', command=lambda: insert('A'))
- A.place(x=195, y=100, width=30, height=30)
- D = Button(root, text='D', command=lambda: insert('D'))
- D.place(x=240, y=100, width=30, height=30)
- five = Button(root, text='5', command=lambda: insert('5'))
- five.place(x=15, y=140, width=30, height=30)
- six = Button(root, text='6', command=lambda: insert('6'))
- six.place(x=60, y=140, width=30, height=30)
- seven = Button(root, text='7', command=lambda: insert('7'))
- seven.place(x=105, y=140, width=30, height=30)
- eight = Button(root, text='8', command=lambda: insert('8'))
- eight.place(x=150, y=140, width=30, height=30)
- B = Button(root, text='B', command=lambda: insert('B'))
- B.place(x=195, y=140, width=30, height=30)
- E = Button(root, text='E', command=lambda: insert('E'))
- E.place(x=240, y=140, width=30, height=30)
- nine = Button(root, text='9', command=lambda: insert('9'))
- nine.place(x=15, y=180, width=30, height=30)
- zero = Button(root, text='0', command=lambda: insert('0'))
- zero.place(x=60, y=180, width=30, height=30)
- C = Button(root, text='C', command=lambda: insert('C'))
- C.place(x=195, y=180, width=30, height=30)
- F = Button(root, text='F', command=lambda: insert('F'))
- F.place(x=240, y=180, width=30, height=30)
- dot = Button(root, text='.', command=lambda: insert('.'))
- dot.place(x=105, y=180, width=30, height=30)
- minus = Button(root, text='-', command=lambda: insert('-'))
- minus.place(x=150, y=180, width=30, height=30)
- to_ten = Button(root, text='В десятичную', command=retroversion)
- to_ten.place(x=15, y=235)
- to_sixteen = Button(root, text='В шестнадцатеричную', command=transfer)
- to_sixteen.place(x=115, y=235, width = 167)
- delete = Button(root, text='Удалить все', command=clear)
- delete.place(x=15, y=265, width = 87)
- back = Button(root, text='Удалить последний элемент', command=delete_last)
- back.place(x=115, y=265)
- # Меню
- main = Menu(root)
- root.config(menu=main)
- main.add_command(label='Информация', command=information)
- main.add_command(label='Повторение действия',
- command=action_repeat)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement