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
- 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())
- Console.WriteLine("==============================")
- Console.WriteLine("Book code: " & books(i).code)
- Console.WriteLine("Title: " & books(i).title)
- Console.WriteLine("Main author: " & books(i).author)
- Console.WriteLine("Year of publication: " & books(i).publicationYear)
- Console.WriteLine("==============================")
- 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(",")
- Console.WriteLine("==============================")
- Console.WriteLine("Book code: " & bookArr(0))
- Console.WriteLine("Title: " & bookArr(1))
- Console.WriteLine("Main author: " & bookArr(2))
- Console.WriteLine("Year of publication: " & bookArr(3))
- Console.WriteLine("==============================")
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement