Advertisement
nein_yards

pm

Jan 26th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1. ReadOnly maxNumberBooks As Integer = 10
  2. ReadOnly bookByteSize As Integer = 80 + 2
  3. ' 80 for chars in (random) bookString, 2 for metadata
  4. Structure book
  5. Public code As Integer
  6. Public title As String
  7. Public author As String
  8. Public publicationYear As Integer
  9. End Structure
  10. Sub Main()
  11. Console.ReadLine()
  12. End Sub
  13.  
  14. Sub taskOneDemo()
  15. Dim myBooks() As book = inputBooks(10)
  16. storeBooksSerial(myBooks)
  17. readBooksSerial()
  18.  
  19. storeBookRandom(myBooks(0))
  20. readBookRandom(myBooks(0).code)
  21. End Sub
  22. Sub taskTwoDemo()
  23. Dim colorTree As New BinarySearchTree(14)
  24. colorTree.addNode("maroon")
  25. colorTree.addNode("black")
  26. colorTree.addNode("silver")
  27. colorTree.addNode("amber")
  28. colorTree.addNode("indigo")
  29. colorTree.addNode("red")
  30. colorTree.addNode("violet")
  31. colorTree.addNode("grey")
  32. colorTree.addNode("lime green")
  33.  
  34. colorTree.addNode("pink")
  35. colorTree.addNode("yellow")
  36. colorTree.addNode("blue")
  37. colorTree.addNode("purple")
  38. colorTree.addNode("fuchsia")
  39. colorTree.addNode("turquoise")
  40. Console.WriteLine(colorTree.indexOf("pink"))
  41. colorTree.inOrder()
  42. End Sub
  43. Sub taskThreeDemo()
  44. Dim myShelf As Shelf = New Shelf(12)
  45. myShelf.addTool(New Tool("spear", 12.34, "spear.jpg"))
  46. myShelf.addTool(New Tool("sword", 34.22, "sword.png"))
  47. myShelf.addTool(New Tool("fire", 39.22, "fire.png"))
  48. outputToolsFromShelf(myShelf)
  49. End Sub
  50.  
  51. ' TASK ONE
  52. Function inputBooks(n)
  53. Dim books(n - 1) As book
  54. For i = 0 To n - 1
  55. Dim bookCode As Integer
  56. Do
  57. Console.Write("Enter book code (100-999): ")
  58. bookCode = Convert.ToInt32(Console.ReadLine())
  59. Loop Until 100 <= bookCode And bookCode <= 999
  60. books(i).code = bookCode
  61.  
  62. Console.Write("Enter title: ")
  63. books(i).title = Console.ReadLine()
  64. Console.Write("Enter author: ")
  65. books(i).author = Console.ReadLine()
  66. Console.Write("Enter publication year: ")
  67. books(i).publicationYear = Convert.ToInt32(Console.ReadLine())
  68.  
  69. outputBook(books(i).code, books(i).title, books(i).author, books(i).publicationYear)
  70. Next
  71. Return books
  72. End Function
  73.  
  74. Sub storeBooksSerial(books() As book)
  75. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksSerial.txt", OpenMode.Append)
  76. Dim bookString As String
  77. For i = 0 To books.Length - 1
  78. bookString = books(i).code & "," &
  79. books(i).title & "," &
  80. books(i).author & "," &
  81. books(i).publicationYear
  82. PrintLine(1, bookString)
  83. Next
  84. FileClose(1)
  85. End Sub
  86.  
  87. Sub readBooksSerial()
  88. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksSerial.txt", OpenMode.Input)
  89. Dim bookArr() As String
  90. While Not EOF(1)
  91. bookArr = LineInput(1).Split(",")
  92. outputBook(bookArr(0), bookArr(1), bookArr(2), bookArr(3))
  93. End While
  94. FileClose(1)
  95. End Sub
  96.  
  97. Function hash(key)
  98. Return key Mod maxNumberBooks
  99. End Function
  100.  
  101. Sub storeBookRandom(book As book)
  102. Dim collision As String
  103. Dim bookString As String = Convert.ToString(book.code) & "," &
  104. book.title.PadRight(45, " ") & "," &
  105. book.author.PadRight(25, " ") & "," &
  106. Convert.ToString(book.publicationYear).PadLeft(4, "0")
  107.  
  108. Dim address As Integer = hash(book.code)
  109. Dim initialAddress As Integer = hash(book.code)
  110. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksRandom.txt", OpenMode.Random, OpenAccess.ReadWrite, , bookByteSize)
  111. Seek(1, address)
  112. FileGet(1, collision)
  113. While collision <> ""
  114. address += 1
  115. If address >= maxNumberBooks Then
  116. address = 0
  117. End If
  118. If address = initialAddress Then
  119. Console.WriteLine("Error: File is full")
  120. FileClose(1)
  121. Exit Sub
  122. End If
  123. Seek(1, address)
  124. FileGet(1, collision)
  125. End While
  126. FilePut(1, bookString)
  127. FileClose(1)
  128. End Sub
  129. Sub readBookRandom(bookCode As Integer)
  130. Dim bookString As String
  131. Dim address As Integer = hash(bookCode)
  132. Dim initialAddress As Integer = address
  133. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksRandom.txt", OpenMode.Random, OpenAccess.Read, , bookByteSize)
  134. Seek(1, address)
  135. FileGet(1, bookString)
  136.  
  137. While Convert.ToInt32(bookString.Substring(0, 3)) <> bookCode
  138. address += 1
  139. If address >= maxNumberBooks Then
  140. address = 0
  141. End If
  142. If address = initialAddress Then
  143. Console.WriteLine("Book code not found!")
  144. FileClose(1)
  145. Exit Sub
  146. End If
  147. Seek(1, address)
  148. FileGet(1, bookString)
  149. End While
  150.  
  151. FileClose(1)
  152. outputBook(bookString.Substring(0, 3),
  153. bookString.Substring(4, 45).TrimEnd(" "),
  154. bookString.Substring(50, 25).TrimEnd(" "),
  155. bookString.Substring(76, 4))
  156. Console.ReadLine()
  157. End Sub
  158. Sub outputBook(code As String, title As String, author As String, publicationYear As String)
  159. Console.WriteLine("==============================")
  160. Console.WriteLine("Book code: " & code)
  161. Console.WriteLine("Title: " & title)
  162. Console.WriteLine("Main author: " & author)
  163. Console.WriteLine("Year of publication: " & publicationYear)
  164. Console.WriteLine("==============================")
  165. End Sub
  166.  
  167. 'TASK 2
  168. Structure treeNode
  169. Public data As String
  170. Public left As Integer
  171. Public right As Integer
  172. End Structure
  173. ReadOnly nullPtr As Integer = -1
  174. Class BinarySearchTree
  175. Dim head As Integer
  176. Dim freeNodePtr As Integer
  177. Dim tree() As treeNode
  178. Sub New(maxIndex As Integer)
  179. ReDim tree(maxIndex)
  180. head = -1
  181. freeNodePtr = 0
  182. For i = 0 To maxIndex
  183. tree(i).data = ""
  184. tree(i).left = i + 1
  185. tree(i).right = nullPtr
  186. Next
  187. tree(maxIndex).left = nullPtr
  188. End Sub
  189. Sub addNode(data As String)
  190. If freeNodePtr = -1 Then
  191. Console.WriteLine("Error: Color tree is full!")
  192. Exit Sub
  193. End If
  194. Dim newNodePtr = freeNodePtr
  195. freeNodePtr = tree(freeNodePtr).left
  196.  
  197. tree(newNodePtr).data = data
  198. tree(newNodePtr).left = nullPtr
  199. tree(newNodePtr).right = nullPtr
  200.  
  201. Dim prevNodePtr, currNodePtr As Integer
  202. Dim turnedLeft As Boolean
  203. currNodePtr = head
  204. While currNodePtr <> nullPtr
  205. prevNodePtr = currNodePtr
  206. If data < tree(currNodePtr).data Then
  207. currNodePtr = tree(currNodePtr).left
  208. turnedLeft = True
  209. Else
  210. currNodePtr = tree(currNodePtr).right
  211. turnedLeft = False
  212. End If
  213. End While
  214. If head = nullPtr Then
  215. head = newNodePtr
  216. Else
  217. If turnedLeft Then
  218. tree(prevNodePtr).left = newNodePtr
  219. Else
  220. tree(prevNodePtr).right = newNodePtr
  221. End If
  222. End If
  223. End Sub
  224. Sub inOrder()
  225. inOrderRecur(head)
  226. End Sub
  227. Private Sub inOrderRecur(currNodePtr As Integer)
  228. If tree(currNodePtr).left <> nullPtr Then
  229. inOrderRecur(tree(currNodePtr).left)
  230. End If
  231. Console.WriteLine(tree(currNodePtr).data)
  232. If tree(currNodePtr).right <> nullPtr Then
  233. inOrderRecur(tree(currNodePtr).right)
  234. End If
  235. End Sub
  236. Function indexOf(data As String)
  237. Dim currNodePtr As Integer
  238. currNodePtr = head
  239. While currNodePtr <> nullPtr
  240. If data < tree(currNodePtr).data Then
  241. currNodePtr = tree(currNodePtr).left
  242. ElseIf data > tree(currNodePtr).data Then
  243. currNodePtr = tree(currNodePtr).right
  244. Else
  245. Return currNodePtr
  246. End If
  247. End While
  248. Return -1
  249. End Function
  250. Sub dump()
  251. Console.WriteLine("Head: " & head)
  252. Console.WriteLine("Free Ptr: " & freeNodePtr)
  253. For Each node In tree
  254. Console.WriteLine(Convert.ToString(node.left).PadLeft(4) & " | " &
  255. node.data.PadRight(10) & " | " &
  256. Convert.ToString(node.right).PadLeft(4))
  257. Next
  258. End Sub
  259. End Class
  260.  
  261. 'TASK 3
  262. Class Tool
  263. Private name As String
  264. Private cost As Decimal
  265. Private imageFileName As String
  266. Sub New(name As String, cost As Decimal, imageFileName As String)
  267. Me.name = name
  268. Me.cost = cost
  269. Me.imageFileName = imageFileName
  270. End Sub
  271.  
  272. Function getName()
  273. Return name
  274. End Function
  275. Sub setName(name)
  276. Me.name = name
  277. End Sub
  278. Function getCost()
  279. Return cost
  280. End Function
  281. Sub setCost(cost)
  282. Me.cost = cost
  283. End Sub
  284. Function getImageFileName()
  285. Return imageFileName
  286. End Function
  287. Sub setImageFileName(imageFileName)
  288. Me.imageFileName = imageFileName
  289. End Sub
  290.  
  291. End Class
  292. Class Shelf
  293. Public position As Integer
  294. Public tools(10) As Tool
  295. Private nextToolIndex As Integer
  296.  
  297. Sub New(position As Integer)
  298. Me.position = position
  299. nextToolIndex = 0
  300. End Sub
  301.  
  302. Sub addTool(tool As Tool)
  303. If nextToolIndex = 10 Then
  304. Console.WriteLine("Error: Shelf is full")
  305. Else
  306. tools(nextToolIndex) = tool
  307. End If
  308. nextToolIndex += 1
  309. End Sub
  310.  
  311. End Class
  312.  
  313. Sub outputToolsFromShelf(shelf As Shelf)
  314. Dim i As Integer = 0
  315. While Not shelf.tools(i) Is Nothing
  316. Console.WriteLine("Tool name: " & shelf.tools(i).getName)
  317. Console.WriteLine("Cost: " & shelf.tools(i).getCost)
  318. Console.WriteLine()
  319. i += 1
  320. End While
  321. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement