Option Explicit On
Option Strict On
Imports System.Math

Public Class Form4

    Public Function f(ByVal x As Double) As Double 'исследуемая функция
        If RadioButton1.Checked = True Then
            Return x ^ 2 - 2 * x - 3
        ElseIf RadioButton2.Checked = True Then
            Return x ^ 2 - 4 * x + 4
        End If
    End Function

    Public Function ff(ByVal x As Double) As String
        Return Format(x, "0.00000") 'функция для выводов результата с 5 знаками после запятой
    End Function

    Public Sub vvod1(ByRef a As Double, ByRef b As Double, ByRef Eps As Double) 'ввод исходных данных
        a = 0
        b = 4
        Eps = 0.001
    End Sub

    Public Sub vivod1(ByVal a As Double, ByVal b As Double, ByVal Eps As Double, ByVal n As Double, ByVal h As Double, ByVal I As Double, ByVal Ans As String) 'вывод конечных результатов
        TextBox1.Text = CStr(a)
        TextBox2.Text = CStr(b)
        TextBox3.Text = CStr(Eps)
        TextBox4.Text = CStr(n)
        TextBox5.Text = CStr(h)
        TextBox6.Text = CStr(I)
        TextBox7.Text = Ans
    End Sub

    Public Sub vivod2(ByVal a As Double, ByVal b As Double, ByVal Eps As Double, ByVal n As Double, ByVal h As Double, ByVal I As Double, ByVal Ans As String) 'вывод конечных результатов
        TextBox1.Text = CStr(a)
        TextBox2.Text = CStr(b)
        TextBox3.Text = CStr(Eps)
        TextBox8.Text = CStr(n)
        TextBox9.Text = CStr(h)
        TextBox10.Text = CStr(I)
        TextBox11.Text = Ans
    End Sub

    Public Function probel_redactor(ByVal n_probelov As Integer, ByVal q As String) As String
        Dim str As String

        If CInt(q) >= 10 Then n_probelov = n_probelov - 2
        If CInt(q) >= 100 Then n_probelov = n_probelov - 2

        If q.IndexOf("-") = 0 Then
            str = Space(n_probelov - 1) + q
        Else
            str = Space(n_probelov) + q
        End If

        Return str
    End Function

    Public Sub prog1(ByVal a As Double, ByVal b As Double, ByVal Eps As Double, ByRef n As Double, ByRef h As Double, ByRef I As Double, ByRef Ans As String) 'вычисление определенного интеграла методом средних прямоугольников
        Dim I0 As Double
        Dim k As Double
        n = 1
        h = b - a
        I = h * f((a + b) / 2)
        Do
            n = 2 * n
            h = (b - a) / n
            I0 = I
            I = 0
            For k = 0 To n - 1
                I = I + f(a + h / 2 + k * h)
            Next
            I = I * h
            Ans = Ans + probel_redactor(25, CStr(n)) + probel_redactor(25, ff(h)) + probel_redactor(25, ff(I)) + vbNewLine 'кол-во итераций, значения шагов, значения функции
        Loop Until Abs(I - I0) / 3 < Eps
    End Sub

    Public Sub prog2(ByVal a As Double, ByVal b As Double, ByVal Eps As Double, ByRef n As Double, ByRef h As Double, ByRef I As Double, ByRef Ans As String) 'вычисление определенного интеграла методом Симпсона
        Dim I0, k, x As Double
        Dim c As Integer
        n = 2 'начальное кол-во итераций
        h = (b - a) / n 'шаг
        I = h / 3 * (f(a) + 4 * f((a + b) / 2) + f(b))
        Do
            n = 2 * n
            h = (b - a) / n
            I0 = I
            c = 4
            x = a
            I = f(a) + f(b)
            For k = 1 To n - 1
                x = x + h
                I = I + c * f(x)
                c = 6 - c
            Next
            I = I * h / 3
            Ans = Ans + probel_redactor(25, CStr(n)) + probel_redactor(25, ff(h)) + probel_redactor(25, ff(I)) + vbNewLine 'кол-во итераций, значения шагов, значения функции
        Loop Until Abs(I - I0) / 15 < Eps
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As Double 'левая граница отрезка
        Dim b As Double 'правая граница отрезка
        Dim n As Double 'начальное кол-во итераций
        Dim h As Double 'шаг
        Dim Eps As Double 'точность
        Dim I As Double 'значение функции
        Dim Ans As String = "" 'строка под кол-во итераций, значения шагов и значения функции
        vvod1(a, b, Eps) 'ввод исходных данных
        prog1(a, b, Eps, n, h, I, Ans) 'вычисление определенного интеграла методом средних прямоугольников
        vivod1(a, b, Eps, n, h, I, Ans) 'вывод конечных результатов
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim a As Double 'левая граница отрезка
        Dim b As Double 'правая граница отрезка
        Dim n As Double 'начальное кол-во итераций
        Dim h As Double 'шаг
        Dim Eps As Double 'точность
        Dim I As Double 'значение функции
        Dim Ans As String = "" 'строка под кол-во итераций, значения шагов и значения функции
        vvod1(a, b, Eps) 'ввод исходных данных
        prog2(a, b, Eps, n, h, I, Ans) 'вычисление определенного интеграла методом Симпсона
        vivod2(a, b, Eps, n, h, I, Ans) 'вывод конечных результатов
    End Sub

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

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

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