Option Strict On
Imports System.IO
Module Module1
    Function f(ByVal x As Double) As Double
        Return 1 / (x + 273.15)
    End Function
    Function g(ByVal y As Double, ByVal x As Double, ByVal B As Double) As Double
        Return (Math.Log10(y * 133.322) + B * Math.Log10(x + 273.15))
    End Function
    Sub Main()
        Dim read As StreamReader = New StreamReader("data.csv")
       
        Dim N As Integer = Integer.Parse(read.ReadLine())
        Dim x(N - 1) As Double
        Dim y(N - 1) As Double
        Dim line As String

        For i As Integer = 0 To N - 1
            line = read.ReadLine()
            x(i) = Double.Parse(line.Substring(0, line.IndexOf(";")))
            y(i) = Double.Parse(line.Substring(line.IndexOf(";") + 1))
        Next i
        Const B As Double = 0.8403
        Dim Xmean As Double = 0
        Dim Ymean As Double = 0
        For i As Integer = 0 To N - 1
            Xmean = Xmean + f(x(i))
            Ymean = Ymean + g(y(i), x(i), B)
        Next i

        Xmean = Xmean / N
        Ymean = Ymean / N
        Dim numerator As Double = 0
        Dim denominator As Double = 0

        Dim sum(4) As Double
        For i As Integer = 0 To N - 1
            numerator = numerator + (f(x(i)) - Xmean) * (g(y(i), x(i), B) - Ymean)
            denominator = denominator + (f(x(i)) - Xmean) ^ 2
            sum(0) = sum(0) + f(x(i))
            sum(1) = sum(1) + (f(x(i))) ^ 2
            sum(2) = sum(2) + g(y(i), x(i), B)
            sum(3) = sum(3) + g(y(i), x(i), B) ^ 2
            sum(4) = sum(4) + f(x(i)) * g(y(i), x(i), B)
        Next i
        Dim kr As Double = Math.Abs((N * sum(4) - sum(0) * sum(2)) / (Math.Sqrt((N * sum(1) - sum(0) ^ 2) * (N * sum(3) - sum(2) ^ 2))))
        Dim A As Double = numerator / denominator
        Dim B_ As Double = Ymean - A * Xmean
        Const R As Double = 8.31441
        Dim H As Double = -A * R * Math.Log(10)
        Console.WriteLine(" Уравнение прямой:")
        Console.WriteLine()
        Console.WriteLine(" Y = {0:f6} * X + {1:f6}", A, B_)
        Console.WriteLine()
        Console.WriteLine()
        Console.WriteLine(" Коэффициент корреляции: r = {0:F4}", kr)
        Console.WriteLine()
        Console.WriteLine("-------------------------------------------------")
        Console.WriteLine()
        Console.WriteLine(" i    Xi      1/Xi      Yi      A*Xi+B     Delta")
        Console.WriteLine()
        For i As Integer = 0 To N - 1
            Console.WriteLine("{0,3}  {1,7:F2} {2:F5} {3:F4} {4:F4} {5:F6}", (i + 1), (x(i) + 273.15), f(x(i)), _
                              g(y(i), x(i), B), A * f(x(i)) + B_, _
                           g(y(i), x(i), B) - (A * f(x(i)) + B_))
        Next i
        Console.WriteLine()
        Console.WriteLine()
        Console.WriteLine("  DHv = {0:f1}", H)
        read.Close()
        Dim write As StreamWriter = New StreamWriter("Sasha.csv")
        For i As Integer = 0 To N - 1
            write.WriteLine("   {0:F5} {1:F4} {2:F4} ", f(x(i)), _
                            g(y(i), x(i), B), A * f(x(i)) + B_, g(y(i), x(i), B))
        Next i
        write.Close()
        Console.ReadLine()
    End Sub

End Module
