Option Strict On
Imports System.IO
Module Module1
    Function f(ByVal T As Double) As Double
        Return 1 / (T + 273.15)
    End Function
    Function g(ByVal P As Double, ByVal T As Double, ByVal B As Double) As Double
        Return (Math.Log10(P * 133.322) + B * Math.Log10(T + 273.15))
    End Function
    Sub Main()
        Dim reader As StreamReader
        reader = New StreamReader("Data.txt")
        Dim N As Integer = Integer.Parse(reader.ReadLine())
        Dim P(N - 1) As Double
        Dim T(N - 1) As Double
        Dim line As String
        For i As Integer = 0 To N - 1
            line = reader.ReadLine()
            T(i) = Double.Parse(line.Substring(0, line.IndexOf(";")))
            P(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 += f(T(i))
            Ymean += g(P(i), T(i), B)
        Next i
        Xmean *= 1 / N
        Ymean *= 1 / 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(T(i)) - Xmean) * (g(P(i), T(i), B) - Ymean)
            denominator = denominator + (f(T(i)) - Xmean) ^ 2
            sum(0) += f(T(i))
            sum(1) += (f(T(i))) ^ 2
            sum(2) += g(P(i), T(i), B)
            sum(3) += g(P(i), T(i), B) ^ 2
            sum(4) += f(T(i)) * g(P(i), T(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), (T(i) + 273.15), (1 / (T(i) + 273.15)), (Math.Log10(P(i) * 133.322) + B * Math.Log10(T(i) + 273.15)), _
                              (a / (T(i) + 273.15) + b_), ((Math.Log10(P(i) * 133.322) + B * Math.Log10(T(i) + 273.15)) - a / (T(i) + 273.15)))
        Next i
        Console.WriteLine()
        Console.WriteLine()
        Console.WriteLine("  DHv = {0:f1}", H)
        reader.Close()
        Console.ReadLine()
    End Sub

End Module
