In: Computer Science
Hello,
In C#, we are creating a simple calculator that takes two operands and one operator and calculates a result. We need to include a private method to calculate the result as well as try-catch statements for exceptions. My code that i have so far is below, it executes and calculates, but there is an issue with the way i have done the method. Any help or info about my method errors would be great, Thank you!
**********************************
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 SimpleCalculator
{
/***********************************
* Displays simple calculations from the
* data the user enters.
* *********************************/
public partial class simpleCalculator : Form
{
public simpleCalculator()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs
e)
{
/*******************************
* This method calcultes the result from the
* user entry in the operands and operaator boxes.
* *****************************/
try
{
//converts the data frm the user entry frm txt to decimal and
string.
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);
string operator1 = Convert.ToString(txtOperator1.Text);
//method to perform the calculations.
decimal result = Calculate(operand1, operand2, operator1);
}
//the catch exceptions, will try the above code and any exceptions
will be caught by the ones below
//and throw uo a message box with an error message.
catch (FormatException)
{
MessageBox.Show("A format exception has occurred. Please check all
entries." + "\n\n",
"Entry Error");
}
catch (OverflowException)
{
MessageBox.Show("An overflow exception has occurred. Please enter
smaller values",
"Entry Error");
}
catch (DivideByZeroException)
{
MessageBox.Show("Divide-by-zero error. Please enter a non-zero
value",
"Entry Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
//where the calculate method is called.
private decimal Calculate(decimal operand1, decimal operand2,
string operator1)
{
//initilized the result to decimal and zero.
decimal result = 0m;
//code to check the operator1 entry and compare it to either /,
*, - or +
//then do the corresponding calculation.
if (operator1 == "/")
{
result = operand1 / operand2;
}
else if (operator1 == "*")
{
result = operand1 * operand2;
}
else if (operator1 == "+")
{
result = operand1 + operand2;
}
else if (operator1 == "-")
{
result = operand1 - operand2;
}
//code to display the reslt in the result box
txtResult.Text = result.ToString();
}
}
//code to exit form
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
//code to clear the result box after calculation has been performed
and the operand1 box is cleared.
private void ClearResult(object sender, EventArgs e) =>
txtResult.Text = "";
}
}
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new Windows Forms Application in C# is created using Visual Studio 2017 with name "SimpleCalculator".This application contains a form with name "SimpleCalculator.cs".Below are the files associated with SimpleCalculator.
1.Form1.cs[Design]
2.Form1.cs
//namespaces
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;
//application namespace
namespace SimpleCalculator
{
/***********************************
* Displays simple calculations from the
* data the user enters.
* *********************************/
public partial class simpleCalculator : Form
{
public simpleCalculator()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs
e)
{
/*******************************
* This method calcultes the result from the
* user entry in the operands and operaator boxes.
* *****************************/
try
{
//converts the data frm the user entry frm txt to decimal and
string.
decimal operand1 = Convert.ToDecimal(txtOperand1.Text);//taking
operand1
decimal operand2 = Convert.ToDecimal(txtOperand2.Text);////taking
operand2
string operator1 = Convert.ToString(txtOperator1.Text);//taking
operator1
//method to perform the calculations,method call
decimal result = Calculate(operand1, operand2, operator1);
//code to display the reslt in the result box
txtResult.Text = result.ToString();
}
//the catch exceptions, will try the above code and any exceptions
will be caught by the ones below
//and throw uo a message box with an error message.
catch (FormatException)
{
MessageBox.Show("A format exception has occurred. Please check all
entries." + "\n\n",
"Entry Error");
}
catch (OverflowException)
{
MessageBox.Show("An overflow exception has occurred. Please enter
smaller values",
"Entry Error");
}
catch (DivideByZeroException)
{
MessageBox.Show("Divide-by-zero error. Please enter a non-zero
value",
"Entry Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
}
//where the calculate method is called.
private decimal Calculate(decimal operand1, decimal operand2,
string operator1)
{
//initilized the result to decimal and zero.
decimal result = 0m;
//code to check the operator1 entry and compare it to either /,
*, - or +
//then do the corresponding calculation.
if (operator1 == "/")
{
result = operand1 / operand2;//dividion
}
else if (operator1 == "*")
{
result = operand1 * operand2;//multiplication
}
else if (operator1 == "+")
{
result = operand1 + operand2;//addition
}
else if (operator1 == "-")
{
result = operand1 - operand2;//subtraction
}
return result;//return result
}
//code to clear the result box after calculation has been performed
and the operand1 box is cleared.
private void ClearResult_Click(object sender, EventArgs e)
{
txtOperand1.Text = "";
txtOperand2.Text = "";
txtOperator1.Text = "";
txtResult.Text = "";//clear result from the textbox
}
//code to exit form
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
===============================================
Output : Run application using F5 and will get the screen as shown below
Screen 1 :
Screen 2:Screen when numbers are not entered
Screen 3:Screen showing addition
Screen 4:Screen showing subtraction
Screen 5:Screen showing multiplication
Screen 6:Screen showing division
Screen 7:Screen when second number is zero
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.