Advertisement
nein_yards

PM Task 1 2021

Jan 9th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 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. Function inputBooks(n)
  14. Dim books(n - 1) As book
  15. For i = 0 To n - 1
  16. Dim bookCode As Integer
  17. Do
  18. Console.Write("Enter book code (100-999): ")
  19. bookCode = Convert.ToInt32(Console.ReadLine())
  20. Loop Until 100 <= bookCode And bookCode <= 999
  21. books(i).code = bookCode
  22.  
  23. Console.Write("Enter title: ")
  24. books(i).title = Console.ReadLine()
  25. Console.Write("Enter author: ")
  26. books(i).author = Console.ReadLine()
  27. Console.Write("Enter publication year: ")
  28. books(i).publicationYear = Convert.ToInt32(Console.ReadLine())
  29.  
  30. Console.WriteLine("==============================")
  31. Console.WriteLine("Book code: " & books(i).code)
  32. Console.WriteLine("Title: " & books(i).title)
  33. Console.WriteLine("Main author: " & books(i).author)
  34. Console.WriteLine("Year of publication: " & books(i).publicationYear)
  35. Console.WriteLine("==============================")
  36. Next
  37. Return books
  38. End Function
  39.  
  40. Sub storeBooksSerial(books() As book)
  41. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksSerial.txt", OpenMode.Append)
  42. Dim bookString As String
  43. For i = 0 To books.Length - 1
  44. bookString = books(i).code & "," &
  45. books(i).title & "," &
  46. books(i).author & "," &
  47. books(i).publicationYear
  48. PrintLine(1, bookString)
  49. Next
  50. FileClose(1)
  51. End Sub
  52.  
  53. Sub readBooksSerial()
  54. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksSerial.txt", OpenMode.Input)
  55. Dim bookArr() As String
  56. While Not EOF(1)
  57. bookArr = LineInput(1).Split(",")
  58. Console.WriteLine("==============================")
  59. Console.WriteLine("Book code: " & bookArr(0))
  60. Console.WriteLine("Title: " & bookArr(1))
  61. Console.WriteLine("Main author: " & bookArr(2))
  62. Console.WriteLine("Year of publication: " & bookArr(3))
  63. Console.WriteLine("==============================")
  64. End While
  65. FileClose(1)
  66. End Sub
  67.  
  68. Function hash(key)
  69. Return key Mod maxNumberBooks
  70. End Function
  71.  
  72. Sub storeBookRandom(book As book)
  73. Dim collision As String
  74. Dim bookString As String = Convert.ToString(book.code) & "," &
  75. book.title.PadRight(45, " ") & "," &
  76. book.author.PadRight(25, " ") & "," &
  77. Convert.ToString(book.publicationYear).PadLeft(4, "0")
  78.  
  79. Dim address As Integer = hash(book.code)
  80. Dim initialAddress As Integer = hash(book.code)
  81. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksRandom.txt", OpenMode.Random, OpenAccess.ReadWrite, , bookByteSize)
  82. Seek(1, address)
  83. FileGet(1, collision)
  84. While collision <> ""
  85. address += 1
  86. If address >= maxNumberBooks Then
  87. address = 0
  88. End If
  89. If address = initialAddress Then
  90. Console.WriteLine("Error: File is full")
  91. FileClose(1)
  92. Exit Sub
  93. End If
  94. Seek(1, address)
  95. FileGet(1, collision)
  96. End While
  97. FilePut(1, bookString)
  98. FileClose(1)
  99. End Sub
  100. Sub readBookRandom(bookCode As Integer)
  101. Dim bookString As String
  102. Dim address As Integer = hash(bookCode)
  103. Dim initialAddress As Integer = address
  104. FileOpen(1, "C:\Users\tfqsy\OneDrive\Documents\BooksRandom.txt", OpenMode.Random, OpenAccess.Read, , bookByteSize)
  105. Seek(1, address)
  106. FileGet(1, bookString)
  107.  
  108. While Convert.ToInt32(bookString.Substring(0, 3)) <> bookCode
  109. address += 1
  110. If address >= maxNumberBooks Then
  111. address = 0
  112. End If
  113. If address = initialAddress Then
  114. Console.WriteLine("Book code not found!")
  115. FileClose(1)
  116. Exit Sub
  117. End If
  118. Seek(1, address)
  119. FileGet(1, bookString)
  120. End While
  121.  
  122. FileClose(1)
  123. outputBook(bookString.Substring(0, 3),
  124. bookString.Substring(4, 45).TrimEnd(" "),
  125. bookString.Substring(50, 25).TrimEnd(" "),
  126. bookString.Substring(76, 4))
  127. Console.ReadLine()
  128. End Sub
  129. Sub outputBook(code As String, title As String, author As String, publicationYear As String)
  130. Console.WriteLine("==============================")
  131. Console.WriteLine("Book code: " & code)
  132. Console.WriteLine("Title: " & title)
  133. Console.WriteLine("Main author: " & author)
  134. Console.WriteLine("Year of publication: " & publicationYear)
  135. Console.WriteLine("==============================")
  136. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement