diff --git a/App.config b/App.config new file mode 100644 index 00000000..731f6de6 --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CPE200Lab1.csproj b/CPE200Lab1.csproj new file mode 100644 index 00000000..656f6a26 --- /dev/null +++ b/CPE200Lab1.csproj @@ -0,0 +1,94 @@ + + + + + Debug + AnyCPU + {C2B44135-F1AC-48A9-BD70-3BE2C38C2A15} + WinExe + CPE200Lab1 + CPE200Lab1 + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + Form + + + ExtendForm.cs + + + Form + + + MainForm.cs + + + + + + ExtendForm.cs + + + MainForm.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/CPE200Lab1.sln b/CPE200Lab1.sln new file mode 100644 index 00000000..b1434dc4 --- /dev/null +++ b/CPE200Lab1.sln @@ -0,0 +1,40 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26730.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPE200Lab1", "CPE200Lab1\CPE200Lab1.csproj", "{C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPE200Lab1Tests", "CPE200Lab1Tests\CPE200Lab1Tests.csproj", "{C92330F4-DC76-46B0-970C-73126DE55500}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CPE200Lab1Test", "CPE200Lab1Test\CPE200Lab1Test.csproj", "{0E964631-C30F-4089-ADD6-D2F374C1E02D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C2B44135-F1AC-48A9-BD70-3BE2C38C2A15}.Release|Any CPU.Build.0 = Release|Any CPU + + {C92330F4-DC76-46B0-970C-73126DE55500}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C92330F4-DC76-46B0-970C-73126DE55500}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C92330F4-DC76-46B0-970C-73126DE55500}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C92330F4-DC76-46B0-970C-73126DE55500}.Release|Any CPU.Build.0 = Release|Any CPU + + {0E964631-C30F-4089-ADD6-D2F374C1E02D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0E964631-C30F-4089-ADD6-D2F374C1E02D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0E964631-C30F-4089-ADD6-D2F374C1E02D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0E964631-C30F-4089-ADD6-D2F374C1E02D}.Release|Any CPU.Build.0 = Release|Any CPU + + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6158DCB9-3A90-418E-912B-34F8621B5B33} + EndGlobalSection +EndGlobal diff --git a/CPE200Lab1Test.csproj b/CPE200Lab1Test.csproj new file mode 100644 index 00000000..02d58672 --- /dev/null +++ b/CPE200Lab1Test.csproj @@ -0,0 +1,110 @@ + + + + + + + + + Debug + AnyCPU + {0E964631-C30F-4089-ADD6-D2F374C1E02D} + + Library + Properties + CPE200Lab1Test + CPE200Lab1Test + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + + + + + {C2B44135-F1AC-48A9-BD70-3BE2C38C2A15} + + CPE200Lab1 + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + + \ No newline at end of file diff --git a/CPE200Lab1Tests.csproj b/CPE200Lab1Tests.csproj new file mode 100644 index 00000000..8323f56c --- /dev/null +++ b/CPE200Lab1Tests.csproj @@ -0,0 +1,106 @@ + + + + + Debug + AnyCPU + {C92330F4-DC76-46B0-970C-73126DE55500} + Library + Properties + CPE200Lab1Tests + CPE200Lab1Tests + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.1.18\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.1.18\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + + + + + + + {C2B44135-F1AC-48A9-BD70-3BE2C38C2A15} + CPE200Lab1 + + + + + + + False + + + False + + + False + + + False + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + \ No newline at end of file diff --git a/CalculatorEngine.cs b/CalculatorEngine.cs new file mode 100644 index 00000000..32a646c8 --- /dev/null +++ b/CalculatorEngine.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + class CalculatorEngine : SimpleCalculatorEngine + { + protected double firstOperand; + protected double secondOperand; + + public void setFirstOperand(string num) + { + firstOperand = Convert.ToDouble(num); + } + + public void setSecondOperand(string num) + { + secondOperand = Convert.ToDouble(num); + } + + public string calculate(string oper) + { + string[] parts = oper.Split(' '); + if (isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2])) + { + return calculate(parts[1], parts[0], parts[2], 4); + } + else + { + return "E"; + } + } + + + + + } +} diff --git a/CalculatorEngineTests.cs b/CalculatorEngineTests.cs new file mode 100644 index 00000000..c998b919 --- /dev/null +++ b/CalculatorEngineTests.cs @@ -0,0 +1,247 @@ +using CPE200Lab1; +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace CPE200Lab1.Tests +{ + [TestClass()] + public class CalculatorEngineTest + { + // + // Test process() + // + [TestMethod()] + public void Process_BasicPlus_Test() + { + string input = "1 + 2"; + string expected = "3"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.Process(input); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Process_BasicMinus_Test() + { + string input = "5 - 3"; + string expected = "2"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.Process(input); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Process_BasicMultiply_Test() + { + string input = "5 X 3"; + string expected = "15"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.Process(input); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Process_BasicDivide_Test() + { + string input = "6 ÷ 3"; + string expected = "2"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.Process(input); + Assert.AreEqual(expected, actual); + } + + [TestMethod()] + public void Process_ErrorDivideByZero_Test() + { + string input = "6 ÷ 0"; + string expected = "E"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.Process(input); + Assert.AreEqual(expected, actual); + } + + [TestMethod()] + public void Process_ErrorMalformate_Test() + { + string input = "1+2"; + string expected = "E"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.Process(input); + Assert.AreEqual(expected, actual); + } + + + // + // Test calculate() + // + [TestMethod()] + public void Calculate_BasicPlus_Test() + { + string firstOperand = "1"; + string secondOperand = "2"; + string expected = "3"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("+", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_BasicNegativePlus_Test() + { + string firstOperand = "-1"; + string secondOperand = "2"; + string expected = "1"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("+", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_BasicMinus_Test() + { + string firstOperand = "2"; + string secondOperand = "1"; + string expected = "1"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("-", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_BasicNegativeMinus_Test() + { + string firstOperand = "2"; + string secondOperand = "-1"; + string expected = "3"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("-", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + public void Calculate_BasicMinusNegative_Test() + { + string firstOperand = "1"; + string secondOperand = "2"; + string expected = "-1"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("-", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_BasicMultiply_Test() + { + string firstOperand = "2"; + string secondOperand = "2"; + string expected = "4"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("X", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_BasicNegativeMultiply_Test() + { + string firstOperand = "-2"; + string secondOperand = "2"; + string expected = "-4"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("X", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_BasicDivide_Test() + { + string firstOperand = "4"; + string secondOperand = "2"; + string expected = "2"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("÷", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_BasicNegativeDivide_Test() + { + string firstOperand = "-4"; + string secondOperand = "2"; + string expected = "-2"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("÷", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + [TestMethod()] + public void Calculate_DivideByZero_Test() + { + string firstOperand = "4"; + string secondOperand = "0"; + string expected = "E"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.calculate("÷", firstOperand, secondOperand); + Assert.AreEqual(expected, actual); + } + + + // + // Test Unary() + // + [TestMethod()] + public void UnaryCalculate_BasicSquareRoot_Test() + { + string operand = "4"; + string expected = "2"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.unaryCalculate("√", operand); + Assert.AreEqual(expected, actual); + } + + [TestMethod()] + public void UnaryCalculate_BasicOneOverX_Test() + { + string operand = "4"; + string expected = "0.25"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.unaryCalculate("1/x", operand); + Assert.AreEqual(expected, actual); + } + + [TestMethod()] + public void UnaryCalculate_ErrorOneOverX_Test() + { + string operand = "0"; + string expected = "E"; + string actual; + + SimpleCalculatorEngine engine = new SimpleCalculatorEngine(); + actual = engine.unaryCalculate("1/x", operand); + Assert.AreEqual(expected, actual); + } + } +} + diff --git a/ExtendForm.Designer.cs b/ExtendForm.Designer.cs new file mode 100644 index 00000000..fde1d13e --- /dev/null +++ b/ExtendForm.Designer.cs @@ -0,0 +1,344 @@ +namespace CPE200Lab1 +{ + partial class ExtendForm + { + /// + /// 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.btnBack = new System.Windows.Forms.Button(); + this.btnClear = new System.Windows.Forms.Button(); + this.btnDivide = new System.Windows.Forms.Button(); + this.btnEqual = new System.Windows.Forms.Button(); + this.btnDot = new System.Windows.Forms.Button(); + this.btn0 = new System.Windows.Forms.Button(); + this.btnSign = new System.Windows.Forms.Button(); + this.btnPlus = new System.Windows.Forms.Button(); + this.btn3 = new System.Windows.Forms.Button(); + this.btn2 = new System.Windows.Forms.Button(); + this.btn1 = new System.Windows.Forms.Button(); + this.btnMinus = new System.Windows.Forms.Button(); + this.btn6 = new System.Windows.Forms.Button(); + this.btn5 = new System.Windows.Forms.Button(); + this.btn4 = new System.Windows.Forms.Button(); + this.btnMultiply = new System.Windows.Forms.Button(); + this.btn9 = new System.Windows.Forms.Button(); + this.btn8 = new System.Windows.Forms.Button(); + this.btn7 = new System.Windows.Forms.Button(); + this.lblDisplay = new System.Windows.Forms.Label(); + this.btnSpace = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // btnBack + // + this.btnBack.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnBack.Location = new System.Drawing.Point(294, 105); + this.btnBack.Name = "btnBack"; + this.btnBack.Size = new System.Drawing.Size(88, 64); + this.btnBack.TabIndex = 39; + this.btnBack.Text = "<"; + this.btnBack.UseVisualStyleBackColor = true; + this.btnBack.Click += new System.EventHandler(this.btnBack_Click); + // + // btnClear + // + this.btnClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnClear.Location = new System.Drawing.Point(294, 175); + this.btnClear.Name = "btnClear"; + this.btnClear.Size = new System.Drawing.Size(88, 64); + this.btnClear.TabIndex = 38; + this.btnClear.Text = "C"; + this.btnClear.UseVisualStyleBackColor = true; + this.btnClear.Click += new System.EventHandler(this.btnClear_Click); + // + // btnDivide + // + this.btnDivide.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnDivide.Location = new System.Drawing.Point(388, 105); + this.btnDivide.Name = "btnDivide"; + this.btnDivide.Size = new System.Drawing.Size(88, 64); + this.btnDivide.TabIndex = 36; + this.btnDivide.Text = "÷"; + this.btnDivide.UseVisualStyleBackColor = true; + this.btnDivide.Click += new System.EventHandler(this.btnBinaryOperator_Click); + // + // btnEqual + // + this.btnEqual.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnEqual.Location = new System.Drawing.Point(294, 245); + this.btnEqual.Name = "btnEqual"; + this.btnEqual.Size = new System.Drawing.Size(88, 64); + this.btnEqual.TabIndex = 35; + this.btnEqual.Text = "="; + this.btnEqual.UseVisualStyleBackColor = true; + this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click); + // + // btnDot + // + this.btnDot.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnDot.Location = new System.Drawing.Point(200, 315); + this.btnDot.Name = "btnDot"; + this.btnDot.Size = new System.Drawing.Size(88, 64); + this.btnDot.TabIndex = 34; + this.btnDot.Text = "."; + this.btnDot.UseVisualStyleBackColor = true; + this.btnDot.Click += new System.EventHandler(this.btnDot_Click); + // + // btn0 + // + this.btn0.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn0.Location = new System.Drawing.Point(106, 315); + this.btn0.Name = "btn0"; + this.btn0.Size = new System.Drawing.Size(88, 64); + this.btn0.TabIndex = 33; + this.btn0.Text = "0"; + this.btn0.UseVisualStyleBackColor = true; + this.btn0.Click += new System.EventHandler(this.btnNumber_Click); + // + // btnSign + // + this.btnSign.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnSign.Location = new System.Drawing.Point(12, 315); + this.btnSign.Name = "btnSign"; + this.btnSign.Size = new System.Drawing.Size(88, 64); + this.btnSign.TabIndex = 32; + this.btnSign.Text = "±"; + this.btnSign.UseVisualStyleBackColor = true; + this.btnSign.Click += new System.EventHandler(this.btnSign_Click); + // + // btnPlus + // + this.btnPlus.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnPlus.Location = new System.Drawing.Point(388, 315); + this.btnPlus.Name = "btnPlus"; + this.btnPlus.Size = new System.Drawing.Size(88, 64); + this.btnPlus.TabIndex = 31; + this.btnPlus.Text = "+"; + this.btnPlus.UseVisualStyleBackColor = true; + this.btnPlus.Click += new System.EventHandler(this.btnBinaryOperator_Click); + // + // btn3 + // + this.btn3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn3.Location = new System.Drawing.Point(200, 245); + this.btn3.Name = "btn3"; + this.btn3.Size = new System.Drawing.Size(88, 64); + this.btn3.TabIndex = 30; + this.btn3.Text = "3"; + this.btn3.UseVisualStyleBackColor = true; + this.btn3.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn2 + // + this.btn2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn2.Location = new System.Drawing.Point(106, 245); + this.btn2.Name = "btn2"; + this.btn2.Size = new System.Drawing.Size(88, 64); + this.btn2.TabIndex = 29; + this.btn2.Text = "2"; + this.btn2.UseVisualStyleBackColor = true; + this.btn2.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn1 + // + this.btn1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn1.Location = new System.Drawing.Point(12, 245); + this.btn1.Name = "btn1"; + this.btn1.Size = new System.Drawing.Size(88, 64); + this.btn1.TabIndex = 28; + this.btn1.Text = "1"; + this.btn1.UseVisualStyleBackColor = true; + this.btn1.Click += new System.EventHandler(this.btnNumber_Click); + // + // btnMinus + // + this.btnMinus.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMinus.Location = new System.Drawing.Point(388, 245); + this.btnMinus.Name = "btnMinus"; + this.btnMinus.Size = new System.Drawing.Size(88, 64); + this.btnMinus.TabIndex = 27; + this.btnMinus.Text = "-"; + this.btnMinus.UseVisualStyleBackColor = true; + this.btnMinus.Click += new System.EventHandler(this.btnBinaryOperator_Click); + // + // btn6 + // + this.btn6.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn6.Location = new System.Drawing.Point(200, 175); + this.btn6.Name = "btn6"; + this.btn6.Size = new System.Drawing.Size(88, 64); + this.btn6.TabIndex = 26; + this.btn6.Text = "6"; + this.btn6.UseVisualStyleBackColor = true; + this.btn6.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn5 + // + this.btn5.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn5.Location = new System.Drawing.Point(106, 175); + this.btn5.Name = "btn5"; + this.btn5.Size = new System.Drawing.Size(88, 64); + this.btn5.TabIndex = 25; + this.btn5.Text = "5"; + this.btn5.UseVisualStyleBackColor = true; + this.btn5.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn4 + // + this.btn4.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn4.Location = new System.Drawing.Point(12, 175); + this.btn4.Name = "btn4"; + this.btn4.Size = new System.Drawing.Size(88, 64); + this.btn4.TabIndex = 24; + this.btn4.Text = "4"; + this.btn4.UseVisualStyleBackColor = true; + this.btn4.Click += new System.EventHandler(this.btnNumber_Click); + // + // btnMultiply + // + this.btnMultiply.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMultiply.Location = new System.Drawing.Point(388, 175); + this.btnMultiply.Name = "btnMultiply"; + this.btnMultiply.Size = new System.Drawing.Size(88, 64); + this.btnMultiply.TabIndex = 23; + this.btnMultiply.Text = "X"; + this.btnMultiply.UseVisualStyleBackColor = true; + this.btnMultiply.Click += new System.EventHandler(this.btnBinaryOperator_Click); + // + // btn9 + // + this.btn9.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn9.Location = new System.Drawing.Point(200, 105); + this.btn9.Name = "btn9"; + this.btn9.Size = new System.Drawing.Size(88, 64); + this.btn9.TabIndex = 22; + this.btn9.Text = "9"; + this.btn9.UseVisualStyleBackColor = true; + this.btn9.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn8 + // + this.btn8.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn8.Location = new System.Drawing.Point(106, 105); + this.btn8.Name = "btn8"; + this.btn8.Size = new System.Drawing.Size(88, 64); + this.btn8.TabIndex = 21; + this.btn8.Text = "8"; + this.btn8.UseVisualStyleBackColor = true; + this.btn8.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn7 + // + this.btn7.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn7.Location = new System.Drawing.Point(12, 105); + this.btn7.Name = "btn7"; + this.btn7.Size = new System.Drawing.Size(88, 64); + this.btn7.TabIndex = 20; + this.btn7.Text = "7"; + this.btn7.UseVisualStyleBackColor = true; + this.btn7.Click += new System.EventHandler(this.btnNumber_Click); + // + // lblDisplay + // + this.lblDisplay.AutoSize = true; + this.lblDisplay.BackColor = System.Drawing.SystemColors.ControlLightLight; + this.lblDisplay.Font = new System.Drawing.Font("Microsoft Sans Serif", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblDisplay.Location = new System.Drawing.Point(12, 9); + this.lblDisplay.MinimumSize = new System.Drawing.Size(450, 0); + this.lblDisplay.Name = "lblDisplay"; + this.lblDisplay.Size = new System.Drawing.Size(450, 55); + this.lblDisplay.TabIndex = 40; + this.lblDisplay.Text = "0"; + this.lblDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // btnSpace + // + this.btnSpace.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnSpace.Location = new System.Drawing.Point(294, 315); + this.btnSpace.Name = "btnSpace"; + this.btnSpace.Size = new System.Drawing.Size(88, 64); + this.btnSpace.TabIndex = 41; + this.btnSpace.Text = "Space"; + this.btnSpace.UseVisualStyleBackColor = true; + this.btnSpace.Click += new System.EventHandler(this.btnSpace_Click); + // + // ExtendForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(482, 386); + this.Controls.Add(this.btnSpace); + this.Controls.Add(this.lblDisplay); + this.Controls.Add(this.btnBack); + this.Controls.Add(this.btnClear); + this.Controls.Add(this.btnDivide); + this.Controls.Add(this.btnEqual); + this.Controls.Add(this.btnDot); + this.Controls.Add(this.btn0); + this.Controls.Add(this.btnSign); + this.Controls.Add(this.btnPlus); + this.Controls.Add(this.btn3); + this.Controls.Add(this.btn2); + this.Controls.Add(this.btn1); + this.Controls.Add(this.btnMinus); + this.Controls.Add(this.btn6); + this.Controls.Add(this.btn5); + this.Controls.Add(this.btn4); + this.Controls.Add(this.btnMultiply); + this.Controls.Add(this.btn9); + this.Controls.Add(this.btn8); + this.Controls.Add(this.btn7); + this.Name = "ExtendForm"; + this.Text = "ExtendForm"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button btnBack; + private System.Windows.Forms.Button btnClear; + private System.Windows.Forms.Button btnDivide; + private System.Windows.Forms.Button btnEqual; + private System.Windows.Forms.Button btnDot; + private System.Windows.Forms.Button btn0; + private System.Windows.Forms.Button btnSign; + private System.Windows.Forms.Button btnPlus; + private System.Windows.Forms.Button btn3; + private System.Windows.Forms.Button btn2; + private System.Windows.Forms.Button btn1; + private System.Windows.Forms.Button btnMinus; + private System.Windows.Forms.Button btn6; + private System.Windows.Forms.Button btn5; + private System.Windows.Forms.Button btn4; + private System.Windows.Forms.Button btnMultiply; + private System.Windows.Forms.Button btn9; + private System.Windows.Forms.Button btn8; + private System.Windows.Forms.Button btn7; + private System.Windows.Forms.Label lblDisplay; + private System.Windows.Forms.Button btnSpace; + } +} \ No newline at end of file diff --git a/ExtendForm.cs b/ExtendForm.cs new file mode 100644 index 00000000..664da6a7 --- /dev/null +++ b/ExtendForm.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace CPE200Lab1 +{ + public partial class ExtendForm : Form + { + private bool isNumberPart = false; + private bool isContainDot = false; + private bool isSpaceAllowed = false; + private RPNCalculatorEngine engine; + + public ExtendForm() + { + InitializeComponent(); + engine = new RPNCalculatorEngine(); + } + + private bool isOperator(char ch) + { + switch(ch) { + case '+': + case '-': + case 'X': + case '÷': + return true; + } + return false; + } + + private void btnNumber_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (lblDisplay.Text is "0") + { + lblDisplay.Text = ""; + } + if (!isNumberPart) + { + isNumberPart = true; + isContainDot = false; + } + lblDisplay.Text += ((Button)sender).Text; + isSpaceAllowed = true; + } + + private void btnBinaryOperator_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + isNumberPart = false; + isContainDot = false; + string current = lblDisplay.Text; + if (current[current.Length - 1] != ' ' || isOperator(current[current.Length - 2])) + { + lblDisplay.Text += " " + ((Button)sender).Text + " "; + isSpaceAllowed = false; + } + } + + private void btnBack_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + // check if the last one is operator + string current = lblDisplay.Text; + if (current[current.Length - 1] is ' ' && current.Length > 2 && isOperator(current[current.Length - 2])) + { + lblDisplay.Text = current.Substring(0, current.Length - 3); + } else + { + lblDisplay.Text = current.Substring(0, current.Length - 1); + } + if (lblDisplay.Text is "") + { + lblDisplay.Text = "0"; + } + } + + private void btnClear_Click(object sender, EventArgs e) + { + lblDisplay.Text = "0"; + isContainDot = false; + isNumberPart = false; + isSpaceAllowed = false; + } + + private void btnEqual_Click(object sender, EventArgs e) + { + string result = engine.Process(lblDisplay.Text); + if (result is "E") + { + lblDisplay.Text = "Error"; + } else + { + lblDisplay.Text = result; + isSpaceAllowed = true; + isContainDot = false; + isNumberPart = true; + } + } + + private void btnSign_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (isNumberPart) + { + return; + } + string current = lblDisplay.Text; + if (current is "0") + { + lblDisplay.Text = "-"; + } else if (current[current.Length - 1] is '-') + { + lblDisplay.Text = current.Substring(0, current.Length - 1); + if (lblDisplay.Text is "") + { + lblDisplay.Text = "0"; + } + } else + { + lblDisplay.Text = current + "-"; + } + isSpaceAllowed = false; + } + + private void btnDot_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if(!isContainDot) + { + isContainDot = true; + lblDisplay.Text += "."; + isSpaceAllowed = false; + } + } + + private void btnSpace_Click(object sender, EventArgs e) + { + if(lblDisplay.Text is "Error") + { + return; + } + if(isSpaceAllowed) + { + lblDisplay.Text += " "; + isSpaceAllowed = false; + } + } + } +} diff --git a/ExtendForm.resx b/ExtendForm.resx new file mode 100644 index 00000000..1af7de15 --- /dev/null +++ b/ExtendForm.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/MainForm.Designer.cs b/MainForm.Designer.cs new file mode 100644 index 00000000..62f086ee --- /dev/null +++ b/MainForm.Designer.cs @@ -0,0 +1,429 @@ +namespace CPE200Lab1 +{ + partial class MainForm + { + /// + /// 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.btn7 = new System.Windows.Forms.Button(); + this.btn8 = new System.Windows.Forms.Button(); + this.btn9 = new System.Windows.Forms.Button(); + this.btnMultiply = new System.Windows.Forms.Button(); + this.btnMinus = new System.Windows.Forms.Button(); + this.btn6 = new System.Windows.Forms.Button(); + this.btn5 = new System.Windows.Forms.Button(); + this.btn4 = new System.Windows.Forms.Button(); + this.btnPlus = new System.Windows.Forms.Button(); + this.btn3 = new System.Windows.Forms.Button(); + this.btn2 = new System.Windows.Forms.Button(); + this.btn1 = new System.Windows.Forms.Button(); + this.btnEqual = new System.Windows.Forms.Button(); + this.btnDot = new System.Windows.Forms.Button(); + this.btn0 = new System.Windows.Forms.Button(); + this.btnSign = new System.Windows.Forms.Button(); + this.btnDivide = new System.Windows.Forms.Button(); + this.btnPercent = new System.Windows.Forms.Button(); + this.btnClear = new System.Windows.Forms.Button(); + this.btnBack = new System.Windows.Forms.Button(); + this.lblDisplay = new System.Windows.Forms.Label(); + this.btnMC = new System.Windows.Forms.Button(); + this.btnMR = new System.Windows.Forms.Button(); + this.btnMP = new System.Windows.Forms.Button(); + this.btnMM = new System.Windows.Forms.Button(); + this.btnSqr = new System.Windows.Forms.Button(); + this.btnOox = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // btn7 + // + this.btn7.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn7.Location = new System.Drawing.Point(12, 377); + this.btn7.Name = "btn7"; + this.btn7.Size = new System.Drawing.Size(88, 64); + this.btn7.TabIndex = 0; + this.btn7.Text = "7"; + this.btn7.UseVisualStyleBackColor = true; + this.btn7.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn8 + // + this.btn8.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn8.Location = new System.Drawing.Point(106, 377); + this.btn8.Name = "btn8"; + this.btn8.Size = new System.Drawing.Size(88, 64); + this.btn8.TabIndex = 1; + this.btn8.Text = "8"; + this.btn8.UseVisualStyleBackColor = true; + this.btn8.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn9 + // + this.btn9.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn9.Location = new System.Drawing.Point(200, 377); + this.btn9.Name = "btn9"; + this.btn9.Size = new System.Drawing.Size(88, 64); + this.btn9.TabIndex = 2; + this.btn9.Text = "9"; + this.btn9.UseVisualStyleBackColor = true; + this.btn9.Click += new System.EventHandler(this.btnNumber_Click); + // + // btnMultiply + // + this.btnMultiply.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMultiply.Location = new System.Drawing.Point(294, 377); + this.btnMultiply.Name = "btnMultiply"; + this.btnMultiply.Size = new System.Drawing.Size(88, 64); + this.btnMultiply.TabIndex = 3; + this.btnMultiply.Text = "X"; + this.btnMultiply.UseVisualStyleBackColor = true; + this.btnMultiply.Click += new System.EventHandler(this.btnOperator_Click); + // + // btnMinus + // + this.btnMinus.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMinus.Location = new System.Drawing.Point(294, 447); + this.btnMinus.Name = "btnMinus"; + this.btnMinus.Size = new System.Drawing.Size(88, 64); + this.btnMinus.TabIndex = 7; + this.btnMinus.Text = "-"; + this.btnMinus.UseVisualStyleBackColor = true; + this.btnMinus.Click += new System.EventHandler(this.btnOperator_Click); + // + // btn6 + // + this.btn6.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn6.Location = new System.Drawing.Point(200, 447); + this.btn6.Name = "btn6"; + this.btn6.Size = new System.Drawing.Size(88, 64); + this.btn6.TabIndex = 6; + this.btn6.Text = "6"; + this.btn6.UseVisualStyleBackColor = true; + this.btn6.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn5 + // + this.btn5.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn5.Location = new System.Drawing.Point(106, 447); + this.btn5.Name = "btn5"; + this.btn5.Size = new System.Drawing.Size(88, 64); + this.btn5.TabIndex = 5; + this.btn5.Text = "5"; + this.btn5.UseVisualStyleBackColor = true; + this.btn5.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn4 + // + this.btn4.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn4.Location = new System.Drawing.Point(12, 447); + this.btn4.Name = "btn4"; + this.btn4.Size = new System.Drawing.Size(88, 64); + this.btn4.TabIndex = 4; + this.btn4.Text = "4"; + this.btn4.UseVisualStyleBackColor = true; + this.btn4.Click += new System.EventHandler(this.btnNumber_Click); + // + // btnPlus + // + this.btnPlus.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnPlus.Location = new System.Drawing.Point(294, 517); + this.btnPlus.Name = "btnPlus"; + this.btnPlus.Size = new System.Drawing.Size(88, 64); + this.btnPlus.TabIndex = 11; + this.btnPlus.Text = "+"; + this.btnPlus.UseVisualStyleBackColor = true; + this.btnPlus.Click += new System.EventHandler(this.btnOperator_Click); + // + // btn3 + // + this.btn3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn3.Location = new System.Drawing.Point(200, 517); + this.btn3.Name = "btn3"; + this.btn3.Size = new System.Drawing.Size(88, 64); + this.btn3.TabIndex = 10; + this.btn3.Text = "3"; + this.btn3.UseVisualStyleBackColor = true; + this.btn3.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn2 + // + this.btn2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn2.Location = new System.Drawing.Point(106, 517); + this.btn2.Name = "btn2"; + this.btn2.Size = new System.Drawing.Size(88, 64); + this.btn2.TabIndex = 9; + this.btn2.Text = "2"; + this.btn2.UseVisualStyleBackColor = true; + this.btn2.Click += new System.EventHandler(this.btnNumber_Click); + // + // btn1 + // + this.btn1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn1.Location = new System.Drawing.Point(12, 517); + this.btn1.Name = "btn1"; + this.btn1.Size = new System.Drawing.Size(88, 64); + this.btn1.TabIndex = 8; + this.btn1.Text = "1"; + this.btn1.UseVisualStyleBackColor = true; + this.btn1.Click += new System.EventHandler(this.btnNumber_Click); + // + // btnEqual + // + this.btnEqual.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnEqual.Location = new System.Drawing.Point(294, 587); + this.btnEqual.Name = "btnEqual"; + this.btnEqual.Size = new System.Drawing.Size(88, 64); + this.btnEqual.TabIndex = 15; + this.btnEqual.Text = "="; + this.btnEqual.UseVisualStyleBackColor = true; + this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click); + // + // btnDot + // + this.btnDot.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnDot.Location = new System.Drawing.Point(200, 587); + this.btnDot.Name = "btnDot"; + this.btnDot.Size = new System.Drawing.Size(88, 64); + this.btnDot.TabIndex = 14; + this.btnDot.Text = "."; + this.btnDot.UseVisualStyleBackColor = true; + this.btnDot.Click += new System.EventHandler(this.btnDot_Click); + // + // btn0 + // + this.btn0.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btn0.Location = new System.Drawing.Point(106, 587); + this.btn0.Name = "btn0"; + this.btn0.Size = new System.Drawing.Size(88, 64); + this.btn0.TabIndex = 13; + this.btn0.Text = "0"; + this.btn0.UseVisualStyleBackColor = true; + this.btn0.Click += new System.EventHandler(this.btnNumber_Click); + // + // btnSign + // + this.btnSign.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnSign.Location = new System.Drawing.Point(12, 587); + this.btnSign.Name = "btnSign"; + this.btnSign.Size = new System.Drawing.Size(88, 64); + this.btnSign.TabIndex = 12; + this.btnSign.Text = "±"; + this.btnSign.UseVisualStyleBackColor = true; + this.btnSign.Click += new System.EventHandler(this.btnSign_Click); + // + // btnDivide + // + this.btnDivide.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnDivide.Location = new System.Drawing.Point(294, 307); + this.btnDivide.Name = "btnDivide"; + this.btnDivide.Size = new System.Drawing.Size(88, 64); + this.btnDivide.TabIndex = 16; + this.btnDivide.Text = "÷"; + this.btnDivide.UseVisualStyleBackColor = true; + this.btnDivide.Click += new System.EventHandler(this.btnOperator_Click); + // + // btnPercent + // + this.btnPercent.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnPercent.Location = new System.Drawing.Point(200, 307); + this.btnPercent.Name = "btnPercent"; + this.btnPercent.Size = new System.Drawing.Size(88, 64); + this.btnPercent.TabIndex = 17; + this.btnPercent.Text = "%"; + this.btnPercent.UseVisualStyleBackColor = true; + this.btnPercent.Click += new System.EventHandler(this.btnOperator_Click); + // + // btnClear + // + this.btnClear.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnClear.Location = new System.Drawing.Point(12, 237); + this.btnClear.Name = "btnClear"; + this.btnClear.Size = new System.Drawing.Size(88, 64); + this.btnClear.TabIndex = 18; + this.btnClear.Text = "C"; + this.btnClear.UseVisualStyleBackColor = true; + this.btnClear.Click += new System.EventHandler(this.btnClear_Click); + // + // btnBack + // + this.btnBack.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnBack.Location = new System.Drawing.Point(106, 237); + this.btnBack.Name = "btnBack"; + this.btnBack.Size = new System.Drawing.Size(88, 64); + this.btnBack.TabIndex = 19; + this.btnBack.Text = "<"; + this.btnBack.UseVisualStyleBackColor = true; + this.btnBack.Click += new System.EventHandler(this.btnBack_Click); + // + // lblDisplay + // + this.lblDisplay.AutoSize = true; + this.lblDisplay.BackColor = System.Drawing.SystemColors.ControlLightLight; + this.lblDisplay.Font = new System.Drawing.Font("Microsoft Sans Serif", 54F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblDisplay.Location = new System.Drawing.Point(12, 54); + this.lblDisplay.MinimumSize = new System.Drawing.Size(365, 0); + this.lblDisplay.Name = "lblDisplay"; + this.lblDisplay.Size = new System.Drawing.Size(365, 82); + this.lblDisplay.TabIndex = 20; + this.lblDisplay.Text = "0"; + this.lblDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // btnMC + // + this.btnMC.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMC.Location = new System.Drawing.Point(12, 167); + this.btnMC.Name = "btnMC"; + this.btnMC.Size = new System.Drawing.Size(88, 64); + this.btnMC.TabIndex = 21; + this.btnMC.Text = "MC"; + this.btnMC.UseVisualStyleBackColor = true; + this.btnMC.Click += new System.EventHandler(this.btnMC_Click); + // + // btnMR + // + this.btnMR.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMR.Location = new System.Drawing.Point(106, 167); + this.btnMR.Name = "btnMR"; + this.btnMR.Size = new System.Drawing.Size(88, 64); + this.btnMR.TabIndex = 22; + this.btnMR.Text = "MR"; + this.btnMR.UseVisualStyleBackColor = true; + this.btnMR.Click += new System.EventHandler(this.btnMR_Click); + // + // btnMP + // + this.btnMP.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMP.Location = new System.Drawing.Point(200, 167); + this.btnMP.Name = "btnMP"; + this.btnMP.Size = new System.Drawing.Size(88, 64); + this.btnMP.TabIndex = 23; + this.btnMP.Text = "M+"; + this.btnMP.UseVisualStyleBackColor = true; + this.btnMP.Click += new System.EventHandler(this.btnMP_Click); + // + // btnMM + // + this.btnMM.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnMM.Location = new System.Drawing.Point(294, 167); + this.btnMM.Name = "btnMM"; + this.btnMM.Size = new System.Drawing.Size(88, 64); + this.btnMM.TabIndex = 24; + this.btnMM.Text = "M-"; + this.btnMM.UseVisualStyleBackColor = true; + this.btnMM.Click += new System.EventHandler(this.btnMM_Click); + // + // btnSqr + // + this.btnSqr.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnSqr.Location = new System.Drawing.Point(12, 307); + this.btnSqr.Name = "btnSqr"; + this.btnSqr.Size = new System.Drawing.Size(88, 64); + this.btnSqr.TabIndex = 25; + this.btnSqr.Text = "√"; + this.btnSqr.UseVisualStyleBackColor = true; + this.btnSqr.Click += new System.EventHandler(this.btnUnaryOperator_Click); + // + // btnOox + // + this.btnOox.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.btnOox.Location = new System.Drawing.Point(106, 307); + this.btnOox.Name = "btnOox"; + this.btnOox.Size = new System.Drawing.Size(88, 64); + this.btnOox.TabIndex = 26; + this.btnOox.Text = "1/x"; + this.btnOox.UseVisualStyleBackColor = true; + this.btnOox.Click += new System.EventHandler(this.btnUnaryOperator_Click); + // + // MainForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(394, 666); + this.Controls.Add(this.btnOox); + this.Controls.Add(this.btnSqr); + this.Controls.Add(this.btnMM); + this.Controls.Add(this.btnMP); + this.Controls.Add(this.btnMR); + this.Controls.Add(this.btnMC); + this.Controls.Add(this.lblDisplay); + this.Controls.Add(this.btnBack); + this.Controls.Add(this.btnClear); + this.Controls.Add(this.btnPercent); + this.Controls.Add(this.btnDivide); + this.Controls.Add(this.btnEqual); + this.Controls.Add(this.btnDot); + this.Controls.Add(this.btn0); + this.Controls.Add(this.btnSign); + this.Controls.Add(this.btnPlus); + this.Controls.Add(this.btn3); + this.Controls.Add(this.btn2); + this.Controls.Add(this.btn1); + this.Controls.Add(this.btnMinus); + this.Controls.Add(this.btn6); + this.Controls.Add(this.btn5); + this.Controls.Add(this.btn4); + this.Controls.Add(this.btnMultiply); + this.Controls.Add(this.btn9); + this.Controls.Add(this.btn8); + this.Controls.Add(this.btn7); + this.Name = "MainForm"; + this.Text = "Form1"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button btn7; + private System.Windows.Forms.Button btn8; + private System.Windows.Forms.Button btn9; + private System.Windows.Forms.Button btnMultiply; + private System.Windows.Forms.Button btnMinus; + private System.Windows.Forms.Button btn6; + private System.Windows.Forms.Button btn5; + private System.Windows.Forms.Button btn4; + private System.Windows.Forms.Button btnPlus; + private System.Windows.Forms.Button btn3; + private System.Windows.Forms.Button btn2; + private System.Windows.Forms.Button btn1; + private System.Windows.Forms.Button btnEqual; + private System.Windows.Forms.Button btnDot; + private System.Windows.Forms.Button btn0; + private System.Windows.Forms.Button btnSign; + private System.Windows.Forms.Button btnDivide; + private System.Windows.Forms.Button btnPercent; + private System.Windows.Forms.Button btnClear; + private System.Windows.Forms.Button btnBack; + private System.Windows.Forms.Label lblDisplay; + private System.Windows.Forms.Button btnMC; + private System.Windows.Forms.Button btnMR; + private System.Windows.Forms.Button btnMP; + private System.Windows.Forms.Button btnMM; + private System.Windows.Forms.Button btnSqr; + private System.Windows.Forms.Button btnOox; + } +} + diff --git a/MainForm.cs b/MainForm.cs new file mode 100644 index 00000000..1c21ecb6 --- /dev/null +++ b/MainForm.cs @@ -0,0 +1,269 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace CPE200Lab1 +{ + public partial class MainForm : Form + { + private bool hasDot; + private bool isAllowBack; + private bool isAfterOperater; + private bool isAfterEqual; + private string firstOperand; + private string operate; + private double memory; + private SimpleCalculatorEngine engine; + + private void resetAll() + { + lblDisplay.Text = "0"; + isAllowBack = true; + hasDot = false; + isAfterOperater = false; + isAfterEqual = false; + firstOperand = null; + } + + + + public MainForm() + { + InitializeComponent(); + memory = 0; + engine = new SimpleCalculatorEngine(); + resetAll(); + } + + private void btnNumber_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (isAfterEqual) + { + resetAll(); + } + if (isAfterOperater) + { + lblDisplay.Text = "0"; + } + if(lblDisplay.Text.Length is 8) + { + return; + } + isAllowBack = true; + string digit = ((Button)sender).Text; + if(lblDisplay.Text is "0") + { + lblDisplay.Text = ""; + } + lblDisplay.Text += digit; + isAfterOperater = false; + } + + private void btnUnaryOperator_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (isAfterOperater) + { + return; + } + operate = ((Button)sender).Text; + firstOperand = lblDisplay.Text; + string result = engine.unaryCalculate(operate, firstOperand); + if (result is "E" || result.Length > 8) + { + lblDisplay.Text = "Error"; + } + else + { + lblDisplay.Text = result; + } + + } + + private void btnOperator_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (isAfterOperater) + { + return; + } + if(firstOperand != null) + { + string secondOperand = lblDisplay.Text; + string result = engine.calculate(operate, firstOperand, secondOperand); + if (result is "E" || result.Length > 8) + { + lblDisplay.Text = "Error"; + } + else + { + lblDisplay.Text = result; + } + } + operate = ((Button)sender).Text; + switch (operate) + { + case "+": + case "-": + case "X": + case "÷": + firstOperand = lblDisplay.Text; + isAfterOperater = true; + break; + case "%": + // your code here + break; + } + isAllowBack = false; + } + + private void btnEqual_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + string secondOperand = lblDisplay.Text; + string result = engine.calculate(operate, firstOperand, secondOperand); + if (result is "E" || result.Length > 8) + { + lblDisplay.Text = "Error"; + } + else + { + lblDisplay.Text = result; + } + isAfterEqual = true; + } + + private void btnDot_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (isAfterEqual) + { + resetAll(); + } + if (lblDisplay.Text.Length is 8) + { + return; + } + if (!hasDot) + { + lblDisplay.Text += "."; + hasDot = true; + } + } + + private void btnSign_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (isAfterEqual) + { + resetAll(); + } + // already contain negative sign + if (lblDisplay.Text.Length is 8) + { + return; + } + if(lblDisplay.Text[0] is '-') + { + lblDisplay.Text = lblDisplay.Text.Substring(1, lblDisplay.Text.Length - 1); + } else + { + lblDisplay.Text = "-" + lblDisplay.Text; + } + } + + private void btnClear_Click(object sender, EventArgs e) + { + resetAll(); + } + + private void btnBack_Click(object sender, EventArgs e) + { + if (lblDisplay.Text is "Error") + { + return; + } + if (isAfterEqual) + { + return; + } + if (!isAllowBack) + { + return; + } + if(lblDisplay.Text != "0") + { + string current = lblDisplay.Text; + char rightMost = current[current.Length - 1]; + if(rightMost is '.') + { + hasDot = false; + } + lblDisplay.Text = current.Substring(0, current.Length - 1); + if(lblDisplay.Text is "" || lblDisplay.Text is "-") + { + lblDisplay.Text = "0"; + } + } + } + + private void btnMP_Click(object sender, EventArgs e) + { + if(lblDisplay.Text is "Error") + { + return; + } + memory += Convert.ToDouble(lblDisplay.Text); + isAfterOperater = true; + } + + private void btnMC_Click(object sender, EventArgs e) + { + memory = 0; + } + + private void btnMM_Click(object sender, EventArgs e) + { + if(lblDisplay.Text is "Error") + { + return; + } + memory -= Convert.ToDouble(lblDisplay.Text); + isAfterOperater = true; + } + + private void btnMR_Click(object sender, EventArgs e) + { + if(lblDisplay.Text is "error") + { + return; + } + lblDisplay.Text = memory.ToString(); + } + } +} diff --git a/MainForm.resx b/MainForm.resx new file mode 100644 index 00000000..1af7de15 --- /dev/null +++ b/MainForm.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/Program.cs b/Program.cs new file mode 100644 index 00000000..81ab08f1 --- /dev/null +++ b/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace CPE200Lab1 +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new ExtendForm()); + } + } +} diff --git a/RPNCalculatorEngine.cs b/RPNCalculatorEngine.cs new file mode 100644 index 00000000..15886b1f --- /dev/null +++ b/RPNCalculatorEngine.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + public class RPNCalculatorEngine : SimpleCalculatorEngine + { + public string Process(string str) + { + if (str == "" || str == null) + { + return "E"; + } + + Stack myStack = new Stack(); + string[] parts = str.Split(' '); + string result; + string mun1, mun2; + + if ((parts.Length == 1 || parts.Length == 2) && parts[0] != "0") + { + return "E"; + } + + for (int i = 0; i < parts.Length; i++) + { + if (isNumber(parts[i])) + { + myStack.Push(parts[i]); + } + + else if (isOperator(parts[i])) + { + try + + { + mun2 = myStack.Pop(); + mun1 = myStack.Pop(); + result = calculate(parts[i], mun1, mun2, 4); + } + catch(Exception) + { + return "E"; + } + + myStack.Push(result); + + + } + + else if (parts[i]=="++") + { + return "E"; + } + + } + + if(myStack.Count() == 1) + { + result = myStack.Pop(); + return result; + } + else + { + return "E"; + } + + } + } + +} diff --git a/RPNCalculatorEngineTest.cs b/RPNCalculatorEngineTest.cs new file mode 100644 index 00000000..45df4cb8 --- /dev/null +++ b/RPNCalculatorEngineTest.cs @@ -0,0 +1,26 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using CPE200Lab1; + +namespace CPE200Lab1Test +{ + [TestClass] + public class RPNCalculatorEngineTest + { + RPNCalculatorEngine engine = new RPNCalculatorEngine(); + + [TestMethod] + public void ConstructorTest() + { + RPNCalculatorEngine engine = new RPNCalculatorEngine(); + Assert.IsNotNull(engine); + Assert.IsInstanceOfType(engine, typeof(RPNCalculatorEngine)); + } + + [TestMethod] + public void EmptyArgumentTest() + { + RPNCalculatorEngine engine = new RPNCalculatorEngine(); + } + } +} diff --git a/RPNCalculatorEngineTests.cs b/RPNCalculatorEngineTests.cs new file mode 100644 index 00000000..121b9b45 --- /dev/null +++ b/RPNCalculatorEngineTests.cs @@ -0,0 +1,124 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using CPE200Lab1; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1.Tests +{ + [TestClass()] + public class RPNCalculatorEngineTests + { + RPNCalculatorEngine engine; + + [TestInitialize()] + public void Initialize() + { + engine = new RPNCalculatorEngine(); + } + + [TestMethod()] + public void ConstructorTest() + { + Assert.IsNotNull(engine); + Assert.IsInstanceOfType(engine, typeof(RPNCalculatorEngine)); + } + + [TestMethod()] + public void EmptyArgumentTest() + { + Assert.AreEqual("E", engine.Process(null)); + Assert.AreEqual("E", engine.Process("")); + } + + [TestMethod()] + public void BasicCalcuationOneTest() + { + Assert.AreEqual("0", engine.Process("0")); + + Assert.AreEqual("2", engine.Process("1 1 + ")); + Assert.AreEqual("0", engine.Process("1 1 - ")); + Assert.AreEqual("1", engine.Process("1 1 X ")); + Assert.AreEqual("1", engine.Process("1 1 ÷ ")); + } + + [TestMethod()] + public void BasicCalcuationTwoTest() + { + Assert.AreEqual("10", engine.Process("5 5 + ")); + Assert.AreEqual("10", engine.Process("20 10 - ")); + Assert.AreEqual("10", engine.Process("5 2 X ")); + Assert.AreEqual("10", engine.Process("10 1 ÷")); + } + + [TestMethod()] + public void BasicCalcuationThreeTest() + { + Assert.AreEqual("20", engine.Process("10 10 + ")); + Assert.AreEqual("-10", engine.Process("10 20 - ")); + Assert.AreEqual("100", engine.Process("10 10 X ")); + Assert.AreEqual("10", engine.Process("100 10 ÷")); + } + + [TestMethod()] + public void BasicCalcuationFourTest() + { + Assert.AreEqual("-1", engine.Process("1 2 - ")); + Assert.AreEqual("-1", engine.Process("-1 1 X ")); + Assert.AreEqual("-1", engine.Process("1 -1 X ")); + Assert.AreEqual("-1", engine.Process("1 -1 ÷")); + Assert.AreEqual("-1", engine.Process("-1 1 ÷")); + } + + [TestMethod()] + public void BasicCalcuationFiveTest() + { + Assert.AreEqual("0.5", engine.Process("1 2 ÷")); + Assert.AreEqual("0.3333", engine.Process("1 3 ÷")); + Assert.AreEqual("0.25", engine.Process("1 4 ÷")); + Assert.AreEqual("0.1667", engine.Process("1 6 ÷")); + Assert.AreEqual("0.125", engine.Process("1 8 ÷")); + } + + [TestMethod()] + public void ComplexCalcuationTest() + { + Assert.AreEqual("3", engine.Process("1 1 1 + + ")); + Assert.AreEqual("-1", engine.Process("1 1 1 + - ")); + Assert.AreEqual("1", engine.Process("1 1 1 - + ")); + Assert.AreEqual("1", engine.Process("1 1 1 - - ")); + Assert.AreEqual("6", engine.Process("2 2 2 X + ")); + Assert.AreEqual("8", engine.Process("2 2 2 + X ")); + Assert.AreEqual("0.5", engine.Process("2 2 2 + ÷")); + Assert.AreEqual("3", engine.Process("2 2 2 ÷ +")); + } + + [TestMethod()] + public void DividedByZeroTest() + { + Assert.AreEqual("E", engine.Process("0 0 ÷ ")); + Assert.AreEqual("E", engine.Process("1 0 ÷ ")); + Assert.AreEqual("E", engine.Process("1 2 2 - ÷ ")); + } + + [TestMethod()] + public void InvalideFormatTest() + { + Assert.AreEqual("E", engine.Process("+")); + Assert.AreEqual("E", engine.Process("1+")); + Assert.AreEqual("E", engine.Process("+1")); + Assert.AreEqual("E", engine.Process("1 +")); + Assert.AreEqual("E", engine.Process("+ 1")); + Assert.AreEqual("E", engine.Process("1 1")); + Assert.AreEqual("E", engine.Process("+ 1 1")); + Assert.AreEqual("E", engine.Process("1 1 ++")); + Assert.AreEqual("E", engine.Process("1 1 + +")); + Assert.AreEqual("E", engine.Process("1 1 ++ +")); + Assert.AreEqual("E", engine.Process("1 1 + + +")); + Assert.AreEqual("E", engine.Process("1 1 1 + ")); + Assert.AreEqual("E", engine.Process("1 1 1 + ")); + } + } +} \ No newline at end of file diff --git a/SimpleCalculatorEngine.cs b/SimpleCalculatorEngine.cs new file mode 100644 index 00000000..16bd5b20 --- /dev/null +++ b/SimpleCalculatorEngine.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + public class SimpleCalculatorEngine + { + protected bool isNumber(string str) + { + double retNum; + return Double.TryParse(str, out retNum); + } + + protected bool isOperator(string str) + { + switch(str) { + case "+": + case "-": + case "X": + case "÷": + return true; + } + return false; + } + + public string Process(string str) + { + + //Split input string to multiple parts by space + List parts = str.Split(' ').ToList(); + string result; + if(parts.Count==1) + { + return "E"; + } + //As long as we have more than one part + while(parts.Count > 1) + { + //Check if the first three is ready for calcuation + if(!(isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2]))) + { + return "E"; + } else + { + //Calculate the first three + result = calculate(parts[1], parts[0], parts[2], 4); + //Remove the first three + parts.RemoveRange(0, 3); + // Put back the result + parts.Insert(0, result); + } + } + return parts[0]; + } + public string unaryCalculate(string operate, string operand, int maxOutputSize = 8) + { + switch (operate) + { + case "√": + { + double result; + string[] parts; + int remainLength; + + result = Math.Sqrt(Convert.ToDouble(operand)); + // split between integer part and fractional part + parts = result.ToString().Split('.'); + // if integer part length is already break max output, return error + if (parts[0].Length > maxOutputSize) + { + return "E"; + } + // calculate remaining space for fractional part. + remainLength = maxOutputSize - parts[0].Length - 1; + // trim the fractional part gracefully. = + return result.ToString(); + } + case "1/x": + if(operand != "0") + { + double result; + string[] parts; + int remainLength; + + result = (1.0 / Convert.ToDouble(operand)); + // split between integer part and fractional part + parts = result.ToString().Split('.'); + // if integer part length is already break max output, return error + if (parts[0].Length > maxOutputSize) + { + return "E"; + } + // calculate remaining space for fractional part. + remainLength = maxOutputSize - parts[0].Length - 1; + // trim the fractional part gracefully. = + return result.ToString(); + } + break; + } + return "E"; + } + + public string calculate(string operate, string firstOperand, string secondOperand, int maxOutputSize = 8) + { + switch (operate) + { + case "+": + return (Convert.ToDouble(firstOperand) + Convert.ToDouble(secondOperand)).ToString(); + case "-": + return (Convert.ToDouble(firstOperand) - Convert.ToDouble(secondOperand)).ToString(); + case "X": + return (Convert.ToDouble(firstOperand) * Convert.ToDouble(secondOperand)).ToString(); + case "÷": + // Not allow devide be zero + if (secondOperand != "0") + { + double result; + string[] parts; + int remainLength; + + result = (Convert.ToDouble(firstOperand) / Convert.ToDouble(secondOperand)); + // split between integer part and fractional part + parts = result.ToString().Split('.'); + // if integer part length is already break max output, return error + if (parts[0].Length > maxOutputSize) + { + return "E"; + } + // calculate remaining space for fractional part. + remainLength = maxOutputSize - parts[0].Length - 1; + // trim the fractional part gracefully. = + return result.ToString("0.####"); + } + break; + case "%": + + //your code here + break; + } + return "E"; + } + } +} diff --git a/packages.config b/packages.config new file mode 100644 index 00000000..d8c1b909 --- /dev/null +++ b/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file