Advertisement
100hahahaha

Untitled

May 28th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.10 KB | None | 0 0
  1. import math
  2. import time
  3.  
  4. SIZE = 4
  5. A_SIZE = 4
  6. Y_SIZE = 11
  7. S_SIZE = 3
  8. R_SIZE = 3
  9.  
  10. '''
  11. def mnoj(arr, res):
  12.    if res in arr:
  13.        return True
  14.    return False
  15. '''
  16. def mnoj(arr, len, res):
  17.     for i in range(len):
  18.         if arr[i] == res:
  19.             return True
  20.     return False
  21.  
  22.  
  23.  
  24. def toDec(a):
  25.     num = 0
  26.     for i in range(SIZE - 1, -1, -1):
  27.         num += int(math.pow(2, (SIZE - 1) - i)) * a[i]
  28.     return num
  29.  
  30.  
  31. def updateY(a, x, y):
  32.     y[0] = not a[0] and not a[1] and not a[2]
  33.     y[1] = not a[0] and not a[1] and a[2]
  34.     y[2] = not a[0] and not a[1] and a[2]
  35.     y[3] = not a[0] and a[1] and not a[2] and x[0]
  36.     y[4] = not a[0] and a[1] and a[2]
  37.     y[5] = not a[0] and a[1] and a[2]
  38.     y[6] = not a[0] and a[1] and a[2]
  39.     y[7] = a[0] and not a[1] and not a[2] and x[1] and x[2]
  40.     y[8] = a[0] and not a[1] and a[2] and x[3]
  41.     y[9] = a[0] and not a[1] and not a[2] and x[1] and not x[2]
  42.  
  43.  
  44. def updateS(a, s, x):
  45.     s[0] = not a[0] and a[1] and a[2]
  46.     s[1] = (not a[0] and not a[1] and a[2]) or (a[0] and not a[1] and not a[2] and not x[0])
  47.     s[2] = (not a[0] and not a[1] and not a[2]) or (not a[0] and a[1] and not a[2] and not x[1]) or (not a[0] and a[1] and not a[2] and x[1]) or (a[0] and not a[1] and not a[2] and x[0] and x[1] and not x[2])
  48.  
  49.  
  50. # u1 u2 u3 в отчете (функции возбуждения)
  51. def updateR(a, x, r):
  52.     r[0] = (a[0] * (not a[1]) * (not a[2]) * x[0] * (not x[1])) + (a[0] * (not a[1]) * (not a[2]) * (not x[0])) + (a[0] * (not a[1]) * a[2] * x[2]) + (a[0] * (not a[1]) * a[2] * (not x[2]))
  53.     r[1] = (not a[0]) * a[1] * a[2]
  54.     r[2] = ((not a[0]) * (not a[1]) * a[2]) + ((not a[0]) * a[1] * a[2]) + (a[0] * (not a[1]) * a[2] * x[2]) + (a[0] * (not a[1]) * a[2] * (not x[2]))
  55.  
  56.  
  57. def changeA(a, s, r):
  58.     for i in range(1, A_SIZE):
  59.         if s[i]:
  60.             a[i] = True
  61.         if r[i]:
  62.             a[i] = False
  63.  
  64.  
  65. def printVector(v):
  66.     for i in range(1, Y_SIZE):
  67.         print(v[i], end=" ")
  68.     # print()
  69.  
  70.  
  71. def printVectorA(v):
  72.     for i in range(1, A_SIZE):
  73.         print(v[i], end=" ")
  74.     # print()
  75.  
  76.  
  77. def addState(S, a, y):
  78.     print("Состояние: S{}".format(S))
  79.     print("Вывод состояний a: ", end="")
  80.     printVectorA(a)
  81.     print("\nВывод состояний y: ", end="")
  82.     printVector(y)
  83.     print()
  84.  
  85.  
  86. check = int(input("1. Логические выражения. 2. Множество единичных наборов. "))
  87. if check == 1:
  88.     x = [False] * SIZE
  89.     for i in range(SIZE):
  90.         x[i] = bool(input("Введите x: ").split())
  91.  
  92.     start = time.perf_counter()
  93.  
  94.     a = [False] * (A_SIZE + 1)
  95.     y = [False] * (Y_SIZE + 1)
  96.     s = [False] * (S_SIZE + 1)
  97.     r = [False] * (R_SIZE + 1)
  98.     checkFirst = [False] * int(math.pow(2, SIZE))
  99.     checkSecond = [False] * int(math.pow(2, SIZE))
  100.     # checkFirst = [False] * (1 << SIZE)
  101.     # checkSecond = [False] * (1 << SIZE)
  102.     S = 0
  103.     while not checkSecond[S]:
  104.         S = toDec(a)
  105.         if checkFirst[S]:
  106.             checkSecond[S] = True
  107.         else:
  108.             checkFirst[S] = True
  109.         if checkSecond[S]:
  110.             break
  111.         addState(S, a, y)
  112.         updateY(a, x, y)
  113.         updateR(a, x, r)
  114.         updateS(a, s, x)
  115.         changeA(a, s, r)
  116.     if S == 0:
  117.         addState(S, a, y)
  118.         stop = time.perf_counter()
  119.         print(f"\nВремя работы: {stop - start:0.4f} секунд")
  120.         print("Конец")
  121.     else:
  122.         addState(S, a, y)
  123.         stop = time.perf_counter()
  124.         print(f"\nВремя работы: {stop - start:0.4f} секунд")
  125.         print("Цикл")
  126. else:
  127.     y1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  128.     y2 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
  129.     y3 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
  130.     y4 = [40, 41, 42, 43, 44, 45, 46, 47]
  131.     y5 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
  132.     y6 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
  133.     y7 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
  134.     y8 = [70, 71, 78, 79]
  135.     y9 = [81, 83, 85, 87, 89, 91, 93, 95]
  136.     y10 = [68, 69, 76, 77]
  137.     w1 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
  138.     w2 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 64, 65, 66, 67, 73, 74, 75, 72]
  139.     w3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 40, 41, 42, 43, 44, 45, 46, 47, 32, 33, 34, 35, 36, 37,
  140.           38, 39, 70, 71, 78, 79]
  141.     u1 = [68, 69, 77, 76, 64, 65, 66, 67, 73, 74, 75, 72, 81, 83, 85, 87, 89, 91, 93, 95, 80, 82, 84, 86, 88, 90, 92,
  142.           94]
  143.     u2 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
  144.     u3 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 81, 83, 85, 87, 89, 91, 93, 95, 80, 82, 84,
  145.           86, 88, 90, 92, 94, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
  146.  
  147.     X = [0]*4
  148.     Y = [0]*10
  149.     S = [0, 0, 0]
  150.  
  151.     for i in range(4):
  152.         X[i] = bool(input("Введите x: ").split())
  153.     CS = [0] * 7
  154.     res = 0
  155.  
  156.     start2 = time.perf_counter()
  157.  
  158.     for i in range(3):
  159.         CS[i] = S[i]
  160.     for i in range(4):
  161.         CS[i + 3] = X[i]
  162.     res = 0
  163.     for i in range(8):
  164.         for j in range(6, -1, -1):
  165.             res += pow(2, j) * CS[6 - j]
  166.         print("\nS(", end="")
  167.         for j in range(3):
  168.             print(S[j], end="")
  169.         if mnoj(y1, 16, res):
  170.             Y[0] = 1
  171.         else:
  172.             Y[0] = 0
  173.         if mnoj(y2, 16, res):
  174.             Y[1] = 1
  175.         else:
  176.             Y[1] = 0
  177.         if mnoj(y3, 16, res):
  178.             Y[2] = 1
  179.         else:
  180.             Y[2] = 0
  181.         if mnoj(y4, 8, res):
  182.             Y[3] = 1
  183.         else:
  184.             Y[3] = 0
  185.         if mnoj(y5, 16, res):
  186.             Y[4] = 1
  187.         else:
  188.             Y[4] = 0
  189.         if mnoj(y6, 16, res):
  190.             Y[5] = 1
  191.         else:
  192.             Y[5] = 0
  193.         if mnoj(y7, 16, res):
  194.             Y[6] = 1
  195.         else:
  196.             Y[6] = 0
  197.         if mnoj(y8, 4, res):
  198.             Y[7] = 1
  199.         else:
  200.             Y[7] = 0
  201.         if mnoj(y9, 8, res):
  202.             Y[8] = 1
  203.         else:
  204.             Y[8] = 0
  205.         if mnoj(y10, 4, res):
  206.             Y[9] = 1
  207.         else:
  208.             Y[9] = 0
  209.         if mnoj(w1, 16, res):
  210.             S[0] = 1
  211.         if mnoj(w2, 24, res):
  212.             S[1] = 1
  213.         if mnoj(w3, 36, res):
  214.             S[2] = 1
  215.         if mnoj(u1, 28, res):
  216.             S[0] = 0
  217.         if mnoj(u2, 16, res):
  218.             S[1] = 0
  219.         if mnoj(u3, 48, res):
  220.             S[2] = 0
  221.  
  222.         print(") -> S(", end="")
  223.         for j in range(3):
  224.             print(S[j], end="")
  225.         print(") Y(1-10):", end="")
  226.         for j in range(10):
  227.             print(" ", Y[j], end="")
  228.         for j in range(3):
  229.             CS[j] = S[j]
  230.         if S[0] + S[1] + S[2] == 0:
  231.             print("\nПрограмма завершена")
  232.             break
  233.  
  234.     stop2 = time.perf_counter()
  235.     print(f"\nВремя работы: {stop2 - start2:0.4f} секунд")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement