You are on page 1of 19

Міністерство освіти і науки України

Економіко-правничий коледж

Звіт з лабораторної роботи № 9

Тема: Структурне проектування програмного забезпечення

Виконав: Наврось Олександр


студент гр. к121-19
Прийняв: Чопорова О.В.

Запоріжжя 2023
Мета роботи: вивчення особливостей використання об’єктно-
орієнтованого підходу на практиці програмування, дослідження
особливостей перетворення зв’язків предметної області у об’єктну модель та
відповідний програмний код
Завдання:
Лістинг програми:

using System;

using System.Collections;

using System.Collections.Generic;

namespace Algebra

internal class Program

static void Main(string[] args)

class Vector

private int count;

private double[] data;

public Vector(long n)

count = (int)n;

data = new double[count];

public Vector(long n, double init)

count = (int)n;

data = new double[count];

for (int i = 0; i < count; i++)

data[i] = init;
}

public Vector(double[] arr)

count = arr.Length;

data = new double[count];

for (int i = 0; i < count; i++)

data[i] = arr[i];

public static Vector operator +(Vector v1, Vector v2)

if (v1.count != v2.count)

throw new ArgumentException("Vectors must have the same length");

Vector result = new Vector(v1.count);

for (int i = 0; i < v1.count; i++)

result.data[i] = v1.data[i] + v2.data[i];

return result;

public static Vector operator -(Vector v1, Vector v2)

if (v1.count != v2.count)
{

throw new ArgumentException("Vectors must have the same length");

Vector result = new Vector(v1.count);

for (int i = 0; i < v1.count; i++)

result.data[i] = v1.data[i] - v2.data[i];

return result;

public static double operator *(Vector v1, Vector v2)

if (v1.count != v2.count)

throw new ArgumentException("Vectors must have the same length");

double result = 0;

for (int i = 0; i < v1.count; i++)

result += v1.data[i] * v2.data[i];

return result;

public static Vector operator *(double s, Vector v)

Vector result = new Vector(v.count);


for (int i = 0; i < v.count; i++)

result.data[i] = s * v.data[i];

return result;

public static Vector operator /(Vector v, double s)

Vector result = new Vector(v.count);

for (int i = 0; i < v.count; i++)

result.data[i] = v.data[i] / s;

return result;

public double this[int index]

get { return data[index]; }

set { data[index] = value; }

public int size()

return count;

public Vector Add(Vector other)

{
if (this.count != other.count)

throw new ArgumentException("Vectors must have same size");

Vector result = new Vector(this.count);

for (int i = 0; i < this.count; i++)

result[i] = this[i] + other[i];

return result;

public Vector Subtract(Vector other)

if (this.count != other.count)

throw new ArgumentException("Vectors must have same size");

Vector result = new Vector(this.count);

for (int i = 0; i < this.count; i++)

result[i] = this[i] - other[i];

return result;

public double abs()


{

double sumOfSquares = 0.0;

foreach (double x in data)

sumOfSquares += x * x;

return Math.Sqrt(sumOfSquares);

public void read()

Console.WriteLine($"Enter {count} values:");

for (int i = 0; i < count; i++)

data[i] = double.Parse(Console.ReadLine());

public void write()

Console.Write("[ ");

for (int i = 0; i < count; i++)

Console.Write($"{data[i]} ");

Console.WriteLine("]");

abstract class Matrix


{

protected int rows;

protected int columns;

public int Size =>rows * columns;

public Matrix(int rows, int columns)

this.rows = rows;

this.columns = columns;

public abstract Matrix Add(Matrix m);

public abstract Matrix Subtract(Matrix m);

public abstract Matrix Multiply(Matrix m);

public abstract double this[int i, int j] { get; set; }

public void Read()

Console.WriteLine($"Enter the {rows}x{columns} matrix:");

for (int i = 0; i < rows; i++)

for (int j = 0; j < columns; j++)

this[i, j] = double.Parse(Console.ReadLine());

public void Write()


{

for (int i = 0; i < rows; i++)

for (int j = 0; j < columns; j++)

Console.Write($"{this[i, j]} ");

Console.WriteLine();

class RectMatrix : Matrix

private Vector[] data;

private int columns;

private int rows;

public RectMatrix(int rows, int columns) : base(rows, columns)

data = new Vector[rows];

for (int i = 0; i < rows; i++)

data[i] = new Vector(columns);

public RectMatrix(int rows, int columns, double init) : base(rows, columns)

data = new Vector[rows];


for (int i = 0; i < rows; i++)

data[i] = new Vector(columns, init);

public RectMatrix(double[][] arr) : base(arr.Length, arr[0].Length)

data = new Vector[rows];

for (int i = 0; i < rows; i++)

data[i] = new Vector(arr[i]);

public override Matrix Add(Matrix m)

RectMatrix rm = (RectMatrix)m;

if (rows != rm.rows || columns != rm.columns)

throw new ArgumentException("Matrices must be of same size");

RectMatrix result = new RectMatrix(rows, columns);

for (int i = 0; i < rows; i++)

result.data[i] = data[i].Add(rm.data[i]);

return result;

public override Matrix Subtract(Matrix m)


{

RectMatrix rm = (RectMatrix)m;

if (rows != rm.rows || columns != rm.columns)

throw new ArgumentException("Matrices must be of same size");

RectMatrix result = new RectMatrix(rows, columns);

for (int i = 0; i < rows; i++)

result.data[i] = data[i].Subtract(rm.data[i]);

return result;

public override Matrix Multiply(Matrix m)

RectMatrix rm = (RectMatrix)m;

if (columns != rm.rows)

throw new ArgumentException("Number of columns in first matrix must match number of rows
in second matrix");

RectMatrix result = new RectMatrix(rows, rm.columns);

for (int i = 0; i < rows; i++)

for (int j = 0; j < rm.columns; j++)

double sum = 0;

for (int k = 0; k < columns; k++)

sum += data[i][k] * rm.data[k][j];


}

result[i, j] = sum;

return result;

public override double this[int i, int j]

get { return data[i][j]; }

set { data[i][j] = value; }

class QuadMatrix : RectMatrix

public QuadMatrix(int size) : base(size, size) { }

public QuadMatrix(double[][] arr) : base(arr) { }

public double Determinant()

if (rows != columns)

throw new InvalidOperationException("Matrix must be square to have a determinant");

if (rows == 1)

return this[0, 0];

}
if (rows == 2)

return this[0, 0] * this[1, 1] - this[0, 1] * this[1, 0];

double det = 0;

int sign = 1;

for (int i = 0; i < rows; i++)

QuadMatrix subMatrix = SubMatrix(0, i);

det += sign * this[0, i] * subMatrix.Determinant();

sign = -sign;

return det;

private QuadMatrix SubMatrix(int rowToRemove, int colToRemove)

QuadMatrix subMatrix = new QuadMatrix(rows - 1);

for (int i = 0, ii = 0; i < rows; i++)

if (i == rowToRemove)

continue;

for (int j = 0, jj = 0; j < columns; j++)

if (j == colToRemove)

continue;

subMatrix[ii, jj] = this[i, j];


jj++;

ii++;

return subMatrix;

class SymmetricMatrix : QuadMatrix

private double[][] data;

private int columns;

private int rows;

public SymmetricMatrix(int size) : base(size)

data = new double[rows][];

for (int i = 0; i < rows; i++)

data[i] = new double[i + 1];

public SymmetricMatrix(double[][] arr) : base(arr)

if (!IsSymmetric(arr))

throw new ArgumentException("Matrix is not symmetric");

data = new double[rows][];

for (int i = 0; i < rows; i++)

data[i] = new double[i + 1];


for (int j = 0; j <= i; j++)

data[i][j] = arr[i][j];

public override double this[int i, int j]

get

if (i < j)

int temp = i;

i = j;

j = temp;

return data[i][j];

set

if (i < j)

int temp = i;

i = j;

j = temp;

data[i][j] = value;

private bool IsSymmetric(double[][] arr)


{

for (int i = 0; i < rows; i++)

for (int j = 0; j <= i; j++)

if (arr[i][j] != arr[j][i])

return false;

return true;

class Cramer

private QuadMatrix matrix;

private Vector vector;

}
Приклади використання розроблених методів:
Пошук визначника квадратної матриці
QuadMatrix A = new QuadMatrix(2);

A.Read();

double det = A.Determinant();

Console.WriteLine("The determinant of A is: " + det);

Пошук кореня з суми квадратів координат вектора


Vector v = new Vector(new double[] { 1, 2, 3, 4 });

double squareRoot = v.abs();

Console.WriteLine("The squareRoot is: " + squareRoot);

You might also like