Лабораторная работа по «Автоматизированной системе обработки информации и управления»
Автор: przsergey • Июнь 23, 2023 • Лабораторная работа • 3,501 Слов (15 Страниц) • 195 Просмотры
ФГБОУ ВО
Ижевский Государственный Технический
Университет имени М. Т. Калашникова
Кафедра «Автоматизированные системы обработки информации и управления»
Отчет по лабораторной работе №1
Вариант 21
Выполнил:
Студент гр. Б22-782-1зу
Буланков Р. М.
Проверил:
Ст. преподаватель
Исупов Н. С.
Ижевск 2023
Цель работы: произвести сортировку элементов квадратной матрицы в соответствии с заданной схемой 3, порядком сортировки по не убыванию и методом упорядочивания перестановками.
Программный код:
import random
import tkinter as tk
root = tk.Tk()
root.configure(bg="#008080")
root.title("Буланков Р.М. Лабараторные 1,2")
# Переменные
# Лаб 1
StopSort = False
StopSort2 = False
StopSort3 = False
SortDone = False
Marker = None
OrderSort = [12, 8, 13, 14, 9, 4, 0, 5, 10, 15, 11, 6, 1, 2, 7, 3]
ListSort = [[3,0], [2,0], [3,1], [3,2], [2,1], [1,0], [0,0], [1,1],
[2,2], [3,3], [2,3], [1,2], [0,1], [0,2], [1,3], [0,3]]
SpeedSort = 0.025
#
# Лабараторная 1
#
#
# Структуры 1
table_canvas = tk.Canvas(root, bg="white", bd=2, relief="sunken", width=195, height=195)
# Функции
def generate_numbers():
table_canvas.delete("numbers") # Clear previously generated numbers
if StateButton.get() == "even":
for i in range(4):
for j in range(4):
x = i * 50 + 25
y = j * 50 + 25
number = random.randint(-99, 99)
table_canvas.create_text(x, y, text=str(number), tags="numbers")
else:
for i in range(4):
for j in range(4):
x = i * 50 + 25
y = j * 50 + 25
number = random.randint(0, 99)
table_canvas.create_text(x, y, text=str(number), tags="numbers")
def toggle_numbers():
StateButton.set("even" if StateButton.get() == "odd" else "odd")
generate_numbers()
toggle_button.config(text="Нечетные" if StateButton.get() == "odd" else "Четные")
def toggle_highlight():
global label, toggle_button, progressbar, StopSort, StopSort2 , StopSort3, SortDone
StateButton2.set("eve" if StateButton2.get() == "odo" else "odo")
toggle_highlight_button.config(text="Сортировка!" if StateButton2.get() == "odo" else "Повторить")
StopSort = False
if StateButton2.get() == "eve":
StopSort2 = True
StopSort3 = True
numbers = [int(table_canvas.itemcget(tag, "text")) for tag in table_canvas.find_withtag("numbers")]
threading.Thread(target=bubble_sort, args=(numbers,)).start()
toggle_button.grid_forget()
label.grid(row=3, padx=40, pady=40, sticky="wn")
elif SortDone == True:
SortDone = False
for r in range(16):
table_canvas.delete('square' + str(r))
table_canvas.delete('square')
generate_numbers()
label.grid_forget()
progressbar.grid_forget()
StopSort2 = False
StopSort3 = False
label.config(text="Сортировка ...", width=11, height=1, relief="sunken")
toggle_button.grid(row=3, padx=40, pady=40, sticky="w")
else:
StopSort = True
toggle_highlight_button.config(state="disabled")
root.update()
for r in range(16):
table_canvas.delete('square' + str(r))
table_canvas.delete('square')
generate_numbers()
label.grid_forget()
progressbar.grid_forget()
StopSort2 = False
StopSort3 = False
label.config(text="Сортировка ...", width=11, height=1, relief="sunken")
toggle_button.grid(row=3, padx=40, pady=40, sticky="w")
def bubble_sort(numbers):
global Marker, StopSort2, progressbar, SortDone
Marker = 16
progressbar.configure(value=0)
progressbar.grid(row=3, padx=40, pady=40, sticky="ws")
if SpeedSort == 0:
for i in range(15):
progressbar["value"] = i
root.update_idletasks()
Marker = Marker - 1
for j in range(Marker):
try:
if StopSort == True:
toggle_highlight_button.config(state="normal")
root.update()
return
if numbers[OrderSort[j]] > numbers[OrderSort[j + 1]]:
numbers[OrderSort[j]], numbers[OrderSort[j + 1]] = \
numbers[OrderSort[j + 1]], numbers[OrderSort[j]]
table_canvas.delete("numbers")
except IndexError:
pass
side = 49
x, y = ListSort[Marker]
x1, y1 = x * 50 - 23 + side // 2, y * 50 - 23 + side // 2
x2, y2 = x * 50 + 26 + side // 2, y * 50 + 26 + side // 2
table_canvas.create_rectangle(x1, y1, x2, y2, fill='steel blue', outline='', tags=f'square')
side = 49
x, y = ListSort[0]
x1, y1 = x * 50 - 23 + side // 2, y * 50 - 23 + side // 2
x2, y2 = x * 50 + 26 + side // 2, y * 50 + 26 + side // 2
table_canvas.create_rectangle(x1, y1, x2, y2, fill='steel blue', outline='', tags=f'square')
for k, number in enumerate(numbers):
y = (k % 4) * 50 + 25
x = (k // 4) * 50 + 25
table_canvas.create_text(x, y, text=str(number), tags="numbers")
progressbar.grid_forget()
label.config(text="Готово!", width=11, height=2, relief="sunken")
StopSort2 = False
SortDone = True
else:
for i in range(15):
progressbar["value"] = i
root.update_idletasks()
Marker = Marker - 1
for j in range(Marker):
try:
if StopSort == True:
toggle_highlight_button.config(state="normal")
root.update()
return
if SpeedSort == 0:
progressbar.grid_forget()
bubble_sort(numbers)
return
if numbers[OrderSort[j]] > numbers[OrderSort[j+1]]:
numbers[OrderSort[j]], numbers[OrderSort[j+1]] = \
numbers[OrderSort[j+1]], numbers[OrderSort[j]]
table_canvas.delete("numbers")
for k, number in enumerate(numbers):
y = (k % 4) * 50 + 25
x = (k // 4) * 50 + 25
table_canvas.create_text(x, y, text=str(number), tags="numbers")
highlight(numbers, j, "pink")
time.sleep(SpeedSort*5)
else:
highlight(numbers, j, "slategray2")
time.sleep(SpeedSort)
except IndexError:
pass
for r in range(16):
table_canvas.delete('square' + str(r))
side = 49
x, y = ListSort[0]
x1, y1 = x * 50 - 23 + side // 2, y * 50 - 23 + side // 2
x2, y2 = x * 50 + 26 + side // 2, y * 50 + 26 + side // 2
table_canvas.create_rectangle(x1, y1, x2, y2, fill='steel blue', outline='', tags=f'square')
x = x * 50 + 25
y = y * 50 + 25
table_canvas.create_text(x, y, text=str(numbers[OrderSort[0]]), tags="numbers")
x, y = ListSort[1]
x1, y1 = x * 50 - 23 + side // 2, y * 50 - 23 + side // 2
x2, y2 = x * 50 + 26 + side // 2, y * 50 + 26 + side // 2
table_canvas.create_rectangle(x1, y1, x2, y2, fill='steel blue', outline='', tags=f'square')
x = x * 50 + 25
y = y * 50 + 25
table_canvas.create_text(x, y, text=str(numbers[OrderSort[1]]), tags="numbers")
progressbar.grid_forget()
label.config(text="Готово!", width=11, height=2, relief="sunken")
SortDone = True
StopSort2 = False
def highlight(numbers, index, color):
for k, number in enumerate(numbers):
if StopSort == True:
toggle_highlight_button.config(state="normal")
root.update()
return
if SpeedSort == 0:
progressbar.grid_forget()
bubble_sort(numbers)
return
if k == index:
side = 49
x, y = ListSort[index]
x1, y1 = x * 50 - 23 + side//2, y * 50 - 23 + side//2
x2, y2 = x * 50 + 26 + side//2, y * 50 + 26 + side//2
table_canvas.create_rectangle(x1, y1, x2, y2, fill=str(color), outline='', tags=f'square' + str(index))
x = x * 50 + 25
y = y * 50 + 25
table_canvas.create_text(x, y, text=str(numbers[OrderSort[index]]), tags="numbers")
x, y = ListSort[index + 1]
x1, y1 = x * 50 - 23 + side // 2, y * 50 - 23 + side // 2
x2, y2 = x * 50 + 26 + side // 2, y * 50 + 26 + side // 2
table_canvas.create_rectangle(x1, y1, x2, y2, fill=str(color), outline='',
tags=f'square' + str(index + 1))
x = x * 50 + 25
y = y * 50 + 25
table_canvas.create_text(x, y, text=str(numbers[OrderSort[index + 1]]), tags="numbers")
elif k < Marker + 1:
table_canvas.delete('square' + str(index - 1))
table_canvas.delete('square' + str(index + Marker))
else:
side = 49
x, y = ListSort[Marker + 1]
x1, y1 = x * 50 - 23 + side // 2, y * 50 - 23 + side // 2
x2, y2 = x * 50 + 26 + side // 2, y * 50 + 26 + side // 2
table_canvas.create_rectangle(x1, y1, x2, y2, fill='steel blue', outline='', tags=f'square')
x = x * 50 + 25
y = y * 50 + 25
table_canvas.create_text(x, y, text=str(numbers[OrderSort[Marker+1]]), tags="numbers")
def update_SpeedSort(val):
global SpeedSort
box = slider.get()
if box == 0:
SpeedSort = 0
elif box == 1:
SpeedSort = 0.025
elif box == 2:
SpeedSort = 0.05
elif box == 3:
SpeedSort = 0.075
elif box == 4:
SpeedSort = 0.1
return
# Структуры 2
lab1_label = tk.Label(root, text="Лабораторная 1\n\nПроизвести сортировку элементов квадратной матрицы "
"в\nсоответствии с заданной схемой, порядком сортировки по\nне убыванию"
" и методом упорядочивания «Пузырьком»",
font=("Times New Roman", 12), bg="white", fg="black", width=49, height=6, justify="left")
table_canvas2 = tk.Canvas(root, bg="white", bd=2, relief="sunken", width=195, height=195)
toggle_button = tk.Button(root, text="Нечетные" if StateButton.get() == "odd" else "Четные",
command=toggle_numbers,width=11, height=2, bg="burlywood",
activebackground="darksalmon")
toggle_highlight_button = tk.Button(root, text="Сортировка!" if StateButton2.get() == "odo" else "Повторить",
command=toggle_highlight, width=11, height=2, bg="burlywood",
activebackground="darksalmon")
label2 = tk.Label(root, text="Скорость", width=8, height=2, anchor='w')
label3 = tk.Label(root, text="x1.0 x0.8 x0.6 x0.4 x0.2", width=20, height=2, anchor='s')
slider = tk.Scale(root, from_=0, to=4, length=137, resolution=1,
orient=tk.HORIZONTAL, command=update_SpeedSort, showvalue=False)
slider.set(1)
progressbar = ttk.Progressbar(root, orient="horizontal", length=83, mode="determinate",
maximum=14, style="burlywood.Horizontal.TProgressbar")
progressbar.grid_forget()
label = tk.Label(root, text="Сортировка ...", width=11, height=1, relief="sunken")
label.grid_forget()
# Стили
style = ttk.Style()
style.theme_use("default")
style.configure("burlywood.Horizontal.TProgressbar", foreground='burlywood', background='burlywood')
# Рисунки в таблицах
table_canvas.create_line(50, 0, 50, 210, width=1)
table_canvas.create_line(100, 0, 100, 210, width=1)
table_canvas.create_line(150, 0, 150, 210, width=1)
table_canvas.create_line(0, 50, 210, 50, width=1)
table_canvas.create_line(0, 100, 210, 100, width=1)
table_canvas.create_line(0, 150, 210, 150, width=1)
table_canvas2.create_line(50, 0, 50, 210, width=1)
table_canvas2.create_line(100, 0, 100, 210, width=1)
table_canvas2.create_line(150, 0, 150, 210, width=1)
table_canvas2.create_line(0, 50, 210, 50, width=1)
table_canvas2.create_line(0, 100, 210, 100, width=1)
table_canvas2.create_line(0, 150, 210, 150, width=1)
table_canvas2.create_line(125, 25, 175, 25, width=2)
table_canvas2.create_line(125, 25, 175, 75, width=2)
table_canvas2.create_line(175, 75, 175, 125, width=2)
table_canvas2.create_line(75, 25, 175, 125, width=2)
table_canvas2.create_line(25, 25, 75, 25, width=2)
table_canvas2.create_line(25, 25, 175, 175, width=2)
table_canvas2.create_line(125, 175, 175, 175, width=2)
table_canvas2.create_line(25, 75, 125, 175, width=2)
table_canvas2.create_line(25, 125, 25, 75, width=2)
table_canvas2.create_line(25, 125, 75, 175, width=2)
table_canvas2.create_line(25, 175, 75, 175, arrow="first", width=2)
...