Advertisement
ivan4ov4

Valeriq Nikolova Kursova po VB prerabotena s funtsii

Jan 31st, 2021 (edited)
1,528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.IO
  2.  
  3. Module Module1
  4.     Sub Main()
  5.         Dim X(,), Y(,) As Double
  6.         Dim M As Integer, N As Integer
  7.         Dim i As Integer, j As Integer
  8.         Dim compute As String
  9.         Dim startFile As Boolean
  10.  
  11.         Console.WriteLine("Do you want to open file [Y]es/[N]o")
  12.         compute = Console.ReadLine()
  13.         If compute = "Y" Then
  14.             startFile = True
  15.         Else
  16.             startFile = False
  17.         End If
  18.  
  19.         If startFile Then
  20.             Dim dataReturn = readFile(M, N, X, Y)
  21.             M = dataReturn.item1
  22.             N = dataReturn.item2
  23.             X = dataReturn.item3
  24.             Y = dataReturn.item4
  25.         Else
  26.             Dim dataReturn = readFromConsole(M, N, X, Y)
  27.             M = dataReturn.item1
  28.             N = dataReturn.item2
  29.             X = dataReturn.item3
  30.             Y = dataReturn.item4
  31.         End If
  32.  
  33.         printResult(M, N, X, Y)
  34.  
  35.     End Sub
  36.     'read file form location
  37.    Function readFile(M As Double, N As Double, X(,) As Double, Y(,) As Double)
  38.         Dim fileLocation As String
  39.         Console.WriteLine("Insert file location:")
  40.         fileLocation = Console.ReadLine()
  41.         'fileLocation = "test.txt" 'remove for production
  42.        Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader(fileLocation)
  43.  
  44.         M = reader.ReadLine
  45.         Console.WriteLine("M count: " & M)
  46.         N = reader.ReadLine
  47.         Console.WriteLine("N count: " & N)
  48.  
  49.         ReDim X(M, N)
  50.         For i = 1 To M
  51.             For j = 1 To N
  52.                 X(i, j) = reader.ReadLine
  53.             Next j
  54.         Next i
  55.  
  56.         ReDim Y(M, N)
  57.         For i = 1 To M
  58.             For j = 1 To N
  59.                 Y(i, j) = reader.ReadLine
  60.             Next j
  61.         Next i
  62.  
  63.         reader.Close()
  64.         Return (M, N, X, Y)
  65.     End Function
  66.  
  67.     'read data form console
  68.    Function readFromConsole(M As Integer, N As Integer, X(,) As Double, Y(,) As Double)
  69.         Do
  70.             Console.Write("M=")
  71.             M = Console.ReadLine()
  72.         Loop Until M > 1 And M <= 10
  73.         Do
  74.             Console.Write("N=")
  75.             N = Console.ReadLine()
  76.         Loop Until N > 1 And N <= 10
  77.  
  78.         ReDim X(M, N)
  79.         For i = 1 To M
  80.             For j = 1 To N
  81.                 Console.Write("X(" & i & "," & j & "): ")
  82.                 X(i, j) = Console.ReadLine()
  83.             Next j
  84.         Next i
  85.         ReDim Y(M, N)
  86.         For i = 1 To M
  87.             For j = 1 To N
  88.                 Console.Write("Y(" & i & "," & j & "): ")
  89.                 Y(i, j) = Console.ReadLine()
  90.             Next j
  91.         Next i
  92.         Return (M, N, X, Y)
  93.     End Function
  94.  
  95.     'print data function
  96.    Function printResult(M As Integer, N As Integer, X(,) As Double, Y(,) As Double)
  97.         Dim Z(,) As Double
  98.         ReDim Z(M, N)
  99.         For i = 1 To M
  100.             For j = 1 To N
  101.                 If (i + j) Mod 2 = 0 Then ' i+j e chetno
  102.                    Z(i, j) = X(i, j) + Y(i, j)
  103.                 Else ' i+j e nechetno
  104.                    Z(i, j) = X(i, j) - Y(i, j)
  105.                 End If
  106.             Next j
  107.         Next i
  108.         Console.WriteLine("Матрица Z(M,N):")
  109.         For i = 1 To M
  110.             For j = 1 To N
  111.                 Console.Write(Z(i, j) & " ")
  112.             Next j
  113.             Console.WriteLine()
  114.         Next i
  115.  
  116.         Console.ReadLine() 'za da spre programata i da moje da se vidi krainiqt rezultat
  117.  
  118.     End Function
  119.  
  120.  
  121.  
  122. End Module
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement