-
Notifications
You must be signed in to change notification settings - Fork 0
Hw2 calculator #3
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
Open
MikePuzanov
wants to merge
11
commits into
master
Choose a base branch
from
hw2Calculator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
28eab77
Interface and calculator function
lykwer 530b51a
add test and calculator function
lykwer 7763177
fix defects
lykwer b896f7b
change tests
lykwer d37a657
appveyor.yml
lykwer b70cd04
fix test
MikePuzanov e8cc8d7
Update hw2Calculator/hw2Calculator/Calculator.cs
MikePuzanov 123ffef
fix mistakes
e777cb0
fix mistakes
2ce25a6
fix mistakes and tests
00a16c8
fix tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| image: Visual Studio 2019 | ||
|
|
||
| build_script: | ||
| - For /R %%I in (*.sln) do dotnet test %%I | ||
|
|
||
| test: off |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| using NUnit.Framework; | ||
| using System.Collections.Generic; | ||
| using System; | ||
| using System.Linq; | ||
|
|
||
| namespace Hw2Calculator.Test | ||
| { | ||
| public class CalculatorTests | ||
| { | ||
| private bool CheckTrueExpression(bool isCorrect, double result, double mainResult) | ||
| { | ||
| return !isCorrect || Math.Abs(result - mainResult) < 0.000001; | ||
| } | ||
|
|
||
| private static (string Test, double Result)[] correctDataForTests = new[] | ||
| { | ||
| ("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), | ||
| }; | ||
|
|
||
| [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<TestCaseData> TestCorrectData | ||
| => | ||
| correctDataForTests.SelectMany(DataForTests => new TestCaseData[] | ||
| { | ||
| new TestCaseData(new StackList(), DataForTests.Test, DataForTests.Result), | ||
| new TestCaseData(new StackArray(), DataForTests.Test, DataForTests.Result) | ||
| }); | ||
|
|
||
| private static (string Test, double Result)[] incorrectDataForTests = new[] | ||
| { | ||
| ("2 + 2", 0.0), | ||
| ("2 / 0", 0.0), | ||
| ("2 +", 0.0), | ||
| ("2 asd", 0.0), | ||
| ("2 2 + 2", 0.0), | ||
| }; | ||
|
|
||
| [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<TestCaseData> TestIncorrectDate | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Date --- в смысле "свидание"? |
||
| => | ||
| incorrectDataForTests.SelectMany(DataForTests => new TestCaseData[] | ||
| { | ||
| new TestCaseData(new StackList(), DataForTests.Test, DataForTests.Result), | ||
| new TestCaseData(new StackArray(), DataForTests.Test, DataForTests.Result) | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using NUnit.Framework; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Hw2Calculator.Test | ||
| { | ||
| [TestFixture] | ||
| public class IStackTest | ||
| { | ||
| [TestCaseSource(nameof(Stacks))] | ||
| public void IsEmptyAfterPush(IStack stack) | ||
| { | ||
| stack.Push(9); | ||
| Assert.IsTrue(!(stack.IsEmpty())); | ||
| } | ||
|
|
||
| [TestCaseSource(nameof(Stacks))] | ||
| public void PopAfterPush(IStack stack) | ||
| { | ||
| stack.Push(9); | ||
| Assert.AreEqual(9, stack.Pop()); | ||
| } | ||
|
|
||
| [TestCaseSource(nameof(Stacks))] | ||
| public void CheckDeleteStack(IStack stack) | ||
| { | ||
| stack.Push(9); | ||
| stack.Push(8); | ||
| stack.Push(9); | ||
| stack.ClearStack(); | ||
| Assert.IsTrue((stack.IsEmpty())); | ||
| } | ||
|
|
||
| [TestCaseSource(nameof(Stacks))] | ||
| public void CheckIsEmpty(IStack stack) | ||
| { | ||
| stack.Push(9); | ||
| stack.Pop(); | ||
| Assert.IsTrue((stack.IsEmpty())); | ||
| } | ||
|
|
||
| private static IEnumerable<TestCaseData> Stacks | ||
| => new TestCaseData[] | ||
| { | ||
| new TestCaseData(new StackList()), | ||
| new TestCaseData(new StackArray()), | ||
| }; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
hw2Calculator/hw2Calculator.Test/hw2Calculator.Test.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
|
||
| <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="..\hw2Calculator\Hw2Calculator.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw2Calculator", "hw2Calculator\Hw2Calculator.csproj", "{6910FA32-065A-451E-93C8-D690BA4C486A}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw2Calculator.Test", "hw2Calculator.Test\hw2Calculator.Test.csproj", "{9AB5D195-767E-4A4A-BCC8-66536BBE511F}" | ||
| 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 | ||
| {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 | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {851B8E89-996A-4A60-A0E1-8BC3BA05EEFB} | ||
| EndGlobalSection | ||
| EndGlobal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw2Calculator | ||
| { | ||
| public static class Calculator | ||
| { | ||
| public static (double, bool) CalculatorExpression(string expression, IStack stack) | ||
| { | ||
| string number = ""; | ||
| char[] expressionArray = expression.ToCharArray(0, expression.Length); | ||
| for (int i = 0; i < expression.Length; ++i) | ||
| { | ||
| if (expressionArray[i] == ' ') | ||
| { | ||
| number = ""; | ||
| continue; | ||
| } | ||
| if (Char.IsDigit(expressionArray[i])) | ||
| { | ||
| while (i < expression.Length && expressionArray[i] != ' ') | ||
| { | ||
| number += expressionArray[i]; | ||
| ++i; | ||
| } | ||
| stack.Push(double.Parse(number)); | ||
| number = ""; | ||
| continue; | ||
| } | ||
| if (IsOperator(expressionArray[i])) | ||
| { | ||
| if (stack.IsEmpty()) | ||
| { | ||
| return (0, false); | ||
| } | ||
| double lastNumber = stack.Pop(); | ||
| if (stack.IsEmpty() || (expressionArray[i] == '/' && Math.Abs(lastNumber - 0) < 0.00001)) | ||
| { | ||
| stack.ClearStack(); | ||
| return (0, false); | ||
| } | ||
| stack.Push(lastNumber); | ||
| ArithmeticOperation(expressionArray[i], stack); | ||
| } | ||
| else | ||
| { | ||
| stack.ClearStack(); | ||
| return (0, false); | ||
| } | ||
| } | ||
| if (stack.IsEmpty()) | ||
| { | ||
| return (0, false); | ||
| } | ||
| var result = stack.Pop(); | ||
| if (stack.IsEmpty()) | ||
| { | ||
| return (result, true); | ||
| } | ||
| stack.ClearStack(); | ||
| return (0, false); | ||
| } | ||
|
|
||
| private static bool IsOperator(char symbol) | ||
| => symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'; | ||
|
|
||
| private static void ArithmeticOperation(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; | ||
| case '/': | ||
| stack.Push(number1 / number2); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw2Calculator | ||
| { | ||
| public interface IStack | ||
| { | ||
| void Push(double value); | ||
|
|
||
| double Pop(); | ||
|
|
||
| bool IsEmpty(); | ||
|
|
||
| void ClearStack(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using System; | ||
|
|
||
| namespace Hw2Calculator | ||
| { | ||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| 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; | ||
| switch (choice) | ||
| { | ||
| case 1: | ||
| stack = new StackList(); | ||
| break; | ||
| case 2: | ||
| stack = new StackArray(); | ||
| break; | ||
| default: | ||
| Console.WriteLine("Ошибка ввода!"); | ||
| return; | ||
| } | ||
| Console.WriteLine("Введите выражение в постфиксной форме: "); | ||
| string expression = Console.ReadLine(); | ||
| (var result, var isCorrect) = Calculator.CalculatorExpression(expression, stack); | ||
| if (!isCorrect) | ||
| { | ||
| Console.WriteLine("Ошибка ввода!"); | ||
| return; | ||
| } | ||
| Console.WriteLine("Ответ: " + result); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace Hw2Calculator | ||
| { | ||
| public class StackArray : IStack | ||
| { | ||
| private double[] stackElements; | ||
| private int countNumbersInStack; | ||
|
|
||
| public StackArray() | ||
| { | ||
| stackElements = new double[5]; | ||
| } | ||
|
|
||
| public void Push(double value) | ||
| { | ||
| if (countNumbersInStack == stackElements.Length) | ||
| { | ||
| Array.Resize(ref stackElements, stackElements.Length * 2); | ||
| } | ||
| stackElements[countNumbersInStack] = value; | ||
| countNumbersInStack++; | ||
| } | ||
|
|
||
| public double Pop() | ||
| { | ||
| if (IsEmpty()) | ||
| { | ||
| throw new InvalidOperationException(); | ||
| } | ||
| countNumbersInStack--; | ||
| return stackElements[countNumbersInStack]; | ||
| } | ||
|
|
||
| public bool IsEmpty() | ||
| => countNumbersInStack == 0; | ||
|
|
||
| public void ClearStack() | ||
| { | ||
| stackElements = new double[5]; | ||
| countNumbersInStack = 0; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут лучше через =>