Advertisement
Guest User

Untitled

a guest
May 24th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. from __future__ import unicode_literals
  4. import sys
  5. import os
  6. import random
  7. from PyQt5 import QtCore, QtWidgets
  8. from PyQt5 import QtGui
  9. from PyQt5.QtWidgets import *
  10. from PyQt5.QtGui import QIcon, QFont, QColor
  11. import matplotlib
  12. import matplotlib.pyplot as plt
  13. import numpy as np
  14. import matplotlib.pyplot as plt
  15. from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
  16. from numpy import arange, sin, pi
  17. from pylab import figure, axes, pie, title, show
  18. from matplotlib.figure import Figure
  19.  
  20. class diff_window(QWidget):
  21. def __init__(self):
  22. super().__init__()
  23.  
  24. self.initDiagram()
  25.  
  26. def initDiagram(self):
  27.  
  28. fig = plt.figure()
  29. ax = fig.add_subplot(111) # добавление области рисования ax
  30.  
  31. N = len(ex.arr)
  32. x = ex.arr
  33. y = 100.*np.random.rand(N)
  34.  
  35. rgb = np.array([204,255,51])/255.
  36. myhex = '#660099'
  37.  
  38. ax.plot(x, y, color=myhex)
  39. ax.bar(x, y, color=rgb, alpha=0.75, align='center')
  40.  
  41. ax.set_xticks(x) # установка делений на оси OX
  42. ax.set_xlim(np.min(x)-1, np.max(x)+1) # ограничение области изменения по оси OX
  43. ax.grid(True)
  44.  
  45. print(str(ex.arr) + "\n" + str(type(ex.arr)))
  46. for arr in ex.mergeSort(ex.arr):
  47. pass
  48.  
  49. plt.show()
  50.  
  51.  
  52.  
  53. # self.DiagramField = QText("Enter", self)
  54. # DiagramBox.addWidget(self.DiagramField)
  55.  
  56.  
  57.  
  58. class Example(QWidget):
  59.  
  60. def __init__(self):
  61. super().__init__()
  62.  
  63. self.initUI()
  64.  
  65. def initUI(self):
  66.  
  67. layout = QHBoxLayout(self)
  68. button = QHBoxLayout(self)
  69. self.arr = []
  70.  
  71.  
  72. self.resize(800, 440)
  73. self.center()
  74. self.setWindowTitle('Merge sort')
  75. self.setWindowIcon(QIcon('web.png'))
  76.  
  77. # rand button
  78. self.randButton = QPushButton('Rand', self)
  79. self.randButton.clicked.connect(self.randArray)
  80. button.addWidget(self.randButton)
  81. self.randButton.move(0, 0)
  82. self.randButton.setStyleSheet("""
  83. color: #fff;
  84. text-decoration: none;
  85. background: #3498db;
  86. }
  87. QPushButton:hover { background: #2980b9; }
  88. QPushButton:active { background: #3498db; }
  89. """)
  90.  
  91. self.sort = QPushButton('Sort array', self)
  92. self.sort.clicked.connect(self.sortArray)
  93. button.addWidget(self.sort)
  94. self.sort.move(11, 60)
  95. self.sort.setStyleSheet("""
  96. color: #fff;
  97. text-decoration: none;
  98. background: #3498db;
  99. }
  100. QPushButton:hover { background: #2980b9; }
  101. QPushButton:active { background: #3498db; }
  102. """)
  103.  
  104. self.clear = QPushButton('Clear', self)
  105. self.clear.clicked.connect(self.clearWindow)
  106. button.addWidget(self.clear)
  107. self.clear.move(11, 100)
  108. self.clear.setStyleSheet("""
  109. color: #fff;
  110. text-decoration: none;
  111. background: #3498db;
  112. }
  113. QPushButton:hover { background: #2980b9; }
  114. QPushButton:active { background: #3498db; }
  115. """)
  116.  
  117. self.btn = QPushButton('Enter array', self)
  118. self.btn.clicked.connect(self.getnum)
  119. layout.addWidget(self.btn)
  120. self.btn.setStyleSheet("""
  121. color: #fff;
  122. text-decoration: none;
  123. background: #3498db;
  124. padding: .7em 1.5em;
  125. }
  126. QPushButton:hover { background: #2980b9; }
  127. QPushButton:active { background: #3498db; }
  128. """)
  129. self.randButton.move(11, 140)
  130.  
  131. self.btn = QPushButton('Diagram', self)
  132. self.btn.clicked.connect(self.showDiagram)
  133. layout.addWidget(self.btn)
  134. self.btn.setStyleSheet("""
  135. color: #fff;
  136. text-decoration: none;
  137. background: #3498db;
  138. padding: .7em 1.5em;
  139. }
  140. QPushButton:hover { background: #2980b9; }
  141. QPushButton:active { background: #3498db; }
  142. """)
  143. self.btn.move(11, 240)
  144.  
  145. # Array input field
  146. self.lbl = QLabel('<b>Enter array:</b>', self)
  147. layout.addWidget(self.lbl)
  148.  
  149. # output sorting
  150. self.output = QTextEdit(self)
  151. layout.addWidget(self.output)
  152.  
  153. self.output.setStyleSheet("color:blue")
  154. self.mergeSort( self.arr )
  155.  
  156.  
  157.  
  158. self.show()
  159.  
  160.  
  161. # Asked whether you really sure you want to close
  162. def closeEvent(self, event):
  163.  
  164. reply = QMessageBox.question(self, 'Message',
  165. "Are you sure to quit?", QMessageBox.Yes |
  166. QMessageBox.No, QMessageBox.No)
  167.  
  168. if reply == QMessageBox.Yes:
  169. event.accept()
  170. else:
  171. event.ignore()
  172.  
  173. # Makes the window was centered
  174. def center(self):
  175. qr = self.frameGeometry()
  176. cp = QDesktopWidget().availableGeometry().center()
  177. qr.moveCenter( cp )
  178. self.move(qr.topLeft())
  179.  
  180. # function marge sort
  181. def mergeSort(self, alist):
  182. if len(alist)>1:
  183. self.output.append("<font color=red>Splitting: " + str(alist))
  184. mid = len(alist)//2
  185. lefthalf = alist[:mid]
  186. righthalf = alist[mid:]
  187.  
  188. self.mergeSort(lefthalf)
  189. self.mergeSort(righthalf)
  190.  
  191. i=0
  192. j=0
  193. k=0
  194. while i<len(lefthalf) and j<len(righthalf):
  195. if lefthalf[i]<righthalf[j]:
  196. alist[k]=lefthalf[i]
  197. i=i+1
  198. else:
  199. alist[k]=righthalf[j]
  200. j=j+1
  201. k=k+1
  202.  
  203. while i<len(lefthalf):
  204. alist[k]=lefthalf[i]
  205. i=i+1
  206. k=k+1
  207.  
  208. while j<len(righthalf):
  209. alist[k]=righthalf[j]
  210. j=j+1
  211. k=k+1
  212. self.output.append("<font color=green>Merging: </>"+ str(alist))
  213.  
  214. # Enter array
  215. def getnum(self):
  216. num, ok = QInputDialog.getInt(self, 'integer input dualog',
  217. 'Enter element:')
  218. if ok:
  219. self.arr.append(num)
  220. self.output.setText(str(self.arr))
  221.  
  222. # Enter number in array
  223. def randArray(self, length):
  224. i = 0
  225. length, ok = QInputDialog.getInt(self, 'integer input dualog',
  226. 'question element:')
  227. if ok:
  228. while( i < length):
  229. self.arr.append(random.randint(0, 99))
  230. i+=1
  231. if length > 1:
  232.  
  233. self.output.setText("<b>Your array: </b>" + str(self.arr))
  234. self.mergeSort( self.arr )
  235. self.output.append("\n<b>Result: </b>" + str(self.arr))
  236. else:
  237. self.output.append("<b>Your array: </b>" + str(self.arr))
  238.  
  239. # Displaying sorting results
  240. def sortArray(self):
  241. self.mergeSort(self.arr)
  242. self.output.append("\n<b>Result: </b>" + str(self.arr))
  243.  
  244. # Screen cleaning function
  245. def clearWindow(self):
  246. self.output.clear()
  247. self.arr = []
  248.  
  249. # Diagram window
  250. def showDiagram(self):
  251. self.diff_window = diff_window()
  252. #self.diff_window.resize(600, 400)
  253. #self.diff_window.move(300, 300)
  254. #self.diff_window.setWindowTitle('Diagram')
  255. #self.diff_window.show()
  256.  
  257.  
  258. if __name__ == '__main__':
  259. app = QApplication(sys.argv)
  260. ex = Example()
  261. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement