Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Казанцев Д.Д. МК-1 #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Matrix_v4/Matrix_v4.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Matrix_v4", "Matrix_v4\Matrix_v4.vcxproj", "{5AEBB516-637B-4800-B572-BED6D0554ACC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Debug|x64.ActiveCfg = Debug|x64
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Debug|x64.Build.0 = Debug|x64
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Debug|x86.ActiveCfg = Debug|Win32
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Debug|x86.Build.0 = Debug|Win32
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Release|x64.ActiveCfg = Release|x64
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Release|x64.Build.0 = Release|x64
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Release|x86.ActiveCfg = Release|Win32
{5AEBB516-637B-4800-B572-BED6D0554ACC}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {04F3D4DD-8B62-49AB-AA9F-9E68E77B5920}
EndGlobalSection
EndGlobal
83 changes: 83 additions & 0 deletions Matrix_v4/Matrix_v4/Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "Matrix.h"
using namespace std;

Matrix::Matrix(int rows, int cols) {
if (rows < 0 || cols < 0) {
throw out_of_range("Number of rows and columns must be non-negative");
}
num_rows = rows;
num_cols = cols;
data.assign(rows, vector<int>(cols, 0));
}


void Matrix::Reset(int rows, int cols) {
if (rows < 0 || cols < 0) {
throw out_of_range("Number of rows and columns must be non-negative");
}
num_rows = rows;
num_cols = cols;
data.assign(rows, vector<int>(cols, 0));
}

int Matrix::At(int row, int col) const {
if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) {
throw out_of_range("Index out of range");
}
return data[row][col];
}

int& Matrix::At(int row, int col) {
if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) {
throw out_of_range("Index out of range");
}
return data[row][col];
}

int Matrix::GetRows() const {
return num_rows;
}

int Matrix::GetCols() const {
return num_cols;
}

istream& operator>>(istream& in, Matrix& matrix) {
int rows, cols;
in >> rows >> cols;
matrix.Reset(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
in >> matrix.At(i, j);
}
}
return in;
}

ostream& operator<<(ostream& out, const Matrix& matrix) {
out << matrix.GetRows() << " " << matrix.GetCols() << "\n";
for (int i = 0; i < matrix.GetRows(); ++i) {
for (int j = 0; j < matrix.GetCols(); ++j) {
out << matrix.At(i, j) << " ";
}
out << "\n";
}
return out;
}

bool operator==(const Matrix& lhs, const Matrix& rhs) {
return lhs.num_rows == rhs.num_rows && lhs.num_cols == rhs.num_cols && lhs.data == rhs.data;
}

Matrix operator+(const Matrix& lhs, const Matrix& rhs) {
if (lhs.num_rows != rhs.num_rows || lhs.num_cols != rhs.num_cols) {
throw invalid_argument("Matrices must have the same dimensions for addition");
}
Matrix result(lhs.num_rows, lhs.num_cols);
for (int i = 0; i < lhs.num_rows; ++i) {
for (int j = 0; j < lhs.num_cols; ++j) {
result.At(i, j) = lhs.At(i, j) + rhs.At(i, j);
}
}
return result;
}
29 changes: 29 additions & 0 deletions Matrix_v4/Matrix_v4/Matrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;

class Matrix {
private:
int num_rows;
int num_cols;
vector<vector<int>> data;
public:
Matrix() : num_rows(0), num_cols(0) {}
Matrix(int rows, int cols);
Matrix(const Matrix& other) : num_rows(other.num_rows), num_cols(other.num_cols), data(other.data) {}
Matrix(Matrix&& other) noexcept : num_rows(other.num_rows), num_cols(other.num_cols), data(move(other.data)) {
other.num_rows = 0;
other.num_cols = 0;
}
void Reset(int rows, int cols);
int At(int row, int col) const;
int& At(int row, int col);
int GetRows() const;
int GetCols() const;
friend istream& operator>>(istream& in, Matrix& matrix);
friend ostream& operator<<(ostream& out, const Matrix& matrix);
friend bool operator==(const Matrix& lhs, const Matrix& rhs);
friend Matrix operator+(const Matrix& lhs, const Matrix& rhs);
};
69 changes: 69 additions & 0 deletions Matrix_v4/Matrix_v4/Matrix_v4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <vector>
#include <stdexcept>
#include "Matrix.h"
using namespace std;

int main() {
try {
// Создание матрицы по умолчанию
Matrix m1;
cout << "Matrix m1 (default):\n" << m1 << "\n";

// Создание матрицы с заданными размерами
Matrix m2(2, 3);
cout << "Matrix m2 (2x3):\n" << m2 << "\n";

// Установка значений в матрице
m2.At(0, 0) = 1;
m2.At(0, 1) = 2;
m2.At(0, 2) = 3;
m2.At(1, 0) = 4;
m2.At(1, 1) = 5;
m2.At(1, 2) = 6;
cout << "Matrix m2 after setting values:\n" << m2 << "\n";

// Использование конструктора копирования
Matrix m3 = m2;
cout << "Matrix m3 (copy of m2):\n" << m3 << "\n";

// Использование оператора перемещения
Matrix m4 = move(m3);
cout << "Matrix m4 (moved from m3):\n" << m4 << "\n";

// Проверка метода Reset
m4.Reset(3, 2);
cout << "Matrix m4 after Reset to 3x2:\n" << m4 << "\n";

// Проверка операторов ввода и вывода
cout << "Enter a matrix (format: rows cols followed by elements):\n";
Matrix m5;
cin >> m5;
cout << "Matrix m5 (input):\n" << m5 << "\n";

// Проверка оператора равенства
cout << "m2 == m5: " << (m2 == m5 ? "true" : "false") << "\n";

// Проверка оператора сложения
Matrix m6(2, 3);
m6.At(0, 0) = 7;
m6.At(0, 1) = 8;
m6.At(0, 2) = 9;
m6.At(1, 0) = 10;
m6.At(1, 1) = 11;
m6.At(1, 2) = 12;
cout << "Matrix m6:\n" << m6 << "\n";

Matrix m7 = m2 + m6;
cout << "Matrix m7 (m2 + m6):\n" << m7 << "\n";

}
catch (const out_of_range& e) {
cerr << "Out of range error: " << e.what() << "\n";
}
catch (const invalid_argument& e) {
cerr << "Invalid argument error: " << e.what() << "\n";
}

return 0;
}
139 changes: 139 additions & 0 deletions Matrix_v4/Matrix_v4/Matrix_v4.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{5aebb516-637b-4800-b572-bed6d0554acc}</ProjectGuid>
<RootNamespace>Matrixv4</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Matrix.cpp" />
<ClCompile Include="Matrix_v4.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Matrix.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
30 changes: 30 additions & 0 deletions Matrix_v4/Matrix_v4/Matrix_v4.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Исходные файлы">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Файлы заголовков">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Файлы ресурсов">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Matrix_v4.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="Matrix.cpp">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Matrix.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions Matrix_v4/Matrix_v4/Matrix_v4.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
Binary file not shown.
Loading
Loading