diff --git a/Test1.1Task1/Test1.1Task1.Test/Test1.1Task1.Test.csproj b/Test1.1Task1/Test1.1Task1.Test/Test1.1Task1.Test.csproj new file mode 100644 index 0000000..2da2b49 --- /dev/null +++ b/Test1.1Task1/Test1.1Task1.Test/Test1.1Task1.Test.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + Test1._1Task1.Test + + false + + + + + + + + + + + + + diff --git a/Test1.1Task1/Test1.1Task1.Test/UnitTest1.cs b/Test1.1Task1/Test1.1Task1.Test/UnitTest1.cs new file mode 100644 index 0000000..b3b730c --- /dev/null +++ b/Test1.1Task1/Test1.1Task1.Test/UnitTest1.cs @@ -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)); + } + } +} \ No newline at end of file diff --git a/Test1.1Task1/Test1.1Task1.sln b/Test1.1Task1/Test1.1Task1.sln new file mode 100644 index 0000000..5eccb31 --- /dev/null +++ b/Test1.1Task1/Test1.1Task1.sln @@ -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 diff --git a/Test1.1Task1/Test1.1Task1/Program.cs b/Test1.1Task1/Test1.1Task1/Program.cs new file mode 100644 index 0000000..22e7e1f --- /dev/null +++ b/Test1.1Task1/Test1.1Task1/Program.cs @@ -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); + } + } +} diff --git a/Test1.1Task1/Test1.1Task1/Test1.1Task1.csproj b/Test1.1Task1/Test1.1Task1/Test1.1Task1.csproj new file mode 100644 index 0000000..2972635 --- /dev/null +++ b/Test1.1Task1/Test1.1Task1/Test1.1Task1.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp3.1 + Test1._1Task1 + + + diff --git a/Test1.1Task1/Test1.1Task1/Vectors.cs b/Test1.1Task1/Test1.1Task1/Vectors.cs new file mode 100644 index 0000000..0507e16 --- /dev/null +++ b/Test1.1Task1/Test1.1Task1/Vectors.cs @@ -0,0 +1,223 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Test1._1Task1 +{ + + public class Vectors + { + public Dictionary VectorsDictionary { get; set; } + + public void Add(Vectors vector, int index) + { + VectorsDictionary.Add(index, vector); + } + + public void delete(Vectors vector, int index) + { + VectorsDictionary.Add(index, vector); + } + + public (int index, int number)[] Vector { get; set; } + + 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; + + /// + /// сложение векторов + /// + /// вернет вектор сложения + public (int index, int number)[] Addition(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]; + 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; + } + + /// + /// вычитание выкторов + /// + /// вектор вычитания + 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; + } + + /// + /// проверка на нулевой вектор + /// + /// + /// вернет true если вектор нулевой + public bool CheckZeroVector(Vectors vector) + => vector.Vector.Length == 0; + + /// + /// Скалярное умножение + /// + /// + 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; + } + } +}