From e3f98b57c264c1fca2441aba879da7abbee9c85f Mon Sep 17 00:00:00 2001 From: MinyazevR Date: Fri, 22 Apr 2022 18:51:51 +0300 Subject: [PATCH 1/4] Creating a calculator class, user interface,and writing test for the calculator class --- Calculator/Calculator.sln | 31 ++ Calculator/Calculator/Calculator.cs | 209 +++++++++ Calculator/Calculator/Calculator.csproj | 11 + Calculator/Calculator/Form1.Designer.cs | 420 ++++++++++++++++++ Calculator/Calculator/Form1.cs | 90 ++++ Calculator/Calculator/Form1.resx | 60 +++ Calculator/Calculator/Program.cs | 15 + Calculator/TestCalculator/TestCalculator.cs | 62 +++ .../TestCalculator/TestCalculator.csproj | 21 + 9 files changed, 919 insertions(+) create mode 100644 Calculator/Calculator.sln create mode 100644 Calculator/Calculator/Calculator.cs create mode 100644 Calculator/Calculator/Calculator.csproj create mode 100644 Calculator/Calculator/Form1.Designer.cs create mode 100644 Calculator/Calculator/Form1.cs create mode 100644 Calculator/Calculator/Form1.resx create mode 100644 Calculator/Calculator/Program.cs create mode 100644 Calculator/TestCalculator/TestCalculator.cs create mode 100644 Calculator/TestCalculator/TestCalculator.csproj diff --git a/Calculator/Calculator.sln b/Calculator/Calculator.sln new file mode 100644 index 0000000..d974577 --- /dev/null +++ b/Calculator/Calculator.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32328.378 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator", "Calculator\Calculator.csproj", "{AA661CFD-17A7-407A-A279-8E003F5DB812}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCalculator", "TestCalculator\TestCalculator.csproj", "{D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AA661CFD-17A7-407A-A279-8E003F5DB812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AA661CFD-17A7-407A-A279-8E003F5DB812}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AA661CFD-17A7-407A-A279-8E003F5DB812}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AA661CFD-17A7-407A-A279-8E003F5DB812}.Release|Any CPU.Build.0 = Release|Any CPU + {D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D49CDDEA-D0AD-45D2-BF98-4B3AAB027C59}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6B5AF291-04A2-4889-A9EF-EA812796302C} + EndGlobalSection +EndGlobal diff --git a/Calculator/Calculator/Calculator.cs b/Calculator/Calculator/Calculator.cs new file mode 100644 index 0000000..1343a0c --- /dev/null +++ b/Calculator/Calculator/Calculator.cs @@ -0,0 +1,209 @@ +namespace Calculator; + +public class Calculator +{ + private string? FirstOperand { get; set; } + + private string? SecondOperand { get; set; } + + private string? Operator { get; set; } + + private string? SecondOperator { get; set; } + + public string? Text { get; private set; } + + public static bool IsOperator(string text) => text == "/" || text == "*" || text == "-" || text == "+" || text == "±" || text == "=" || text == "√" || text == "C" || text == "⌫"; + + public string? CalculateExpression(string[] expression) + { + for (int i = 0; i < expression.Length; i++) + { + if(!AddOperatorOrOperand(expression[i])) + { + GetValue(); + } + } + return FirstOperand; + } + + public string? GetValue() + { + if (FirstOperand == null || SecondOperand == null) + { + return FirstOperand; + } + + float firstNumber = float.Parse(FirstOperand); + float secondNumber = float.Parse(SecondOperand); + switch (Operator) + { + case "+": + { + FirstOperand = (firstNumber + secondNumber).ToString(); + break; + } + case "-": + { + FirstOperand = (firstNumber - secondNumber).ToString(); + break; + } + case "*": + { + FirstOperand = (firstNumber * secondNumber).ToString(); + break; + } + case "/": + { + if (Math.Abs(secondNumber - 0) < 0.000000000000000000000000000001) + { + FirstOperand = SecondOperand = Operator = SecondOperand = null; + } + break; + } + default: + { + return null; + } + } + + SecondOperand = null; + Text = $"{FirstOperand} {SecondOperator} "; + Operator = SecondOperator; + return FirstOperand; + } + + public bool AddOperatorOrOperand(string text) + { + if (text == "") + { + return true; + } + + if (!IsOperator(text)) + { + if (text == ",") + { + if (Text!= null && Text.Contains(',') || FirstOperand == null) + { + return true; + } + } + if (Operator == null) + { + FirstOperand += text; + } + else + { + SecondOperand += text; + } + + Text += text; + return true; + } + + if (FirstOperand == null) + { + return true; + } + + switch (text) + { + case "±": + { + Text = GetValue(); + if (FirstOperand != null) + { + FirstOperand = FirstOperand[0] == '-' ? FirstOperand[1..FirstOperand.Length] : $"-{FirstOperand[0..FirstOperand.Length]}"; + } + + Operator = null; + Text = FirstOperand; + return true; + } + + case "√": + { + Text = GetValue(); + if (FirstOperand != null) + { + FirstOperand = FirstOperand[0] == '-' ? null : Math.Sqrt(double.Parse(FirstOperand)).ToString(); + } + + Text = FirstOperand; + return true; + } + + case "⌫": + { + if (SecondOperand != null) + { + if (SecondOperand.Length == 1) + { + SecondOperand = null; + Text = $"{FirstOperand} {Operator} "; + } + + else + { + SecondOperand = SecondOperand[0..(SecondOperand.Length - 1)]; + Text = $"{FirstOperand} {Operator} {SecondOperand}"; + } + } + + else if (Operator != null) + { + Operator = null; + Text = $"{FirstOperand} "; + } + + else if (FirstOperand != null) + { + if (FirstOperand.Length == 1) + { + FirstOperand = null; + Text = ""; + } + + else + { + FirstOperand = FirstOperand[0..(FirstOperand.Length - 1)]; + Text = $"{FirstOperand}"; + } + } + + return true; + } + + case "C": + { + Text = FirstOperand = SecondOperand = Operator = SecondOperator = null; + return false; + } + + case "=": + { + Text = FirstOperand = GetValue(); + SecondOperand = Operator = SecondOperator = null; + return true; + } + + default: + { + Text += " " + text + " "; + if (Operator != null) + { + if (SecondOperand == null) + { + Operator = text; + } + SecondOperator = text; + return false; + } + Operator = text; + return false; + } + } + } +} + + diff --git a/Calculator/Calculator/Calculator.csproj b/Calculator/Calculator/Calculator.csproj new file mode 100644 index 0000000..b57c89e --- /dev/null +++ b/Calculator/Calculator/Calculator.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + \ No newline at end of file diff --git a/Calculator/Calculator/Form1.Designer.cs b/Calculator/Calculator/Form1.Designer.cs new file mode 100644 index 0000000..c74512d --- /dev/null +++ b/Calculator/Calculator/Form1.Designer.cs @@ -0,0 +1,420 @@ +namespace Calculator +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); + this.Multiplication = new System.Windows.Forms.Button(); + this.Subtraction = new System.Windows.Forms.Button(); + this.Addition = new System.Windows.Forms.Button(); + this.Zero = new System.Windows.Forms.Button(); + this.One = new System.Windows.Forms.Button(); + this.Comma = new System.Windows.Forms.Button(); + this.Two = new System.Windows.Forms.Button(); + this.Three = new System.Windows.Forms.Button(); + this.Four = new System.Windows.Forms.Button(); + this.Seven = new System.Windows.Forms.Button(); + this.Root = new System.Windows.Forms.Button(); + this.Five = new System.Windows.Forms.Button(); + this.Six = new System.Windows.Forms.Button(); + this.Eight = new System.Windows.Forms.Button(); + this.Nine = new System.Windows.Forms.Button(); + this.PlusMinus = new System.Windows.Forms.Button(); + this.Division = new System.Windows.Forms.Button(); + this.Equally = new System.Windows.Forms.Button(); + this.Erase = new System.Windows.Forms.Button(); + this.EraseEverything = new System.Windows.Forms.Button(); + this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.tableLayoutPanel1.SuspendLayout(); + this.tableLayoutPanel2.SuspendLayout(); + this.SuspendLayout(); + // + // tableLayoutPanel1 + // + this.tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.tableLayoutPanel1.ColumnCount = 4; + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.tableLayoutPanel1.Controls.Add(this.Multiplication, 3, 1); + this.tableLayoutPanel1.Controls.Add(this.Subtraction, 3, 2); + this.tableLayoutPanel1.Controls.Add(this.Addition, 3, 3); + this.tableLayoutPanel1.Controls.Add(this.Zero, 0, 4); + this.tableLayoutPanel1.Controls.Add(this.One, 0, 3); + this.tableLayoutPanel1.Controls.Add(this.Comma, 1, 4); + this.tableLayoutPanel1.Controls.Add(this.Two, 1, 3); + this.tableLayoutPanel1.Controls.Add(this.Three, 2, 3); + this.tableLayoutPanel1.Controls.Add(this.Four, 0, 2); + this.tableLayoutPanel1.Controls.Add(this.Seven, 0, 1); + this.tableLayoutPanel1.Controls.Add(this.Root, 0, 0); + this.tableLayoutPanel1.Controls.Add(this.Five, 1, 2); + this.tableLayoutPanel1.Controls.Add(this.Six, 2, 2); + this.tableLayoutPanel1.Controls.Add(this.Eight, 1, 1); + this.tableLayoutPanel1.Controls.Add(this.Nine, 2, 1); + this.tableLayoutPanel1.Controls.Add(this.PlusMinus, 2, 4); + this.tableLayoutPanel1.Controls.Add(this.Division, 3, 4); + this.tableLayoutPanel1.Controls.Add(this.Equally, 3, 0); + this.tableLayoutPanel1.Controls.Add(this.Erase, 2, 0); + this.tableLayoutPanel1.Controls.Add(this.EraseEverything, 1, 0); + this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 143); + this.tableLayoutPanel1.Name = "tableLayoutPanel1"; + this.tableLayoutPanel1.RowCount = 5; + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.tableLayoutPanel1.Size = new System.Drawing.Size(409, 373); + this.tableLayoutPanel1.TabIndex = 1; + // + // Multiplication + // + this.Multiplication.Dock = System.Windows.Forms.DockStyle.Fill; + this.Multiplication.Location = new System.Drawing.Point(309, 77); + this.Multiplication.Name = "Multiplication"; + this.Multiplication.Size = new System.Drawing.Size(97, 68); + this.Multiplication.TabIndex = 15; + this.Multiplication.Text = "*"; + this.Multiplication.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Multiplication.UseVisualStyleBackColor = true; + this.Multiplication.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Subtraction + // + this.Subtraction.Dock = System.Windows.Forms.DockStyle.Fill; + this.Subtraction.Location = new System.Drawing.Point(309, 151); + this.Subtraction.Name = "Subtraction"; + this.Subtraction.Size = new System.Drawing.Size(97, 68); + this.Subtraction.TabIndex = 11; + this.Subtraction.Text = "-"; + this.Subtraction.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Subtraction.UseVisualStyleBackColor = true; + this.Subtraction.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Addition + // + this.Addition.Dock = System.Windows.Forms.DockStyle.Fill; + this.Addition.Location = new System.Drawing.Point(309, 225); + this.Addition.Name = "Addition"; + this.Addition.Size = new System.Drawing.Size(97, 68); + this.Addition.TabIndex = 7; + this.Addition.Text = "+"; + this.Addition.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Addition.UseVisualStyleBackColor = true; + this.Addition.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Zero + // + this.Zero.Dock = System.Windows.Forms.DockStyle.Fill; + this.Zero.Location = new System.Drawing.Point(3, 299); + this.Zero.Name = "Zero"; + this.Zero.Size = new System.Drawing.Size(96, 71); + this.Zero.TabIndex = 0; + this.Zero.Text = "0"; + this.Zero.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Zero.UseVisualStyleBackColor = true; + this.Zero.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // One + // + this.One.Dock = System.Windows.Forms.DockStyle.Fill; + this.One.Location = new System.Drawing.Point(3, 225); + this.One.Name = "One"; + this.One.Size = new System.Drawing.Size(96, 68); + this.One.TabIndex = 4; + this.One.Tag = ""; + this.One.Text = "1"; + this.One.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.One.UseVisualStyleBackColor = true; + this.One.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Comma + // + this.Comma.Dock = System.Windows.Forms.DockStyle.Fill; + this.Comma.Location = new System.Drawing.Point(105, 299); + this.Comma.Name = "Comma"; + this.Comma.Size = new System.Drawing.Size(96, 71); + this.Comma.TabIndex = 1; + this.Comma.Text = ","; + this.Comma.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Comma.UseVisualStyleBackColor = true; + this.Comma.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Two + // + this.Two.Dock = System.Windows.Forms.DockStyle.Fill; + this.Two.Location = new System.Drawing.Point(105, 225); + this.Two.Name = "Two"; + this.Two.Size = new System.Drawing.Size(96, 68); + this.Two.TabIndex = 5; + this.Two.Text = "2"; + this.Two.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Two.UseVisualStyleBackColor = true; + this.Two.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Three + // + this.Three.Dock = System.Windows.Forms.DockStyle.Fill; + this.Three.Location = new System.Drawing.Point(207, 225); + this.Three.Name = "Three"; + this.Three.Size = new System.Drawing.Size(96, 68); + this.Three.TabIndex = 6; + this.Three.Text = "3"; + this.Three.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Three.UseVisualStyleBackColor = true; + this.Three.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Four + // + this.Four.Dock = System.Windows.Forms.DockStyle.Fill; + this.Four.Location = new System.Drawing.Point(3, 151); + this.Four.Name = "Four"; + this.Four.Size = new System.Drawing.Size(96, 68); + this.Four.TabIndex = 8; + this.Four.Text = "4"; + this.Four.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Four.UseVisualStyleBackColor = true; + this.Four.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Seven + // + this.Seven.Dock = System.Windows.Forms.DockStyle.Fill; + this.Seven.Location = new System.Drawing.Point(3, 77); + this.Seven.Name = "Seven"; + this.Seven.Size = new System.Drawing.Size(96, 68); + this.Seven.TabIndex = 12; + this.Seven.Text = "7"; + this.Seven.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Seven.UseVisualStyleBackColor = true; + this.Seven.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Root + // + this.Root.Dock = System.Windows.Forms.DockStyle.Fill; + this.Root.Location = new System.Drawing.Point(3, 3); + this.Root.Name = "Root"; + this.Root.Size = new System.Drawing.Size(96, 68); + this.Root.TabIndex = 16; + this.Root.Text = "√"; + this.Root.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Root.UseVisualStyleBackColor = true; + this.Root.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Five + // + this.Five.Dock = System.Windows.Forms.DockStyle.Fill; + this.Five.Location = new System.Drawing.Point(105, 151); + this.Five.Name = "Five"; + this.Five.Size = new System.Drawing.Size(96, 68); + this.Five.TabIndex = 9; + this.Five.Text = "5"; + this.Five.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Five.UseVisualStyleBackColor = true; + this.Five.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Six + // + this.Six.Dock = System.Windows.Forms.DockStyle.Fill; + this.Six.Location = new System.Drawing.Point(207, 151); + this.Six.Name = "Six"; + this.Six.Size = new System.Drawing.Size(96, 68); + this.Six.TabIndex = 10; + this.Six.Text = "6"; + this.Six.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Six.UseVisualStyleBackColor = true; + this.Six.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Eight + // + this.Eight.Dock = System.Windows.Forms.DockStyle.Fill; + this.Eight.Location = new System.Drawing.Point(105, 77); + this.Eight.Name = "Eight"; + this.Eight.Size = new System.Drawing.Size(96, 68); + this.Eight.TabIndex = 13; + this.Eight.Text = "8"; + this.Eight.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Eight.UseVisualStyleBackColor = true; + this.Eight.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Nine + // + this.Nine.Dock = System.Windows.Forms.DockStyle.Fill; + this.Nine.Location = new System.Drawing.Point(207, 77); + this.Nine.Name = "Nine"; + this.Nine.Size = new System.Drawing.Size(96, 68); + this.Nine.TabIndex = 14; + this.Nine.Text = "9"; + this.Nine.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Nine.UseVisualStyleBackColor = true; + this.Nine.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // PlusMinus + // + this.PlusMinus.Dock = System.Windows.Forms.DockStyle.Fill; + this.PlusMinus.Location = new System.Drawing.Point(207, 299); + this.PlusMinus.Name = "PlusMinus"; + this.PlusMinus.Size = new System.Drawing.Size(96, 71); + this.PlusMinus.TabIndex = 2; + this.PlusMinus.Text = "±"; + this.PlusMinus.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.PlusMinus.UseVisualStyleBackColor = true; + this.PlusMinus.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Division + // + this.Division.Dock = System.Windows.Forms.DockStyle.Fill; + this.Division.Location = new System.Drawing.Point(309, 299); + this.Division.Name = "Division"; + this.Division.Size = new System.Drawing.Size(97, 71); + this.Division.TabIndex = 3; + this.Division.Text = "/"; + this.Division.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Division.UseVisualStyleBackColor = true; + this.Division.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Equally + // + this.Equally.Dock = System.Windows.Forms.DockStyle.Fill; + this.Equally.Location = new System.Drawing.Point(309, 3); + this.Equally.Name = "Equally"; + this.Equally.Size = new System.Drawing.Size(97, 68); + this.Equally.TabIndex = 19; + this.Equally.Text = "="; + this.Equally.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Equally.UseVisualStyleBackColor = true; + this.Equally.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // Erase + // + this.Erase.Dock = System.Windows.Forms.DockStyle.Fill; + this.Erase.Location = new System.Drawing.Point(207, 3); + this.Erase.Name = "Erase"; + this.Erase.Size = new System.Drawing.Size(96, 68); + this.Erase.TabIndex = 18; + this.Erase.Text = "⌫"; + this.Erase.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.Erase.UseVisualStyleBackColor = true; + this.Erase.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // EraseEverything + // + this.EraseEverything.Dock = System.Windows.Forms.DockStyle.Fill; + this.EraseEverything.Location = new System.Drawing.Point(105, 3); + this.EraseEverything.Name = "EraseEverything"; + this.EraseEverything.Size = new System.Drawing.Size(96, 68); + this.EraseEverything.TabIndex = 17; + this.EraseEverything.Text = "C"; + this.EraseEverything.Font = new System.Drawing.Font("Segoe UI", 16, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.EraseEverything.UseVisualStyleBackColor = true; + this.EraseEverything.Click += new System.EventHandler(this.AllButtonsOneClick); + // + // tableLayoutPanel2 + // + this.tableLayoutPanel2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.tableLayoutPanel2.ColumnCount = 1; + this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.tableLayoutPanel2.Controls.Add(this.label1, 0, 0); + this.tableLayoutPanel2.Controls.Add(this.label2, 0, 1); + this.tableLayoutPanel2.Location = new System.Drawing.Point(3, 2); + this.tableLayoutPanel2.Name = "tableLayoutPanel2"; + this.tableLayoutPanel2.RowCount = 2; + this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 28.57143F)); + this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 71.42857F)); + this.tableLayoutPanel2.Size = new System.Drawing.Size(406, 138); + this.tableLayoutPanel2.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Dock = System.Windows.Forms.DockStyle.Fill; + this.label1.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.label1.Location = new System.Drawing.Point(3, 0); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(400, 39); + this.label1.TabIndex = 0; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Dock = System.Windows.Forms.DockStyle.Fill; + this.label2.Font = new System.Drawing.Font("Segoe UI", 30F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.label2.Location = new System.Drawing.Point(3, 39); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(400, 99); + this.label2.TabIndex = 1; + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(409, 518); + this.Controls.Add(this.tableLayoutPanel2); + this.Controls.Add(this.tableLayoutPanel1); + this.MinimumSize = new System.Drawing.Size(427, 565); + this.Name = "Form1"; + this.Text = "Calculator"; + this.tableLayoutPanel1.ResumeLayout(false); + this.tableLayoutPanel2.ResumeLayout(false); + this.tableLayoutPanel2.PerformLayout(); + this.ResumeLayout(false); + + } + + #endregion + private TableLayoutPanel tableLayoutPanel1; + private Button One; + private Button Two; + private Button Three; + private Button Nine; + private Button Six; + private Button Eight; + private Button Five; + private Button Seven; + private Button Four; + private Button Division; + private Button Multiplication; + private Button Subtraction; + private Button Zero; + private Button Equally; + private Button Addition; + private Button Comma; + private TableLayoutPanel tableLayoutPanel2; + private Label label1; + private Label label2; + private Button Root; + private Button PlusMinus; + private Button Erase; + private Button EraseEverything; + } +} \ No newline at end of file diff --git a/Calculator/Calculator/Form1.cs b/Calculator/Calculator/Form1.cs new file mode 100644 index 0000000..f42b588 --- /dev/null +++ b/Calculator/Calculator/Form1.cs @@ -0,0 +1,90 @@ +namespace Calculator; + +public partial class Form1 : Form +{ + private Calculator calculator; + + public Form1() + { + InitializeComponent(); + calculator = new Calculator(); + } + + private void Reaction(string text) + { + if (calculator.AddOperatorOrOperand(text)) + { + label1.Text = calculator.Text; + } + else + { + label1.Text = calculator.Text; + label2.Text = calculator.GetValue(); + } + } + + private void AllButtonsOneClick(object sender, EventArgs e) + { + Reaction(((Button)sender).Text); + } + + protected override bool ProcessCmdKey(ref Message msg, Keys keyData) + { + switch (keyData) + { + case Keys.Enter: + Reaction("="); + break; + case Keys.NumPad0: + Reaction("0"); + break; + case Keys.NumPad1: + Reaction("1"); + break; + case Keys.NumPad2: + Reaction("2"); + break; + case Keys.NumPad3: + Reaction("3"); + break; + case Keys.NumPad4: + Reaction("4"); + break; + case Keys.NumPad5: + Reaction("5"); + break; + case Keys.NumPad6: + Reaction("6"); + break; + case Keys.NumPad7: + Reaction("7"); + break; + case Keys.NumPad8: + Reaction("8"); + break; + case Keys.NumPad9: + Reaction("9"); + break; + case Keys.Divide: + Reaction("/"); + break; + case Keys.Multiply: + Reaction("*"); + break; + case Keys.Add: + Reaction("+"); + break; + case Keys.Subtract: + Reaction("-"); + break; + case Keys.Delete: + Reaction(","); + break; + case Keys.Back: + Reaction("⌫"); + break; + } + + return true; + } +} \ No newline at end of file diff --git a/Calculator/Calculator/Form1.resx b/Calculator/Calculator/Form1.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Calculator/Calculator/Form1.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Calculator/Calculator/Program.cs b/Calculator/Calculator/Program.cs new file mode 100644 index 0000000..2b998c0 --- /dev/null +++ b/Calculator/Calculator/Program.cs @@ -0,0 +1,15 @@ +namespace Calculator +{ + public static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + public static void Main() + { + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); + } + } +} \ No newline at end of file diff --git a/Calculator/TestCalculator/TestCalculator.cs b/Calculator/TestCalculator/TestCalculator.cs new file mode 100644 index 0000000..d87088c --- /dev/null +++ b/Calculator/TestCalculator/TestCalculator.cs @@ -0,0 +1,62 @@ +namespace TestCalculator; + +using NUnit.Framework; +using Calculator; +using System.Collections.Generic; + +public class TestCalculator +{ + Calculator calculator = new(); + + [SetUp] + public void Setup() + { + calculator = new(); + } + + private static IEnumerable Expressions() => new TestCaseData[] + { + new TestCaseData(null , ""), + new TestCaseData("1", "1 + 2"), + new TestCaseData("4", "4 *"), + new TestCaseData("7", "7 = "), + new TestCaseData("-7", "7 ±"), + new TestCaseData("-12", "7 + 5 ±"), + new TestCaseData("-7", "7 + ±"), + new TestCaseData("-8", "7 + ± - 1 +"), + new TestCaseData("7,6", "7 , 6 "), + new TestCaseData("-7,1", "7 , 1 ± +"), + new TestCaseData(null, " 1 1 C"), + new TestCaseData("1", "1 - "), + new TestCaseData("2", "4 √"), + new TestCaseData("2", "4 √"), + new TestCaseData("5", "4 + 21 √"), + new TestCaseData(null, "4 ± √"), + new TestCaseData(null, "4 ⌫"), + new TestCaseData("4", "4 5 ⌫"), + new TestCaseData(null, "⌫"), + new TestCaseData(null, ","), + new TestCaseData("5,6", "5 , 6"), + new TestCaseData("5,6", "5 , 6 ,"), + new TestCaseData(null, "5 / 0 -"), + new TestCaseData("5", "5 / 0"), + new TestCaseData("-2", "5 - 7 + 3"), + new TestCaseData(null, "+"), + new TestCaseData(null, "-"), + new TestCaseData(null, "*"), + new TestCaseData(null, "/"), + new TestCaseData(null, "="), + new TestCaseData("0", "0000123 - 123 +"), + new TestCaseData("5", "6, - 1 +"), + new TestCaseData("-1", "6, ± + 5 ="), + new TestCaseData(null, " 1 C"), + new TestCaseData(null, " 1 78 C"), + + }; + + [TestCaseSource(nameof(Expressions))] + public void Test1(string expectedValue, string value) + { + Assert.AreEqual(expectedValue, calculator.CalculateExpression(value.Split(' '))); + } +} \ No newline at end of file diff --git a/Calculator/TestCalculator/TestCalculator.csproj b/Calculator/TestCalculator/TestCalculator.csproj new file mode 100644 index 0000000..4b5d942 --- /dev/null +++ b/Calculator/TestCalculator/TestCalculator.csproj @@ -0,0 +1,21 @@ + + + + net6.0-windows + enable + + false + + + + + + + + + + + + + + From 895f20bd1ac85b954c1a344e62e10eb885534e7a Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 1 Jun 2022 23:49:00 +0300 Subject: [PATCH 2/4] add comments --- Calculator/Calculator/Calculator.cs | 16 ++++++++++++++++ Calculator/Calculator/Program.cs | 21 ++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Calculator/Calculator/Calculator.cs b/Calculator/Calculator/Calculator.cs index 1343a0c..d1386cf 100644 --- a/Calculator/Calculator/Calculator.cs +++ b/Calculator/Calculator/Calculator.cs @@ -1,5 +1,8 @@ namespace Calculator; +/// +/// Class representing Calculator +/// public class Calculator { private string? FirstOperand { get; set; } @@ -12,6 +15,11 @@ public class Calculator public string? Text { get; private set; } + /// + /// Is the string an operetor + /// + /// Input string + /// True if the string is an operator public static bool IsOperator(string text) => text == "/" || text == "*" || text == "-" || text == "+" || text == "±" || text == "=" || text == "√" || text == "C" || text == "⌫"; public string? CalculateExpression(string[] expression) @@ -26,6 +34,10 @@ public class Calculator return FirstOperand; } + /// + /// A function for calculating an expression of the form operator operand operator + /// + /// The resulting value public string? GetValue() { if (FirstOperand == null || SecondOperand == null) @@ -72,6 +84,10 @@ public class Calculator return FirstOperand; } + /// + /// Function for initializing an operator or operand + /// + /// public bool AddOperatorOrOperand(string text) { if (text == "") diff --git a/Calculator/Calculator/Program.cs b/Calculator/Calculator/Program.cs index 2b998c0..551e5ad 100644 --- a/Calculator/Calculator/Program.cs +++ b/Calculator/Calculator/Program.cs @@ -1,15 +1,14 @@ -namespace Calculator +namespace Calculator; + +public static class Program { - public static class Program + /// + /// The main entry point for the application. + /// + [STAThread] + public static void Main() { - /// - /// The main entry point for the application. - /// - [STAThread] - public static void Main() - { - ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); - } + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); } } \ No newline at end of file From f573af6ff13ae87fe50307975ad0761f350242b5 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 00:03:41 +0300 Subject: [PATCH 3/4] rename yml --- .github/workflows/{dotnet.yml => Calculator.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{dotnet.yml => Calculator.yml} (96%) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/Calculator.yml similarity index 96% rename from .github/workflows/dotnet.yml rename to .github/workflows/Calculator.yml index a6a59da..1e6a924 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/Calculator.yml @@ -1,6 +1,6 @@ name: Build -on: [push, pull_request] +on: [push] jobs: build: From 68911bd3e5ec0da75f181d51a4c223073d94ebb2 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 2 Jun 2022 01:25:23 +0300 Subject: [PATCH 4/4] rename yml --- .github/workflows/{Calculator.yml => dotnet.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{Calculator.yml => dotnet.yml} (100%) diff --git a/.github/workflows/Calculator.yml b/.github/workflows/dotnet.yml similarity index 100% rename from .github/workflows/Calculator.yml rename to .github/workflows/dotnet.yml