-
Notifications
You must be signed in to change notification settings - Fork 0
Hw7 calculator #13
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: master
Are you sure you want to change the base?
Hw7 calculator #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using NUnit.Framework; | ||
| using System; | ||
|
|
||
| namespace Hw7CalculatorWinForms.Test | ||
| { | ||
| public class Tests | ||
| { | ||
| [Test] | ||
| public void AdditionTest() | ||
| { | ||
| double number = 2; | ||
| CalculatorTools.Calculate(ref number, 5, "+"); | ||
| Assert.AreEqual(number, 7); | ||
| number = 2; | ||
| CalculatorTools.Calculate(ref number, -5, "+"); | ||
| Assert.AreEqual(number, -3); | ||
| } | ||
|
|
||
| [Test] | ||
| public void SubtractionTest() | ||
| { | ||
| double number = 2; | ||
| CalculatorTools.Calculate(ref number, 5, "-"); | ||
| Assert.AreEqual(number, -3); | ||
| number = 2; | ||
| CalculatorTools.Calculate(ref number, -5, "-"); | ||
| Assert.AreEqual(number, 7); | ||
| } | ||
|
|
||
| [Test] | ||
| public void MultiplicationTest() | ||
| { | ||
| var number = 2.0; | ||
| CalculatorTools.Calculate(ref number, 5, "*"); | ||
| Assert.AreEqual(number, 10); | ||
| number = 2; | ||
| CalculatorTools.Calculate(ref number, -5, "*"); | ||
| Assert.AreEqual(number, -10); | ||
| var number1 = -2.0; | ||
| CalculatorTools.Calculate(ref number1, -5, "*"); | ||
| Assert.AreEqual(number1, 10); | ||
| } | ||
|
|
||
| [Test] | ||
| public void DivideTest() | ||
| { | ||
| var number = 10.0; | ||
| CalculatorTools.Calculate(ref number, 5, "/"); | ||
| Assert.AreEqual(number, 2); | ||
| number = 10; | ||
| CalculatorTools.Calculate(ref number, -5, "/"); | ||
| Assert.AreEqual(number, -2); | ||
| } | ||
|
|
||
| [Test] | ||
| public void DivideExceptionTest() | ||
| { | ||
| var number = 2.0; | ||
| Assert.Throws<DivideByZeroException> (() => CalculatorTools.Calculate(ref number, 0, "/")); | ||
| } | ||
| } | ||
| } |
| 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="..\hw7CalculatorWinForms\Hw7CalculatorWinForms.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| 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.31205.134 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw7CalculatorWinForms", "hw7CalculatorWinForms\Hw7CalculatorWinForms.csproj", "{64316CB1-E781-4469-90CA-9C1F5CDE923C}" | ||
| EndProject | ||
| Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw7CalculatorWinForms.Test", "hw7CalculatorWinForms.Test\hw7CalculatorWinForms.Test.csproj", "{E823189C-A44F-4668-BCA8-B042FB3BD6D5}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {64316CB1-E781-4469-90CA-9C1F5CDE923C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {64316CB1-E781-4469-90CA-9C1F5CDE923C}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {64316CB1-E781-4469-90CA-9C1F5CDE923C}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {64316CB1-E781-4469-90CA-9C1F5CDE923C}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| {E823189C-A44F-4668-BCA8-B042FB3BD6D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {E823189C-A44F-4668-BCA8-B042FB3BD6D5}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {E823189C-A44F-4668-BCA8-B042FB3BD6D5}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {E823189C-A44F-4668-BCA8-B042FB3BD6D5}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {501A679A-B9EC-4660-AD72-6ADD85C065A7} | ||
| EndGlobalSection | ||
| EndGlobal |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Data; | ||
| using System.Drawing; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using System.Windows.Forms; | ||
|
|
||
| namespace Hw7CalculatorWinForms | ||
| { | ||
| /// <summary> | ||
| /// класс с интерфейс калькулятора | ||
| /// </summary> | ||
| public partial class CalculatorForms : Form | ||
| { | ||
| private string numberFirst = ""; | ||
|
|
||
| private string numberSecond = ""; | ||
|
|
||
| private int countNumber = 0; | ||
|
|
||
| private string sign = ""; | ||
|
|
||
| private bool isMinus = false; | ||
|
|
||
| public CalculatorForms() | ||
| => InitializeComponent(); | ||
|
|
||
| private void button1_MouseClick(object sender, MouseEventArgs e) | ||
| { | ||
| if (sign != "") | ||
| { | ||
| countNumber = 1; | ||
| } | ||
| if (countNumber == 0) | ||
| { | ||
| numberFirst += (sender as Button).Text; | ||
| buttonText.Text = numberFirst; | ||
| } | ||
| else | ||
| { | ||
| numberSecond += (sender as Button).Text; | ||
| buttonText.Text = numberSecond; | ||
| } | ||
| } | ||
|
|
||
| private void buttonPlusMinus_MouseClick(object sender, MouseEventArgs e) | ||
| { | ||
| if (!isMinus) | ||
| { | ||
| if (countNumber == 0) | ||
| { | ||
| numberFirst = "-" + numberFirst; | ||
| buttonText.Text = numberFirst; | ||
| } | ||
| else | ||
| { | ||
| numberSecond = "-" + numberSecond; | ||
| buttonText.Text = numberSecond; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (countNumber == 0) | ||
| { | ||
| numberFirst = numberFirst.Substring(1); | ||
| buttonText.Text = numberFirst; | ||
| } | ||
| else | ||
| { | ||
| numberSecond = numberSecond.Substring(1); | ||
| buttonText.Text = numberSecond; | ||
| } | ||
| } | ||
| isMinus = !isMinus; | ||
|
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. Как это... ненадёжно. Очень легко забыть isMinus сбросить, и оно будет откусывать первую цифру обычного числа. Попробуйте, например, ввести 1, нажать +/-, нажать C, нажать 2 и снова +/-. А ещё так можно получить креш, подумайте как :) |
||
| } | ||
|
|
||
| private void buttonSign_MouseClick(object sender, MouseEventArgs e) | ||
| { | ||
| sign = (sender as Button).Text; | ||
| if (numberFirst != "" && numberSecond != "") | ||
| { | ||
| double number1 = double.Parse(numberFirst); | ||
| double number2 = double.Parse(numberSecond); | ||
| CalculatorTools.Calculate(ref number1, number2, sign); | ||
| numberFirst = Convert.ToString(number1); | ||
| numberSecond = ""; | ||
| sign = ""; | ||
| countNumber = 1; | ||
| buttonText.Text = numberFirst; | ||
| } | ||
| } | ||
|
|
||
| private void buttonEqual_MouseClick(object sender, MouseEventArgs e) | ||
| { | ||
| double number1 = double.Parse(numberFirst); | ||
| double number2 = double.Parse(numberSecond); | ||
| try | ||
| { | ||
| CalculatorTools.Calculate(ref number1, number2, sign); | ||
| } | ||
| catch (DivideByZeroException) | ||
| { | ||
| numberFirst =""; | ||
| numberSecond = ""; | ||
| sign = ""; | ||
| buttonText.Text = "Error"; | ||
| countNumber = 0; | ||
| return; | ||
| } | ||
| numberFirst = Convert.ToString(number1); | ||
| numberSecond = ""; | ||
| sign = ""; | ||
| countNumber = 1; | ||
| buttonText.Text = numberFirst; | ||
| } | ||
|
|
||
| private void buttonDelete_MouseClick(object sender, MouseEventArgs e) | ||
| { | ||
| if (sign == "") | ||
| { | ||
| if (numberFirst.Length == 0) | ||
| { | ||
| return; | ||
| } | ||
| numberFirst = numberFirst.Substring(0, numberFirst.Length - 1); | ||
| buttonText.Text = numberFirst; | ||
| if (numberFirst == "") | ||
| { | ||
| isMinus = false; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (numberSecond.Length == 0) | ||
| { | ||
| return; | ||
| } | ||
| numberSecond = numberSecond.Substring(0, numberSecond.Length - 1); | ||
| buttonText.Text = numberSecond; | ||
| if (numberSecond == "") | ||
| { | ||
| isMinus = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void buttonResetAll_MouseClick(object sender, MouseEventArgs e) | ||
| { | ||
| numberFirst = ""; | ||
| numberSecond = ""; | ||
| sign = ""; | ||
| countNumber = 0; | ||
| isMinus = false; | ||
| buttonText.Text = "0"; | ||
| } | ||
|
|
||
| private void buttonResetString_MouseClick(object sender, MouseEventArgs e) | ||
| { | ||
| if (sign == "") | ||
| { | ||
| numberFirst = numberSecond = sign = ""; | ||
| countNumber = 0; | ||
| buttonText.Text = "0"; | ||
| isMinus = false; | ||
| } | ||
| else | ||
| { | ||
| isMinus = false; | ||
| numberSecond = ""; | ||
| buttonText.Text = numberFirst; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <root> | ||
| <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> | ||
| <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> | ||
| <xsd:element name="root" msdata:IsDataSet="true"> | ||
| <xsd:complexType> | ||
| <xsd:choice maxOccurs="unbounded"> | ||
| <xsd:element name="metadata"> | ||
| <xsd:complexType> | ||
| <xsd:sequence> | ||
| <xsd:element name="value" type="xsd:string" minOccurs="0" /> | ||
| </xsd:sequence> | ||
| <xsd:attribute name="name" use="required" type="xsd:string" /> | ||
| <xsd:attribute name="type" type="xsd:string" /> | ||
| <xsd:attribute name="mimetype" type="xsd:string" /> | ||
| <xsd:attribute ref="xml:space" /> | ||
| </xsd:complexType> | ||
| </xsd:element> | ||
| <xsd:element name="assembly"> | ||
| <xsd:complexType> | ||
| <xsd:attribute name="alias" type="xsd:string" /> | ||
| <xsd:attribute name="name" type="xsd:string" /> | ||
| </xsd:complexType> | ||
| </xsd:element> | ||
| <xsd:element name="data"> | ||
| <xsd:complexType> | ||
| <xsd:sequence> | ||
| <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | ||
| <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> | ||
| </xsd:sequence> | ||
| <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> | ||
| <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> | ||
| <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> | ||
| <xsd:attribute ref="xml:space" /> | ||
| </xsd:complexType> | ||
| </xsd:element> | ||
| <xsd:element name="resheader"> | ||
| <xsd:complexType> | ||
| <xsd:sequence> | ||
| <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> | ||
| </xsd:sequence> | ||
| <xsd:attribute name="name" type="xsd:string" use="required" /> | ||
| </xsd:complexType> | ||
| </xsd:element> | ||
| </xsd:choice> | ||
| </xsd:complexType> | ||
| </xsd:element> | ||
| </xsd:schema> | ||
| <resheader name="resmimetype"> | ||
| <value>text/microsoft-resx</value> | ||
| </resheader> | ||
| <resheader name="version"> | ||
| <value>2.0</value> | ||
| </resheader> | ||
| <resheader name="reader"> | ||
| <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
| </resheader> | ||
| <resheader name="writer"> | ||
| <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> | ||
| </resheader> | ||
| </root> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Hw7CalculatorWinForms | ||
| { | ||
| /// <summary> | ||
| /// класс для подсчета значений | ||
| /// </summary> | ||
| static public class CalculatorTools | ||
| { | ||
| /// <summary> | ||
| /// Считает значение двух чисел по знаку | ||
| /// </summary> | ||
| /// <param name="number1">первое число</param> | ||
| /// <param name="number2">второе число</param> | ||
| /// <param name="sign">арифмитический знак</param> | ||
| static public void Calculate(ref double number1, double number2, string sign) | ||
| { | ||
| switch (sign) | ||
| { | ||
| case "+": | ||
| number1 = number1 + number2; | ||
| break; | ||
| case "-": | ||
| number1 = number1 - number2; | ||
| break; | ||
| case "*": | ||
| number1 = number1 * number2; | ||
| break; | ||
| case "/": | ||
| if (number2 == 0) | ||
| { | ||
| throw new DivideByZeroException(); | ||
| } | ||
| number1 = number1 / number2; | ||
| break; | ||
| } | ||
| } | ||
|
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. Негусто. Как раз сюда можно было бы вынести управление состоянием из формы с калькулятором, которая должна быть максимально простой |
||
| } | ||
| } | ||
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.
Нужны комментарии