Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ReadOnly maxNumberBooks As Integer = 10
- ReadOnly bookByteSize As Integer = 80 + 2
- ' 80 for chars in (random) bookString, 2 for metadata
- Structure book
- Public code As Integer
- Public title As String
- Public author As String
- Public publicationYear As Integer
- End Structure
- Sub Main()
- Console.ReadLine()
- End Sub
- Sub taskOneDemo()
- Dim myBooks() As book = inputBooks(10)
- storeBooksSerial(myBooks)
- readBooksSerial()
- storeBookRandom(myBooks(0))
- readBookRandom(myBooks(0).code)
- End Sub
- Sub taskTwoDemo()
- Dim colorTree As New BinarySearchTree(14)
- colorTree.addNode("maroon")
- colorTree.addNode("black")
- colorTree.addNode("silver")
- colorTree.addNode("amber")
- colorTree.addNode("indigo")
- colorTree.addNode("red")
- colorTree.addNode("violet")
- colorTree.addNode("grey")
- colorTree.addNode("lime green")
- colorTree.addNode("pink")
- colorTree.addNode("yellow")
- colorTree.addNode("blue")
- colorTree.addNode("purple")
- colorTree.addNode("fuchsia")
- colorTree.addNode("turquoise")
- Console.WriteLine(colorTree.indexOf("pink"))
- colorTree.inOrder()
- End Sub
- Sub taskThreeDemo()
- Dim myShelf As Shelf = New Shelf(12)
- myShelf.addTool(New Tool("spear", 12.34, "spear.jpg"))
- myShelf.addTool(New Tool("sword", 34.22, "sword.png"))
- myShelf.addTool(New Tool("fire", 39.22, "fire.png"))
- outputToolsFromShelf(myShelf)
- End Sub
- ' TASK ONE
- Function inputBooks(n)
- Dim books(n - 1) As book
- For i = 0 To n - 1
- Dim bookCode As Integer
- Do
- Console.Write("Enter book code (100-999): ")
- bookCode = Convert.ToInt32(Console.ReadLine())
- Loop Until 100 <= bookCode And bookCode <= 999
- books(i).code = bookCode
- Console.Write("Enter title: ")
- books(i).title = Console.ReadLine()
- Console.Write("Enter author: ")
- books(i).author = Console.ReadLine()
- Console.Write("Enter publication year: ")
- books(i).publicationYear = Convert.ToInt32(Console.ReadLine())
- outputBook(books(i).code, books(i).title, books(i).author, books(i).publicationYear)
- Next
- Return books
- End Function
- Sub storeBooksSerial(books() As book)
- FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksSerial.txt", OpenMode.Append)
- Dim bookString As String
- For i = 0 To books.Length - 1
- bookString = books(i).code & "," &
- books(i).title & "," &
- books(i).author & "," &
- books(i).publicationYear
- PrintLine(1, bookString)
- Next
- FileClose(1)
- End Sub
- Sub readBooksSerial()
- FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksSerial.txt", OpenMode.Input)
- Dim bookArr() As String
- While Not EOF(1)
- bookArr = LineInput(1).Split(",")
- outputBook(bookArr(0), bookArr(1), bookArr(2), bookArr(3))
- End While
- FileClose(1)
- End Sub
- Function hash(key)
- Return key Mod maxNumberBooks
- End Function
- Sub storeBookRandom(book As book)
- Dim collision As String
- Dim bookString As String = Convert.ToString(book.code) & "," &
- book.title.PadRight(45, " ") & "," &
- book.author.PadRight(25, " ") & "," &
- Convert.ToString(book.publicationYear).PadLeft(4, "0")
- Dim address As Integer = hash(book.code)
- Dim initialAddress As Integer = hash(book.code)
- FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksRandom.txt", OpenMode.Random, OpenAccess.ReadWrite, , bookByteSize)
- Seek(1, address)
- FileGet(1, collision)
- While collision <> ""
- address += 1
- If address >= maxNumberBooks Then
- address = 0
- End If
- If address = initialAddress Then
- Console.WriteLine("Error: File is full")
- FileClose(1)
- Exit Sub
- End If
- Seek(1, address)
- FileGet(1, collision)
- End While
- FilePut(1, bookString)
- FileClose(1)
- End Sub
- Sub readBookRandom(bookCode As Integer)
- Dim bookString As String
- Dim address As Integer = hash(bookCode)
- Dim initialAddress As Integer = address
- FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksRandom.txt", OpenMode.Random, OpenAccess.Read, , bookByteSize)
- Seek(1, address)
- FileGet(1, bookString)
- While Convert.ToInt32(bookString.Substring(0, 3)) <> bookCode
- address += 1
- If address >= maxNumberBooks Then
- address = 0
- End If
- If address = initialAddress Then
- Console.WriteLine("Book code not found!")
- FileClose(1)
- Exit Sub
- End If
- Seek(1, address)
- FileGet(1, bookString)
- End While
- FileClose(1)
- outputBook(bookString.Substring(0, 3),
- bookString.Substring(4, 45).TrimEnd(" "),
- bookString.Substring(50, 25).TrimEnd(" "),
- bookString.Substring(76, 4))
- Console.ReadLine()
- End Sub
- Sub outputBook(code As String, title As String, author As String, publicationYear As String)
- Console.WriteLine("==============================")
- Console.WriteLine("Book code: " & code)
- Console.WriteLine("Title: " & title)
- Console.WriteLine("Main author: " & author)
- Console.WriteLine("Year of publication: " & publicationYear)
- Console.WriteLine("==============================")
- End Sub
- 'TASK 2
- Structure treeNode
- Public data As String
- Public left As Integer
- Public right As Integer
- End Structure
- ReadOnly nullPtr As Integer = -1
- Class BinarySearchTree
- Dim head As Integer
- Dim freeNodePtr As Integer
- Dim tree() As treeNode
- Sub New(maxIndex As Integer)
- ReDim tree(maxIndex)
- head = -1
- freeNodePtr = 0
- For i = 0 To maxIndex
- tree(i).data = ""
- tree(i).left = i + 1
- tree(i).right = nullPtr
- Next
- tree(maxIndex).left = nullPtr
- End Sub
- Sub addNode(data As String)
- If freeNodePtr = -1 Then
- Console.WriteLine("Error: Color tree is full!")
- Exit Sub
- End If
- Dim newNodePtr = freeNodePtr
- freeNodePtr = tree(freeNodePtr).left
- tree(newNodePtr).data = data
- tree(newNodePtr).left = nullPtr
- tree(newNodePtr).right = nullPtr
- Dim prevNodePtr, currNodePtr As Integer
- Dim turnedLeft As Boolean
- currNodePtr = head
- While currNodePtr <> nullPtr
- prevNodePtr = currNodePtr
- If data < tree(currNodePtr).data Then
- currNodePtr = tree(currNodePtr).left
- turnedLeft = True
- Else
- currNodePtr = tree(currNodePtr).right
- turnedLeft = False
- End If
- End While
- If head = nullPtr Then
- head = newNodePtr
- Else
- If turnedLeft Then
- tree(prevNodePtr).left = newNodePtr
- Else
- tree(prevNodePtr).right = newNodePtr
- End If
- End If
- End Sub
- Sub inOrder()
- inOrderRecur(head)
- End Sub
- Private Sub inOrderRecur(currNodePtr As Integer)
- If tree(currNodePtr).left <> nullPtr Then
- inOrderRecur(tree(currNodePtr).left)
- End If
- Console.WriteLine(tree(currNodePtr).data)
- If tree(currNodePtr).right <> nullPtr Then
- inOrderRecur(tree(currNodePtr).right)
- End If
- End Sub
- Function indexOf(data As String)
- Dim currNodePtr As Integer
- currNodePtr = head
- While currNodePtr <> nullPtr
- If data < tree(currNodePtr).data Then
- currNodePtr = tree(currNodePtr).left
- ElseIf data > tree(currNodePtr).data Then
- currNodePtr = tree(currNodePtr).right
- Else
- Return currNodePtr
- End If
- End While
- Return -1
- End Function
- Sub dump()
- Console.WriteLine("Head: " & head)
- Console.WriteLine("Free Ptr: " & freeNodePtr)
- For Each node In tree
- Console.WriteLine(Convert.ToString(node.left).PadLeft(4) & " | " &
- node.data.PadRight(10) & " | " &
- Convert.ToString(node.right).PadLeft(4))
- Next
- End Sub
- End Class
- 'TASK 3
- Class Tool
- Private name As String
- Private cost As Decimal
- Private imageFileName As String
- Sub New(name As String, cost As Decimal, imageFileName As String)
- Me.name = name
- Me.cost = cost
- Me.imageFileName = imageFileName
- End Sub
- Function getName()
- Return name
- End Function
- Sub setName(name)
- Me.name = name
- End Sub
- Function getCost()
- Return cost
- End Function
- Sub setCost(cost)
- Me.cost = cost
- End Sub
- Function getImageFileName()
- Return imageFileName
- End Function
- Sub setImageFileName(imageFileName)
- Me.imageFileName = imageFileName
- End Sub
- End Class
- Class Shelf
- Public position As Integer
- Public tools(10) As Tool
- Private nextToolIndex As Integer
- Sub New(position As Integer)
- Me.position = position
- nextToolIndex = 0
- End Sub
- Sub addTool(tool As Tool)
- If nextToolIndex = 10 Then
- Console.WriteLine("Error: Shelf is full")
- Else
- tools(nextToolIndex) = tool
- End If
- nextToolIndex += 1
- End Sub
- End Class
- Sub outputToolsFromShelf(shelf As Shelf)
- Dim i As Integer = 0
- While Not shelf.tools(i) Is Nothing
- Console.WriteLine("Tool name: " & shelf.tools(i).getName)
- Console.WriteLine("Cost: " & shelf.tools(i).getCost)
- Console.WriteLine()
- i += 1
- End While
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement