Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. def pirs(operator,main_list,operator1):
  2.     if main_list[operator-1]!=')' and main_list[operator+1]!='(':
  3.         x = main_list[operator-1]
  4.         y = main_list[operator+1]
  5.         del(main_list[operator])
  6.         del(main_list[operator])
  7.         if operator1 =='-':
  8.             main_list[i-1]= x + '(' + y + '↓' + y + ')'
  9.         if operator1 =='*':
  10.             main_list[i-1]="((" + x + '↓' + x + ") ↓ (" + y + '↓' + y + "))"
  11.         if operator1 =='+':
  12.             main_list[i-1]="((" + x + '↓' + y + ") ↓ (" + x + '↓' + y + "))"
  13.         if operator1 =='%':
  14.            main_list[i-1]= '(' + x + "↓(" + x + '↓' + y + "))"
  15.     elif main_list[operator-1]==')' and main_list[operator+1]!='(':
  16.         x = main_list[operator-2]
  17.         y = main_list[operator+1]
  18.         del(main_list[operator])
  19.         del(main_list[operator])
  20.         if operator1 =='-':
  21.             main_list[i-2]= x + '(' + y + '↓' + y + ')'
  22.         if operator1 =='*':
  23.             main_list[i-2]="((" + x + '↓' + x + ") ↓ (" + y + '↓' + y + "))"
  24.         if operator1 =='+':
  25.             main_list[i-2]="((" + x + '↓' + y + ") ↓ (" + x + '↓' + y + "))"
  26.         if operator1 =='%':
  27.            main_list[i-2]= '(' + x + "↓↓(" + x + '↓' + y + "))"
  28. def pirs1(operator,main_list,operator1):
  29.     x = main_list[operator-1]
  30.     y = main_list[operator+2]
  31.     del(main_list[operator-1])
  32.     del(main_list[operator-1])
  33.     if operator1 =='-':
  34.         main_list[i]= x + '(' + y + '|' + y + ')'
  35.     if operator1 =='*':
  36.         main_list[i]="((" + x + '↓' + x + ") ↓ (" + y + '↓' + y + "))"
  37.     if operator1 =='+':
  38.         main_list[i]="((" + x + '↓' + y + ") ↓ (" + x + '↓' + y + "))"
  39.     if operator1 =='%':
  40.        main_list[i]= '(' + x + "↓(" + x + '↓' + y + "))"  
  41. a=['%','-','+','*']
  42. while True:
  43.     try:
  44.         line = input("Введите формулу\n")
  45.         line = line.replace('%', ' % ').replace('-', ' - ').replace('+', ' + ').replace('*', ' * ').replace(')', ' ) ').replace('(', ' ( ')
  46.         lst1 = list(line.split())
  47.         lst =(line.replace('%', ' ').replace('-', ' ').replace('+', ' ').replace('*', ' ').replace(')', ' ').replace('(', ' ').split())
  48.         lst= list(dict.fromkeys(lst))
  49.         for i in range(len(lst)):
  50.             if lst[i+i] == '0':
  51.                 lst.insert(i+i+1,'0')
  52.             elif lst[i+i] == '1':
  53.                 lst.insert(i+i+1,'1')
  54.             else:
  55.                 print(lst[i+i]," = ",end="")
  56.                 lst.insert(i+i+1,(input()))
  57.         lst = dict(zip(lst[::2],lst[1::2]))
  58.         for item in lst1:
  59.            if item in lst:
  60.                 lst1[lst1.index(item)]=lst[item]
  61.         work=lst1
  62.         lst1 = ''.join(lst1)
  63.         lst1 = lst1.replace('*',' and ').replace('+',' or ').replace('-',' is not ').replace('%',' ^ ')
  64.         eval(lst1)
  65.         i=0
  66.         while set(a).intersection(set(work))!=set():
  67.             i=0
  68.             while i < (len(work)-1):
  69.                 if work[i]=='*' or work[i]=='-' or work[i]=='+' or work[i]=='%':
  70.                     if  i < (len(work)-1) :
  71.                         if work[i+1]!='(':
  72.                             pirs(i,work,work[i])
  73.                             i=i-1
  74.                         elif work[i+1]=='(' and work[i+3]==')':
  75.                               pirs1(i,work,work[i])
  76.                               i=i-1
  77.                         else:
  78.                             i=i+1
  79.                 else:
  80.                     i=i+1
  81.         print(*work)
  82.     except:
  83.         print("Something went wrong!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement