Advertisement
Ilmen

mencalc.py

Aug 7th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.12 KB | None | 0 0
  1. import time
  2. import random
  3. import curses
  4.  
  5. # + num -> prime factors list
  6. # + frac -> irreducible frac
  7.  
  8. # ?+ pause button
  9. # ?+ save scores
  10.  
  11. TIME = 1000
  12.  
  13. # The timer class    
  14. class Timer():
  15.     def __init__(self):
  16.         pass
  17. #        self.target = time.time() + 5
  18.     def init(self, duration):
  19.         self.target = time.time() + duration
  20.     def add_five(self):
  21.         self.target += 5
  22.     def get_left(self):
  23.         return int(self.target - time.time())
  24.  
  25. def curses_clearline(scr, ln):
  26.     y, x = scr.getyx()
  27.     scr.move(ln, 0)
  28.     scr.clrtoeol()
  29.     scr.move(y, x)
  30.  
  31. DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', \
  32.           '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G']
  33.  
  34. def intposnot(number, radix):
  35.     n = int(number)
  36.     r = int(radix)
  37.     assert (r <= 16)
  38.     if n == 0:
  39.         return "0"
  40.     s = ""
  41.     while (n > 0):
  42.         s = DIGITS[n % r] + s
  43.         n //= r
  44.     return s
  45.  
  46. NI_NSIZE    = 0
  47. NI_RADIX    = 1
  48. NI_PARAM3   = 2
  49. NI_DISPTIME = 3
  50.  
  51. # The main program
  52. stdscr = curses.initscr()
  53. stdscr.nodelay(True)
  54. curses.noecho()
  55. t = Timer()
  56. q = False
  57. op = '+'
  58. nsize = 3
  59. radix = 2*5
  60. param3 = 0
  61. input_display_duration = 0
  62. # ^ formula display duration (for forcing to keep the input in mind).
  63. dr = [0, 0]
  64. sr = [0, 0]
  65. num_input_mode = NI_NSIZE
  66. # This is the main loop done in curses, but you can implement it with
  67. # a GUI toolkit or any other method you wish.
  68. while not q:
  69.     if op == '=':
  70.         radix2 = param3
  71.         if radix2 < 2:
  72.             radix2 = 2
  73.         n1 = random.randint(0, radix ** nsize - 1)
  74.         hl_left = "#{}->#{}:  ".format(DIGITS[radix], DIGITS[param3])
  75.         hl_val = "{}".format(intposnot(n1, radix))
  76.     else:
  77.         if param3 == 0:
  78.             size2 = nsize
  79.         else:
  80.             size2 = param3
  81.         if not (op == '-' and nsize == 1):
  82.             n1 = random.randint(2, radix ** nsize - 1)
  83.             n2 = random.randint(2, radix ** size2 - 1)
  84.         else:
  85.             n1 = random.randint(radix, radix * 2 - 1)
  86.             n2 = random.randint(2, radix - 1)
  87.         if op == '^' or op == '_':
  88.             n2 = param3
  89.             if n2 < 2:
  90.                 n2 = 2
  91.         elif (op == '-' or op == '/') and n1 < n2:
  92.             n1, n2 = n2, n1
  93.         hl_left = "#{}:  ".format(DIGITS[radix])
  94.         if (op == '!'):
  95.             hl_val = "{}!".format(intposnot(n1, radix))
  96.         else:
  97.             hl_val = "{} " + op + " {}"
  98.             hl_val = hl_val.format(intposnot(n1, radix), intposnot(n2, radix))
  99.     if sr[0] > 0:
  100.         hl_right = "  |  success: %d / %d = %.3f; mean time: %.3fs"
  101.         hl_right %= (sr[1], sr[0], float(sr[1]) / sr[0], dr[1] / dr[0])
  102.     else:
  103.         hl_right = ""
  104. #    stdscr.hline(0, 0, ' ', 64)
  105.     curses_clearline(stdscr, 0)
  106.     stdscr.addstr(0, 0, hl_left + hl_val + hl_right)
  107.     input_display_end_time = input_display_duration + time.time()
  108.     t.init(TIME)
  109.     d = time.time()
  110.     while True:
  111.         left = t.get_left()
  112.         if left <= 0:
  113.             break
  114.         if input_display_duration > 0 and time.time() >= input_display_end_time:
  115.             curses_clearline(stdscr, 0)
  116.             stdscr.addstr(0, 0, hl_left + op.ljust(len(hl_val), ' ') + hl_right)
  117.         stdscr.addstr(1, 0, '%s sec\n' % str(left).zfill(3))
  118.         c = stdscr.getch()
  119.         if c == ord('i'):
  120.             t.add_five()
  121.         elif c == ord('x'):
  122.             break
  123.         elif c == ord('q'):
  124.             q = True
  125.             break
  126.     d = time.time() - d
  127.     dr[1] += d
  128.     dr[0] += 1
  129.     curses_clearline(stdscr, 1)
  130.     if op == '=':
  131.         v = intposnot(n1, radix2)
  132.     elif op == '+':
  133.         v = intposnot(n1 + n2, radix)
  134.     elif op == '-':
  135.         v = intposnot(n1 - n2, radix)
  136.     elif op == '*':
  137.         v = intposnot(n1 * n2, radix)
  138.     elif op == '/':
  139.         v = "[" + intposnot(n1 / n2, radix) \
  140.           + ", " + intposnot(n1 % n2, radix) + "]"
  141.     elif op == '^':
  142.         v = intposnot(n1 ** n2, radix)
  143.     elif op == '_':
  144.         rt = int(n1 ** (1. / n2))
  145.         v = "[" + intposnot(rt, radix) \
  146.           + ", " + intposnot(n1 - rt ** n2, radix) + "]"
  147.     elif op == '!':
  148.         n2 = 1
  149.         while n1 > 1:
  150.             n2 *= n1
  151.             n1 -= 1
  152.         v = intposnot(n2, radix)
  153.     elif op == '~':
  154.         # gcd(n1, n2)
  155.         if n1 < n2:
  156.             n1, n2 = n2, n1
  157.         while True:
  158.             r = n1 % n2
  159.             if r == 0:
  160.                 break
  161.             n1, n2 = n2, r
  162.         v = intposnot(n2, radix)
  163.     curses_clearline(stdscr, 0)
  164.     stdscr.addstr(0, 0, hl_left + hl_val + hl_right)
  165.     stdscr.addstr(1, 0, "   = {}  |  {}s  |  mean: {}s\n".format( \
  166.                         v, '%.3f' % d, '%.3f' % (dr[1] / dr[0])))
  167.     t.init(16)
  168.     while True:
  169.         if t.get_left() <= 0:
  170.             break
  171.         c = stdscr.getch()
  172.         if c in (ord('+'), ord('-'), ord('*'), ord('/'), ord('^'), \
  173.                  ord('_'), ord('!'), ord('~'), ord('=')):
  174.             op = chr(c)
  175.         elif c in range(0x100) and chr(c) in DIGITS:
  176.             val = DIGITS.index(chr(c))
  177.             if num_input_mode == NI_NSIZE and val > 0:
  178.                 nsize = val
  179.             elif num_input_mode == NI_RADIX and val > 0:
  180.                 radix = val
  181.             elif num_input_mode == NI_PARAM3:
  182.                 param3 = val
  183.             elif num_input_mode == NI_DISPTIME:
  184.                 input_display_duration = val
  185.         elif c == ord(','):
  186.             num_input_mode = NI_NSIZE
  187.         elif c == ord(':'):
  188.             num_input_mode = NI_RADIX
  189.         elif c == ord(';'):
  190.             num_input_mode = NI_PARAM3
  191.         elif c == ord('&'):
  192.             num_input_mode = NI_DISPTIME
  193.         elif c == ord('r'):
  194.             dr = [0, 0]
  195.             sr = [0, 0]
  196.             break
  197.         elif c == ord('c'):
  198.             sr[0] += 1
  199.             sr[1] += 1
  200.             break
  201.         elif c == ord('w'):
  202.             sr[0] += 1
  203.             break
  204.         elif c == ord('x'):
  205.             break
  206.         elif c == ord('q'):
  207.             q = True
  208.             break
  209. # Final operations start here
  210. stdscr.keypad(0)
  211. curses.echo()
  212. curses.endwin()
  213. # print '\nTime is up!\n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement