From 28eab773c343ed3ed2550bd49c26003729f07cda Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 12 Mar 2021 11:22:31 +0300 Subject: [PATCH 01/10] Interface and calculator function --- hw2Calculator/hw2Calculator.sln | 25 +++++ hw2Calculator/hw2Calculator/Calculator.cs | 97 +++++++++++++++++++ hw2Calculator/hw2Calculator/IStack.cs | 17 ++++ hw2Calculator/hw2Calculator/Program.cs | 11 +++ hw2Calculator/hw2Calculator/StackArray.cs | 49 ++++++++++ hw2Calculator/hw2Calculator/StackList.cs | 37 +++++++ .../hw2Calculator/hw2Calculator.csproj | 8 ++ 7 files changed, 244 insertions(+) create mode 100644 hw2Calculator/hw2Calculator.sln create mode 100644 hw2Calculator/hw2Calculator/Calculator.cs create mode 100644 hw2Calculator/hw2Calculator/IStack.cs create mode 100644 hw2Calculator/hw2Calculator/Program.cs create mode 100644 hw2Calculator/hw2Calculator/StackArray.cs create mode 100644 hw2Calculator/hw2Calculator/StackList.cs create mode 100644 hw2Calculator/hw2Calculator/hw2Calculator.csproj diff --git a/hw2Calculator/hw2Calculator.sln b/hw2Calculator/hw2Calculator.sln new file mode 100644 index 0000000..16c306d --- /dev/null +++ b/hw2Calculator/hw2Calculator.sln @@ -0,0 +1,25 @@ + +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}") = "hw2Calculator", "hw2Calculator\hw2Calculator.csproj", "{6910FA32-065A-451E-93C8-D690BA4C486A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6910FA32-065A-451E-93C8-D690BA4C486A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6910FA32-065A-451E-93C8-D690BA4C486A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6910FA32-065A-451E-93C8-D690BA4C486A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6910FA32-065A-451E-93C8-D690BA4C486A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {851B8E89-996A-4A60-A0E1-8BC3BA05EEFB} + EndGlobalSection +EndGlobal diff --git a/hw2Calculator/hw2Calculator/Calculator.cs b/hw2Calculator/hw2Calculator/Calculator.cs new file mode 100644 index 0000000..2d45d50 --- /dev/null +++ b/hw2Calculator/hw2Calculator/Calculator.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw2Calculator +{ + class Calculator + { + public static double CalculatorExpression(string expression, ref bool IsCorrect, IStack stack) + { + string number = ""; + char[] experssionArray = expression.ToCharArray(0, expression.Length); + for (int i = 0; i < expression.Length; ++i) + { + if (experssionArray[i] == ' ') + { + number = ""; + continue; + } + if (Char.IsDigit(experssionArray[i])) + { + while (i < expression.Length && experssionArray[i] != ' ') + { + number += experssionArray[i]; + ++i; + } + stack.Push(double.Parse(number)); + number = ""; + continue; + } + if (IsOperand(experssionArray[i])) + { + if (stack.IsEmpty()) + { + IsCorrect = false; + return 0; + } + double lastNumber = stack.Pop(); + if (stack.IsEmpty() || (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) + { + stack.DeleteStack(); + IsCorrect = false; + return 0; + } + stack.Push(lastNumber); + AphyphyticAction(experssionArray[i], stack); + } + else + { + stack.DeleteStack(); + IsCorrect = false; + return 0; + } + } + if (stack.IsEmpty()) + { + IsCorrect = false; + return 0; + } + var result = stack.Pop(); + if (stack.IsEmpty()) + { + IsCorrect = true; + return result; + } + stack.DeleteStack(); + IsCorrect = false; + return 0; + } + + private static bool IsOperand(char symbol) + { + return symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'; + } + + private static void AphyphyticAction(char operation, IStack stack) + { + var number2 = stack.Pop(); + var number1 = stack.Pop(); + switch (operation) + { + case '+': + stack.Push(number1 + number2); + break; + case '-': + stack.Push(number1 - number2); + break; + case '*': + stack.Push(number1 * number2); + break; + default: + stack.Push(number1 / number2); + break; + } + } + } +} \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/IStack.cs b/hw2Calculator/hw2Calculator/IStack.cs new file mode 100644 index 0000000..2cfdb48 --- /dev/null +++ b/hw2Calculator/hw2Calculator/IStack.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw2Calculator +{ + interface IStack + { + void Push(double value); + + double Pop(); + + bool IsEmpty(); + + void DeleteStack(); + } +} \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/Program.cs b/hw2Calculator/hw2Calculator/Program.cs new file mode 100644 index 0000000..6f0328b --- /dev/null +++ b/hw2Calculator/hw2Calculator/Program.cs @@ -0,0 +1,11 @@ +using System; + +namespace hw2Calculator +{ + class Program + { + static void Main(string[] args) + { + } + } +} diff --git a/hw2Calculator/hw2Calculator/StackArray.cs b/hw2Calculator/hw2Calculator/StackArray.cs new file mode 100644 index 0000000..6411384 --- /dev/null +++ b/hw2Calculator/hw2Calculator/StackArray.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw2Calculator +{ + class StackArray : IStack + { + private double[] stackElements; + private int index; + + public StackArray() + { + stackElements = new double[5]; + } + + public void Push(double value) + { + if (index == stackElements.Length) + { + + } + stackElements[index] = value; + index++; + } + + public double Pop() + { + if (IsEmpty()) + { + throw new InvalidOperationException(); + } + index--; + double delete = stackElements[index]; + return delete; + } + + public bool IsEmpty() + { + return index == 0; + } + + public void DeleteStack() + { + stackElements = new double[5]; + index = 0; + } + } +} \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/StackList.cs b/hw2Calculator/hw2Calculator/StackList.cs new file mode 100644 index 0000000..8fef59b --- /dev/null +++ b/hw2Calculator/hw2Calculator/StackList.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw2Calculator +{ + class StackList : IStack + { + private class StackElement + { + public double value; + public StackElement next; + } + + private StackElement head; + + public void Push(double value) + => head = new StackElement() { value = value, next = head }; + + public double Pop() + { + if (IsEmpty()) + { + throw new InvalidOperationException(); + } + double answer = head.value; + head = head.next; + return answer; + } + + public bool IsEmpty() + => head == null; + + public void DeleteStack() + => head = null; + } +} \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/hw2Calculator.csproj b/hw2Calculator/hw2Calculator/hw2Calculator.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/hw2Calculator/hw2Calculator/hw2Calculator.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + From 530b51a4da39d7cca1ff19a38ff8062fbca959d1 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 12 Mar 2021 15:30:45 +0300 Subject: [PATCH 02/10] add test and calculator function --- hw2Calculator/hw2Calculator/Calculator.cs | 4 +-- hw2Calculator/hw2Calculator/Program.cs | 42 ++++++++++++++++++++++- hw2Calculator/hw2Calculator/Test.cs | 37 ++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 hw2Calculator/hw2Calculator/Test.cs diff --git a/hw2Calculator/hw2Calculator/Calculator.cs b/hw2Calculator/hw2Calculator/Calculator.cs index 2d45d50..44c7654 100644 --- a/hw2Calculator/hw2Calculator/Calculator.cs +++ b/hw2Calculator/hw2Calculator/Calculator.cs @@ -43,7 +43,7 @@ public static double CalculatorExpression(string expression, ref bool IsCorrect, return 0; } stack.Push(lastNumber); - AphyphyticAction(experssionArray[i], stack); + ArithmeticOperation(experssionArray[i], stack); } else { @@ -73,7 +73,7 @@ private static bool IsOperand(char symbol) return symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'; } - private static void AphyphyticAction(char operation, IStack stack) + private static void ArithmeticOperation(char operation, IStack stack) { var number2 = stack.Pop(); var number1 = stack.Pop(); diff --git a/hw2Calculator/hw2Calculator/Program.cs b/hw2Calculator/hw2Calculator/Program.cs index 6f0328b..746e026 100644 --- a/hw2Calculator/hw2Calculator/Program.cs +++ b/hw2Calculator/hw2Calculator/Program.cs @@ -6,6 +6,46 @@ class Program { static void Main(string[] args) { + if (!Test.TestCalculator()) + { + Console.WriteLine("Тест не пройден"); + return; + } + Console.WriteLine("Тест пройден"); + Console.WriteLine("Меню:"); + Console.WriteLine("1 - стэк на списках."); + Console.WriteLine("2 - стэк на массиве."); + Console.WriteLine("Ваш выбор:"); + var str = Console.ReadLine(); + if (!int.TryParse(str, out int choice)) + { + Console.WriteLine("Ошибка ввода"); + return; + } + IStack stack; + if (choice == 1) + { + stack = new StackList(); + } + else if (choice == 2) + { + stack = new StackArray(); + } + else + { + Console.WriteLine("Ошибка ввода"); + return; + } + Console.WriteLine("Введите выражение в постфиксной форме: "); + string expression = Console.ReadLine(); + bool isCorrect = false; + var result = Calculator.CalculatorExpression(expression, ref isCorrect, stack); + if (!isCorrect) + { + Console.WriteLine("Ошибка ввода"); + return; + } + Console.WriteLine("Ответ: " + result); } } -} +} \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/Test.cs b/hw2Calculator/hw2Calculator/Test.cs new file mode 100644 index 0000000..88266dc --- /dev/null +++ b/hw2Calculator/hw2Calculator/Test.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw2Calculator +{ + class Test + { + public static bool TestCalculator() + { + string expresion1 = "2 4 * 2 -"; + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + bool isCorrect1 = false; + bool isCorrect2 = false; + if ((Calculator.CalculatorExpression(expresion1, ref isCorrect1, stack1) != 6 && isCorrect1 != true) || (Calculator.CalculatorExpression(expresion1, ref isCorrect2, stack2) != 6 && isCorrect2 != true)) + { + return false; + } + string expresion2 = "2 4 * 2"; + Calculator.CalculatorExpression(expresion2, ref isCorrect1, stack1); + Calculator.CalculatorExpression(expresion2, ref isCorrect2, stack2); + if (isCorrect1 != false || isCorrect2 != false) + { + return false; + } + string expresion3 = "*"; + Calculator.CalculatorExpression(expresion3, ref isCorrect1, stack1); + Calculator.CalculatorExpression(expresion3, ref isCorrect2, stack2); + if (isCorrect1 != false || isCorrect2 != false) + { + return false; + } + return true; + } + } +} \ No newline at end of file From 77631778899900cd61090ee276d431c09d9780fb Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 12 Mar 2021 17:09:21 +0300 Subject: [PATCH 03/10] fix defects --- hw2Calculator/hw2Calculator/Calculator.cs | 8 ++++---- hw2Calculator/hw2Calculator/Program.cs | 12 ++++++------ hw2Calculator/hw2Calculator/StackArray.cs | 18 +++++++++--------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/hw2Calculator/hw2Calculator/Calculator.cs b/hw2Calculator/hw2Calculator/Calculator.cs index 44c7654..6f3220b 100644 --- a/hw2Calculator/hw2Calculator/Calculator.cs +++ b/hw2Calculator/hw2Calculator/Calculator.cs @@ -30,13 +30,13 @@ public static double CalculatorExpression(string expression, ref bool IsCorrect, } if (IsOperand(experssionArray[i])) { - if (stack.IsEmpty()) + /*if (stack.IsEmpty()) { IsCorrect = false; return 0; - } + }*/ double lastNumber = stack.Pop(); - if (stack.IsEmpty() || (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) + if (/* stack.IsEmpty() || */ (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) { stack.DeleteStack(); IsCorrect = false; @@ -88,7 +88,7 @@ private static void ArithmeticOperation(char operation, IStack stack) case '*': stack.Push(number1 * number2); break; - default: + case '/': stack.Push(number1 / number2); break; } diff --git a/hw2Calculator/hw2Calculator/Program.cs b/hw2Calculator/hw2Calculator/Program.cs index 746e026..348eb78 100644 --- a/hw2Calculator/hw2Calculator/Program.cs +++ b/hw2Calculator/hw2Calculator/Program.cs @@ -4,14 +4,14 @@ namespace hw2Calculator { class Program { - static void Main(string[] args) + static void Main() { if (!Test.TestCalculator()) { - Console.WriteLine("Тест не пройден"); + Console.WriteLine("Тест не пройден!"); return; } - Console.WriteLine("Тест пройден"); + Console.WriteLine("Тест пройден!"); Console.WriteLine("Меню:"); Console.WriteLine("1 - стэк на списках."); Console.WriteLine("2 - стэк на массиве."); @@ -19,7 +19,7 @@ static void Main(string[] args) var str = Console.ReadLine(); if (!int.TryParse(str, out int choice)) { - Console.WriteLine("Ошибка ввода"); + Console.WriteLine("Ошибка ввода!"); return; } IStack stack; @@ -33,7 +33,7 @@ static void Main(string[] args) } else { - Console.WriteLine("Ошибка ввода"); + Console.WriteLine("Ошибка ввода!"); return; } Console.WriteLine("Введите выражение в постфиксной форме: "); @@ -42,7 +42,7 @@ static void Main(string[] args) var result = Calculator.CalculatorExpression(expression, ref isCorrect, stack); if (!isCorrect) { - Console.WriteLine("Ошибка ввода"); + Console.WriteLine("Ошибка ввода!"); return; } Console.WriteLine("Ответ: " + result); diff --git a/hw2Calculator/hw2Calculator/StackArray.cs b/hw2Calculator/hw2Calculator/StackArray.cs index 6411384..be14d3b 100644 --- a/hw2Calculator/hw2Calculator/StackArray.cs +++ b/hw2Calculator/hw2Calculator/StackArray.cs @@ -7,7 +7,7 @@ namespace hw2Calculator class StackArray : IStack { private double[] stackElements; - private int index; + private int countNumbersInStack; public StackArray() { @@ -16,12 +16,12 @@ public StackArray() public void Push(double value) { - if (index == stackElements.Length) + if (countNumbersInStack == stackElements.Length) { - + Array.Resize(ref stackElements, stackElements.Length * 2); } - stackElements[index] = value; - index++; + stackElements[countNumbersInStack] = value; + countNumbersInStack++; } public double Pop() @@ -30,20 +30,20 @@ public double Pop() { throw new InvalidOperationException(); } - index--; - double delete = stackElements[index]; + countNumbersInStack--; + double delete = stackElements[countNumbersInStack]; return delete; } public bool IsEmpty() { - return index == 0; + return countNumbersInStack == 0; } public void DeleteStack() { stackElements = new double[5]; - index = 0; + countNumbersInStack = 0; } } } \ No newline at end of file From b896f7bde99e11b6e2bf38094fe8a5cbbc55bbc1 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Tue, 23 Mar 2021 20:38:36 +0300 Subject: [PATCH 04/10] change tests --- .../hw2Calculator.Test/CalculatorTest.cs | 60 +++++++++++++++++++ .../hw2Calculator.Test/IStackTest.cs | 54 +++++++++++++++++ .../hw2Calculator.Test.csproj | 19 ++++++ hw2Calculator/hw2Calculator.sln | 8 ++- hw2Calculator/hw2Calculator/Calculator.cs | 8 +-- hw2Calculator/hw2Calculator/IStack.cs | 2 +- hw2Calculator/hw2Calculator/Program.cs | 6 -- hw2Calculator/hw2Calculator/StackArray.cs | 2 +- hw2Calculator/hw2Calculator/StackList.cs | 2 +- 9 files changed, 147 insertions(+), 14 deletions(-) create mode 100644 hw2Calculator/hw2Calculator.Test/CalculatorTest.cs create mode 100644 hw2Calculator/hw2Calculator.Test/IStackTest.cs create mode 100644 hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj diff --git a/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs new file mode 100644 index 0000000..5ec9540 --- /dev/null +++ b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs @@ -0,0 +1,60 @@ +using NUnit.Framework; + +namespace hw2Calculator.Test +{ + public class Tests + { + private bool CheckTrueExpression(bool isCorrect1, bool isCorrect2, double result1, double result2, double mainResult) + { + if ((!isCorrect1 || !isCorrect2) || (result1 != mainResult || result2 != mainResult)) + { + return false; + } + return true; + } + + private bool CheckFalseExpression(bool isCorrect1, bool isCorrect2) + { + return (!isCorrect1 && !isCorrect2); + } + + [TestCase] + public void TestTrueExpression() + { + string expresion = "2 4 * 2 -"; + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + bool isCorrect1 = false; + bool isCorrect2 = false; + var result1 = Calculator.CalculatorExpression(expresion, ref isCorrect1, stack1); + var result2 = Calculator.CalculatorExpression(expresion, ref isCorrect2, stack2); + Assert.IsTrue(CheckTrueExpression(isCorrect1, isCorrect2, result1, result2, 6.0)); + } + + [TestCase] + public void TestFalseExpression() + { + string expresion = "2 4 * 2"; + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + bool isCorrect1 = false; + bool isCorrect2 = false; + Calculator.CalculatorExpression(expresion, ref isCorrect1, stack1); + Calculator.CalculatorExpression(expresion, ref isCorrect2, stack2); + Assert.IsTrue(CheckFalseExpression(isCorrect1, isCorrect2)); + } + + [TestCase] + public void TestDivisionByZero() + { + string expresion = "2 0 /"; + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + bool isCorrect1 = false; + bool isCorrect2 = false; + Calculator.CalculatorExpression(expresion, ref isCorrect1, stack1); + Calculator.CalculatorExpression(expresion, ref isCorrect2, stack2); + Assert.IsTrue(CheckFalseExpression(isCorrect1, isCorrect2)); + } + } +} \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator.Test/IStackTest.cs b/hw2Calculator/hw2Calculator.Test/IStackTest.cs new file mode 100644 index 0000000..8c6aef1 --- /dev/null +++ b/hw2Calculator/hw2Calculator.Test/IStackTest.cs @@ -0,0 +1,54 @@ +using NUnit.Framework; + +namespace hw2Calculator.Test +{ + [TestFixture] + class IStackTest + { + [TestCase] + public void IsEmptyAfterPush() + { + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + stack1.Push(9); + stack2.Push(8); + Assert.IsTrue(!(stack1.IsEmpty() && stack2.IsEmpty())); + } + + [TestCase] + public void PopAfterPush() + { + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + stack1.Push(9); + stack2.Push(8); + Assert.IsTrue((stack1.Pop() == 9 && stack2.Pop() == 8)); + } + + [TestCase] + public void CheckDeleteStack() + { + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + stack1.Push(8); + stack1.Push(9); + stack2.Push(8); + stack2.Push(9); + stack1.DeleteStack(); + stack2.DeleteStack(); + Assert.IsTrue((stack1.IsEmpty() && stack2.IsEmpty())); + } + + [TestCase] + public void CheckIsEmpty() + { + IStack stack1 = new StackList(); + IStack stack2 = new StackArray(); + stack1.Push(9); + stack2.Push(8); + stack1.Pop(); + stack2.Pop(); + Assert.IsTrue((stack1.IsEmpty() && stack2.IsEmpty())); + } + } +} \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj b/hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj new file mode 100644 index 0000000..d5fa794 --- /dev/null +++ b/hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + diff --git a/hw2Calculator/hw2Calculator.sln b/hw2Calculator/hw2Calculator.sln index 16c306d..7b9155e 100644 --- a/hw2Calculator/hw2Calculator.sln +++ b/hw2Calculator/hw2Calculator.sln @@ -3,7 +3,9 @@ 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}") = "hw2Calculator", "hw2Calculator\hw2Calculator.csproj", "{6910FA32-065A-451E-93C8-D690BA4C486A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw2Calculator", "hw2Calculator\hw2Calculator.csproj", "{6910FA32-065A-451E-93C8-D690BA4C486A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw2Calculator.Test", "hw2Calculator.Test\hw2Calculator.Test.csproj", "{9AB5D195-767E-4A4A-BCC8-66536BBE511F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {6910FA32-065A-451E-93C8-D690BA4C486A}.Debug|Any CPU.Build.0 = Debug|Any CPU {6910FA32-065A-451E-93C8-D690BA4C486A}.Release|Any CPU.ActiveCfg = Release|Any CPU {6910FA32-065A-451E-93C8-D690BA4C486A}.Release|Any CPU.Build.0 = Release|Any CPU + {9AB5D195-767E-4A4A-BCC8-66536BBE511F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9AB5D195-767E-4A4A-BCC8-66536BBE511F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9AB5D195-767E-4A4A-BCC8-66536BBE511F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9AB5D195-767E-4A4A-BCC8-66536BBE511F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/hw2Calculator/hw2Calculator/Calculator.cs b/hw2Calculator/hw2Calculator/Calculator.cs index 6f3220b..fc59809 100644 --- a/hw2Calculator/hw2Calculator/Calculator.cs +++ b/hw2Calculator/hw2Calculator/Calculator.cs @@ -4,7 +4,7 @@ namespace hw2Calculator { - class Calculator + public static class Calculator { public static double CalculatorExpression(string expression, ref bool IsCorrect, IStack stack) { @@ -30,13 +30,13 @@ public static double CalculatorExpression(string expression, ref bool IsCorrect, } if (IsOperand(experssionArray[i])) { - /*if (stack.IsEmpty()) + if (stack.IsEmpty()) { IsCorrect = false; return 0; - }*/ + } double lastNumber = stack.Pop(); - if (/* stack.IsEmpty() || */ (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) + if ( stack.IsEmpty() || (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) { stack.DeleteStack(); IsCorrect = false; diff --git a/hw2Calculator/hw2Calculator/IStack.cs b/hw2Calculator/hw2Calculator/IStack.cs index 2cfdb48..cabb3c5 100644 --- a/hw2Calculator/hw2Calculator/IStack.cs +++ b/hw2Calculator/hw2Calculator/IStack.cs @@ -4,7 +4,7 @@ namespace hw2Calculator { - interface IStack + public interface IStack { void Push(double value); diff --git a/hw2Calculator/hw2Calculator/Program.cs b/hw2Calculator/hw2Calculator/Program.cs index 348eb78..1b6b26c 100644 --- a/hw2Calculator/hw2Calculator/Program.cs +++ b/hw2Calculator/hw2Calculator/Program.cs @@ -6,12 +6,6 @@ class Program { static void Main() { - if (!Test.TestCalculator()) - { - Console.WriteLine("Тест не пройден!"); - return; - } - Console.WriteLine("Тест пройден!"); Console.WriteLine("Меню:"); Console.WriteLine("1 - стэк на списках."); Console.WriteLine("2 - стэк на массиве."); diff --git a/hw2Calculator/hw2Calculator/StackArray.cs b/hw2Calculator/hw2Calculator/StackArray.cs index be14d3b..6158f89 100644 --- a/hw2Calculator/hw2Calculator/StackArray.cs +++ b/hw2Calculator/hw2Calculator/StackArray.cs @@ -4,7 +4,7 @@ namespace hw2Calculator { - class StackArray : IStack + public class StackArray : IStack { private double[] stackElements; private int countNumbersInStack; diff --git a/hw2Calculator/hw2Calculator/StackList.cs b/hw2Calculator/hw2Calculator/StackList.cs index 8fef59b..fbf1ac8 100644 --- a/hw2Calculator/hw2Calculator/StackList.cs +++ b/hw2Calculator/hw2Calculator/StackList.cs @@ -4,7 +4,7 @@ namespace hw2Calculator { - class StackList : IStack + public class StackList : IStack { private class StackElement { From d37a657313848d4d25f23f996dc34d2c6acaa4ae Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Wed, 7 Apr 2021 18:13:51 +0300 Subject: [PATCH 05/10] appveyor.yml --- appveyor.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..e7e7cb3 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,6 @@ +image: Visual Studio 2019 + +build_script: + - For /R %%I in (*.sln) do dotnet test %%I + +test: off \ No newline at end of file From b70cd04417d7bc505aba1a39f579899f23fd6621 Mon Sep 17 00:00:00 2001 From: MikePuzanov <71733172+MikePuzanov@users.noreply.github.com> Date: Tue, 20 Apr 2021 20:12:40 +0300 Subject: [PATCH 06/10] fix test Co-authored-by: Yurii Litvinov --- hw2Calculator/hw2Calculator/Test.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/hw2Calculator/hw2Calculator/Test.cs b/hw2Calculator/hw2Calculator/Test.cs index 88266dc..5abb93c 100644 --- a/hw2Calculator/hw2Calculator/Test.cs +++ b/hw2Calculator/hw2Calculator/Test.cs @@ -27,11 +27,7 @@ public static bool TestCalculator() string expresion3 = "*"; Calculator.CalculatorExpression(expresion3, ref isCorrect1, stack1); Calculator.CalculatorExpression(expresion3, ref isCorrect2, stack2); - if (isCorrect1 != false || isCorrect2 != false) - { - return false; - } - return true; + return isCorrect1 == false && isCorrect2 == false; } } -} \ No newline at end of file +} From e8cc8d74b94931aeac8390543ae296c7a61d1fd6 Mon Sep 17 00:00:00 2001 From: MikePuzanov <71733172+MikePuzanov@users.noreply.github.com> Date: Tue, 20 Apr 2021 20:21:11 +0300 Subject: [PATCH 07/10] Update hw2Calculator/hw2Calculator/Calculator.cs Co-authored-by: Yurii Litvinov --- hw2Calculator/hw2Calculator/Calculator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hw2Calculator/hw2Calculator/Calculator.cs b/hw2Calculator/hw2Calculator/Calculator.cs index fc59809..7d9d841 100644 --- a/hw2Calculator/hw2Calculator/Calculator.cs +++ b/hw2Calculator/hw2Calculator/Calculator.cs @@ -36,7 +36,7 @@ public static double CalculatorExpression(string expression, ref bool IsCorrect, return 0; } double lastNumber = stack.Pop(); - if ( stack.IsEmpty() || (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) + if (stack.IsEmpty() || (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) { stack.DeleteStack(); IsCorrect = false; @@ -94,4 +94,4 @@ private static void ArithmeticOperation(char operation, IStack stack) } } } -} \ No newline at end of file +} From 123ffefbf2c3ed92ff65e78c197032acbf1baf44 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 20 Apr 2021 22:30:57 +0300 Subject: [PATCH 08/10] fix mistakes --- .../hw2Calculator.Test/CalculatorTest.cs | 31 +++++-------- .../hw2Calculator.Test/IStackTest.cs | 11 ++--- hw2Calculator/hw2Calculator/Calculator.cs | 44 ++++++++----------- hw2Calculator/hw2Calculator/Program.cs | 22 +++++----- hw2Calculator/hw2Calculator/Test.cs | 20 ++++----- 5 files changed, 54 insertions(+), 74 deletions(-) diff --git a/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs index 5ec9540..1a396be 100644 --- a/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs +++ b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs @@ -6,11 +6,8 @@ public class Tests { private bool CheckTrueExpression(bool isCorrect1, bool isCorrect2, double result1, double result2, double mainResult) { - if ((!isCorrect1 || !isCorrect2) || (result1 != mainResult || result2 != mainResult)) - { - return false; - } - return true; + return (!isCorrect1 || !isCorrect2) || (result1 - mainResult < 0.000001 || result2 - mainResult < 0.000001); + } private bool CheckFalseExpression(bool isCorrect1, bool isCorrect2) @@ -18,42 +15,36 @@ private bool CheckFalseExpression(bool isCorrect1, bool isCorrect2) return (!isCorrect1 && !isCorrect2); } - [TestCase] + [Test] public void TestTrueExpression() { string expresion = "2 4 * 2 -"; IStack stack1 = new StackList(); IStack stack2 = new StackArray(); - bool isCorrect1 = false; - bool isCorrect2 = false; - var result1 = Calculator.CalculatorExpression(expresion, ref isCorrect1, stack1); - var result2 = Calculator.CalculatorExpression(expresion, ref isCorrect2, stack2); + (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion, stack1); + (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion, stack2); Assert.IsTrue(CheckTrueExpression(isCorrect1, isCorrect2, result1, result2, 6.0)); } - [TestCase] + [Test] public void TestFalseExpression() { string expresion = "2 4 * 2"; IStack stack1 = new StackList(); IStack stack2 = new StackArray(); - bool isCorrect1 = false; - bool isCorrect2 = false; - Calculator.CalculatorExpression(expresion, ref isCorrect1, stack1); - Calculator.CalculatorExpression(expresion, ref isCorrect2, stack2); + (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion, stack1); + (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion, stack2); Assert.IsTrue(CheckFalseExpression(isCorrect1, isCorrect2)); } - [TestCase] + [Test] public void TestDivisionByZero() { string expresion = "2 0 /"; IStack stack1 = new StackList(); IStack stack2 = new StackArray(); - bool isCorrect1 = false; - bool isCorrect2 = false; - Calculator.CalculatorExpression(expresion, ref isCorrect1, stack1); - Calculator.CalculatorExpression(expresion, ref isCorrect2, stack2); + (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion, stack1); + (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion, stack2); Assert.IsTrue(CheckFalseExpression(isCorrect1, isCorrect2)); } } diff --git a/hw2Calculator/hw2Calculator.Test/IStackTest.cs b/hw2Calculator/hw2Calculator.Test/IStackTest.cs index 8c6aef1..2e3480a 100644 --- a/hw2Calculator/hw2Calculator.Test/IStackTest.cs +++ b/hw2Calculator/hw2Calculator.Test/IStackTest.cs @@ -5,7 +5,7 @@ namespace hw2Calculator.Test [TestFixture] class IStackTest { - [TestCase] + [Test] public void IsEmptyAfterPush() { IStack stack1 = new StackList(); @@ -15,17 +15,18 @@ public void IsEmptyAfterPush() Assert.IsTrue(!(stack1.IsEmpty() && stack2.IsEmpty())); } - [TestCase] + [Test] public void PopAfterPush() { IStack stack1 = new StackList(); IStack stack2 = new StackArray(); stack1.Push(9); stack2.Push(8); - Assert.IsTrue((stack1.Pop() == 9 && stack2.Pop() == 8)); + Assert.AreEqual(stack1.Pop(), 9); + Assert.AreEqual(stack2.Pop(), 8); } - [TestCase] + [Test] public void CheckDeleteStack() { IStack stack1 = new StackList(); @@ -39,7 +40,7 @@ public void CheckDeleteStack() Assert.IsTrue((stack1.IsEmpty() && stack2.IsEmpty())); } - [TestCase] + [Test] public void CheckIsEmpty() { IStack stack1 = new StackList(); diff --git a/hw2Calculator/hw2Calculator/Calculator.cs b/hw2Calculator/hw2Calculator/Calculator.cs index fc59809..e925c86 100644 --- a/hw2Calculator/hw2Calculator/Calculator.cs +++ b/hw2Calculator/hw2Calculator/Calculator.cs @@ -6,66 +6,60 @@ namespace hw2Calculator { public static class Calculator { - public static double CalculatorExpression(string expression, ref bool IsCorrect, IStack stack) + public static (double, bool) CalculatorExpression(string expression, IStack stack) { string number = ""; - char[] experssionArray = expression.ToCharArray(0, expression.Length); + char[] expressionArray = expression.ToCharArray(0, expression.Length); for (int i = 0; i < expression.Length; ++i) { - if (experssionArray[i] == ' ') + if (expressionArray[i] == ' ') { number = ""; continue; } - if (Char.IsDigit(experssionArray[i])) + if (Char.IsDigit(expressionArray[i])) { - while (i < expression.Length && experssionArray[i] != ' ') + while (i < expression.Length && expressionArray[i] != ' ') { - number += experssionArray[i]; + number += expressionArray[i]; ++i; } stack.Push(double.Parse(number)); number = ""; continue; } - if (IsOperand(experssionArray[i])) + if (IsOperand(expressionArray[i])) { if (stack.IsEmpty()) { - IsCorrect = false; - return 0; + return (0, false); } double lastNumber = stack.Pop(); - if ( stack.IsEmpty() || (experssionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) + if (stack.IsEmpty() || (expressionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) { stack.DeleteStack(); - IsCorrect = false; - return 0; + return (0, false); } stack.Push(lastNumber); - ArithmeticOperation(experssionArray[i], stack); + ArithmeticOperation(expressionArray[i], stack); } else { stack.DeleteStack(); - IsCorrect = false; - return 0; + return (0, false); } } if (stack.IsEmpty()) { - IsCorrect = false; - return 0; + return (0, false); } var result = stack.Pop(); if (stack.IsEmpty()) { - IsCorrect = true; - return result; + return (result, true); } stack.DeleteStack(); - IsCorrect = false; - return 0; + return (0, false); } private static bool IsOperand(char symbol) @@ -81,16 +75,16 @@ private static void ArithmeticOperation(char operation, IStack stack) { case '+': stack.Push(number1 + number2); - break; + break; case '-': stack.Push(number1 - number2); - break; + break; case '*': stack.Push(number1 * number2); - break; + break; case '/': stack.Push(number1 / number2); - break; + break; } } } diff --git a/hw2Calculator/hw2Calculator/Program.cs b/hw2Calculator/hw2Calculator/Program.cs index 1b6b26c..6a03e5a 100644 --- a/hw2Calculator/hw2Calculator/Program.cs +++ b/hw2Calculator/hw2Calculator/Program.cs @@ -17,23 +17,21 @@ static void Main() return; } IStack stack; - if (choice == 1) + switch (choice) { - stack = new StackList(); - } - else if (choice == 2) - { - stack = new StackArray(); - } - else - { - Console.WriteLine("Ошибка ввода!"); + case 1: + stack = new StackList(); + break; + case 2: + stack = new StackArray(); + break; + default: + Console.WriteLine("Ошибка ввода!"); return; } Console.WriteLine("Введите выражение в постфиксной форме: "); string expression = Console.ReadLine(); - bool isCorrect = false; - var result = Calculator.CalculatorExpression(expression, ref isCorrect, stack); + (var result, var isCorrect) = Calculator.CalculatorExpression(expression, stack); if (!isCorrect) { Console.WriteLine("Ошибка ввода!"); diff --git a/hw2Calculator/hw2Calculator/Test.cs b/hw2Calculator/hw2Calculator/Test.cs index 88266dc..bbaf786 100644 --- a/hw2Calculator/hw2Calculator/Test.cs +++ b/hw2Calculator/hw2Calculator/Test.cs @@ -11,27 +11,23 @@ public static bool TestCalculator() string expresion1 = "2 4 * 2 -"; IStack stack1 = new StackList(); IStack stack2 = new StackArray(); - bool isCorrect1 = false; - bool isCorrect2 = false; - if ((Calculator.CalculatorExpression(expresion1, ref isCorrect1, stack1) != 6 && isCorrect1 != true) || (Calculator.CalculatorExpression(expresion1, ref isCorrect2, stack2) != 6 && isCorrect2 != true)) + (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion1, stack1); + (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion1, stack2); + if ((result1 != 6 && isCorrect1 != true) || (result2 != 6 && isCorrect2 != true)) { return false; } string expresion2 = "2 4 * 2"; - Calculator.CalculatorExpression(expresion2, ref isCorrect1, stack1); - Calculator.CalculatorExpression(expresion2, ref isCorrect2, stack2); + (result1, isCorrect1) = Calculator.CalculatorExpression(expresion2, stack1); + (result2, isCorrect2) = Calculator.CalculatorExpression(expresion2, stack2); if (isCorrect1 != false || isCorrect2 != false) { return false; } string expresion3 = "*"; - Calculator.CalculatorExpression(expresion3, ref isCorrect1, stack1); - Calculator.CalculatorExpression(expresion3, ref isCorrect2, stack2); - if (isCorrect1 != false || isCorrect2 != false) - { - return false; - } - return true; + (result1, isCorrect1) = Calculator.CalculatorExpression(expresion3, stack1); + (result2, isCorrect2) = Calculator.CalculatorExpression(expresion3, stack2); + return isCorrect1 == false && isCorrect2 == false; } } } \ No newline at end of file From 2ce25a65626750c5d4cfcf94ea26ea34c4d39e61 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 18 May 2021 02:38:43 +0300 Subject: [PATCH 09/10] fix mistakes and tests --- .../hw2Calculator.Test/CalculatorTest.cs | 85 ++++++++++++------- .../hw2Calculator.Test/IStackTest.cs | 40 ++++----- .../hw2Calculator.Test.csproj | 2 +- hw2Calculator/hw2Calculator.sln | 4 +- hw2Calculator/hw2Calculator/Calculator.cs | 16 ++-- hw2Calculator/hw2Calculator/IStack.cs | 4 +- hw2Calculator/hw2Calculator/Program.cs | 2 +- hw2Calculator/hw2Calculator/StackArray.cs | 9 +- hw2Calculator/hw2Calculator/StackList.cs | 4 +- hw2Calculator/hw2Calculator/Test.cs | 2 +- 10 files changed, 91 insertions(+), 77 deletions(-) diff --git a/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs index 2456c85..9672981 100644 --- a/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs +++ b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs @@ -1,50 +1,71 @@ using NUnit.Framework; +using System.Collections.Generic; +using System; +using System.Linq; -namespace hw2Calculator.Test +namespace Hw2Calculator.Test { - public class Tests + public class CalculatorTests { - private bool CheckTrueExpression(bool isCorrect1, bool isCorrect2, double result1, double result2, double mainResult) + private bool CheckTrueExpression(bool isCorrect, double result, double mainResult) { - return (!isCorrect1 || !isCorrect2) || (result1 - mainResult < 0.000001 || result2 - mainResult < 0.000001); + return !isCorrect || Math.Abs(result - mainResult) < 0.000001; } - private bool CheckFalseExpression(bool isCorrect1, bool isCorrect2) + private static (string Test, double Result)[] CorrectDataForTests = new[] { - return (!isCorrect1 && !isCorrect2); - } + ("2 4 * 2 -", 6.0), + ("2 2 +", 4.0), + ("16 5 -", 11.0), + ("5 5 *", 25.0), + ("5 -5 *", -25.0), + ("-5 -5 *", 25), + ("25 5 /", 5), + ("25 -5 /", -5), + ("-25 -5 /", 5), + ("-25 -5 /", 5), + ("-25 -5 /", 5), + ("1 2 + 5 2 - *", 9), + ("6 3 - 1 2 * /", 1.5), + }; - [Test] - public void TestTrueExpression() + [TestCaseSource(nameof(CorrectDataForTest))] + public void CalculatorTestForCorrectData(IStack stack, string expresion, double mainResult) { - string expresion = "2 4 * 2 -"; - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); - (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion, stack1); - (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion, stack2); - Assert.IsTrue(CheckTrueExpression(isCorrect1, isCorrect2, result1, result2, 6.0)); + (var result, var isCorrect) = Calculator.CalculatorExpression(expresion, stack); + Assert.IsTrue(CheckTrueExpression(isCorrect, result, mainResult)); } - [Test] - public void TestFalseExpression() + private static IEnumerable CorrectDataForTest + => + CorrectDataForTests.SelectMany(DataForTests => new TestCaseData[] + { + new TestCaseData(new StackList(), DataForTests.Test, DataForTests.Result), + new TestCaseData(new StackArray(), DataForTests.Test, DataForTests.Result) + }); + + private static string[] UncorrectDataForTests = new[] { - string expresion = "2 4 * 2"; - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); - (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion, stack1); - (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion, stack2); - Assert.IsTrue(CheckFalseExpression(isCorrect1, isCorrect2)); - } + ("2 + 2"), + ("2 / 0"), + ("2 +"), + ("2 asd"), + ("2 2 + 2"), + }; - [Test] - public void TestDivisionByZero() + [TestCaseSource(nameof(CorrectDataForTest))] + public void CalculatorTestForUncorrectData(IStack stack, string expresion, double mainResult) { - string expresion = "2 0 /"; - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); - (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion, stack1); - (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion, stack2); - Assert.IsTrue(CheckFalseExpression(isCorrect1, isCorrect2)); + (var result, var isCorrect) = Calculator.CalculatorExpression(expresion, stack); + Assert.IsTrue(CheckTrueExpression(isCorrect, result, mainResult)); } + + private static IEnumerable UncorrectDataForTest + => + UncorrectDataForTests.SelectMany(DataForTests => new TestCaseData[] + { + new TestCaseData(new StackList(), UncorrectDataForTests), + new TestCaseData(new StackArray(), UncorrectDataForTests) + }); } } \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator.Test/IStackTest.cs b/hw2Calculator/hw2Calculator.Test/IStackTest.cs index 2e3480a..dd818a3 100644 --- a/hw2Calculator/hw2Calculator.Test/IStackTest.cs +++ b/hw2Calculator/hw2Calculator.Test/IStackTest.cs @@ -1,52 +1,48 @@ using NUnit.Framework; -namespace hw2Calculator.Test +namespace Hw2Calculator.Test { [TestFixture] - class IStackTest + public class IStackTest { - [Test] - public void IsEmptyAfterPush() + IStack stack1; + IStack stack2; + + [SetUp] + public void Setup() { - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); + stack1 = new StackList(); + stack2 = new StackArray(); stack1.Push(9); stack2.Push(8); + } + + [Test] + public void IsEmptyAfterPush() + { Assert.IsTrue(!(stack1.IsEmpty() && stack2.IsEmpty())); } [Test] public void PopAfterPush() { - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); - stack1.Push(9); - stack2.Push(8); - Assert.AreEqual(stack1.Pop(), 9); - Assert.AreEqual(stack2.Pop(), 8); + Assert.AreEqual(9, stack1.Pop()); + Assert.AreEqual(8, stack2.Pop()); } [Test] public void CheckDeleteStack() { - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); stack1.Push(8); stack1.Push(9); - stack2.Push(8); - stack2.Push(9); - stack1.DeleteStack(); - stack2.DeleteStack(); + stack1.ClearStack(); + stack2.ClearStack(); Assert.IsTrue((stack1.IsEmpty() && stack2.IsEmpty())); } [Test] public void CheckIsEmpty() { - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); - stack1.Push(9); - stack2.Push(8); stack1.Pop(); stack2.Pop(); Assert.IsTrue((stack1.IsEmpty() && stack2.IsEmpty())); diff --git a/hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj b/hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj index d5fa794..57c346a 100644 --- a/hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj +++ b/hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj @@ -13,7 +13,7 @@ - + diff --git a/hw2Calculator/hw2Calculator.sln b/hw2Calculator/hw2Calculator.sln index 7b9155e..b1573bc 100644 --- a/hw2Calculator/hw2Calculator.sln +++ b/hw2Calculator/hw2Calculator.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31005.135 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw2Calculator", "hw2Calculator\hw2Calculator.csproj", "{6910FA32-065A-451E-93C8-D690BA4C486A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw2Calculator", "hw2Calculator\Hw2Calculator.csproj", "{6910FA32-065A-451E-93C8-D690BA4C486A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw2Calculator.Test", "hw2Calculator.Test\hw2Calculator.Test.csproj", "{9AB5D195-767E-4A4A-BCC8-66536BBE511F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw2Calculator.Test", "hw2Calculator.Test\hw2Calculator.Test.csproj", "{9AB5D195-767E-4A4A-BCC8-66536BBE511F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/hw2Calculator/hw2Calculator/Calculator.cs b/hw2Calculator/hw2Calculator/Calculator.cs index 4ff20ba..843ead8 100644 --- a/hw2Calculator/hw2Calculator/Calculator.cs +++ b/hw2Calculator/hw2Calculator/Calculator.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace hw2Calculator +namespace Hw2Calculator { public static class Calculator { @@ -28,7 +28,7 @@ public static (double, bool) CalculatorExpression(string expression, IStack stac number = ""; continue; } - if (IsOperand(expressionArray[i])) + if (IsOperator(expressionArray[i])) { if (stack.IsEmpty()) { @@ -37,7 +37,7 @@ public static (double, bool) CalculatorExpression(string expression, IStack stac double lastNumber = stack.Pop(); if (stack.IsEmpty() || (expressionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) { - stack.DeleteStack(); + stack.ClearStack(); return (0, false); } stack.Push(lastNumber); @@ -45,7 +45,7 @@ public static (double, bool) CalculatorExpression(string expression, IStack stac } else { - stack.DeleteStack(); + stack.ClearStack(); return (0, false); } } @@ -58,14 +58,12 @@ public static (double, bool) CalculatorExpression(string expression, IStack stac { return (result, true); } - stack.DeleteStack(); + stack.ClearStack(); return (0, false); } - private static bool IsOperand(char symbol) - { - return symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'; - } + private static bool IsOperator(char symbol) + => symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'; private static void ArithmeticOperation(char operation, IStack stack) { diff --git a/hw2Calculator/hw2Calculator/IStack.cs b/hw2Calculator/hw2Calculator/IStack.cs index cabb3c5..7ac5915 100644 --- a/hw2Calculator/hw2Calculator/IStack.cs +++ b/hw2Calculator/hw2Calculator/IStack.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace hw2Calculator +namespace Hw2Calculator { public interface IStack { @@ -12,6 +12,6 @@ public interface IStack bool IsEmpty(); - void DeleteStack(); + void ClearStack(); } } \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/Program.cs b/hw2Calculator/hw2Calculator/Program.cs index 6a03e5a..4a3c7ff 100644 --- a/hw2Calculator/hw2Calculator/Program.cs +++ b/hw2Calculator/hw2Calculator/Program.cs @@ -1,6 +1,6 @@ using System; -namespace hw2Calculator +namespace Hw2Calculator { class Program { diff --git a/hw2Calculator/hw2Calculator/StackArray.cs b/hw2Calculator/hw2Calculator/StackArray.cs index 6158f89..1fa7163 100644 --- a/hw2Calculator/hw2Calculator/StackArray.cs +++ b/hw2Calculator/hw2Calculator/StackArray.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace hw2Calculator +namespace Hw2Calculator { public class StackArray : IStack { @@ -36,11 +36,10 @@ public double Pop() } public bool IsEmpty() - { - return countNumbersInStack == 0; - } + => countNumbersInStack == 0; + - public void DeleteStack() + public void ClearStack() { stackElements = new double[5]; countNumbersInStack = 0; diff --git a/hw2Calculator/hw2Calculator/StackList.cs b/hw2Calculator/hw2Calculator/StackList.cs index fbf1ac8..929dfc5 100644 --- a/hw2Calculator/hw2Calculator/StackList.cs +++ b/hw2Calculator/hw2Calculator/StackList.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace hw2Calculator +namespace Hw2Calculator { public class StackList : IStack { @@ -31,7 +31,7 @@ public double Pop() public bool IsEmpty() => head == null; - public void DeleteStack() + public void ClearStack() => head = null; } } \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/Test.cs b/hw2Calculator/hw2Calculator/Test.cs index e789810..50b0389 100644 --- a/hw2Calculator/hw2Calculator/Test.cs +++ b/hw2Calculator/hw2Calculator/Test.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace hw2Calculator +namespace Hw2Calculator { class Test { From 00a16c80f5d79b3e5a8e03f4f9a6af8cb1efb8a1 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Fri, 4 Jun 2021 01:56:29 +0300 Subject: [PATCH 10/10] fix tests --- .../hw2Calculator.Test/CalculatorTest.cs | 32 +++++------ .../hw2Calculator.Test/IStackTest.cs | 57 +++++++++---------- hw2Calculator/hw2Calculator/StackArray.cs | 4 +- hw2Calculator/hw2Calculator/Test.cs | 33 ----------- 4 files changed, 44 insertions(+), 82 deletions(-) delete mode 100644 hw2Calculator/hw2Calculator/Test.cs diff --git a/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs index 9672981..aa328e0 100644 --- a/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs +++ b/hw2Calculator/hw2Calculator.Test/CalculatorTest.cs @@ -12,7 +12,7 @@ private bool CheckTrueExpression(bool isCorrect, double result, double mainResul return !isCorrect || Math.Abs(result - mainResult) < 0.000001; } - private static (string Test, double Result)[] CorrectDataForTests = new[] + private static (string Test, double Result)[] correctDataForTests = new[] { ("2 4 * 2 -", 6.0), ("2 2 +", 4.0), @@ -29,43 +29,43 @@ private static (string Test, double Result)[] CorrectDataForTests = new[] ("6 3 - 1 2 * /", 1.5), }; - [TestCaseSource(nameof(CorrectDataForTest))] + [TestCaseSource(nameof(TestCorrectData))] public void CalculatorTestForCorrectData(IStack stack, string expresion, double mainResult) { (var result, var isCorrect) = Calculator.CalculatorExpression(expresion, stack); Assert.IsTrue(CheckTrueExpression(isCorrect, result, mainResult)); } - private static IEnumerable CorrectDataForTest + private static IEnumerable TestCorrectData => - CorrectDataForTests.SelectMany(DataForTests => new TestCaseData[] + correctDataForTests.SelectMany(DataForTests => new TestCaseData[] { new TestCaseData(new StackList(), DataForTests.Test, DataForTests.Result), new TestCaseData(new StackArray(), DataForTests.Test, DataForTests.Result) }); - private static string[] UncorrectDataForTests = new[] + private static (string Test, double Result)[] incorrectDataForTests = new[] { - ("2 + 2"), - ("2 / 0"), - ("2 +"), - ("2 asd"), - ("2 2 + 2"), + ("2 + 2", 0.0), + ("2 / 0", 0.0), + ("2 +", 0.0), + ("2 asd", 0.0), + ("2 2 + 2", 0.0), }; - [TestCaseSource(nameof(CorrectDataForTest))] - public void CalculatorTestForUncorrectData(IStack stack, string expresion, double mainResult) + [TestCaseSource(nameof(TestIncorrectDate))] + public void CalculatorTestForIncorrectData(IStack stack, string expresion, double mainResult) { (var result, var isCorrect) = Calculator.CalculatorExpression(expresion, stack); Assert.IsTrue(CheckTrueExpression(isCorrect, result, mainResult)); } - private static IEnumerable UncorrectDataForTest + private static IEnumerable TestIncorrectDate => - UncorrectDataForTests.SelectMany(DataForTests => new TestCaseData[] + incorrectDataForTests.SelectMany(DataForTests => new TestCaseData[] { - new TestCaseData(new StackList(), UncorrectDataForTests), - new TestCaseData(new StackArray(), UncorrectDataForTests) + new TestCaseData(new StackList(), DataForTests.Test, DataForTests.Result), + new TestCaseData(new StackArray(), DataForTests.Test, DataForTests.Result) }); } } \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator.Test/IStackTest.cs b/hw2Calculator/hw2Calculator.Test/IStackTest.cs index dd818a3..0330d97 100644 --- a/hw2Calculator/hw2Calculator.Test/IStackTest.cs +++ b/hw2Calculator/hw2Calculator.Test/IStackTest.cs @@ -1,51 +1,48 @@ using NUnit.Framework; +using System.Collections.Generic; namespace Hw2Calculator.Test { [TestFixture] public class IStackTest { - IStack stack1; - IStack stack2; - - [SetUp] - public void Setup() + [TestCaseSource(nameof(Stacks))] + public void IsEmptyAfterPush(IStack stack) { - stack1 = new StackList(); - stack2 = new StackArray(); - stack1.Push(9); - stack2.Push(8); + stack.Push(9); + Assert.IsTrue(!(stack.IsEmpty())); } - [Test] - public void IsEmptyAfterPush() + [TestCaseSource(nameof(Stacks))] + public void PopAfterPush(IStack stack) { - Assert.IsTrue(!(stack1.IsEmpty() && stack2.IsEmpty())); + stack.Push(9); + Assert.AreEqual(9, stack.Pop()); } - [Test] - public void PopAfterPush() + [TestCaseSource(nameof(Stacks))] + public void CheckDeleteStack(IStack stack) { - Assert.AreEqual(9, stack1.Pop()); - Assert.AreEqual(8, stack2.Pop()); + stack.Push(9); + stack.Push(8); + stack.Push(9); + stack.ClearStack(); + Assert.IsTrue((stack.IsEmpty())); } - [Test] - public void CheckDeleteStack() + [TestCaseSource(nameof(Stacks))] + public void CheckIsEmpty(IStack stack) { - stack1.Push(8); - stack1.Push(9); - stack1.ClearStack(); - stack2.ClearStack(); - Assert.IsTrue((stack1.IsEmpty() && stack2.IsEmpty())); + stack.Push(9); + stack.Pop(); + Assert.IsTrue((stack.IsEmpty())); } - [Test] - public void CheckIsEmpty() - { - stack1.Pop(); - stack2.Pop(); - Assert.IsTrue((stack1.IsEmpty() && stack2.IsEmpty())); - } + private static IEnumerable Stacks + => new TestCaseData[] + { + new TestCaseData(new StackList()), + new TestCaseData(new StackArray()), + }; } } \ No newline at end of file diff --git a/hw2Calculator/hw2Calculator/StackArray.cs b/hw2Calculator/hw2Calculator/StackArray.cs index 1fa7163..2578a1d 100644 --- a/hw2Calculator/hw2Calculator/StackArray.cs +++ b/hw2Calculator/hw2Calculator/StackArray.cs @@ -31,14 +31,12 @@ public double Pop() throw new InvalidOperationException(); } countNumbersInStack--; - double delete = stackElements[countNumbersInStack]; - return delete; + return stackElements[countNumbersInStack]; } public bool IsEmpty() => countNumbersInStack == 0; - public void ClearStack() { stackElements = new double[5]; diff --git a/hw2Calculator/hw2Calculator/Test.cs b/hw2Calculator/hw2Calculator/Test.cs deleted file mode 100644 index 50b0389..0000000 --- a/hw2Calculator/hw2Calculator/Test.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Hw2Calculator -{ - class Test - { - public static bool TestCalculator() - { - string expresion1 = "2 4 * 2 -"; - IStack stack1 = new StackList(); - IStack stack2 = new StackArray(); - (var result1, var isCorrect1) = Calculator.CalculatorExpression(expresion1, stack1); - (var result2, var isCorrect2) = Calculator.CalculatorExpression(expresion1, stack2); - if ((result1 != 6 && isCorrect1 != true) || (result2 != 6 && isCorrect2 != true)) - { - return false; - } - string expresion2 = "2 4 * 2"; - (result1, isCorrect1) = Calculator.CalculatorExpression(expresion2, stack1); - (result2, isCorrect2) = Calculator.CalculatorExpression(expresion2, stack2); - if (isCorrect1 != false || isCorrect2 != false) - { - return false; - } - string expresion3 = "*"; - (result1, isCorrect1) = Calculator.CalculatorExpression(expresion3, stack1); - (result2, isCorrect2) = Calculator.CalculatorExpression(expresion3, stack2); - return isCorrect1 == false && isCorrect2 == false; - } - } -}