Advertisement
100hahahaha

Untitled

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