Skip to content
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
20 changes: 20 additions & 0 deletions Test1.1Task1/Test1.1Task1.Test/Test1.1Task1.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Test1._1Task1.Test</RootNamespace>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Test1.1Task1\Test1.1Task1.csproj" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions Test1.1Task1/Test1.1Task1.Test/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NUnit.Framework;

namespace Test1._1Task1.Test
{
public class Tests
{
private Vectors vector1;
private Vectors vector2;

[SetUp]
public void Setup()
{
var vectorCoor2 = new (int, int)[3];
vectorCoor2[0] = (3, 1);
vectorCoor2[1] = (6, 1);
vectorCoor2[2] = (9, 1);
vector2 = new Vectors(10, vectorCoor2);
var vectorCoor1 = new (int, int)[4];
vectorCoor1[0] = (0, 1);
vectorCoor1[1] = (2, 1);
vectorCoor1[2] = (5, 1);
vectorCoor1[3] = (9, 1);
vector1 = new Vectors(10, vectorCoor1);
}

[TestCase]
public void TestAddition()
{
var vector = new (int, int)[6];
vector[0] = (0, 1);
vector[1] = (2, 1);
vector[2] = (3, 1);
vector[3] = (5, 1);
vector[4] = (6, 1);
vector[5] = (9, 2);
Assert.IsTrue(vector == vector1.Addition(vector1, vector2));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не-а, это будет ссылочное равенство. Assert.AreEqual бы помог, он сравнивает массивы поэлементно

}
}
}
31 changes: 31 additions & 0 deletions Test1.1Task1/Test1.1Task1.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 16
VisualStudioVersion = 16.0.31005.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test1.1Task1", "Test1.1Task1\Test1.1Task1.csproj", "{3DF1DD34-D661-404E-808E-C2406AF80C0D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test1.1Task1.Test", "Test1.1Task1.Test\Test1.1Task1.Test.csproj", "{2D93CC45-BB7D-4B1A-BB9C-E2AB94920997}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3DF1DD34-D661-404E-808E-C2406AF80C0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3DF1DD34-D661-404E-808E-C2406AF80C0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3DF1DD34-D661-404E-808E-C2406AF80C0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3DF1DD34-D661-404E-808E-C2406AF80C0D}.Release|Any CPU.Build.0 = Release|Any CPU
{2D93CC45-BB7D-4B1A-BB9C-E2AB94920997}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2D93CC45-BB7D-4B1A-BB9C-E2AB94920997}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2D93CC45-BB7D-4B1A-BB9C-E2AB94920997}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2D93CC45-BB7D-4B1A-BB9C-E2AB94920997}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9852C525-E330-4655-BB72-9784A305B02A}
EndGlobalSection
EndGlobal
30 changes: 30 additions & 0 deletions Test1.1Task1/Test1.1Task1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;

namespace Test1._1Task1
{
class Program
{
static void Main(string[] args)
{
var vectorCoor2 = new (int, int)[3];
vectorCoor2[0] = (3, 1);
vectorCoor2[1] = (6, 1);
vectorCoor2[2] = (9, 1);
var vector2 = new Vectors(10, vectorCoor2);
var vectorCoor1 = new (int, int)[4];
vectorCoor1[0] = (0, 1);
vectorCoor1[1] = (2, 1);
vectorCoor1[2] = (5, 1);
vectorCoor1[3] = (9, 1);
var vector1 = new Vectors(10, vectorCoor1);
var vector = new (int, int)[6];
vector[0] = (0, 1);
vector[1] = (2, 1);
vector[2] = (3, 1);
vector[3] = (5, 1);
vector[4] = (6, 1);
vector[5] = (9, 2);
vector = vector1.Addition(vector1, vector2);
}
}
}
9 changes: 9 additions & 0 deletions Test1.1Task1/Test1.1Task1/Test1.1Task1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Test1._1Task1</RootNamespace>
</PropertyGroup>

