Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.96 KB | None | 0 0
  1. from math import *
  2. from tkinter import *
  3. from tkinter import messagebox
  4. from math import *
  5.  
  6.  
  7. def transfer():
  8.     num = inp_window.get()
  9.     bastard = '0123456789ABCDEF-.'
  10.     bastard_1 = '0123456789-.'
  11.     for i in range(len(num)):
  12.         if not(num[i] in bastard_1):
  13.             messagebox.showerror("Ошибка ввода", "Введены символы, которых "
  14.                                                  "нет в десятичной "
  15.                                                  "системе счисления.")
  16.             inp_window.delete(0, END)
  17.             return
  18.  
  19.     if '.' in num:
  20.         num_1 = ''
  21.         num_2 = ''
  22.         i = 0
  23.         while num[i] != '.':
  24.             num_1 += num[i]
  25.             i += 1
  26.         for j in range(i + 1, len(num)):
  27.             num_2 += num[j]
  28.  
  29.         num_1 = int(num_1)
  30.         result_1 = ''
  31.         while num_1:
  32.             num_1, residue = divmod(num_1, 16)
  33.             result_1 = bastard[residue] + result_1
  34.  
  35.         num_2 = '0.' + num_2
  36.         num_2 = float(num_2)
  37.         print(num_2)
  38.         result_2 = ''
  39.         while len(result_2) < 6:
  40.             temp = num_2 * 16
  41.             result_2 = result_2 + bastard[int(temp)]
  42.             num_2 = temp - int(temp)
  43.         result = result_1 + '.' + result_2
  44.  
  45.     else:
  46.         num = int(num)
  47.         result = ''
  48.         while num:
  49.             num, residue = divmod(num, 16)
  50.             result = bastard[residue] + result
  51.  
  52.     inp_window.delete(0, END)
  53.     inp_window.insert(END, result)
  54.  
  55.  
  56. def retroversion():
  57.     num = inp_window.get()
  58.     bastard = '0123456789ABCDEF-.'
  59.     thing = 'ABCDEF'
  60.     for i in range(len(num)):
  61.         if not(num[i] in bastard):
  62.             messagebox.showerror("Ошибка ввода", "Введены символы, которых "
  63.                                                  "нет в шестнадцатеричной "
  64.                                                  "системе счисления.")
  65.             inp_window.delete(0, END)
  66.             return
  67.  
  68.     if '.' in num:
  69.         num_1 = ''
  70.         num_2 = ''
  71.         i = 0
  72.         while num[i] != '.':
  73.             num_1 += num[i]
  74.             i += 1
  75.         for j in range(i + 1, len(num)):
  76.             num_2 += num[j]
  77.  
  78.         total_1 = 0
  79.         for i in range(len(num_1) - 1, -1, -1):
  80.             if num_1[i] in thing:
  81.                 total_1 += (10 + thing.index(num_1[len(num_1) - 1 - i])) * pow(
  82.                     16, i)
  83.             else:
  84.                 total_1 += int(num_1[len(num_1) - 1 - i]) * pow(16, i)
  85.  
  86.         total_2 = 0
  87.         for i in range(len(num_2)):
  88.             if num_2 in thing:
  89.                 total_2 += (10 + thing.index(num_2[i])) * pow(16, -(i + 1))
  90.             else:
  91.                 total_2 += int(num_2[i]) * pow(16, -(i + 1))
  92.         result = str(int(total_1)) + str(total_2)[1:]
  93.     else:
  94.         total = 0
  95.         for i in range(len(num) - 1, -1, -1):
  96.             if num[i] in thing:
  97.                 total += (10 + thing.index(num[len(num) - 1 - i])) * pow(16, i)
  98.             else:
  99.                 total += int(num[len(num) - 1 - i]) * pow(16, i)
  100.         result = str(int(total))
  101.  
  102.     inp_window.delete(0, END)
  103.     inp_window.insert(END, result)
  104.  
  105.  
  106. def information():
  107.     messagebox.showinfo("Информация", "Программа: перевод чисел из "
  108.                         "десятичной системы счисления в "
  109.                         "шестнадцатеричную и обратно." + "\n" +
  110.                         "Разработал: Кишов Гаджи. ИУ7-23Б.")
  111.  
  112.  
  113. def clear():
  114.     global action
  115.     inp_window.delete(0, END)
  116.     action = 'inp_window.delete(0, END)'
  117.  
  118.  
  119. def insert(numeral):
  120.     global action
  121.     inp_window.insert(END, numeral)
  122.     action = 'inp_window.insert(END, ' + str(numeral) + ')'
  123.  
  124.  
  125. def delete_last():
  126.     global action
  127.     inp_window.delete(len(inp_window.get()) - 1, END)
  128.     action = 'inp_window.delete(len(inp_window.get()) - 1, END)'
  129.  
  130.  
  131. def action_repeat():
  132.     exec(action)
  133.  
  134.  
  135. # Поля ввода
  136. action = ''
  137. root = Tk()
  138. root.title('Калькулятор')
  139. root.geometry('285x300')
  140.  
  141. inp = StringVar()
  142. inp_window = Entry(root, textvariable = inp, justify = LEFT, font = (
  143.     "Arial", 15))
  144. inp_window.place(x = 15, y = 45, width = 255, height = 25)
  145. inp_window.focus()
  146.  
  147. one = Button(root, text='1', command=lambda: insert('1'))
  148. one.place(x=15, y=100, width=30, height=30)
  149.  
  150. two = Button(root, text='2', command=lambda: insert('2'))
  151. two.place(x=60, y=100, width=30, height=30)
  152.  
  153. three = Button(root, text='3', command=lambda: insert('3'))
  154. three.place(x=105, y=100, width=30, height=30)
  155.  
  156. four = Button(root, text='4', command=lambda: insert('4'))
  157. four.place(x=150, y=100, width=30, height=30)
  158.  
  159. A = Button(root, text='A', command=lambda: insert('A'))
  160. A.place(x=195, y=100, width=30, height=30)
  161.  
  162. D = Button(root, text='D', command=lambda: insert('D'))
  163. D.place(x=240, y=100, width=30, height=30)
  164.  
  165. five = Button(root, text='5', command=lambda: insert('5'))
  166. five.place(x=15, y=140, width=30, height=30)
  167.  
  168. six = Button(root, text='6', command=lambda: insert('6'))
  169. six.place(x=60, y=140, width=30, height=30)
  170.  
  171. seven = Button(root, text='7', command=lambda: insert('7'))
  172. seven.place(x=105, y=140, width=30, height=30)
  173.  
  174. eight = Button(root, text='8', command=lambda: insert('8'))
  175. eight.place(x=150, y=140, width=30, height=30)
  176.  
  177. B = Button(root, text='B', command=lambda: insert('B'))
  178. B.place(x=195, y=140, width=30, height=30)
  179.  
  180. E = Button(root, text='E', command=lambda: insert('E'))
  181. E.place(x=240, y=140, width=30, height=30)
  182.  
  183. nine = Button(root, text='9', command=lambda: insert('9'))
  184. nine.place(x=15, y=180, width=30, height=30)
  185.  
  186. zero = Button(root, text='0', command=lambda: insert('0'))
  187. zero.place(x=60, y=180, width=30, height=30)
  188.  
  189. C = Button(root, text='C', command=lambda: insert('C'))
  190. C.place(x=195, y=180, width=30, height=30)
  191.  
  192. F = Button(root, text='F', command=lambda: insert('F'))
  193. F.place(x=240, y=180, width=30, height=30)
  194.  
  195. dot = Button(root, text='.', command=lambda: insert('.'))
  196. dot.place(x=105, y=180, width=30, height=30)
  197.  
  198. minus = Button(root, text='-', command=lambda: insert('-'))
  199. minus.place(x=150, y=180, width=30, height=30)
  200.  
  201. to_ten = Button(root, text='В десятичную', command=retroversion)
  202. to_ten.place(x=15, y=235)
  203.  
  204. to_sixteen = Button(root, text='В шестнадцатеричную', command=transfer)
  205. to_sixteen.place(x=115, y=235, width = 167)
  206.  
  207. delete = Button(root, text='Удалить все', command=clear)
  208. delete.place(x=15, y=265, width = 87)
  209.  
  210. back = Button(root, text='Удалить последний элемент', command=delete_last)
  211. back.place(x=115, y=265)
  212.  
  213. # Меню
  214. main = Menu(root)
  215. root.config(menu=main)
  216. main.add_command(label='Информация', command=information)
  217. main.add_command(label='Повторение действия',
  218.                  command=action_repeat)
  219. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement