From 421ce8dac36fa414373dbb465ff65b30b19e08b6 Mon Sep 17 00:00:00 2001 From: IgnatSergeev/Desktop Date: Fri, 21 Apr 2023 23:30:50 +0300 Subject: [PATCH 1/5] Added form and started realisation of core --- C#/forSpbu/Calculator/Calculator.csproj | 11 ++ C#/forSpbu/Calculator/Calculator.csproj.user | 8 + C#/forSpbu/Calculator/CalculatorCore.cs | 118 ++++++++++++ .../Calculator/CalculatorForm.Designer.cs | 173 ++++++++++++++++++ C#/forSpbu/Calculator/CalculatorForm.cs | 29 +++ C#/forSpbu/Calculator/Program.cs | 14 ++ C#/forSpbu/forSpbu.sln | 17 +- 7 files changed, 366 insertions(+), 4 deletions(-) create mode 100644 C#/forSpbu/Calculator/Calculator.csproj create mode 100644 C#/forSpbu/Calculator/Calculator.csproj.user create mode 100644 C#/forSpbu/Calculator/CalculatorCore.cs create mode 100644 C#/forSpbu/Calculator/CalculatorForm.Designer.cs create mode 100644 C#/forSpbu/Calculator/CalculatorForm.cs create mode 100644 C#/forSpbu/Calculator/Program.cs diff --git a/C#/forSpbu/Calculator/Calculator.csproj b/C#/forSpbu/Calculator/Calculator.csproj new file mode 100644 index 0000000..638e474 --- /dev/null +++ b/C#/forSpbu/Calculator/Calculator.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net7.0-windows + enable + true + enable + + + \ No newline at end of file diff --git a/C#/forSpbu/Calculator/Calculator.csproj.user b/C#/forSpbu/Calculator/Calculator.csproj.user new file mode 100644 index 0000000..3a34caa --- /dev/null +++ b/C#/forSpbu/Calculator/Calculator.csproj.user @@ -0,0 +1,8 @@ + + + + + Form + + + \ No newline at end of file diff --git a/C#/forSpbu/Calculator/CalculatorCore.cs b/C#/forSpbu/Calculator/CalculatorCore.cs new file mode 100644 index 0000000..4cf3b08 --- /dev/null +++ b/C#/forSpbu/Calculator/CalculatorCore.cs @@ -0,0 +1,118 @@ +namespace Calculator; + +public class CalculatorCore +{ + private enum Operation + { + Add, + Sub, + Mul, + Div, + Mod, + Clear, + Equal + } + + private enum State + { + FirstOperandNumber, + FirstOperandComma, + SecondOperandNumber, + SecondOperandComma, + Operation, + Error + } + + private Operation StringGetOperation(string operationString) + { + return operationString switch + { + "+" => Operation.Add, + "-" => Operation.Sub, + "*" => Operation.Mul, + "/" => Operation.Div, + "mod" => Operation.Mod, + "CE" => Operation.Clear, + "=" => Operation.Equal, + _ => throw new ArgumentOutOfRangeException(nameof(operationString), operationString, null) + }; + } + + private double StringGetNumber(string numberString) + { + return numberString switch + { + "0" => 0, + "1" => 1, + "2" => 2, + "3" => 3, + "4" => 4, + "5" => 5, + "6" => 6, + "7" => 7, + "8" => 8, + "9" => 9, + _ => throw new ArgumentOutOfRangeException(nameof(numberString), numberString, null) + }; + } + + public void TakeNumberToken(string numberString) + { + switch (_state) + { + case State.Error: + _number = numberString; + _expression = ""; + _state = State.FirstOperandNumber; + break; + case State.FirstOperandComma: + _number += numberString; + _state = State.FirstOperandNumber; + break; + case State.FirstOperandNumber: + _number += numberString; + break; + case State.SecondOperandComma: + _number += numberString; + _state = State.SecondOperandNumber; + break; + case State.SecondOperandNumber: + _number += numberString; + break; + case State.Operation: + _number = numberString; + _state = State.FirstOperandNumber; + break; + default: + throw new ArgumentOutOfRangeException(nameof(_state), _state, null); + } + } + + public void TakeOperationToken(string operationString) + { + switch (_state) + { + case State.Error: + break; + case State.FirstOperandComma: + _state = State.Operation; + _number = _number.Remove(_number.Length - 1); + _expression += _number + operationString; + //? + break; + case State.FirstOperandNumber: + _state = State.Operation; + _expression += _number + operationString; + break; + case State.SecondOperandNumber: + + } + } + + private double firstOperand = 0; + private double secondOperand = 0; + private State _state = State.Error; + private string _number = "0"; + private string _expression = ""; + private const double Delta = 0.0001; +} \ No newline at end of file diff --git a/C#/forSpbu/Calculator/CalculatorForm.Designer.cs b/C#/forSpbu/Calculator/CalculatorForm.Designer.cs new file mode 100644 index 0000000..a047fa3 --- /dev/null +++ b/C#/forSpbu/Calculator/CalculatorForm.Designer.cs @@ -0,0 +1,173 @@ +namespace Calculator; + +partial class CalculatorForm +{ + /// + /// 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() + { + mainLayout = new TableLayoutPanel(); + + expression = new Label(); + numberLabel = new Label(); + buttonsTable = new TableLayoutPanel(); + + for (int i = 0; i <= 9; i++) + { + numberButtons.Add(new Button()); + } + for (int i = 0; i < 8; i++) + { + operationButtons.Add(new Button()); + } + string[] operationsText = { "C", "+", "-", "mod", "*", "/", ",", "=" }; + + + buttonsTable.SuspendLayout(); + mainLayout.SuspendLayout(); + SuspendLayout(); + + + /* + * Calculator form + */ + Size = new Size(400, 400); + MinimumSize = new Size(300, 400); + Text = "Calculator"; + Controls.Add(mainLayout); + + /* + * Main label + */ + mainLayout.Size = new Size(385, 362); + mainLayout.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + mainLayout.ColumnCount = 1; + mainLayout.RowCount = 3; + mainLayout.Controls.Add(expression, 0, 0); + mainLayout.Controls.Add(numberLabel, 0, 1); + mainLayout.Controls.Add(buttonsTable, 0, 2); + mainLayout.RowStyles.Add(new ColumnStyle(SizeType.Percent, 10F)); + mainLayout.RowStyles.Add(new ColumnStyle(SizeType.Percent, 15F)); + mainLayout.RowStyles.Add(new ColumnStyle(SizeType.Percent, 75F)); + + /* + * Expression label + */ + expression.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + expression.TextAlign = ContentAlignment.MiddleRight; + expression.Font = new Font("Segoe UI", 15F, FontStyle.Regular, GraphicsUnit.Pixel); + expression.Text = "2 * 10 + 2"; + + /* + * Number label + */ + numberLabel.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + numberLabel.TextAlign = ContentAlignment.MiddleRight; + numberLabel.Font = new Font("Segoe UI", 30F, FontStyle.Regular, GraphicsUnit.Pixel); + numberLabel.Text = "22"; + + /* + * Buttons table + */ + buttonsTable.BackColor = Color.Black; + buttonsTable.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + buttonsTable.ColumnCount = 3; + buttonsTable.RowCount = 6; + buttonsTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.ColumnCount)); + buttonsTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.ColumnCount)); + buttonsTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.ColumnCount)); + buttonsTable.RowStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.RowCount)); + buttonsTable.RowStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.RowCount)); + buttonsTable.RowStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.RowCount)); + buttonsTable.RowStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.RowCount)); + buttonsTable.RowStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.RowCount)); + buttonsTable.RowStyles.Add(new ColumnStyle(SizeType.Percent, 100/buttonsTable.RowCount)); + buttonsTable.Controls.Add(numberButtons[0], 1, 5); + buttonsTable.Controls.Add(numberButtons[1], 0, 4); + buttonsTable.Controls.Add(numberButtons[2], 1, 4); + buttonsTable.Controls.Add(numberButtons[3], 2, 4); + buttonsTable.Controls.Add(numberButtons[4], 0, 3); + buttonsTable.Controls.Add(numberButtons[5], 1, 3); + buttonsTable.Controls.Add(numberButtons[6], 2, 3); + buttonsTable.Controls.Add(numberButtons[7], 0, 2); + buttonsTable.Controls.Add(numberButtons[8], 1, 2); + buttonsTable.Controls.Add(numberButtons[9], 2, 2); + buttonsTable.Controls.Add(operationButtons[0], 0, 0); + buttonsTable.Controls.Add(operationButtons[1], 1, 0); + buttonsTable.Controls.Add(operationButtons[2], 2, 0); + buttonsTable.Controls.Add(operationButtons[3], 0, 1); + buttonsTable.Controls.Add(operationButtons[4], 1, 1); + buttonsTable.Controls.Add(operationButtons[5], 2, 1); + buttonsTable.Controls.Add(operationButtons[6], 0, 5); + buttonsTable.Controls.Add(operationButtons[7], 2, 5); + + /* + * Buttons + */ + for (int i = 0; i <= 9; i++) + { + var button = numberButtons[i]; + button.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + button.BackColor = Color.Gray; + button.TextAlign = ContentAlignment.MiddleCenter; + button.Text = i.ToString(); + button.ForeColor = Color.Azure; + button.Font = new Font("Segoe UI", 15F, FontStyle.Regular, GraphicsUnit.Pixel); + button.Click += NumberOnClick; + } + + /* + * Operations + */ + for (int i = 0; i < 8; i++) + { + var button = operationButtons[i]; + button.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; + button.BackColor = (operationsText[i] != "=") ? Color.DimGray : Color.Aqua; + button.TextAlign = ContentAlignment.MiddleCenter; + button.Text = operationsText[i]; + button.ForeColor = (operationsText[i] != "=") ? Color.Azure : Color.Black; + button.Font = new Font("Segoe UI", 15F, FontStyle.Regular, GraphicsUnit.Pixel); + button.Click += OperationOnClick; + } + + + buttonsTable.ResumeLayout(false); + mainLayout.ResumeLayout(false); + ResumeLayout(false); + } + + #endregion + + private TableLayoutPanel mainLayout; + + private Label expression; + private Label numberLabel; + private TableLayoutPanel buttonsTable; + + private List