Option Strict On
Option Explicit On
Imports System.Math
Public Class Form3

    Dim x(2), z(2), a, b, c As Double

    Sub vvod(ByRef x() As Double, ByRef z() As Double) 'ввод исходных данных
        If RadioButton1.Checked = True Then 'тест
            x(0) = -2 'точка А (х1,у1)
            z(0) = 5
            x(1) = 1 'точка В (х2,у2)
            z(1) = -4
            x(2) = 4 'точка С (х3,у3)
            z(2) = 5
        ElseIf RadioButton2.Checked = True Then 'курсовая
            x(0) = 0 'точка А (х1,у1)
            z(0) = 4
            x(1) = 2 'точка В (х2,у2)
            z(1) = 0
            x(2) = 4 'точка С (х3,у3)
            z(2) = 4
        End If
    End Sub

    Public Sub vivod(ByVal x() As Double, ByVal z() As Double, ByVal a As Double, ByVal b As Double, ByVal c As Double, ByVal uravn As String) 'вывод результатов
        TextBox1.Text = CStr(x(0)) : TextBox2.Text = CStr(z(0)) : TextBox3.Text = CStr(x(1)) : TextBox4.Text = CStr(z(1))
        TextBox5.Text = CStr(x(2)) : TextBox6.Text = CStr(z(2)) : TextBox7.Text = CStr(a)
        TextBox8.Text = CStr(b) : TextBox9.Text = CStr(c) : TextBox10.Text = uravn
    End Sub

    Public Sub prioritet(ByRef L(,) As Double) 'перестановка строк 1, 2 и 3 матрицы для удобства вычислений
        Dim j, f, g, q As Integer
        q = Integer.MaxValue
        Dim Box(3) As Double
        For i = 0 To 2
            L(i, 0) = x(i) ^ 2
            L(i, 1) = x(i)
            L(i, 2) = 1
            L(i, 3) = z(i)
        Next
        For k = 0 To 3
            Box(k) = L(0, k)
        Next
        For i = 0 To 2
            If x(i) <> 0 Then
                While f = 0
                    g = g + 1
                    If Abs(x(i)) = g Then f = 1
                    If f = 1 Then
                        If g < q Then
                            q = g
                            j = i
                            g = 0
                        End If
                    End If
                End While
                f = 0
            End If
        Next
       For k = 0 To 3
            L(0, k) = L(j, k)
            L(j, k) = Box(k)
        Next
    End Sub

    Public Sub prioritet2(ByRef L(,) As Double) 'перестановка строк 2 и 3 матрицы для удобства вычислений
        Dim j, f, g, q As Integer
        q = Integer.MaxValue
        Dim Box(3) As Double
        For k = 0 To 3
            Box(k) = L(1, k)
        Next
        For i = 1 To 2
            If L(i, 1) <> 0 Then
                While f = 0
                    g = g + 1
                    If Abs(L(i, 1)) = g Then f = 1
                    If f = 1 Then
                        If g < q Then
                            q = g
                            j = i
                            g = 0
                        End If
                    End If
                End While
                f = 0
            End If
        Next
        For k = 0 To 3
            L(1, k) = L(j, k)
            L(j, k) = Box(k)
        Next
    End Sub

    Public Sub bez_x2(ByRef L(,) As Double) 'исключение переменных 1го столбца матрицы из 2 и 3 уравнений
        Dim per1, per2 As Double
        per1 = -L(1, 0) / L(0, 0) 'переменная 2 строки матрицы
        per2 = -L(2, 0) / L(0, 0) 'переменная 3 строки матрицы
        For i = 0 To 3
            L(1, i) = L(1, i) + L(0, i) * per1
            L(2, i) = L(2, i) + L(0, i) * per2
        Next
    End Sub

    Public Sub bez_x(ByRef L(,) As Double) 'исключение переменных 2го столбца матрицы из 3 уравнения
        Dim per3 As Double
        per3 = -L(2, 1) / L(1, 1) 'переменная 3 строки матрицы
        For i = 0 To 3
            L(2, i) = L(2, i) + L(1, i) * per3
        Next
    End Sub

    Public Sub prog(ByVal x() As Double, ByVal z() As Double, ByRef a As Double, ByRef b As Double, ByRef c As Double, ByRef uravn As String) 'вычисление коэффициентов параболы системой линейных уравнений (метод Гаусса)
        Dim L(2, 3) As Double
        prioritet(L)
        bez_x2(L)
        prioritet2(L)
        bez_x(L)
        c = L(2, 3) / L(2, 2)
        b = (L(1, 3) - c * L(1, 2)) / L(1, 1)
        a = (L(0, 3) - c * L(0, 2) - b * L(0, 1)) / L(0, 0)
        If a = 1 Then uravn = "" Else uravn = CStr(a)
        If b > 0 Then uravn = uravn + "x^2+" + CStr(b) Else uravn = uravn + "x^2" + CStr(b)
        If c > 0 Then uravn = "y=" + uravn + "x+" + CStr(c) Else uravn = "y=" + uravn + "x" + CStr(c)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim uravn As String = "" 'строка для вывода полученного уравнения
        vvod(x, z) 'ввод исходных данных
        prog(x, z, a, b, c, uravn) 'вычисление коэффициентов параболы системой линейных уравнений (метод Гаусса)
        vivod(x, z, a, b, c, uravn) 'вывод результатов
    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Hide()
        Form2.Show()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.Hide()
        Form4.Show()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Hide()
        Form1.Show()
    End Sub

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class