def check_brackets(): if (global_func_val.token == "("): global_func_val.token = global_func_val.get_token( global_func_val.infix_string) check_binary_plus_minus() if (global_func_val.token != ")"): print("Неизвестная ошибка со скобками") else: global_func_val.token = global_func_val.get_token( global_func_val.infix_string) else: check_number()
def check_unary_minus(): check_brackets() while (global_func_val.token == "!"): tmp_token = global_func_val.token global_func_val.token = global_func_val.get_token( global_func_val.infix_string) check_brackets() global_func_val.postfix_string += tmp_token + " "
def check_binary_mul_div(): check_unary_minus() while (global_func_val.token == "*" or global_func_val.token == "/"): tmp_token = global_func_val.token global_func_val.token = global_func_val.get_token( global_func_val.infix_string) check_unary_minus() global_func_val.postfix_string += tmp_token + " "
def translate_button(self): start_time_traslate = 0 end_time_traslate = 0 global_func_val.input_string = self.ui.lineEdit.text().replace( " ", "") self.ui.lineEdit.setText(global_func_val.input_string) self.ui.textBrowser.setText("") self.ui.lineEdit_2.setText("") row_count = self.ui.tableWidget.rowCount() while (row_count >= 0): self.ui.tableWidget.removeRow(row_count) row_count -= 1 if (global_func_val.input_string == ""): self.ui.textBrowser.append( "Вам необходимо ввести исходное выражение") else: global_func_val.folding_string = global_func_val.folding( global_func_val.input_string) self.ui.textBrowser.append("Упрощаем введенное выражение...") self.ui.textBrowser.append("Упрощенное выражение: " + global_func_val.folding_string) global_func_val.infix_string = global_func_val.unary_minus_coding( global_func_val.folding_string) self.ui.textBrowser.append("Кодируем унарный минус...") self.ui.textBrowser.append("Закодированное выражение: " + global_func_val.infix_string) global_func_val.index = 0 global_func_val.token = "" global_func_val.postfix_string = "" global_func_val.result_string = "" global_func_val.num_brackets = 0 isOk = global_func_val.string_validation( global_func_val.infix_string) if (isOk == None): self.ui.textBrowser.append("Переводим в постфиксную запись...") global_func_val.index = 0 global_func_val.token = "" global_func_val.postfix_string = "" global_func_val.result_string = "" global_func_val.num_brackets = 0 global_func_val.token = global_func_val.get_token( global_func_val.infix_string) if (self.ui.radioButton.isChecked()): start_time_traslate = time.time() sort_station.dijkstra_method() end_time_traslate = time.time() else: start_time_traslate = time.time() recursive_descent.check_binary_plus_minus() end_time_traslate = time.time() self.ui.textBrowser.append( "Выражение переведно в постфиксную запись") self.ui.textBrowser.append( "Для перевода потребовалось: " + str((end_time_traslate - start_time_traslate) * 1000000) + " миллисекунд") global_func_val.result_string = global_func_val.postfix_string.replace( "!", "-") self.ui.lineEdit_2.setText(global_func_val.result_string) elif (isOk == 0): errorMessage = 'Первый токен введен неверно' self.ui.textBrowser.append(errorMessage) elif (isOk == 1): errorMessage = 'Последний токен введен неверно' self.ui.textBrowser.append(errorMessage) elif (isOk == 2): errorMessage = "Количество '(' не соответсвует количеству ')'" self.ui.textBrowser.append(errorMessage) elif (isOk == 3): errorMessage = 'Неизвестная ошибка' self.ui.textBrowser.append(errorMessage) else: if isOk[1] == -1: errorMessage = 'После ' + (isOk[0].__str__()) + ' ' + 'с неизвестной позицей' + \ ' стоит некорректный токен: ' + global_func_val.input_string else: errorMessage = 'После ' + (isOk[0].__str__( )) + ' ' + 'с позицей ' + ( isOk[1].__str__() ) + ' стоит некорректный токен: ' + global_func_val.input_string[ 0: isOk[1]] + "<font color=\"Red\">" + global_func_val.input_string[ isOk[1]:isOk[ 2]] + "</font>" + global_func_val.input_string[ isOk[2]:len(global_func_val.input_string)] self.ui.textBrowser.append(errorMessage)
def find_value_button(self): if (global_func_val.postfix_string == ""): self.ui.textBrowser.setText("") self.ui.textBrowser.append( "Введите исходное выражение и переведите его в постфиксную форму" ) else: row_count = self.ui.tableWidget.rowCount() while (row_count >= 0): self.ui.tableWidget.removeRow(row_count) row_count -= 1 self.ui.textBrowser.append("Находим значение выражения...") start_time_find_value = 0 end_time_find_value = 0 row = 0 stack = [] global_func_val.index = 0 tmp_token = global_func_val.get_token( global_func_val.postfix_string) start_time_find_value = time.time() while tmp_token != None: if global_func_val.isNum(tmp_token): stack.append(tmp_token) data = [] data.append(QtWidgets.QTableWidgetItem(tmp_token)) data[0].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem( "поместить в вершину стека")) data[1].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem(stack.__str__().replace( "'", "").replace(",", " ").replace("[", "").replace("]", ""))) data[2].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.ui.tableWidget.insertRow(row) for i in range(3): self.ui.tableWidget.setItem(row, i, data[i]) row += 1 elif (tmp_token == "!"): tmp_val_1 = stack.pop() tmp_val_1 = round((float(tmp_val_1) * -1), 4) stack.append(tmp_val_1) data = [] data.append(QtWidgets.QTableWidgetItem("-")) data[0].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem( "сменить знак первого операнда в вершине стека")) data[1].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem(stack.__str__().replace( "'", "").replace(",", " ").replace("[", "").replace("]", ""))) data[2].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.ui.tableWidget.insertRow(row) for i in range(3): self.ui.tableWidget.setItem(row, i, data[i]) row += 1 elif (tmp_token == "+"): tmp_val_2 = stack.pop() tmp_val_1 = stack.pop() stack.append( round((float(tmp_val_1) + float(tmp_val_2)), 4)) data = [] data.append(QtWidgets.QTableWidgetItem(tmp_token)) data[0].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem( "сложение двух первых операндов в вершине стека")) data[1].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem(stack.__str__().replace( "'", "").replace(",", " ").replace("[", "").replace("]", ""))) data[2].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.ui.tableWidget.insertRow(row) for i in range(3): self.ui.tableWidget.setItem(row, i, data[i]) row += 1 elif (tmp_token == "-"): tmp_val_2 = stack.pop() tmp_val_1 = stack.pop() stack.append( round((float(tmp_val_1) - float(tmp_val_2)), 4)) data = [] data.append(QtWidgets.QTableWidgetItem(tmp_token)) data[0].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem( "вычитание двух первых операндов в вершине стека")) data[1].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem(stack.__str__().replace( "'", "").replace(",", " ").replace("[", "").replace("]", ""))) data[2].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.ui.tableWidget.insertRow(row) for i in range(3): self.ui.tableWidget.setItem(row, i, data[i]) row += 1 elif (tmp_token == "*"): tmp_val_2 = stack.pop() tmp_val_1 = stack.pop() stack.append( round((float(tmp_val_1) * float(tmp_val_2)), 4)) data = [] data.append(QtWidgets.QTableWidgetItem(tmp_token)) data[0].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem( "умножение двух первых операндов в вершине стека")) data[1].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem(stack.__str__().replace( "'", "").replace(",", " ").replace("[", "").replace("]", ""))) data[2].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.ui.tableWidget.insertRow(row) for i in range(3): self.ui.tableWidget.setItem(row, i, data[i]) row += 1 elif (tmp_token == "/"): tmp_val_2 = stack.pop() tmp_val_1 = stack.pop() try: stack.append( round((float(tmp_val_1) / float(tmp_val_2)), 4)) except ZeroDivisionError: self.ui.textBrowser.append("Обнаружено деление на 0!") return "" data = [] data.append(QtWidgets.QTableWidgetItem(tmp_token)) data[0].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem( "деление двух первых операндов в вершине стека")) data[1].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) data.append( QtWidgets.QTableWidgetItem(stack.__str__().replace( "'", "").replace(",", " ").replace("[", "").replace("]", ""))) data[2].setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) self.ui.tableWidget.insertRow(row) for i in range(3): self.ui.tableWidget.setItem(row, i, data[i]) row += 1 tmp_token = global_func_val.get_token( global_func_val.postfix_string) end_time_find_value = time.time() self.ui.textBrowser.append("Значение выражения = " + str(stack.pop())) self.ui.textBrowser.append("Для решения потребовалось: " + str( (end_time_find_value - start_time_find_value) * 1000) + " миллисекунд")
def check_number(): if global_func_val.isNum(global_func_val.token): global_func_val.postfix_string += global_func_val.token + " " global_func_val.token = global_func_val.get_token( global_func_val.infix_string)
def dijkstra_method(): global stack while global_func_val.token != None: if (global_func_val.isNum(global_func_val.token)): global_func_val.postfix_string += global_func_val.token + " " elif (global_func_val.token == "!"): if (len(stack) == 0): stack.append(global_func_val.token) else: tmp_token = stack.pop() if (tmp_token != "!"): stack.append(tmp_token) stack.append(global_func_val.token) else: while (tmp_token == "!"): global_func_val.postfix_string += tmp_token + " " if (len(stack) != 0): tmp_token = stack.pop() else: tmp_token = "" if (tmp_token != ""): stack.append(tmp_token) stack.append(global_func_val.token) elif (global_func_val.token == "*" or global_func_val.token == "/"): if (len(stack) == 0): stack.append(global_func_val.token) else: tmp_token = stack.pop() if (tmp_token == "+" or tmp_token == "-" or tmp_token == "("): stack.append(tmp_token) stack.append(global_func_val.token) else: while (tmp_token == "!" or tmp_token == "*" or tmp_token == "/"): global_func_val.postfix_string += tmp_token + " " if (len(stack) != 0): tmp_token = stack.pop() else: tmp_token = "" if (tmp_token != ""): stack.append(tmp_token) stack.append(global_func_val.token) elif (global_func_val.token == "+" or global_func_val.token == "-"): if (len(stack) == 0): stack.append(global_func_val.token) else: tmp_token = stack.pop() if (tmp_token == "("): stack.append(tmp_token) stack.append(global_func_val.token) else: while (tmp_token != "(" and tmp_token != ""): global_func_val.postfix_string += tmp_token + " " if (len(stack) != 0): tmp_token = stack.pop() else: tmp_token = "" if (tmp_token != ""): stack.append(tmp_token) stack.append(global_func_val.token) elif (global_func_val.token == "("): stack.append(global_func_val.token) elif (global_func_val.token == ")"): if (len(stack) != 0): tmp_token = stack.pop() while (tmp_token != "(" and tmp_token != ""): if (len(stack) != 0): global_func_val.postfix_string += tmp_token + " " tmp_token = stack.pop() else: tmp_token = "" global_func_val.token = global_func_val.get_token( global_func_val.infix_string) while (len(stack) != 0): global_func_val.postfix_string += stack.pop() + " "