Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def pirs(operator,main_list,operator1):
- if main_list[operator-1]!=')' and main_list[operator+1]!='(':
- x = main_list[operator-1]
- y = main_list[operator+1]
- del(main_list[operator])
- del(main_list[operator])
- if operator1 =='-':
- main_list[i-1]= x + '(' + y + '↓' + y + ')'
- if operator1 =='*':
- main_list[i-1]="((" + x + '↓' + x + ") ↓ (" + y + '↓' + y + "))"
- if operator1 =='+':
- main_list[i-1]="((" + x + '↓' + y + ") ↓ (" + x + '↓' + y + "))"
- if operator1 =='%':
- main_list[i-1]= '(' + x + "↓(" + x + '↓' + y + "))"
- elif main_list[operator-1]==')' and main_list[operator+1]!='(':
- x = main_list[operator-2]
- y = main_list[operator+1]
- del(main_list[operator])
- del(main_list[operator])
- if operator1 =='-':
- main_list[i-2]= x + '(' + y + '↓' + y + ')'
- if operator1 =='*':
- main_list[i-2]="((" + x + '↓' + x + ") ↓ (" + y + '↓' + y + "))"
- if operator1 =='+':
- main_list[i-2]="((" + x + '↓' + y + ") ↓ (" + x + '↓' + y + "))"
- if operator1 =='%':
- main_list[i-2]= '(' + x + "↓↓(" + x + '↓' + y + "))"
- def pirs1(operator,main_list,operator1):
- x = main_list[operator-1]
- y = main_list[operator+2]
- del(main_list[operator-1])
- del(main_list[operator-1])
- if operator1 =='-':
- main_list[i]= x + '(' + y + '|' + y + ')'
- if operator1 =='*':
- main_list[i]="((" + x + '↓' + x + ") ↓ (" + y + '↓' + y + "))"
- if operator1 =='+':
- main_list[i]="((" + x + '↓' + y + ") ↓ (" + x + '↓' + y + "))"
- if operator1 =='%':
- main_list[i]= '(' + x + "↓(" + x + '↓' + y + "))"
- a=['%','-','+','*']
- while True:
- try:
- line = input("Введите формулу\n")
- line = line.replace('%', ' % ').replace('-', ' - ').replace('+', ' + ').replace('*', ' * ').replace(')', ' ) ').replace('(', ' ( ')
- lst1 = list(line.split())
- lst =(line.replace('%', ' ').replace('-', ' ').replace('+', ' ').replace('*', ' ').replace(')', ' ').replace('(', ' ').split())
- lst= list(dict.fromkeys(lst))
- for i in range(len(lst)):
- if lst[i+i] == '0':
- lst.insert(i+i+1,'0')
- elif lst[i+i] == '1':
- lst.insert(i+i+1,'1')
- else:
- print(lst[i+i]," = ",end="")
- lst.insert(i+i+1,(input()))
- lst = dict(zip(lst[::2],lst[1::2]))
- for item in lst1:
- if item in lst:
- lst1[lst1.index(item)]=lst[item]
- work=lst1
- lst1 = ''.join(lst1)
- lst1 = lst1.replace('*',' and ').replace('+',' or ').replace('-',' is not ').replace('%',' ^ ')
- eval(lst1)
- i=0
- while set(a).intersection(set(work))!=set():
- i=0
- while i < (len(work)-1):
- if work[i]=='*' or work[i]=='-' or work[i]=='+' or work[i]=='%':
- if i < (len(work)-1) :
- if work[i+1]!='(':
- pirs(i,work,work[i])
- i=i-1
- elif work[i+1]=='(' and work[i+3]==')':
- pirs1(i,work,work[i])
- i=i-1
- else:
- i=i+1
- else:
- i=i+1
- print(*work)
- except:
- print("Something went wrong!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement