diff --git a/C# Beginners/BasicCalculator/App.config b/C# Beginners/BasicCalculator/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/C# Beginners/BasicCalculator/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/C# Beginners/BasicCalculator/BasicCalculator.csproj b/C# Beginners/BasicCalculator/BasicCalculator.csproj new file mode 100644 index 0000000..0bc1180 --- /dev/null +++ b/C# Beginners/BasicCalculator/BasicCalculator.csproj @@ -0,0 +1,92 @@ + + + + + Debug + AnyCPU + {0FB8FF87-B543-44CE-BE5C-9E6EE23AC755} + WinExe + Properties + BasicCalculator + BasicCalculator + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + Form1.cs + + + + + + + Form1.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + \ No newline at end of file diff --git a/C# Beginners/BasicCalculator/BasicCalculator.sln b/C# Beginners/BasicCalculator/BasicCalculator.sln new file mode 100644 index 0000000..29934dc --- /dev/null +++ b/C# Beginners/BasicCalculator/BasicCalculator.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicCalculator", "BasicCalculator.csproj", "{0FB8FF87-B543-44CE-BE5C-9E6EE23AC755}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {0FB8FF87-B543-44CE-BE5C-9E6EE23AC755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0FB8FF87-B543-44CE-BE5C-9E6EE23AC755}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0FB8FF87-B543-44CE-BE5C-9E6EE23AC755}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0FB8FF87-B543-44CE-BE5C-9E6EE23AC755}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/C# Beginners/BasicCalculator/Form1.Designer.cs b/C# Beginners/BasicCalculator/Form1.Designer.cs new file mode 100644 index 0000000..b202175 --- /dev/null +++ b/C# Beginners/BasicCalculator/Form1.Designer.cs @@ -0,0 +1,356 @@ +namespace BasicCalculator +{ + 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.UserInputText = new System.Windows.Forms.TextBox(); + this.CalculationResultText = new System.Windows.Forms.Label(); + this.ButtonsPanel = new System.Windows.Forms.TableLayoutPanel(); + this.EqualsButton = new System.Windows.Forms.Button(); + this.DotButton = new System.Windows.Forms.Button(); + this.ZeroButton = new System.Windows.Forms.Button(); + this.PlusButton = new System.Windows.Forms.Button(); + this.ThreeButton = new System.Windows.Forms.Button(); + this.TwoButton = new System.Windows.Forms.Button(); + this.OneButton = new System.Windows.Forms.Button(); + this.MinusButton = new System.Windows.Forms.Button(); + this.SixButton = new System.Windows.Forms.Button(); + this.FiveButton = new System.Windows.Forms.Button(); + this.FourButton = new System.Windows.Forms.Button(); + this.TimesButton = new System.Windows.Forms.Button(); + this.NineButton = new System.Windows.Forms.Button(); + this.EightButton = new System.Windows.Forms.Button(); + this.SevenButton = new System.Windows.Forms.Button(); + this.DivideButton = new System.Windows.Forms.Button(); + this.DelButton = new System.Windows.Forms.Button(); + this.CEButton = new System.Windows.Forms.Button(); + this.ButtonsPanel.SuspendLayout(); + this.SuspendLayout(); + // + // UserInputText + // + this.UserInputText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.UserInputText.Location = new System.Drawing.Point(11, 12); + this.UserInputText.Name = "UserInputText"; + this.UserInputText.Size = new System.Drawing.Size(578, 29); + this.UserInputText.TabIndex = 0; + // + // CalculationResultText + // + this.CalculationResultText.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.CalculationResultText.Location = new System.Drawing.Point(11, 54); + this.CalculationResultText.Name = "CalculationResultText"; + this.CalculationResultText.Size = new System.Drawing.Size(578, 77); + this.CalculationResultText.TabIndex = 1; + this.CalculationResultText.Text = "Please enter an equation and press enter or ="; + // + // ButtonsPanel + // + this.ButtonsPanel.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.ButtonsPanel.ColumnCount = 4; + this.ButtonsPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.ButtonsPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.ButtonsPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.ButtonsPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); + this.ButtonsPanel.Controls.Add(this.EqualsButton, 3, 4); + this.ButtonsPanel.Controls.Add(this.DotButton, 2, 4); + this.ButtonsPanel.Controls.Add(this.ZeroButton, 1, 4); + this.ButtonsPanel.Controls.Add(this.PlusButton, 3, 3); + this.ButtonsPanel.Controls.Add(this.ThreeButton, 2, 3); + this.ButtonsPanel.Controls.Add(this.TwoButton, 1, 3); + this.ButtonsPanel.Controls.Add(this.OneButton, 0, 3); + this.ButtonsPanel.Controls.Add(this.MinusButton, 3, 2); + this.ButtonsPanel.Controls.Add(this.SixButton, 2, 2); + this.ButtonsPanel.Controls.Add(this.FiveButton, 1, 2); + this.ButtonsPanel.Controls.Add(this.FourButton, 0, 2); + this.ButtonsPanel.Controls.Add(this.TimesButton, 3, 1); + this.ButtonsPanel.Controls.Add(this.NineButton, 2, 1); + this.ButtonsPanel.Controls.Add(this.EightButton, 1, 1); + this.ButtonsPanel.Controls.Add(this.SevenButton, 0, 1); + this.ButtonsPanel.Controls.Add(this.DivideButton, 3, 0); + this.ButtonsPanel.Controls.Add(this.DelButton, 2, 0); + this.ButtonsPanel.Controls.Add(this.CEButton, 0, 0); + this.ButtonsPanel.Location = new System.Drawing.Point(16, 134); + this.ButtonsPanel.Name = "ButtonsPanel"; + this.ButtonsPanel.RowCount = 5; + this.ButtonsPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.ButtonsPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.ButtonsPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.ButtonsPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.ButtonsPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F)); + this.ButtonsPanel.Size = new System.Drawing.Size(573, 486); + this.ButtonsPanel.TabIndex = 2; + // + // EqualsButton + // + this.EqualsButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.EqualsButton.Location = new System.Drawing.Point(432, 391); + this.EqualsButton.Name = "EqualsButton"; + this.EqualsButton.Size = new System.Drawing.Size(138, 92); + this.EqualsButton.TabIndex = 19; + this.EqualsButton.Text = "="; + this.EqualsButton.UseVisualStyleBackColor = true; + this.EqualsButton.Click += new System.EventHandler(this.EqualsButton_Click); + // + // DotButton + // + this.DotButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.DotButton.Location = new System.Drawing.Point(289, 391); + this.DotButton.Name = "DotButton"; + this.DotButton.Size = new System.Drawing.Size(137, 92); + this.DotButton.TabIndex = 18; + this.DotButton.Text = "."; + this.DotButton.UseVisualStyleBackColor = true; + this.DotButton.Click += new System.EventHandler(this.DotButton_Click); + // + // ZeroButton + // + this.ZeroButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.ZeroButton.Location = new System.Drawing.Point(146, 391); + this.ZeroButton.Name = "ZeroButton"; + this.ZeroButton.Size = new System.Drawing.Size(137, 92); + this.ZeroButton.TabIndex = 17; + this.ZeroButton.Text = "0"; + this.ZeroButton.UseVisualStyleBackColor = true; + this.ZeroButton.Click += new System.EventHandler(this.ZeroButton_Click); + // + // PlusButton + // + this.PlusButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.PlusButton.Location = new System.Drawing.Point(432, 294); + this.PlusButton.Name = "PlusButton"; + this.PlusButton.Size = new System.Drawing.Size(138, 91); + this.PlusButton.TabIndex = 15; + this.PlusButton.Text = "+"; + this.PlusButton.UseVisualStyleBackColor = true; + this.PlusButton.Click += new System.EventHandler(this.PlusButton_Click); + // + // ThreeButton + // + this.ThreeButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.ThreeButton.Location = new System.Drawing.Point(289, 294); + this.ThreeButton.Name = "ThreeButton"; + this.ThreeButton.Size = new System.Drawing.Size(137, 91); + this.ThreeButton.TabIndex = 14; + this.ThreeButton.Text = "3"; + this.ThreeButton.UseVisualStyleBackColor = true; + this.ThreeButton.Click += new System.EventHandler(this.ThreeButton_Click); + // + // TwoButton + // + this.TwoButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.TwoButton.Location = new System.Drawing.Point(146, 294); + this.TwoButton.Name = "TwoButton"; + this.TwoButton.Size = new System.Drawing.Size(137, 91); + this.TwoButton.TabIndex = 13; + this.TwoButton.Text = "2"; + this.TwoButton.UseVisualStyleBackColor = true; + this.TwoButton.Click += new System.EventHandler(this.TwoButton_Click); + // + // OneButton + // + this.OneButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.OneButton.Location = new System.Drawing.Point(3, 294); + this.OneButton.Name = "OneButton"; + this.OneButton.Size = new System.Drawing.Size(137, 91); + this.OneButton.TabIndex = 12; + this.OneButton.Text = "1"; + this.OneButton.UseVisualStyleBackColor = true; + this.OneButton.Click += new System.EventHandler(this.OneButton_Click); + // + // MinusButton + // + this.MinusButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.MinusButton.Location = new System.Drawing.Point(432, 197); + this.MinusButton.Name = "MinusButton"; + this.MinusButton.Size = new System.Drawing.Size(138, 91); + this.MinusButton.TabIndex = 11; + this.MinusButton.Text = "-"; + this.MinusButton.UseVisualStyleBackColor = true; + this.MinusButton.Click += new System.EventHandler(this.MinusButton_Click); + // + // SixButton + // + this.SixButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.SixButton.Location = new System.Drawing.Point(289, 197); + this.SixButton.Name = "SixButton"; + this.SixButton.Size = new System.Drawing.Size(137, 91); + this.SixButton.TabIndex = 10; + this.SixButton.Text = "6"; + this.SixButton.UseVisualStyleBackColor = true; + this.SixButton.Click += new System.EventHandler(this.SixButton_Click); + // + // FiveButton + // + this.FiveButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.FiveButton.Location = new System.Drawing.Point(146, 197); + this.FiveButton.Name = "FiveButton"; + this.FiveButton.Size = new System.Drawing.Size(137, 91); + this.FiveButton.TabIndex = 9; + this.FiveButton.Text = "5"; + this.FiveButton.UseVisualStyleBackColor = true; + this.FiveButton.Click += new System.EventHandler(this.FiveButton_Click); + // + // FourButton + // + this.FourButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.FourButton.Location = new System.Drawing.Point(3, 197); + this.FourButton.Name = "FourButton"; + this.FourButton.Size = new System.Drawing.Size(137, 91); + this.FourButton.TabIndex = 8; + this.FourButton.Text = "4"; + this.FourButton.UseVisualStyleBackColor = true; + this.FourButton.Click += new System.EventHandler(this.FourButton_Click); + // + // TimesButton + // + this.TimesButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.TimesButton.Location = new System.Drawing.Point(432, 100); + this.TimesButton.Name = "TimesButton"; + this.TimesButton.Size = new System.Drawing.Size(138, 91); + this.TimesButton.TabIndex = 7; + this.TimesButton.Text = "X"; + this.TimesButton.UseVisualStyleBackColor = true; + this.TimesButton.Click += new System.EventHandler(this.TimesButton_Click); + // + // NineButton + // + this.NineButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.NineButton.Location = new System.Drawing.Point(289, 100); + this.NineButton.Name = "NineButton"; + this.NineButton.Size = new System.Drawing.Size(137, 91); + this.NineButton.TabIndex = 6; + this.NineButton.Text = "9"; + this.NineButton.UseVisualStyleBackColor = true; + this.NineButton.Click += new System.EventHandler(this.NineButton_Click); + // + // EightButton + // + this.EightButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.EightButton.Location = new System.Drawing.Point(146, 100); + this.EightButton.Name = "EightButton"; + this.EightButton.Size = new System.Drawing.Size(137, 91); + this.EightButton.TabIndex = 5; + this.EightButton.Text = "8"; + this.EightButton.UseVisualStyleBackColor = true; + this.EightButton.Click += new System.EventHandler(this.EightButton_Click); + // + // SevenButton + // + this.SevenButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.SevenButton.Location = new System.Drawing.Point(3, 100); + this.SevenButton.Name = "SevenButton"; + this.SevenButton.Size = new System.Drawing.Size(137, 91); + this.SevenButton.TabIndex = 4; + this.SevenButton.Text = "7"; + this.SevenButton.UseVisualStyleBackColor = true; + this.SevenButton.Click += new System.EventHandler(this.SevenButton_Click); + // + // DivideButton + // + this.DivideButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.DivideButton.Location = new System.Drawing.Point(432, 3); + this.DivideButton.Name = "DivideButton"; + this.DivideButton.Size = new System.Drawing.Size(138, 91); + this.DivideButton.TabIndex = 3; + this.DivideButton.Text = "%"; + this.DivideButton.UseVisualStyleBackColor = true; + this.DivideButton.Click += new System.EventHandler(this.DivideButton_Click); + // + // DelButton + // + this.DelButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.DelButton.Location = new System.Drawing.Point(289, 3); + this.DelButton.Name = "DelButton"; + this.DelButton.Size = new System.Drawing.Size(137, 91); + this.DelButton.TabIndex = 2; + this.DelButton.Text = "Del"; + this.DelButton.UseVisualStyleBackColor = true; + this.DelButton.Click += new System.EventHandler(this.DelButton_Click); + // + // CEButton + // + this.CEButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.CEButton.Dock = System.Windows.Forms.DockStyle.Fill; + this.CEButton.Location = new System.Drawing.Point(3, 3); + this.CEButton.Name = "CEButton"; + this.CEButton.Size = new System.Drawing.Size(137, 91); + this.CEButton.TabIndex = 0; + this.CEButton.Text = "CE"; + this.CEButton.UseVisualStyleBackColor = true; + this.CEButton.Click += new System.EventHandler(this.CEButton_Click); + // + // Form1 + // + this.AcceptButton = this.EqualsButton; + this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.CancelButton = this.CEButton; + this.ClientSize = new System.Drawing.Size(600, 632); + this.Controls.Add(this.ButtonsPanel); + this.Controls.Add(this.CalculationResultText); + this.Controls.Add(this.UserInputText); + this.MinimumSize = new System.Drawing.Size(460, 502); + this.Name = "Form1"; + this.Text = "Basic Calculator"; + this.ButtonsPanel.ResumeLayout(false); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox UserInputText; + private System.Windows.Forms.Label CalculationResultText; + private System.Windows.Forms.TableLayoutPanel ButtonsPanel; + private System.Windows.Forms.Button CEButton; + private System.Windows.Forms.Button EqualsButton; + private System.Windows.Forms.Button DotButton; + private System.Windows.Forms.Button ZeroButton; + private System.Windows.Forms.Button PlusButton; + private System.Windows.Forms.Button ThreeButton; + private System.Windows.Forms.Button TwoButton; + private System.Windows.Forms.Button OneButton; + private System.Windows.Forms.Button MinusButton; + private System.Windows.Forms.Button SixButton; + private System.Windows.Forms.Button FiveButton; + private System.Windows.Forms.Button FourButton; + private System.Windows.Forms.Button TimesButton; + private System.Windows.Forms.Button NineButton; + private System.Windows.Forms.Button EightButton; + private System.Windows.Forms.Button SevenButton; + private System.Windows.Forms.Button DivideButton; + private System.Windows.Forms.Button DelButton; + } +} + diff --git a/C# Beginners/BasicCalculator/Form1.cs b/C# Beginners/BasicCalculator/Form1.cs new file mode 100644 index 0000000..00a088a --- /dev/null +++ b/C# Beginners/BasicCalculator/Form1.cs @@ -0,0 +1,542 @@ +using System; +using System.Linq; +using System.Windows.Forms; + +namespace BasicCalculator +{ + /// + /// A basic calculator + /// + public partial class Form1 : Form + { + #region Constructor + + /// + /// Default constructor + /// + public Form1() + { + InitializeComponent(); + } + + #endregion + + #region Clearing Methods + + /// + /// Clears the user input text + /// + /// The event sender + /// The event arguments + private void CEButton_Click(object sender, EventArgs e) + { + // Clears the text from the user input text box + this.UserInputText.Text = string.Empty; + + // Focus the user input text + FocusInputText(); + } + + /// + /// Deletes the first character in front of the cursor + /// + /// The event sender + /// The event arguments + private void DelButton_Click(object sender, EventArgs e) + { + // Delete the value after the selected position + DeleteTextValue(); + + // Focus the user input text + FocusInputText(); + } + + #endregion + + #region Operator Methods + + /// + /// Adds the Divide character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void DivideButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("/"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the Times character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void TimesButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("*"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the Minus character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void MinusButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("-"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the Plus character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void PlusButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("+"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Calculates the given equation in the user input text + /// + /// The event sender + /// The event arguments + private void EqualsButton_Click(object sender, EventArgs e) + { + // Calculate the equation + CalculateEquation(); + + // Focus the user input text + FocusInputText(); + } + + #endregion + + #region Number Methods + + /// + /// Adds the . character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void DotButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("."); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 0 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void ZeroButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("0"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 1 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void OneButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("1"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 2 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void TwoButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("2"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 3 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void ThreeButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("3"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 4 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void FourButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("4"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 5 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void FiveButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("5"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 6 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void SixButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("6"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 7 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void SevenButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("7"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 8 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void EightButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("8"); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Adds the 9 character to the text at the currently selection position + /// + /// The event sender + /// The event arguments + private void NineButton_Click(object sender, EventArgs e) + { + // Insert the value in the user input text box at the currently selected position + InsertTextValue("9"); + + // Focus the user input text + FocusInputText(); + } + + #endregion + + /// + /// Calculates the given equation and outputs the answer to the user label + /// + private void CalculateEquation() + { + // 1. Enums + // 2. New classes + // 3. For loops + // 4. try/catch + + // 5. Recursive functions + // 6. Switch statements + + this.CalculationResultText.Text = ParseOperation(); + + // Focus the user input text + FocusInputText(); + } + + /// + /// Parses the users equation and calculates the result + /// + /// + private string ParseOperation() + { + try + { + // Get the users equation input + var input = this.UserInputText.Text; + + // Remove all spaces + input = input.Replace(" ", ""); + + // Create a new top-level operation + var operation = new Operation(); + var leftSide = true; + + // Loop through each character of the input + // starting from the left working to the right + for (int i = 0; i < input.Length; i++) + { + // TODO: Handle order priority + // 4.2 + 5.7 * 3 + // It should calculate 5 * 3 first, then 4 + the result (so 4 + 15) + + // Check if the current character is a number + if ("0123456789.".Any(c => input[i] == c)) + { + if (leftSide) + operation.LeftSide = AddNumberPart(operation.LeftSide, input[i]); + else + operation.RightSide = AddNumberPart(operation.RightSide, input[i]); + } + // If it is an operator ( + - * / ) set the operator type + else if ("+-*/".Any(c => input[i] == c)) + { + // If we are on the right side already, we now need to calculate our current operation + // and set the result to the left side of the next operation + if (!leftSide) + { + // Get the operator type + var operatorType = GetOperationType(input[i]); + + // Check if we actually have a right side number + if (operation.RightSide.Length == 0) + { + // Check the operator is not a minus (as they could be creating a negative number) + if (operatorType != OperationType.Minus) + throw new InvalidOperationException($"Operator (+ * / or more than one -) specified without an right side number"); + + // If we got here, the operator type is a minus, and there is no left number currently, so add the minus to the number + operation.RightSide += input[i]; + } + else + { + // Calculate previous equation and set to the left side + operation.LeftSide = CalculateOperation(operation); + + // Set new operator + operation.OperationType = operatorType; + + // Clear the previous right number + operation.RightSide = string.Empty; + } + } + else + { + // Get the operator type + var operatorType = GetOperationType(input[i]); + + // Check if we actually have a left side number + if (operation.LeftSide.Length == 0) + { + // Check the operator is not a minus (as they could be creating a negative number) + if (operatorType != OperationType.Minus) + throw new InvalidOperationException($"Operator (+ * / or more than one -) specified without an left side number"); + + // If we got here, the operator type is a minus, and there is no left number currently, so add the minus to the number + operation.LeftSide += input[i]; + } + else + { + // If we get here, we have a left number and now an operator, so we want to move to the right side + + // Set the operation type + operation.OperationType = operatorType; + + // Move to the right side + leftSide = false; + } + } + } + } + + // If we are done parsing, and there were no exceptions + // calculate the current operation + return CalculateOperation(operation); + } + catch (Exception ex) + { + return $"Invalid equation. {ex.Message}"; + } + } + + /// + /// Calculates an and returns the result + /// + /// The operation to calculate + private string CalculateOperation(Operation operation) + { + // Store the number values of the string representations + decimal left = 0; + decimal right = 0; + + // Check if we have a valid left side number + if (string.IsNullOrEmpty(operation.LeftSide) || !decimal.TryParse(operation.LeftSide, out left)) + throw new InvalidOperationException($"Left side of the operation was not a number. {operation.LeftSide}"); + + // Check if we have a valid right side number + if (string.IsNullOrEmpty(operation.RightSide) || !decimal.TryParse(operation.RightSide, out right)) + throw new InvalidOperationException($"Right side of the operation was not a number. {operation.RightSide}"); + + try + { + switch (operation.OperationType) + { + case OperationType.Add: + return (left + right).ToString(); + case OperationType.Minus: + return (left - right).ToString(); + case OperationType.Divide: + return (left / right).ToString(); + case OperationType.Multiply: + return (left * right).ToString(); + default: + throw new InvalidOperationException($"Unknown operator type when calculating operation. { operation.OperationType }"); + } + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed to calculate operation {operation.LeftSide} {operation.OperationType} {operation.RightSide}. {ex.Message}"); + } + } + + /// + /// Accepts a character and returns the known + /// + /// The character to parse + /// + private OperationType GetOperationType(char character) + { + switch (character) + { + case '+': + return OperationType.Add; + case '-': + return OperationType.Minus; + case '/': + return OperationType.Divide; + case '*': + return OperationType.Multiply; + default: + throw new InvalidOperationException($"Unknown operator type { character }"); + } + } + + /// + /// Attempts to add a new character to the current number, checking for valid characters as it goes + /// + /// The current number string + /// The new character to append to the string + /// + private string AddNumberPart(string currentNumber, char newCharacter) + { + // Check if there is already a . in the number + if (newCharacter == '.' && currentNumber.Contains('.')) + throw new InvalidOperationException($"Number {currentNumber} already contains a . and another cannot be added"); + + return currentNumber + newCharacter; + } + + #region Private Helpers + + /// + /// Focuses the user input text + /// + private void FocusInputText() + { + this.UserInputText.Focus(); + } + + /// + /// Inserts the given text into the user input text box + /// + /// The value to insert + private void InsertTextValue(string value) + { + // Remember selection start + var selectionStart = this.UserInputText.SelectionStart; + + // Set new text + this.UserInputText.Text = this.UserInputText.Text.Insert(this.UserInputText.SelectionStart, value); + + // Restore the selection start + this.UserInputText.SelectionStart = selectionStart + value.Length; + + // Set selection length to zero + this.UserInputText.SelectionLength = 0; + } + + /// + /// Deletes the character to the right of the selection start of the user input text box + /// + private void DeleteTextValue() + { + // If we don't have a value to delete, return + if (this.UserInputText.Text.Length < this.UserInputText.SelectionStart + 1) + return; + + // Remember selection start + var selectionStart = this.UserInputText.SelectionStart; + + // Delete the character to the right of the selection + this.UserInputText.Text = this.UserInputText.Text.Remove(this.UserInputText.SelectionStart, 1); + + // Restore the selection start + this.UserInputText.SelectionStart = selectionStart; + + // Set selection length to zero + this.UserInputText.SelectionLength = 0; + } + + #endregion + } +} diff --git a/C# Beginners/BasicCalculator/Form1.resx b/C# Beginners/BasicCalculator/Form1.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/C# Beginners/BasicCalculator/Form1.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/C# Beginners/BasicCalculator/Operation.cs b/C# Beginners/BasicCalculator/Operation.cs new file mode 100644 index 0000000..f2c2252 --- /dev/null +++ b/C# Beginners/BasicCalculator/Operation.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BasicCalculator +{ + /// + /// Holds information about a single calculator operation + /// + public class Operation + { + #region Public Properties + + /// + /// The left side of the operation + /// + public string LeftSide { get; set; } + + /// + /// The right side of the operation + /// + public string RightSide { get; set; } + + /// + /// The type of operation to perform + /// + public OperationType OperationType { get; set; } + + /// + /// The inner operation to be performed initially before this operation + /// + public Operation InnerOperation { get; set; } + + #endregion + + #region Constructor + + /// + /// Default constructor + /// + public Operation() + { + // Create empty strings instead of having nulls + this.LeftSide = string.Empty; + this.RightSide = string.Empty; + } + + #endregion + } +} diff --git a/C# Beginners/BasicCalculator/OperationType.cs b/C# Beginners/BasicCalculator/OperationType.cs new file mode 100644 index 0000000..adc232c --- /dev/null +++ b/C# Beginners/BasicCalculator/OperationType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BasicCalculator +{ + /// + /// A type of operation the calculator can perform + /// + public enum OperationType + { + /// + /// Adds two values together + /// + Add, + + /// + /// Takes one value from another + /// + Minus, + + /// + /// Divides one number by another + /// + Divide, + + /// + /// Multiplies two numbers together + /// + Multiply + } +} diff --git a/C# Beginners/BasicCalculator/Program.cs b/C# Beginners/BasicCalculator/Program.cs new file mode 100644 index 0000000..d4692d4 --- /dev/null +++ b/C# Beginners/BasicCalculator/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace BasicCalculator +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new Form1()); + } + } +} diff --git a/C# Beginners/BasicCalculator/Properties/AssemblyInfo.cs b/C# Beginners/BasicCalculator/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..2f7dce6 --- /dev/null +++ b/C# Beginners/BasicCalculator/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BasicCalculator")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BasicCalculator")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("0fb8ff87-b543-44ce-be5c-9e6ee23ac755")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/C# Beginners/BasicCalculator/Properties/Resources.Designer.cs b/C# Beginners/BasicCalculator/Properties/Resources.Designer.cs new file mode 100644 index 0000000..fa7f566 --- /dev/null +++ b/C# Beginners/BasicCalculator/Properties/Resources.Designer.cs @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace BasicCalculator.Properties +{ + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("BasicCalculator.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/C# Beginners/BasicCalculator/Properties/Resources.resx b/C# Beginners/BasicCalculator/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/C# Beginners/BasicCalculator/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/C# Beginners/BasicCalculator/Properties/Settings.Designer.cs b/C# Beginners/BasicCalculator/Properties/Settings.Designer.cs new file mode 100644 index 0000000..b94b788 --- /dev/null +++ b/C# Beginners/BasicCalculator/Properties/Settings.Designer.cs @@ -0,0 +1,30 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace BasicCalculator.Properties +{ + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/C# Beginners/BasicCalculator/Properties/Settings.settings b/C# Beginners/BasicCalculator/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/C# Beginners/BasicCalculator/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/C# Beginners/BitwiseOperators/App.config b/C# Beginners/BitwiseOperators/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/C# Beginners/BitwiseOperators/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/C# Beginners/BitwiseOperators/BitwiseOperators.csproj b/C# Beginners/BitwiseOperators/BitwiseOperators.csproj new file mode 100644 index 0000000..74a2ee7 --- /dev/null +++ b/C# Beginners/BitwiseOperators/BitwiseOperators.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {77E04313-9F24-4906-94D7-E678564385D6} + Exe + Properties + BitwiseOperators + BitwiseOperators + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/C# Beginners/BitwiseOperators/BitwiseOperators.sln b/C# Beginners/BitwiseOperators/BitwiseOperators.sln new file mode 100644 index 0000000..f8e4454 --- /dev/null +++ b/C# Beginners/BitwiseOperators/BitwiseOperators.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BitwiseOperators", "BitwiseOperators.csproj", "{77E04313-9F24-4906-94D7-E678564385D6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {77E04313-9F24-4906-94D7-E678564385D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {77E04313-9F24-4906-94D7-E678564385D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {77E04313-9F24-4906-94D7-E678564385D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {77E04313-9F24-4906-94D7-E678564385D6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/C# Beginners/BitwiseOperators/Program.cs b/C# Beginners/BitwiseOperators/Program.cs new file mode 100644 index 0000000..60480f4 --- /dev/null +++ b/C# Beginners/BitwiseOperators/Program.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BitwiseOperators +{ + public enum SomeValues + { + Red = 1, + Blue = 2, + Green = 4, + Black = 8, + White = 16, + Orange = 32, + Yellow = 64, + Pink = 128, + } + + class Program + { + static void Main(string[] args) + { + // + // Binary + // + + // + // Bitwise operators + // + // And & (Both) + // Or | (Either) + // Xor ^ (Exclusive or, different) + // Not ~ (Invert) + // + + byte a = 122; + byte b = 7; + + byte result = (byte)(a & b); + + //Console.WriteLine($"{ Convert.ToString(a, 2).PadLeft(8, '0')} &"); + //Console.WriteLine($"{ Convert.ToString(b, 2).PadLeft(8, '0')}"); + //Console.WriteLine($"--------"); + //Console.WriteLine($"{ Convert.ToString(result, 2).PadLeft(8, '0')}"); + //Console.WriteLine(); + + // + // Bitwise Shifting + // + // Left << + // Right >> + // + + + //byte c = 25; + + //var cResult = (byte)(c << 4); + + //Console.WriteLine($"{ Convert.ToString(c, 2).PadLeft(8, '0')} << 1"); + //Console.WriteLine($"--------"); + //Console.WriteLine($"{ Convert.ToString(cResult, 2).PadLeft(8, '0')}"); + + + // Usage + // + // Toggling boolean + // Enum flags + // Masking + // + + // Invert booleans + var d = true; + d ^= true; + + // Enum flags + var someVals = (byte)(SomeValues.Blue); + Console.WriteLine($"{ Convert.ToString((byte)someVals, 2).PadLeft(8, '0')}"); + + if ((someVals & (byte)SomeValues.Blue) == (byte)SomeValues.Blue) + Console.WriteLine("Blue was included"); + if ((someVals & (byte)SomeValues.White) == (byte)SomeValues.White) + Console.WriteLine("White was included"); + + // Masking + // + // -----1- Input + // 0000010 < Important bit (the mask) + + // 0000010 + + var input = (byte)(SomeValues.White | SomeValues.Blue); + var mask = (byte)SomeValues.Blue; + var r = input & mask; + + + Console.ReadLine(); + } + } +} diff --git a/C# Beginners/BitwiseOperators/Properties/AssemblyInfo.cs b/C# Beginners/BitwiseOperators/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..84e1fa5 --- /dev/null +++ b/C# Beginners/BitwiseOperators/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("BitwiseOperators")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("BitwiseOperators")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("77e04313-9f24-4906-94d7-e678564385d6")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/C# Beginners/ConsoleApplication1/App.config b/C# Beginners/ConsoleApplication1/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/C# Beginners/ConsoleApplication1/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/C# Beginners/ConsoleApplication1/ConsoleApplication1.csproj b/C# Beginners/ConsoleApplication1/ConsoleApplication1.csproj new file mode 100644 index 0000000..b815a89 --- /dev/null +++ b/C# Beginners/ConsoleApplication1/ConsoleApplication1.csproj @@ -0,0 +1,61 @@ + + + + + Debug + AnyCPU + {E7E6E236-2FD2-4D03-AED0-B15841A080B5} + Exe + Properties + ConsoleApplication1 + ConsoleApplication1 + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/C# Beginners/ConsoleApplication1/ConsoleApplication1.sln b/C# Beginners/ConsoleApplication1/ConsoleApplication1.sln new file mode 100644 index 0000000..d8e3022 --- /dev/null +++ b/C# Beginners/ConsoleApplication1/ConsoleApplication1.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1.csproj", "{E7E6E236-2FD2-4D03-AED0-B15841A080B5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E7E6E236-2FD2-4D03-AED0-B15841A080B5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/C# Beginners/ConsoleApplication1/NumberGuesser.cs b/C# Beginners/ConsoleApplication1/NumberGuesser.cs new file mode 100644 index 0000000..04c3798 --- /dev/null +++ b/C# Beginners/ConsoleApplication1/NumberGuesser.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApplication1 +{ + /// + /// Asks the user to guess a number between a certain range and then guesses that number in the fewest possible guesses + /// + public class NumberGuesser + { + #region Public Properties + + /// + /// The largest number we ask the user to guess, between 0 and this number + /// + public int MaximumNumber { get; set; } + + /// + /// The current number of guesses the computer has had + /// + public int CurrentNumberOfGuesses { get; private set; } + + /// + /// The current known minimum number the user is thinking of + /// + public int CurrentGuessMinimum { get; private set; } + + /// + /// The current known maximum number the user is thinking of + /// + public int CurrentGuessMaximum { get; private set; } + + #endregion + + #region .ctor + + /// + /// Default constructor + /// + public NumberGuesser() + { + // Set default maximum number + this.MaximumNumber = 100; + } + + #endregion + + #region Public Methods + + /// + /// Asks the user to think of a number between 0 and the maximum number + /// + public void InformUser() + { + // Ask user to think of a number between 0 and MaximumNumber + Console.WriteLine($"I want you to think of a number between 0 and { this.MaximumNumber }. Ok?"); + Console.ReadLine(); + } + + /// + /// Asks the user a series of questions to discover the number they are thinking of + /// + public void DiscoverNumber() + { + // Clear variables to their initial values before a discovery + this.CurrentNumberOfGuesses = 0; + this.CurrentGuessMinimum = 0; + this.CurrentGuessMaximum = this.MaximumNumber / 2; + + // While the guess isn't the same as the known maximum value + while (this.CurrentGuessMinimum != this.CurrentGuessMaximum) + { + // Increase guess amount (by 1) + this.CurrentNumberOfGuesses++; + + // Ask the user if their number is between the guess range + Console.WriteLine($"Is your number between { this.CurrentGuessMinimum } and { this.CurrentGuessMaximum}?"); + string response = Console.ReadLine(); + + // If the user confirmed their number is within the current range... + if (response?.ToLower().FirstOrDefault() == 'y') + { + // We know the number is between guessFrom and guessTo + // So set the new maximum number + this.MaximumNumber = this.CurrentGuessMaximum; + + // Change the next guess range to be half of the new maximum range + this.CurrentGuessMaximum = this.CurrentGuessMaximum - (this.CurrentGuessMaximum - this.CurrentGuessMinimum) / 2; + } + // The number is greater than guessMax and less than or equal to max + else + { + // The new minimum is one above the old maximum + this.CurrentGuessMinimum = this.CurrentGuessMaximum + 1; + + // Guess the bottom half of the new range + int remainingDifference = this.MaximumNumber - this.CurrentGuessMaximum; + + // Set the guess max to half way between the guessMin and max + // NOTE: Math.Ceiling will round up the remaining difference to 2, if the difference is 3 + this.CurrentGuessMaximum += (int)Math.Ceiling(remainingDifference / 2f); + } + + // If we only have 2 numbers left, guess one of them + if (this.CurrentGuessMinimum + 1 == this.MaximumNumber) + { + // Increase guess amount (by 1) + this.CurrentNumberOfGuesses++; + + // Ask the user if their number is the lower number + Console.WriteLine($"Is your number { this.CurrentGuessMinimum }?"); + response = Console.ReadLine(); + + // If the user confirmed their number is the lower number... + if (response?.ToLower().FirstOrDefault() == 'y') + { + break; + } + else + { + // That means the number must be the higher of the two + this.CurrentGuessMinimum = this.MaximumNumber; + break; + } + } + } + } + + /// + /// Annouces the number the user was thinking of and the number of guesses it took + /// + public void AnnounceResults() + { + // Tell the user their number + Console.WriteLine($"** Your number is { this.CurrentGuessMinimum } **"); + + // Let the user know how many guesses it took + Console.WriteLine($"Guessed in { this.CurrentNumberOfGuesses } guesses"); + + Console.ReadLine(); + } + + #endregion + } +} diff --git a/C# Beginners/ConsoleApplication1/Program.cs b/C# Beginners/ConsoleApplication1/Program.cs new file mode 100644 index 0000000..48d2db2 --- /dev/null +++ b/C# Beginners/ConsoleApplication1/Program.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApplication1 +{ + class Program + { + static void Main(string[] args) + { + // Create a new instance of our number guesser class + var numberGuesser = new NumberGuesser(); + + // Change the default maximum number to 200 + numberGuesser.MaximumNumber = 200; + + // Ask the user to think of a number + numberGuesser.InformUser(); + + // Discover the number the user is thinking of + numberGuesser.DiscoverNumber(); + + // Announce the results + numberGuesser.AnnounceResults(); + } + } +} diff --git a/C# Beginners/ConsoleApplication1/Properties/AssemblyInfo.cs b/C# Beginners/ConsoleApplication1/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a27f870 --- /dev/null +++ b/C# Beginners/ConsoleApplication1/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ConsoleApplication1")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ConsoleApplication1")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e7e6e236-2fd2-4d03-aed0-b15841a080b5")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/C# Beginners/README.md b/C# Beginners/README.md new file mode 100644 index 0000000..49a00d5 --- /dev/null +++ b/C# Beginners/README.md @@ -0,0 +1,6 @@ +# C# Beginners Tutorials +The files from the C# Beginners tutorials on the AngelSix YouTube channel + +http://www.angelsix.com/youtube + + diff --git a/C# Beginners/TTT/App.config b/C# Beginners/TTT/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/C# Beginners/TTT/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/C# Beginners/TTT/App.xaml b/C# Beginners/TTT/App.xaml new file mode 100644 index 0000000..7d80047 --- /dev/null +++ b/C# Beginners/TTT/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/C# Beginners/TTT/App.xaml.cs b/C# Beginners/TTT/App.xaml.cs new file mode 100644 index 0000000..2d14e85 --- /dev/null +++ b/C# Beginners/TTT/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace WpfApplication1 +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/C# Beginners/TTT/MainWindow.xaml b/C# Beginners/TTT/MainWindow.xaml new file mode 100644 index 0000000..ce18bd5 --- /dev/null +++ b/C# Beginners/TTT/MainWindow.xaml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + +