Контрольная работа: Составление программы на алгоритмическом языке, выполняющей указанные преобразования с матрицами

Задание №37

Расположить в матрице A(N,N) элементы в последовательности указанной на рисунке.


Требования к программе:

1.    Составить программу на алгоритмическом языке, выполняющую указанные преобразования с матрицами;

2.    Вывести вектор, полученный случайно;

3.    Вывести преобразованную матрицу с новым содержимым вектора;

4.    Программа должна строиться таким образом, чтобы существовала возможность её использования с массивами любой размерности, содержащими произвольные элементы;

5.    В программе должна быть предусмотрена выдача на печать исходных и преобразованных матриц;

6.    Полученные результаты должны быть занесены в файл последовательного доступа и доступны для просмотра из него.

Изменённые свойства приложения

Begin VB.Form frmMain

BorderStyle = 1 'Fixed Single

Caption = "Задание №37"

ClientHeight = 4365

ClientLeft = 45

ClientTop = 435

ClientWidth = 6180

LinkTopic = "Form1"

MaxButton = 0 'False

ScaleHeight = 4365

ScaleWidth = 6180

StartUpPosition = 2 'CenterScreen

Begin VB.Frame Frame2

Caption = "Управление"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 4125

Left = 4590

TabIndex = 7

Top = 120

Width = 1485

Begin VB.CommandButton cmdSave

Caption = "&Сохранение"

Enabled = 0 'False

Height = 375

Left = 120

TabIndex = 11

Top = 1530

Width = 1245

End

Begin VB.CommandButton cmdPrint

Caption = "&Печать"

Enabled = 0 'False

Height = 375

Left = 120

TabIndex = 10

Top = 930

Width = 1245

End

Begin VB.CommandButton cmdExit

Cancel = -1 'True

Caption = "&Выход"

Height = 375

Left = 120

TabIndex = 9

Top = 3630

Width = 1245

End

Begin VB.CommandButton cmdRun

Caption = "&Решение"

Default = -1 'True

Enabled = 0 'False

Height = 375

Left = 120

TabIndex = 8

Top = 330

Width = 1245

End

End

Begin VB.TextBox txtOriginal

Height = 675

Left = 90

Locked = -1 'True

MultiLine = -1 'True

ScrollBars = 2 'Vertical

TabIndex = 1

Top = 1350

Width = 4485

End

Begin VB.Frame Frame1

Caption = "Управление размерностью матрицы"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 885

Left = 90

TabIndex = 5

Top = 120

Width = 4515

Begin VB.TextBox txtN

Alignment = 2 'Center

Height = 285

Left = 3150

MaxLength = 4

TabIndex = 0

Top = 360

Width = 1215

End

Begin VB.Label Label2

Caption = "Чётная размерность матрицы"

Height = 255

Left = 150

TabIndex = 6

Top = 390

Width = 2895

End

End

Begin VB.TextBox txtResult

Height = 1875

Left = 90

Locked = -1 'True

MultiLine = -1 'True

ScrollBars = 2 'Vertical

TabIndex = 2

Top = 2370

Width = 4485

End

Begin VB.Label Label3

Alignment = 2 'Center

BorderStyle = 1 'Fixed Single

Caption = "Упорядоченная матрица"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 255

Left = 90

TabIndex = 4

Top = 2070

Width = 4485

End

Begin VB.Label Label1

Alignment = 2 'Center

BorderStyle = 1 'Fixed Single

Caption = "Полученный случайный вектор"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 255

Left = 90

TabIndex = 3

Top = 1050

Width = 4485

End

End

Attribute VB_Name = "frmMain"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Текст программы на языке Microsoft Visual Basic 6.00 Ext

Option Explicit

Private Sub cmdExit_Click()

Unload Me

End Sub

Private Sub cmdPrint_Click()

Me.PrintForm

End Sub

Private Sub cmdRun_Click()

txtN.Enabled = False

Dim i As Integer, j As Integer

Dim t As Integer, n As Integer

Dim k As Integer, p As Integer

n = Val(txtN.Text)

txtOriginal = Empty

Dim MV As Integer

MV = n ^ 2 / 2 + n

ReDim a(n, n) As Integer

ReDim x(MV) As Integer

Randomize Timer

For i = 1 To MV

x(i) = Int(Rnd * 90) + 10

txtOriginal = txtOriginal & Str(x(i)) & " "

Next i

k = 0

For i = n To n / 2 - 1 Step -1

If i / 2 = Int(i / 2) Then

For j = n - i + 1 To i

k = k + 1: a(i, j) = x(k)

Next j

Else

For j = i To n - i + 1 Step -1

k = k + 1: a(i, j) = x(k)

Next j

End If

Next i

For i = n / 2 To 1 Step -1

If i / 2 = Int(i / 2) Then

For j = n - i + 1 To i Step -1

k = k + 1

a(i, j) = x(k)

Next j

Else

For j = i To n - i + 1

k = k + 1

a(i, j) = x(k)

Next j

End If

Next i

For i = 1 To n

For j = 1 To n

txtResult = txtResult & Format(a(i, j), "00") & " "

Next j

txtResult = txtResult & vbCrLf

Next i

cmdPrint.Enabled = True

cmdSave.Enabled = True

End Sub

Private Sub cmdSave_Click()

On Error GoTo Err_Lbl:

Open App.Path & "\Result.txt" For Output As #1

Print #1, "Задание №46" & vbCrLf & vbCrLf

Print #1, "Исходная матрица:" & vbCrLf

Print #1, txtOriginal.Text & vbCrLf

Print #1, "Полученная матрица:" & vbCrLf

Print #1, txtResult.Text

Close #1

Exit Sub

Err_Lbl:

MsgBox "Произошла ошибка записи", vbCritical, "Внимание"

End Sub

Private Sub txtN_Change()

If Val(txtN) > 8 Or Val(txtN) = 0 Or Val(txtN) Mod 2 <> 0 Then

cmdRun.Enabled = False

Else

cmdRun.Enabled = True

End If

End Sub

Private Sub txtN_KeyPress(KeyAscii As Integer)

If KeyAscii < 26 Then Exit Sub

If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0

End Sub

Внешний вид приложения:


Результат работы приложения:


Приложение 1

 

Form1

VERSION 5.00

Begin VB.Form frmMain

BorderStyle = 1 'Fixed Single

Caption = "Задание №37"

ClientHeight = 4365

ClientLeft = 45

ClientTop = 435

ClientWidth = 6180

LinkTopic = "Form1"

MaxButton = 0 'False

ScaleHeight = 4365

ScaleWidth = 6180

StartUpPosition = 2 'CenterScreen

Begin VB.Frame Frame2

Caption = "Управление"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 4125

Left = 4590

TabIndex = 7

Top = 120

Width = 1485

Begin VB.CommandButton cmdSave

Caption = "&Сохранение"

Enabled = 0 'False

Height = 375

Left = 120

TabIndex = 11

Top = 1530

Width = 1245

End

Begin VB.CommandButton cmdPrint

Caption = "&Печать"

Enabled = 0 'False

Height = 375

Left = 120

TabIndex = 10

Top = 930

Width = 1245

End

Begin VB.CommandButton cmdExit

Cancel = -1 'True

Caption = "&Выход"

Height = 375

Left = 120

TabIndex = 9

Top = 3630

Width = 1245

End

Begin VB.CommandButton cmdRun

Caption = "&Решение"

Default = -1 'True

Enabled = 0 'False

Height = 375

Left = 120

TabIndex = 8

Top = 330

Width = 1245 End

End

Begin VB.TextBox txtOriginal

Height = 675

Left = 90

Locked = -1 'True

MultiLine = -1 'True

ScrollBars = 2 'Vertical

TabIndex = 1

Top = 1350

Width = 4485

End

Begin VB.Frame Frame1

Caption = "Управление размерностью матрицы"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 885

Left = 90

TabIndex = 5

Top = 120

Width = 4515

Begin VB.TextBox txtN

Alignment = 2 'Center

Height = 285

Left = 3150

MaxLength = 4

TabIndex = 0

Top = 360

Width = 1215

End

Begin VB.Label Label2

Alignment = 2 'Center

BorderStyle = 1 'Fixed Single

Caption = "Чётная размерность матрицы"

Height = 255

Left = 150

TabIndex = 6

Top = 390

Width = 2895

End

End

Begin VB.TextBox txtResult

Height = 1875

Left = 90

Locked = -1 'True

MultiLine = -1 'True

ScrollBars = 2 'Vertical

TabIndex = 2

Top = 2370

Width = 4485

End

Begin VB.Label Label3

Alignment = 2 'Center

BorderStyle = 1 'Fixed Single

Caption = "Упорядоченная матрица"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 255

Left = 90

TabIndex = 4

Top = 2070

Width = 4485

End

Begin VB.Label Label1

Alignment = 2 'Center

BorderStyle = 1 'Fixed Single

Caption = "Полученный случайный вектор"

BeginProperty Font

Name = "MS Sans Serif"

Size = 8.25

Charset = 204

Weight = 700

Underline = 0 'False

Italic = 0 'False

Strikethrough = 0 'False

EndProperty

Height = 255

Left = 90

TabIndex = 3

Top = 1050

Width = 4485

End

End

Attribute VB_Name = "frmMain"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Option Explicit

Private Sub cmdExit_Click()

Unload Me

End Sub

Private Sub cmdPrint_Click()

Me.PrintForm

End Sub

Private Sub cmdRun_Click()

txtN.Enabled = False

Dim i As Integer, j As Integer

Dim t As Integer, n As Integer

Dim k As Integer, p As Integer

n = Val(txtN.Text)

txtOriginal = Empty

Dim MV As Integer

MV = n ^ 2 / 2 + n

ReDim a(n, n) As Integer

ReDim x(MV) As Integer

Randomize Timer

For i = 1 To MV

x(i) = Int(Rnd * 90) + 10

txtOriginal = txtOriginal & Str(x(i)) & " "

Next i

k = 0

For i = n To n / 2 - 1 Step -1

If i / 2 = Int(i / 2) Then

For j = n - i + 1 To i

k = k + 1

a(i, j) = x(k)

Next j

Else

For j = i To n - i + 1 Step -1

k = k + 1

a(i, j) = x(k)

Next j

End If

Next i

For i = n / 2 To 1 Step -1

If i / 2 = Int(i / 2) Then

For j = n - i + 1 To i Step -1

k = k + 1

a(i, j) = x(k)

Next j

Else

For j = i To n - i + 1

k = k + 1

a(i, j) = x(k)

Next j

End If

Next i

For i = 1 To n

For j = 1 To n

txtResult = txtResult & Format(a(i, j), "00") & " "

Next j

txtResult = txtResult & vbCrLf

Next i

cmdPrint.Enabled = True

cmdSave.Enabled = True

End Sub

Private Sub cmdSave_Click()

On Error GoTo Err_Lbl:

Open App.Path & "\Result.txt" For Output As #1

Print #1, "Задание №46" & vbCrLf & vbCrLf

Print #1, "Исходная матрица:" & vbCrLf

Print #1, txtOriginal.Text & vbCrLf

Print #1, "Полученная матрица:" & vbCrLf

Print #1, txtResult.Text

Close #1

Exit Sub

Err_Lbl:

MsgBox "Произошла ошибка записи", vbCritical, "Внимание"

End Sub

Private Sub txtN_Change()

If Val(txtN) > 8 Or Val(txtN) = 0 Or Val(txtN) Mod 2 <> 0 Then

cmdRun.Enabled = False

Else

cmdRun.Enabled = True

End If

End Sub

Private Sub txtN_KeyPress(KeyAscii As Integer)

If KeyAscii < 26 Then Exit Sub

If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0

End Sub


Приложение 2

 

Задание №46

Исходная матрица:

39 41 16 98 68 47 13 87 94 12 12 69 59 46 52 85 71 38 18 19 77 81 92 26 23 94 66 83 28 23 23 15 39 50 51 78 58 22 73 41

Полученная матрица:

39 50 51 78 58 22 73 41

00 15 23 23 28 83 66 00

00 00 92 26 23 94 00 00

00 00 00 81 77 00 00 00

00 00 00 19 18 00 00 00

00 00 52 85 71 38 00 00

00 46 59 69 12 12 94 00

39 41 16 98 68 47 13 87