</Project>
223 changes: 223 additions & 0 deletions Test1.1Task1/Test1.1Task1/Vectors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Test1._1Task1
{

public class Vectors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо больше комментариев

{
public Dictionary<int, Vectors> VectorsDictionary { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Словарь, который отображает число в Vectors, это типа дерево векторов? Что? :)


public void Add(Vectors vector, int index)
{
VectorsDictionary.Add(index, vector);
}

public void delete(Vectors vector, int index)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Методы в C# всегда именуются с заглавной

{
VectorsDictionary.Add(index, vector);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Неправда :)

}

public (int index, int number)[] Vector { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вообще, плохая идея предоставлять public-сеттеры ко всему, чему можно, это нарушает инкапсуляцию


public int Size { get; set; }

public Vectors(int size, (int index, int number)[] vector)
{
Size = size;
Vector = new (int index, int number)[vector.Length];
for (int i = 0; i < vector.Length; i++)
{
Vector[i] = vector[i];
}
}

private bool CheckSize(int size1, int size2)
=> size1 == size2;

/// <summary>
/// сложение векторов
/// </summary>
/// <returns>вернет вектор сложения</returns>
public (int index, int number)[] Addition(Vectors vector1, Vectors vector2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Стоит всегда, где это осмысленно, стремиться к замкнутости операций (то есть чтобы сложение двух векторов возвращало вектор)

{
if (!CheckSize(vector1.Size, vector2.Size))
{
//throw Exception;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему нет? :)

}
int count1 = 0;
int count2 = 0;
int count = 0;
var vector = new (int index, int number)[vector1.Vector.Length + vector2.Vector.Length];
while (count1 < vector1.Vector.Length && count2 < vector2.Vector.Length)
{
var index1 = vector1.Vector[count1].index;
var index2 = vector2.Vector[count2].index;
if (index1 < index2)
{
vector[count] = vector1.Vector[count1];
count++;
count1++;
}
else if (index1 > index2)
{
vector[count] = vector2.Vector[count2];
count++;
count2++;
}
else
{
vector[count].number = vector1.Vector[count1].number + vector2.Vector[count2].number;
vector[count].index = vector1.Vector[count1].index;
count++;
count1++;
count2++;
}
}
if (count1 == vector1.Vector.Length && count2 != vector2.Vector.Length)
{
while (count2 < vector2.Vector.Length)
{
vector[count] = vector2.Vector[count2];
count++;
count2++;
}
}
else if (count2 == vector2.Vector.Length && count1 != vector1.Vector.Length)
{
while (count1 < vector1.Vector.Length)
{
vector[count] = vector1.Vector[count2];
count++;
count1++;
}
}
for (int i = vector.Length - 1; i >= 0; i--)
{
if (vector[i].index != 0 && vector[i].number != 0)
{
Array.Resize(ref vector, i);
break;
}
}
return vector;
}

/// <summary>
/// вычитание выкторов
/// </summary>
/// <returns>вектор вычитания</returns>
public (int index, int number)[] Substraction(Vectors vector1, Vectors vector2)
{
if (!CheckSize(vector1.Size, vector2.Size))
{
//throw Exception;
}
int count1 = 0;
int count2 = 0;
int count = 0;
var vector = new (int index, int number)[vector1.Vector.Length + vector2.Vector.Length];
while (count1 < vector1.Vector.Length && count2 < vector2.Vector.Length)
{
var index1 = vector1.Vector[count1].index;
var index2 = vector2.Vector[count2].index;
if (index1 < index2)
{
vector[count] = vector1.Vector[count1];
count++;
count1++;
}
else if (index1 > index2)
{
vector[count] = vector2.Vector[count2];
vector[count].number = vector2.Vector[count2].number * -1;
count++;
count2++;
}
else
{
vector[count].number = vector1.Vector[count1].number - vector2.Vector[count2].number;
count++;
count1++;
count2++;
}
}
if (count1 == vector1.Vector.Length && count2 != vector2.Vector.Length)
{
while (count2 < vector2.Vector.Length)
{
vector[count] = vector2.Vector[count2];
count++;
count2++;
}
}
else if (count2 == vector2.Vector.Length && count1 != vector1.Vector.Length)
{
while (count1 < vector1.Vector.Length)
{
vector[count] = vector1.Vector[count2];
count++;
count1++;
}
}
for (int i = vector.Length - 1; i >= 0; i--)
{
if (vector[i].index != 0 && vector[i].number != 0)
{
Array.Resize(ref vector, i);
break;
}
}
return vector;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Копипастище :) Можно было просто инвертировать знаки и сложить


/// <summary>
/// проверка на нулевой вектор
/// </summary>
/// <param name="vector"></param>
/// <returns>вернет true если вектор нулевой</returns>
public bool CheckZeroVector(Vectors vector)
=> vector.Vector.Length == 0;

/// <summary>
/// Скалярное умножение
/// </summary>
/// <returns></returns>
public int ScalarMultiplication(Vectors vector1, Vectors vector2)
{
if (!CheckSize(vector1.Size, vector2.Size))
{
//throw Exception;
}
int result = 0;
int count1 = 0;
int count2 = 0;
int count = 0;
while (count1 < vector1.Vector.Length && count2 < vector2.Vector.Length)
{
var index1 = vector1.Vector[count1].index;
var index2 = vector2.Vector[count2].index;
if (index1 < index2)
{
count++;
count1++;
index1 = vector1.Vector[count1].index;
}
else if (index1 > index2)
{
count++;
count2++;
}
else
{
result += vector1.Vector[count1].number * vector2.Vector[count2].number;
count++;
count1++;
count2++;
}
}
return result;
}
}
}