Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import time
- SIZE = 4
- A_SIZE = 3
- Y_SIZE = 11
- S_SIZE = 3
- R_SIZE = 3
- def mnoj(arr, len, res): #функция для множества единичных наборов
- for i in range(len):
- if arr[i] == res:
- return True
- return False
- def toDec(a):
- num = 0
- for i in range(SIZE - 1, -1, -1):
- num += int(math.pow(2, (SIZE - 1) - i)) * a[i]
- return num
- def updateY(a, x, y): # функции выхода
- y[1] = not a[1] and not a[2] and not a[3]
- y[2] = not a[1] and not a[2] and a[3]
- y[3] = not a[1] and not a[2] and a[3]
- y[4] = not a[1] and a[2] and not a[3] and x[0]
- y[5] = not a[1] and a[2] and a[3]
- y[6] = not a[1] and a[2] and a[3]
- y[7] = not a[1] and a[2] and a[3]
- y[8] = a[1] and not a[2] and not a[3] and x[1] and x[2]
- y[9] = a[1] and not a[2] and a[3] and x[3]
- y[10] = a[1] and not a[2] and not a[3] and x[1] and not x[2]
- def updateS(a, s, x): # w1 w2 w3 в отчете (функции возбуждения)
- s[1] = not a[1] and a[2] and a[3]
- 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]
- 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[
- 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]
- def updateR(a, x, r): # u1 u2 u3 в отчете (функции возбуждения)
- 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[
- 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]
- r[2] = not a[1] and a[2] and a[3]
- 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[
- 1] and not a[2] and a[3] and not x[3]
- def changeA(a, s, r):
- for i in range(1, A_SIZE + 1):
- if s[i]:
- a[i] = True
- if r[i]:
- a[i] = False
- def printVector(v):
- for i in range(1, Y_SIZE):
- print(v[i], end=" ")
- print()
- def printVectorA(v):
- for i in range(1, len(v)):
- print(v[i], end=" ")
- print()
- def addState(S, a, y):
- print(f"Состояние: {S}")
- print("Вывод состояний a: ", end="")
- printVectorA(a)
- print("\nВывод состояний y: ", end="")
- printVector(y)
- print()
- check = int(input("1. Логические выражения. 2. Множество единичных наборов. "))
- if check == 1:
- x = [False] * SIZE
- for i in range(SIZE):
- x[i] = bool(input("Введите x: ").split())
- start = time.perf_counter()
- a = [False] * (A_SIZE + 1)
- y = [False] * (Y_SIZE + 1)
- s = [False] * (S_SIZE + 1)
- r = [False] * (R_SIZE + 1)
- # checkFirst = [False] * int(math.pow(2, SIZE))
- # checkSecond = [False] * int(math.pow(2, SIZE))
- checkFirst = [False] * (1 << SIZE)
- checkSecond = [False] * (1 << SIZE)
- S = 0
- while not checkSecond[S]:
- S = toDec(a)
- if checkFirst[S]:
- checkSecond[S] = True
- else:
- checkFirst[S] = True
- if checkSecond[S]:
- break
- addState(S, a, y)
- updateY(a, x, y)
- updateR(a, x, r)
- updateS(a, s, x)
- changeA(a, s, r)
- if S == 0:
- addState(S, a, y)
- stop = time.perf_counter()
- print(f"\nВремя работы: {stop - start:0.4f} секунд")
- print("Конец")
- else:
- addState(S, a, y)
- stop = time.perf_counter()
- print(f"\nВремя работы: {stop - start:0.4f} секунд")
- print("Цикл")
- else: #множество единичных наборов
- y1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
- y2 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
- y3 = [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
- y4 = [40, 41, 42, 43, 44, 45, 46, 47]
- y5 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
- y6 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
- y7 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
- y8 = [70, 71, 78, 79]
- y9 = [81, 83, 85, 87, 89, 91, 93, 95]
- y10 = [68, 69, 76, 77]
- w1 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
- 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]
- 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,
- 38, 39, 70, 71, 78, 79]
- 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,
- 94]
- u2 = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
- 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,
- 86, 88, 90, 92, 94, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
- X = []
- Y = []
- S = [0, 0, 0]
- for i in range(4):
- X.append(bool(input()))
- start2 = time.perf_counter()
- for i in range(7):
- CS = S.copy()
- for j in range(4):
- CS.append(X[j])
- res = 0
- for j in range(6, -1, -1):
- res += pow(2, j) * CS[6 - j]
- print(f"S({S[0]}{S[1]}{S[2]})")
- if res in y1:
- Y.append(1)
- else:
- Y.append(0)
- if res in y2:
- Y.append(1)
- else:
- Y.append(0)
- if res in y3:
- Y.append(1)
- else:
- Y.append(0)
- if res in y4:
- Y.append(1)
- else:
- Y.append(0)
- if res in y5:
- Y.append(1)
- else:
- Y.append(0)
- if res in y6:
- Y.append(1)
- else:
- Y.append(0)
- if res in y7:
- Y.append(1)
- else:
- Y.append(0)
- if res in y8:
- Y.append(1)
- else:
- Y.append(0)
- if res in y9:
- Y.append(1)
- else:
- Y.append(0)
- if res in y10:
- Y.append(1)
- else:
- Y.append(0)
- res = 0
- if mnoj(w1, 16, res):
- S[0] = 1
- if mnoj(w2, 24, res):
- S[1] = 1
- if mnoj(w3, 36, res):
- S[2] = 1
- if mnoj(u1, 28, res):
- S[0] = 0
- if mnoj(u2, 16, res):
- S[1] = 0
- if mnoj(u3, 48, res):
- S[2] = 0
- print(") -> S(", end="")
- for j in range(3):
- print(S[j], end="")
- print(") Y(1-10):", end="")
- for j in range(10):
- print(" ", Y[j], end="")
- for j in range(3):
- CS[j] = S[j]
- if S[0] + S[1] + S[2] == 0:
- print("\nПрограмма завершена")
- break
- stop2 = time.perf_counter()
- print(f"\nВремя работы: {stop2 - start2:0.4f} секунд")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement