-
Notifications
You must be signed in to change notification settings - Fork 0
Stack calculator #4
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
base: main
Are you sure you want to change the base?
Conversation
3125d75 to
8c940cf
Compare
| using System; | ||
| using Stack; | ||
|
|
||
| namespace StackCalculator; |
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.
namespace лучше в самом начале писать
| /// </summary> | ||
| public class Calculator | ||
| { | ||
| private IStack<float> Stack = new StackOnLists<float>(); |
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.
По условию, в коде класса «Стековый калькулятор» не должно быть ни одного упоминания конкретных реализаций стека, даже если очень хочется.
| /// <returns>Expression value</returns> | ||
| public float CountTheExpressionInPostfixForm(string[] inputString) | ||
| { | ||
| int number = 0; |
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.
Слишком рано объявлен. Надо его объявить в месте инициализации: if (int.TryParse(inputString[i], out int number))
Stack/Stack/IStack.cs
Outdated
| /// <summary> | ||
| /// A class representing the stack interface | ||
| /// </summary> | ||
| abstract public class IStack<T> |
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.
Это должен был быть интерфейс, а не абстрактный класс
Stack/Stack/IStack.cs
Outdated
| /// Function that returns the number of elements in the stack | ||
| /// </summary> | ||
| /// <returns>Number of elements in stack</returns> | ||
| abstract public int ReturnNumberOfElements(); |
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.
Идеологически правильнее NumberOfElements();, "Return" тут несколько избыточно.
Stack/Stack/StackOnLists.cs
Outdated
| numberOfElements--; | ||
| return value; | ||
| } | ||
| return default(T); |
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.
| return default(T); | |
| return default; |
Stack/Stack/StackOnLists.cs
Outdated
| if (head == null) | ||
| { | ||
| throw new StackException("Stack is empty"); | ||
| } | ||
| return head == null ? default(T) : head.Value; |
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.
Ну...
Stack/StackTest/StackTest.cs
Outdated
| private static IEnumerable<TestCaseData> Stacks | ||
| => new TestCaseData[] | ||
| { | ||
| new TestCaseData(new StackOnArray<int>()), | ||
| new TestCaseData(new StackOnLists<int>()), | ||
| }; |
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.
| private static IEnumerable<TestCaseData> Stacks | |
| => new TestCaseData[] | |
| { | |
| new TestCaseData(new StackOnArray<int>()), | |
| new TestCaseData(new StackOnLists<int>()), | |
| }; | |
| private static IEnumerable<TestCaseData> Stacks | |
| => new TestCaseData[] | |
| { | |
| new TestCaseData(new StackOnArray<int>()), | |
| new TestCaseData(new StackOnLists<int>()), | |
| }; |
Stack/StackTest/StackTest.cs
Outdated
| public void CheckTopOfTheStackAfterPush(IStack<int> stack) | ||
| { | ||
| stack?.Push(1); | ||
| Assert.AreEqual(stack?.ReturnTopOfTheStack(), 1); |
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.
Наоборот, сначала Expected, затем Actual. Иначе если тест не пройдёт, сообщение об ошибке будет лживым.
Stack/StackTest/StackTest.cs
Outdated
| [TestCaseSource(nameof(Stacks))] | ||
| public void CheckTopOfEmptyStack(IStack<int> stack) | ||
| { | ||
| var exception = Assert.Throws<StackException>(() => stack?.ReturnTopOfTheStack()); |
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.
"?" тут не нужен, стек гарантированно не null
--Each class in a separate file --Changed tests --The stack calculator class knows nothing about the stack implementation
yurii-litvinov
left a comment
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.
Неаккуратно, но по существу всё ок, зачтена.
| } | ||
|
|
||
|
|
||
| [TestCaseSource(nameof(Stacks))] |
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.
| } | |
| [TestCaseSource(nameof(Stacks))] | |
| } | |
| [TestCaseSource(nameof(Stacks))] |
| /// <summary> | ||
| /// A class for testing a stack calculator | ||
| /// </summary> | ||
| public class TestsStackCalculator |
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.
| public class TestsStackCalculator | |
| public class StackCalculatorTests |
Так каноничнее
| public void ShouldThrowsDivideByZeroExceptionWhenDivideByZero(IStack<float> stack) | ||
| { | ||
| string[] firstArray = { "123", "0", "/" }; | ||
| Assert.Throws<System.DivideByZeroException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); |
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.
Вообще, калькулятор должен принимать строку, а не массив, но ладно :)
| public void ShouldThrowsInvalidCharacterExceptionWhenExpressionContainsInvalidCharacter(IStack<float> stack) | ||
| { | ||
| string[] firstArray = { "123", "0", "a" }; | ||
| Assert.Throws<InvalidCharacterException> (() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); |
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.
| Assert.Throws<InvalidCharacterException> (() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); | |
| Assert.Throws<InvalidCharacterException>(() => stackCalculator?.CountTheExpressionInPostfixForm(firstArray, stack)); |
| } | ||
|
|
||
|
|
||
| [TestCaseSource(nameof(Stacks))] |
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.
| } | |
| [TestCaseSource(nameof(Stacks))] | |
| } | |
| [TestCaseSource(nameof(Stacks))] |
yurii-litvinov
left a comment
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.
Сам калькулятор со стеками ещё всё-таки надо немного поправить
| /// Function for counting expressions in postfix form | ||
| /// </summary> | ||
| /// <returns>Expression value</returns> | ||
| public float CountTheExpressionInPostfixForm(string[] inputString, IStack<float> stack) |
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.
Нет-нет, хочется, чтобы калькулятор принимал строку, а не массив строк
| catch (StackIsEmptyException) | ||
| { | ||
| throw; | ||
| } |
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.
Если вы думаете, что ваша жизнь бессмысленна, посмотрите на эти строки кода :)
| /// </summary> | ||
| public class IncorrectExpressionException : Exception | ||
| { | ||
| public IncorrectExpressionException() : base() { } |
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.
Это тоже нечто ненужное. Если в классе не объявлен ни один конструктор, компилятор сам генерирует как раз это.
| { | ||
| if (values != null && numberOfElements == values.Length) | ||
| { | ||
| Array.Resize(ref values, values.Length + 20); |
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.
Лучше увеличивать сразу вдвое. Иначе как у Вас операция Push работает в среднем за линию, а если стек растёт вдвое, то в среднем за константу.
| } | ||
|
|
||
| numberOfElements++; | ||
| if (values == null) |
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.
Кажется, nullability-анализ гарантирует, что values не null, так что это довольно бессмысленная проверка
| } | ||
|
|
||
| T topOfSTack = values[numberOfElements - 1]; | ||
| Array.Clear(values, numberOfElements - 1, 1); |
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.
Это хитрый способ записать values[numberOfElements - 1] = default;
No description provided.