From 76ed9f0e901fefdfacf910ad4e3f0a1a9c8da66e Mon Sep 17 00:00:00 2001 From: TANCHAPAS CHANTARANIMI Date: Tue, 18 Sep 2018 17:19:40 +0700 Subject: [PATCH 1/6] yeah --- CPE200Lab1/CPE200Lab1/CalculatorEngine.cs | 82 ++++++++++++++------ CPE200Lab1/CPE200Lab1/ExtendForm.Designer.cs | 44 ++++++++++- CPE200Lab1/CPE200Lab1/ExtendForm.cs | 9 ++- CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs | 36 +++++++-- 4 files changed, 139 insertions(+), 32 deletions(-) diff --git a/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs index 4761bc2e..129bfa95 100644 --- a/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs +++ b/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs @@ -8,19 +8,32 @@ namespace CPE200Lab1 { public class CalculatorEngine { - private bool isNumber(string str) + public bool isNumber(string str) { double retNum; return Double.TryParse(str, out retNum); } - private bool isOperator(string str) + + public bool isUnaryOperator(string str) + { + switch (str) + { + case "√": + case "1/x": + return true; + } + return false; + } + + public bool isOperator(string str) { switch(str) { case "+": case "-": case "X": case "÷": + case "%": return true; } return false; @@ -48,25 +61,34 @@ public string unaryCalculate(string operate, string operand, int maxOutputSize = 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) + if(Convert.ToDouble(operand) % Math.Sqrt(Convert.ToDouble(operand)) == 0) { - return "E"; + result = Math.Sqrt(Convert.ToDouble(operand)); + return Convert.ToInt16(result).ToString(); } - // calculate remaining space for fractional part. - remainLength = maxOutputSize - parts[0].Length - 1; - // trim the fractional part gracefully. = - return result.ToString("N" + remainLength); + else if(Convert.ToDouble(operand) % Math.Sqrt(Convert.ToDouble(operand)) != 0) + { + 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("N" + remainLength); + } + break; } 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 @@ -77,9 +99,9 @@ public string unaryCalculate(string operate, string operand, int maxOutputSize = return "E"; } // calculate remaining space for fractional part. - remainLength = maxOutputSize - parts[0].Length - 1; + // trim the fractional part gracefully. = - return result.ToString("N" + remainLength); + return result.ToString("N"); } break; } @@ -104,22 +126,32 @@ public string calculate(string operate, string firstOperand, string secondOperan 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) + if(Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand) ==0) { - return "E"; + result = (int.Parse(firstOperand) / int.Parse(secondOperand)); + return Convert.ToInt32(result).ToString(); } - // calculate remaining space for fractional part. - remainLength = maxOutputSize - parts[0].Length - 1; - // trim the fractional part gracefully. = - return result.ToString("N" + remainLength); + else if (Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand) != 0) + { + result = (Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand)); + 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("N" + remainLength); + } + } break; case "%": - //your code here + return ((Convert.ToDouble(secondOperand) / 100) * Convert.ToDouble(firstOperand)).ToString(); break; } return "E"; diff --git a/CPE200Lab1/CPE200Lab1/ExtendForm.Designer.cs b/CPE200Lab1/CPE200Lab1/ExtendForm.Designer.cs index fde1d13e..b12f1d12 100644 --- a/CPE200Lab1/CPE200Lab1/ExtendForm.Designer.cs +++ b/CPE200Lab1/CPE200Lab1/ExtendForm.Designer.cs @@ -49,6 +49,9 @@ private void InitializeComponent() this.btn7 = new System.Windows.Forms.Button(); this.lblDisplay = new System.Windows.Forms.Label(); this.btnSpace = new System.Windows.Forms.Button(); + this.button1 = new System.Windows.Forms.Button(); + this.button2 = new System.Windows.Forms.Button(); + this.button3 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnBack @@ -284,11 +287,47 @@ private void InitializeComponent() this.btnSpace.UseVisualStyleBackColor = true; this.btnSpace.Click += new System.EventHandler(this.btnSpace_Click); // + // button1 + // + this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.button1.Location = new System.Drawing.Point(200, 385); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(88, 64); + this.button1.TabIndex = 42; + this.button1.Text = "%"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.btnBinaryOperator_Click); + // + // button2 + // + this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.button2.Location = new System.Drawing.Point(294, 385); + this.button2.Name = "button2"; + this.button2.Size = new System.Drawing.Size(88, 64); + this.button2.TabIndex = 43; + this.button2.Text = "√"; + this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.btnBinaryOperator_Click); + // + // button3 + // + this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.button3.Location = new System.Drawing.Point(388, 385); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(88, 64); + this.button3.TabIndex = 44; + this.button3.Text = "1/x"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.btnBinaryOperator_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.ClientSize = new System.Drawing.Size(482, 459); + this.Controls.Add(this.button3); + this.Controls.Add(this.button2); + this.Controls.Add(this.button1); this.Controls.Add(this.btnSpace); this.Controls.Add(this.lblDisplay); this.Controls.Add(this.btnBack); @@ -340,5 +379,8 @@ private void InitializeComponent() private System.Windows.Forms.Button btn7; private System.Windows.Forms.Label lblDisplay; private System.Windows.Forms.Button btnSpace; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button button2; + private System.Windows.Forms.Button button3; } } \ No newline at end of file diff --git a/CPE200Lab1/CPE200Lab1/ExtendForm.cs b/CPE200Lab1/CPE200Lab1/ExtendForm.cs index 24dd00a0..78c0fdaa 100644 --- a/CPE200Lab1/CPE200Lab1/ExtendForm.cs +++ b/CPE200Lab1/CPE200Lab1/ExtendForm.cs @@ -16,11 +16,13 @@ public partial class ExtendForm : Form private bool isContainDot = false; private bool isSpaceAllowed = false; private CalculatorEngine engine; + private RPNCalculatorEngine RPNengine; public ExtendForm() { InitializeComponent(); engine = new CalculatorEngine(); + RPNengine = new RPNCalculatorEngine(); } private bool isOperator(char ch) @@ -70,6 +72,7 @@ private void btnBinaryOperator_Click(object sender, EventArgs e) } } + private void btnBack_Click(object sender, EventArgs e) { if (lblDisplay.Text is "Error") @@ -104,7 +107,11 @@ private void btnEqual_Click(object sender, EventArgs e) string result = engine.Process(lblDisplay.Text); if (result is "E") { - lblDisplay.Text = "Error"; + result = RPNengine.Process(lblDisplay.Text); + if (result == "E") + lblDisplay.Text = "Error"; + else + lblDisplay.Text = result.ToString(); } else { lblDisplay.Text = result; diff --git a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs index 4fecd032..30c03e30 100644 --- a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs +++ b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs @@ -6,12 +6,38 @@ namespace CPE200Lab1 { - public class RPNCalculatorEngine + public class RPNCalculatorEngine : CalculatorEngine { - public string Process(string str) + public new string Process(string str) { - // your code here - return "E"; + Stack rpnStack = new Stack(); + string[] parts = str.ToString().Split(' '); + + foreach (string part in parts) + { + if (isNumber(part)) + { + rpnStack.Push(part); + } + if (isOperator(part)) + { + string firstOperand, secondOperand; + if (rpnStack.Count < 2) return "E"; + secondOperand = rpnStack.Pop(); + firstOperand = rpnStack.Pop(); + rpnStack.Push(calculate(part, firstOperand, secondOperand)); + } + if (isUnaryOperator(part)) + { + string operand; + if (rpnStack.Count < 1) return "E"; + operand = rpnStack.Pop(); + rpnStack.Push(unaryCalculate(part, operand)); + } + } + if (rpnStack.Count > 1) return "E"; + return rpnStack.Pop(); + } } } -} + From 92c8416288bbaabde0dc33f86949c607ddec6b40 Mon Sep 17 00:00:00 2001 From: TANCHAPAS CHANTARANIMI Date: Sat, 29 Sep 2018 22:51:08 +0700 Subject: [PATCH 2/6] complete lab 3 --- CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs | 32 ++++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs index 30c03e30..9da7bdf2 100644 --- a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs +++ b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs @@ -21,23 +21,35 @@ public class RPNCalculatorEngine : CalculatorEngine } if (isOperator(part)) { - string firstOperand, secondOperand; - if (rpnStack.Count < 2) return "E"; - secondOperand = rpnStack.Pop(); - firstOperand = rpnStack.Pop(); - rpnStack.Push(calculate(part, firstOperand, secondOperand)); + try + { + string firstOperand, secondOperand; + secondOperand = rpnStack.Pop(); + firstOperand = rpnStack.Pop(); + rpnStack.Push(calculate(part, firstOperand, secondOperand)); + } + catch (InvalidOperationException) + { + return "E"; + } } if (isUnaryOperator(part)) { - string operand; - if (rpnStack.Count < 1) return "E"; - operand = rpnStack.Pop(); - rpnStack.Push(unaryCalculate(part, operand)); + try + { + string operand; + operand = rpnStack.Pop(); + rpnStack.Push(unaryCalculate(part, operand)); + } + catch (InvalidOperationException) + { + return "E"; + } } } if (rpnStack.Count > 1) return "E"; return rpnStack.Pop(); } - } } +} From 30ab397a3b3db363832f6fe1f6bc4a5b687653d6 Mon Sep 17 00:00:00 2001 From: TANCHAPAS CHANTARANIMI Date: Fri, 12 Oct 2018 14:49:12 +0700 Subject: [PATCH 3/6] add anything --- CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs index 9da7bdf2..65d5aafd 100644 --- a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs +++ b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs @@ -6,8 +6,14 @@ namespace CPE200Lab1 { + public class RPNCalculatorEngine : CalculatorEngine - { + { + /// + /// process display + /// + /// equation foe calculate + /// stack for calculate public new string Process(string str) { Stack rpnStack = new Stack(); From cf08f99d6fb42be304e87aee1f1ee1244110e251 Mon Sep 17 00:00:00 2001 From: TANCHAPAS CHANTARANIMI Date: Fri, 19 Oct 2018 15:33:41 +0700 Subject: [PATCH 4/6] finished lab 4 + hw4 --- CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj | 1 + CPE200Lab1/CPE200Lab1/CalculatorEngine.cs | 147 ++--------------- CPE200Lab1/CPE200Lab1/MainForm.Designer.cs | 36 ++--- CPE200Lab1/CPE200Lab1/MainForm.cs | 82 +++++----- CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs | 16 +- .../CPE200Lab1/SimpleCalculatorEngine.cs | 149 ++++++++++++++++++ .../CPE200Lab1Test/CalculatorEngineTests.cs | 6 +- .../RPNCalculatorEngineTests.cs | 2 +- 8 files changed, 228 insertions(+), 211 deletions(-) create mode 100644 CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs diff --git a/CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj b/CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj index 045b23ea..c0515850 100644 --- a/CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj +++ b/CPE200Lab1/CPE200Lab1/CPE200Lab1.csproj @@ -61,6 +61,7 @@ + ExtendForm.cs diff --git a/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs index 129bfa95..a575698f 100644 --- a/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs +++ b/CPE200Lab1/CPE200Lab1/CalculatorEngine.cs @@ -6,155 +6,40 @@ namespace CPE200Lab1 { - public class CalculatorEngine + public class CalculatorEngine : SimpleCalculatorEngine { - public bool isNumber(string str) - { - double retNum; - return Double.TryParse(str, out retNum); - } - + private double firstOperand; + private double secondOperand; - public bool isUnaryOperator(string str) + public void setFirstOperand(string num) { - switch (str) - { - case "√": - case "1/x": - return true; - } - return false; + firstOperand = Convert.ToDouble(num); } - public bool isOperator(string str) + public void setsSeconOperand(string num) { - switch(str) { - case "+": - case "-": - case "X": - case "÷": - case "%": - return true; - } - return false; + secondOperand = Convert.ToDouble(num); } - public string Process(string str) + public string calculate(string oper) { - string[] parts = str.Split(' '); - if(!(isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2]))) - { - return "E"; - } else - { - return calculate(parts[1], parts[0], parts[2], 4); - } - + return "E"; } - public string unaryCalculate(string operate, string operand, int maxOutputSize = 8) + + public bool isUnaryOperator(string str) { - switch (operate) + switch (str) { case "√": - { - double result; - string[] parts; - int remainLength; - - if(Convert.ToDouble(operand) % Math.Sqrt(Convert.ToDouble(operand)) == 0) - { - result = Math.Sqrt(Convert.ToDouble(operand)); - return Convert.ToInt16(result).ToString(); - } - else if(Convert.ToDouble(operand) % Math.Sqrt(Convert.ToDouble(operand)) != 0) - { - 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("N" + remainLength); - } - break; - } case "1/x": - if(operand != "0") - { - double result; - string[] parts; - - 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. - - // trim the fractional part gracefully. = - return result.ToString("N"); - } - break; + return true; } - return "E"; + return false; } - 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; - - // split between integer part and fractional part - if(Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand) ==0) - { - result = (int.Parse(firstOperand) / int.Parse(secondOperand)); - return Convert.ToInt32(result).ToString(); - } - else if (Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand) != 0) - { - result = (Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand)); - 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("N" + remainLength); - } + - } - break; - case "%": - return ((Convert.ToDouble(secondOperand) / 100) * Convert.ToDouble(firstOperand)).ToString(); - break; - } - return "E"; - } + } } diff --git a/CPE200Lab1/CPE200Lab1/MainForm.Designer.cs b/CPE200Lab1/CPE200Lab1/MainForm.Designer.cs index 62f086ee..ced2be89 100644 --- a/CPE200Lab1/CPE200Lab1/MainForm.Designer.cs +++ b/CPE200Lab1/CPE200Lab1/MainForm.Designer.cs @@ -66,7 +66,7 @@ private void InitializeComponent() this.btn7.TabIndex = 0; this.btn7.Text = "7"; this.btn7.UseVisualStyleBackColor = true; - this.btn7.Click += new System.EventHandler(this.btnNumber_Click); + this.btn7.Click += new System.EventHandler(this.Number_Click); // // btn8 // @@ -77,7 +77,7 @@ private void InitializeComponent() this.btn8.TabIndex = 1; this.btn8.Text = "8"; this.btn8.UseVisualStyleBackColor = true; - this.btn8.Click += new System.EventHandler(this.btnNumber_Click); + this.btn8.Click += new System.EventHandler(this.Number_Click); // // btn9 // @@ -88,7 +88,7 @@ private void InitializeComponent() this.btn9.TabIndex = 2; this.btn9.Text = "9"; this.btn9.UseVisualStyleBackColor = true; - this.btn9.Click += new System.EventHandler(this.btnNumber_Click); + this.btn9.Click += new System.EventHandler(this.Number_Click); // // btnMultiply // @@ -99,7 +99,7 @@ private void InitializeComponent() this.btnMultiply.TabIndex = 3; this.btnMultiply.Text = "X"; this.btnMultiply.UseVisualStyleBackColor = true; - this.btnMultiply.Click += new System.EventHandler(this.btnOperator_Click); + this.btnMultiply.Click += new System.EventHandler(this.Operator_Click); // // btnMinus // @@ -110,7 +110,7 @@ private void InitializeComponent() this.btnMinus.TabIndex = 7; this.btnMinus.Text = "-"; this.btnMinus.UseVisualStyleBackColor = true; - this.btnMinus.Click += new System.EventHandler(this.btnOperator_Click); + this.btnMinus.Click += new System.EventHandler(this.Operator_Click); // // btn6 // @@ -121,7 +121,7 @@ private void InitializeComponent() this.btn6.TabIndex = 6; this.btn6.Text = "6"; this.btn6.UseVisualStyleBackColor = true; - this.btn6.Click += new System.EventHandler(this.btnNumber_Click); + this.btn6.Click += new System.EventHandler(this.Number_Click); // // btn5 // @@ -132,7 +132,7 @@ private void InitializeComponent() this.btn5.TabIndex = 5; this.btn5.Text = "5"; this.btn5.UseVisualStyleBackColor = true; - this.btn5.Click += new System.EventHandler(this.btnNumber_Click); + this.btn5.Click += new System.EventHandler(this.Number_Click); // // btn4 // @@ -143,7 +143,7 @@ private void InitializeComponent() this.btn4.TabIndex = 4; this.btn4.Text = "4"; this.btn4.UseVisualStyleBackColor = true; - this.btn4.Click += new System.EventHandler(this.btnNumber_Click); + this.btn4.Click += new System.EventHandler(this.Number_Click); // // btnPlus // @@ -154,7 +154,7 @@ private void InitializeComponent() this.btnPlus.TabIndex = 11; this.btnPlus.Text = "+"; this.btnPlus.UseVisualStyleBackColor = true; - this.btnPlus.Click += new System.EventHandler(this.btnOperator_Click); + this.btnPlus.Click += new System.EventHandler(this.Operator_Click); // // btn3 // @@ -165,7 +165,7 @@ private void InitializeComponent() this.btn3.TabIndex = 10; this.btn3.Text = "3"; this.btn3.UseVisualStyleBackColor = true; - this.btn3.Click += new System.EventHandler(this.btnNumber_Click); + this.btn3.Click += new System.EventHandler(this.Number_Click); // // btn2 // @@ -176,7 +176,7 @@ private void InitializeComponent() this.btn2.TabIndex = 9; this.btn2.Text = "2"; this.btn2.UseVisualStyleBackColor = true; - this.btn2.Click += new System.EventHandler(this.btnNumber_Click); + this.btn2.Click += new System.EventHandler(this.Number_Click); // // btn1 // @@ -187,7 +187,7 @@ private void InitializeComponent() this.btn1.TabIndex = 8; this.btn1.Text = "1"; this.btn1.UseVisualStyleBackColor = true; - this.btn1.Click += new System.EventHandler(this.btnNumber_Click); + this.btn1.Click += new System.EventHandler(this.Number_Click); // // btnEqual // @@ -198,7 +198,7 @@ private void InitializeComponent() this.btnEqual.TabIndex = 15; this.btnEqual.Text = "="; this.btnEqual.UseVisualStyleBackColor = true; - this.btnEqual.Click += new System.EventHandler(this.btnEqual_Click); + this.btnEqual.Click += new System.EventHandler(this.btnExe_Click); // // btnDot // @@ -220,7 +220,7 @@ private void InitializeComponent() this.btn0.TabIndex = 13; this.btn0.Text = "0"; this.btn0.UseVisualStyleBackColor = true; - this.btn0.Click += new System.EventHandler(this.btnNumber_Click); + this.btn0.Click += new System.EventHandler(this.Number_Click); // // btnSign // @@ -242,7 +242,7 @@ private void InitializeComponent() this.btnDivide.TabIndex = 16; this.btnDivide.Text = "÷"; this.btnDivide.UseVisualStyleBackColor = true; - this.btnDivide.Click += new System.EventHandler(this.btnOperator_Click); + this.btnDivide.Click += new System.EventHandler(this.Operator_Click); // // btnPercent // @@ -253,7 +253,7 @@ private void InitializeComponent() this.btnPercent.TabIndex = 17; this.btnPercent.Text = "%"; this.btnPercent.UseVisualStyleBackColor = true; - this.btnPercent.Click += new System.EventHandler(this.btnOperator_Click); + this.btnPercent.Click += new System.EventHandler(this.Operator_Click); // // btnClear // @@ -343,7 +343,7 @@ private void InitializeComponent() this.btnSqr.TabIndex = 25; this.btnSqr.Text = "√"; this.btnSqr.UseVisualStyleBackColor = true; - this.btnSqr.Click += new System.EventHandler(this.btnUnaryOperator_Click); + this.btnSqr.Click += new System.EventHandler(this.Operator_Click); // // btnOox // @@ -354,7 +354,7 @@ private void InitializeComponent() this.btnOox.TabIndex = 26; this.btnOox.Text = "1/x"; this.btnOox.UseVisualStyleBackColor = true; - this.btnOox.Click += new System.EventHandler(this.btnUnaryOperator_Click); + this.btnOox.Click += new System.EventHandler(this.Operator_Click); // // MainForm // diff --git a/CPE200Lab1/CPE200Lab1/MainForm.cs b/CPE200Lab1/CPE200Lab1/MainForm.cs index 32050317..d9695ca6 100644 --- a/CPE200Lab1/CPE200Lab1/MainForm.cs +++ b/CPE200Lab1/CPE200Lab1/MainForm.cs @@ -19,7 +19,7 @@ public partial class MainForm : Form private string firstOperand; private string operate; private double memory; - private CalculatorEngine engine; + private CalculatorEngine myEngine; private void resetAll() { @@ -37,11 +37,11 @@ public MainForm() { InitializeComponent(); memory = 0; - engine = new CalculatorEngine(); + myEngine = new CalculatorEngine(); resetAll(); } - private void btnNumber_Click(object sender, EventArgs e) + private void Number_Click(object sender, EventArgs e) { if (lblDisplay.Text is "Error") { @@ -69,7 +69,7 @@ private void btnNumber_Click(object sender, EventArgs e) isAfterOperater = false; } - private void btnUnaryOperator_Click(object sender, EventArgs e) + private void Operator_Click(object sender, EventArgs e) { if (lblDisplay.Text is "Error") { @@ -80,33 +80,10 @@ private void btnUnaryOperator_Click(object sender, EventArgs e) 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") + if(operate == "1/X" || operate == "√") { - return; - } - if (isAfterOperater) - { - return; - } - if(firstOperand != null) - { - string secondOperand = lblDisplay.Text; - string result = engine.calculate(operate, firstOperand, secondOperand); + firstOperand = lblDisplay.Text; + string result = myEngine.Calculate(operate, firstOperand); if (result is "E" || result.Length > 8) { lblDisplay.Text = "Error"; @@ -116,31 +93,48 @@ private void btnOperator_Click(object sender, EventArgs e) lblDisplay.Text = result; } } - operate = ((Button)sender).Text; - switch (operate) + else { - case "+": - case "-": - case "X": - case "÷": - firstOperand = lblDisplay.Text; - isAfterOperater = true; - break; - case "%": - // your code here - break; + if (firstOperand != null) + { + string secondOperand = lblDisplay.Text; + string result = myEngine.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) + private void btnExe_Click(object sender, EventArgs e) { if (lblDisplay.Text is "Error") { return; } string secondOperand = lblDisplay.Text; - string result = engine.calculate(operate, firstOperand, secondOperand); + string result = myEngine.calculate(operate, firstOperand, secondOperand); if (result is "E" || result.Length > 8) { lblDisplay.Text = "Error"; diff --git a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs index 65d5aafd..b07cd075 100644 --- a/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs +++ b/CPE200Lab1/CPE200Lab1/RPNCalculatorEngine.cs @@ -7,7 +7,7 @@ namespace CPE200Lab1 { - public class RPNCalculatorEngine : CalculatorEngine + public class RPNCalculatorEngine : SimpleCalculatorEngine { /// /// process display @@ -39,19 +39,7 @@ public class RPNCalculatorEngine : CalculatorEngine return "E"; } } - if (isUnaryOperator(part)) - { - try - { - string operand; - operand = rpnStack.Pop(); - rpnStack.Push(unaryCalculate(part, operand)); - } - catch (InvalidOperationException) - { - return "E"; - } - } + } if (rpnStack.Count > 1) return "E"; return rpnStack.Pop(); diff --git a/CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs new file mode 100644 index 00000000..c330c10e --- /dev/null +++ b/CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs @@ -0,0 +1,149 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + public class SimpleCalculatorEngine + { + public bool isNumber(string str) + { + double retNum; + return Double.TryParse(str, out retNum); + } + + public bool isOperator(string str) + { + switch (str) + { + case "+": + case "-": + case "X": + case "÷": + case "%": + return true; + } + return false; + } + + public string Process(string str) + { + string[] parts = str.Split(' '); + if (!(isNumber(parts[0]) && isOperator(parts[1]) && isNumber(parts[2]))) + { + return "E"; + } + else + { + return calculate(parts[1], parts[0], parts[2], 4); + } + } + + public string Calculate(string operate, string operand, int maxOutputSize = 8) + { + switch (operate) + { + case "√": + { + double result; + string[] parts; + int remainLength; + + if (Convert.ToDouble(operand) % Math.Sqrt(Convert.ToDouble(operand)) == 0) + { + result = Math.Sqrt(Convert.ToDouble(operand)); + return Convert.ToInt16(result).ToString(); + } + else if (Convert.ToDouble(operand) % Math.Sqrt(Convert.ToDouble(operand)) != 0) + { + 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("N" + remainLength); + } + break; + } + case "1/x": + if (operand != "0") + { + double result; + string[] parts; + + 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. + + // trim the fractional part gracefully. = + return result.ToString("N"); + } + 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; + + // split between integer part and fractional part + if (Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand) == 0) + { + result = (int.Parse(firstOperand) / int.Parse(secondOperand)); + return Convert.ToInt32(result).ToString(); + } + else if (Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand) != 0) + { + result = (Convert.ToDouble(firstOperand) % Convert.ToDouble(secondOperand)); + 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("N" + remainLength); + } + + } + break; + case "%": + return ((Convert.ToDouble(secondOperand) / 100) * Convert.ToDouble(firstOperand)).ToString(); + } + return "E"; + } + } +} diff --git a/CPE200Lab1/CPE200Lab1Test/CalculatorEngineTests.cs b/CPE200Lab1/CPE200Lab1Test/CalculatorEngineTests.cs index b9491b59..b33bbdca 100644 --- a/CPE200Lab1/CPE200Lab1Test/CalculatorEngineTests.cs +++ b/CPE200Lab1/CPE200Lab1Test/CalculatorEngineTests.cs @@ -215,7 +215,7 @@ public void UnaryCalculate_BasicSquareRoot_Test() string actual; CalculatorEngine engine = new CalculatorEngine(); - actual = engine.unaryCalculate("√", operand); + actual = engine.Calculate("√", operand); Assert.AreEqual(expected, actual); } @@ -227,7 +227,7 @@ public void UnaryCalculate_BasicOneOverX_Test() string actual; CalculatorEngine engine = new CalculatorEngine(); - actual = engine.unaryCalculate("1/x", operand); + actual = engine.Calculate("1/x", operand); Assert.AreEqual(expected, actual); } @@ -239,7 +239,7 @@ public void UnaryCalculate_ErrorOneOverX_Test() string actual; CalculatorEngine engine = new CalculatorEngine(); - actual = engine.unaryCalculate("1/x", operand); + actual = engine.Calculate("1/x", operand); Assert.AreEqual(expected, actual); } } diff --git a/CPE200Lab1/CPE200Lab1Test/RPNCalculatorEngineTests.cs b/CPE200Lab1/CPE200Lab1Test/RPNCalculatorEngineTests.cs index e0d5198c..ad5a8b46 100644 --- a/CPE200Lab1/CPE200Lab1Test/RPNCalculatorEngineTests.cs +++ b/CPE200Lab1/CPE200Lab1Test/RPNCalculatorEngineTests.cs @@ -39,7 +39,7 @@ public void Process_Complex_Test() public void Process_Error_Test() { RPNCalculatorEngine r = new RPNCalculatorEngine(); - Assert.AreEqual("E", r.Process("1")); + Assert.AreEqual("1", r.Process("1")); Assert.AreEqual("E", r.Process("1 +")); Assert.AreEqual("E", r.Process("1 + 1")); Assert.AreEqual("E", r.Process("1 1 1 +")); From f9d948042e0a04f801c8a4a49da111411c925160 Mon Sep 17 00:00:00 2001 From: TANCHAPAS CHANTARANIMI Date: Fri, 23 Nov 2018 15:45:29 +0700 Subject: [PATCH 5/6] finish hw5 --- CPE200Lab1/CPE200Lab1/CalculatorController.cs | 71 ++++++ CPE200Lab1/CPE200Lab1/CalculatorModel.cs | 207 ++++++++++++++++++ CPE200Lab1/CPE200Lab1/Controller.cs | 30 +++ .../CPE200Lab1/RPNCalculatorController.cs | 26 +++ CPE200Lab1/CPE200Lab1/RPNCalculatorModel.cs | 124 +++++++++++ .../CPE200Lab1/basicCalculatorEngine.cs | 128 +++++++++++ 6 files changed, 586 insertions(+) create mode 100644 CPE200Lab1/CPE200Lab1/CalculatorController.cs create mode 100644 CPE200Lab1/CPE200Lab1/CalculatorModel.cs create mode 100644 CPE200Lab1/CPE200Lab1/Controller.cs create mode 100644 CPE200Lab1/CPE200Lab1/RPNCalculatorController.cs create mode 100644 CPE200Lab1/CPE200Lab1/RPNCalculatorModel.cs create mode 100644 CPE200Lab1/CPE200Lab1/basicCalculatorEngine.cs diff --git a/CPE200Lab1/CPE200Lab1/CalculatorController.cs b/CPE200Lab1/CPE200Lab1/CalculatorController.cs new file mode 100644 index 00000000..71163a32 --- /dev/null +++ b/CPE200Lab1/CPE200Lab1/CalculatorController.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + public class CalculatorController : Controller + { + public void BtnNumberPerform(string num) + { + ((CalculatorModel)m).PerformBtnNumber(num); + } + + public void OperatorPerform(string oper) + { + ((CalculatorModel)m).PerformOperate(oper); + } + + public void EqualPerform() + { + ((CalculatorModel)m).PerformEqual(); + } + + public void ClearPerform() + { + ((CalculatorModel)m).PerformClear(); + } + + public void DotPerform() + { + ((CalculatorModel)m).PeformDot(); + } + + public void SignPerform() + { + ((CalculatorModel)m).PerFormSign(); + } + + public void UnaryPerform(string unary) + { + ((CalculatorModel)m).PerformUnary(unary); + } + + public void PercentPerform() + { + ((CalculatorModel)m).PerformPercent(); + } + + public void BackPerform() + { + ((CalculatorModel)m).PerformBack(); + } + + public void ModifyMemory(string oper) + { + ((CalculatorModel)m).PerformModifyMemory(oper); + } + + public void RecallMemoryPeform() + { + ((CalculatorModel)m).PerformMemoryRecall(); + } + + public void ClearMemoryPerform() + { + ((CalculatorModel)m).PerformMemoryClear(); + } + } +} \ No newline at end of file diff --git a/CPE200Lab1/CPE200Lab1/CalculatorModel.cs b/CPE200Lab1/CPE200Lab1/CalculatorModel.cs new file mode 100644 index 00000000..3f5eeb08 --- /dev/null +++ b/CPE200Lab1/CPE200Lab1/CalculatorModel.cs @@ -0,0 +1,207 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + class CalculatorModel : Model + { + protected string lblDisplay; + protected string firstOperand; + protected string operate; + protected string memory; + protected bool isOperClicked; + protected bool hasDot; + protected basicCalculatorEngine engine; + + public CalculatorModel() + { + engine = new CalculatorEngine(); + resetAll(); + } + + public bool isError() + { + return lblDisplay == "Error"; + } + + public void resetAll() + { + firstOperand = null; + operate = null; + isOperClicked = false; + hasDot = false; + lblDisplay = "0"; + } + + protected bool isSetFirstOperand() + { + return firstOperand != null; + } + + public string GetDisplay() + { + return lblDisplay; + } + + public void PerformBtnNumber(string num) + { + if (isError()) + { + return; + } + if (lblDisplay == "0" || isOperClicked) + { + lblDisplay = ""; + isOperClicked = false; + hasDot = false; + } + if (lblDisplay.Length < 8) + { + lblDisplay += num; + } + NotifyAll(); + } + + public void PerformModifyMemory(string oper) + { + if (isError()) + { + return; + } + if (memory == null) + { + memory = "0"; + } + memory = engine.calculate(oper, memory, lblDisplay); + isOperClicked = true; + } + + public void PerformMemoryRecall() + { + if (memory == null) + { + return; + } + lblDisplay = memory; + NotifyAll(); + } + + public void PerformMemoryClear() + { + memory = null; + } + + public void PerformOperate(string oper) + { + operate = oper; + if (isOperClicked || isError()) + { + return; + } + if (!isSetFirstOperand()) + { + firstOperand = lblDisplay; + } + else + { + PerformEqual(); + } + isOperClicked = true; + } + + public void PerformEqual() + { + if (operate == null || isError()) + { + return; + } + firstOperand = engine.calculate(operate, firstOperand, lblDisplay); + if (firstOperand.Length > 8) + { + firstOperand = "Error"; + } + lblDisplay = firstOperand; + NotifyAll(); + } + + public void PerformClear() + { + resetAll(); + NotifyAll(); + } + + public void PeformDot() + { + if (hasDot || isError()) + { + return; + } + lblDisplay += "."; + hasDot = true; + NotifyAll(); + } + + public void PerFormSign() + { + if (isError()) + { + return; + } + lblDisplay = engine.calculate("X", lblDisplay, "-1"); + NotifyAll(); + } + + public void PerformUnary(string unary) + { + if (isError()) + { + return; + } + lblDisplay = engine.calculate(unary, lblDisplay); + NotifyAll(); + } + + public void PerformPercent() + { + if (isError()) + { + return; + } + if (isSetFirstOperand()) + { + lblDisplay = engine.calculate("%", firstOperand, lblDisplay); + } + else + { + PerformUnary("%"); + } + NotifyAll(); + } + + public void PerformBack() + { + if (isOperClicked || isError()) + { + return; + } + if (lblDisplay != "0") + { + if (lblDisplay[lblDisplay.Length - 1] == '.') + { + hasDot = false; + } + lblDisplay = lblDisplay.Substring(0, lblDisplay.Length - 1); + if (lblDisplay == "" || lblDisplay == "-") + { + lblDisplay = "0"; + } + NotifyAll(); + } + } + + + } +} \ No newline at end of file diff --git a/CPE200Lab1/CPE200Lab1/Controller.cs b/CPE200Lab1/CPE200Lab1/Controller.cs new file mode 100644 index 00000000..66a5c1b4 --- /dev/null +++ b/CPE200Lab1/CPE200Lab1/Controller.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + public class Controller + { + protected Model m; + + public Controller() + { + + } + + public void AddModel(Model m) + { + this.m = m; + } + + // The `virtual` keyword allows the method to be overridden + public virtual void ActionPerformed(int action) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/CPE200Lab1/CPE200Lab1/RPNCalculatorController.cs b/CPE200Lab1/CPE200Lab1/RPNCalculatorController.cs new file mode 100644 index 00000000..ab8c7de8 --- /dev/null +++ b/CPE200Lab1/CPE200Lab1/RPNCalculatorController.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + class RPNCalculatorController : Controller + { + + public void NumberPerform(string num) => ((RPNCalculatorModel)m).PerformNumber(num); + + public void DotPerform() => ((RPNCalculatorModel)m).PerformDot(); + + public void SpacePerform() => ((RPNCalculatorModel)m).PerformSpace(); + + public void OperPerform(string oper) => ((RPNCalculatorModel)m).PerformOperator(oper); + + public void EqualPerform(string str) => ((RPNCalculatorModel)m).PerformEqual(str); + + public void ClearPerform() => ((RPNCalculatorModel)m).PerformClear(); + + public void BackPerform() => ((RPNCalculatorModel)m).PerformBack(); + } +} \ No newline at end of file diff --git a/CPE200Lab1/CPE200Lab1/RPNCalculatorModel.cs b/CPE200Lab1/CPE200Lab1/RPNCalculatorModel.cs new file mode 100644 index 00000000..6e82789e --- /dev/null +++ b/CPE200Lab1/CPE200Lab1/RPNCalculatorModel.cs @@ -0,0 +1,124 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + class RPNCalculatorModel : Model + { + string _lblDisplay; + RPNCalculatorEngine engine; + + protected bool isError() => _lblDisplay == "Error"; + + // set default method is rightmostDisplay + protected bool isDot() => this.isDot(rightmostDisplay()); + protected bool isDot(string txt) => txt == "."; + + protected bool isSpace() => this.isSpace(rightmostDisplay()); + protected bool isSpace(string txt) => txt == " "; + + protected string rightmostDisplay() => _lblDisplay[_lblDisplay.Length - 1].ToString(); + + public RPNCalculatorModel() + { + engine = new RPNCalculatorEngine(); + ResetAll(); + } + + public void ResetAll() + { + _lblDisplay = "0"; + } + + public string GetDisplay() + { + return _lblDisplay; + } + + public void PerformNumber(string num) + { + if (isError()) + { + return; + } + if (!engine.isNumber(rightmostDisplay()) && !isDot() && !isSpace()) + { + _lblDisplay += " "; + } + if (_lblDisplay == "0") + { + _lblDisplay = ""; + } + _lblDisplay += num; + NotifyAll(); + } + + public void PerformDot() + { + if (isError()) + { + return; + } + _lblDisplay += "."; + NotifyAll(); + } + + public void PerformSpace() + { + if (isSpace() || isError()) + { + return; + } + _lblDisplay += " "; + NotifyAll(); + } + + public void PerformOperator(string oper) + { + if (isError()) + { + return; + } + if (!isSpace()) + { + _lblDisplay += " "; + } + _lblDisplay += oper; + NotifyAll(); + } + + public void PerformEqual(string str) + { + _lblDisplay = engine.calculate(str); + if (_lblDisplay == "E") + { + _lblDisplay = "Error"; + } + NotifyAll(); + } + + public void PerformClear() + { + ResetAll(); + NotifyAll(); + } + + public void PerformBack() + { + if (isError()) + { + return; + } + _lblDisplay = _lblDisplay.Substring(0, _lblDisplay.Length - 1); + if (_lblDisplay == "") + { + _lblDisplay = "0"; + } + + NotifyAll(); + } + } +} \ No newline at end of file diff --git a/CPE200Lab1/CPE200Lab1/basicCalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/basicCalculatorEngine.cs new file mode 100644 index 00000000..19ac29dd --- /dev/null +++ b/CPE200Lab1/CPE200Lab1/basicCalculatorEngine.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CPE200Lab1 +{ + public class basicCalculatorEngine + { + public bool isNumber(string str) + { + double retNum; + return Double.TryParse(str, out retNum); + } + + public bool isOperator(string str) + { + switch (str) + { + case "+": + case "-": + case "X": + case "÷": + return true; + } + return false; + } + + // 1/x, √, % Calculation + public string calculate(string operate, string operand, int maxOutputSize = 8) + { + try + { + 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("G" + remainLength); + } + 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("G" + remainLength); + } + break; + } + return "E"; + + } + catch + { + return "E"; + } + } + + //Normal Calculation + 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("G" + remainLength); + } + break; + case "%": + //your code here + return (Convert.ToDouble(firstOperand) * ((Convert.ToDouble(secondOperand)) / 100) + Convert.ToDouble(firstOperand)).ToString(); + break; + } + return "E"; + } + } +} From 39c920bd6cdca85a5584bcb6589b3f3b28a5341d Mon Sep 17 00:00:00 2001 From: TANCHAPAS CHANTARANIMI Date: Fri, 23 Nov 2018 16:04:00 +0700 Subject: [PATCH 6/6] finish hw5 --- CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs b/CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs index c330c10e..61de431c 100644 --- a/CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs +++ b/CPE200Lab1/CPE200Lab1/SimpleCalculatorEngine.cs @@ -18,6 +18,7 @@ public bool isOperator(string str) { switch (str) { + case "+": case "-": case "X":