I need a form that accepts two operands and an operator from the user and then performs the requested operation.
Specifications
Before performing the calculation, the application should check that both operands are numeric and that the operator is one of the following:
add
subtract
multiply
/ divide
It should also check that both operands are between 0 and 1,000,000 (non-inclusive). To perform this data validation, code IsPresent, IsDecimal, and IsValidData methods as described in chapter 7 of the book. In addition, code an IsOperator method that checks for a valid operator.
Code a private method named Calculate that performs the requested operation and returns a decimal value. This method should accept the following arguments:
Argument Description
decimal operand1 The value entered for the first operand. string operator1 One of these four operators: +, -, *, or /. decimal operand2 The value entered for the second operand.
The result of all operations should be rounded to four decimal places.
Heres what I have now:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Simple_Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// input will be converted into strings to display in the textbox
private void btncalculate_Click(object sender, EventArgs e)
{
try
{
if (IsValidData())
{
decimal Operand1 = Convert.ToDecimal(txtNumber1.Text);
decimal Operand2 = Convert.ToDecimal(txtNumber2.Text);
String Operator1 = Convert.ToString(txtOperator.Text);
decimal result = Calculation(Operand1 , Operand2, Operator1);
txtResult.Text = result.ToString("f4");
txtNumber1.Focus();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
}
public bool IsValidData()
{
return IsPresent(txtNumber1, "Operand 1") &&
IsDecimal(txtNumber1, "Operand 1") &&
IsWithinRange(txtNumber1, "Operand 1", 0, 1000000) &&
IsPresent(txtNumber2, "Operand 2") &&
IsDecimal(txtNumber2, "Operand 2") &&
IsWithinRange(txtNumber2, "Operand 2", 0, 1000000) &&
IsOperator(txtNumber1, "Operator 1") &&
IsString(txtNumber1, "Operator 1") &&
IsWithinRange(txtOperator, "Operator", "+", "-", "*", "/");
}
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
public bool IsDecimal(TextBox textBox, string name)
{
try
{
Convert.ToDecimal(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " must be a decimal value.", "Entry Error");
textBox.Focus();
return false;
}
}
public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max)
{
decimal number = Convert.ToDecimal(textBox.Text);
if (number < min || number > max)
{
MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
public bool IsOperator(TextBox textBox, string name)
{
try
{
Convert.ToString(textBox.Text);
return true;
}
catch (FormatException)
{
MessageBox.Show(name + " must be a string.", "Entry Error");
textBox.Focus();
return false;
}
}
// Calculation method showing possible out comes of result
private decimal Calculation(decimal Operand1, decimal Operand2, string Operator1)
{
decimal Result = 0;
switch (Operator1)
{
case "+":
Result = Operand1 + Operand2;
break;
case "-":
Result = Operand1 - Operand2;
break;
case "*":
Result = Operand1 * Operand2;
break;
case "/":
Result = Operand1 / Operand2;
break;
}
}
//close the form
private void btnexit_Click(object sender, EventArgs e)
{
this.Close();
}
//Clearing the textbox;
private void txtNumber1_TextChanged(object sender, EventArgs e)
{
txtOperator.Text = " ";
txtNumber2.Text = " ";
txtResult.Text = " ";
}
private void txtOperator_TextChanged(object sender, EventArgs e)
{
}
private void txtNumber2_TextChanged(object sender, EventArgs e)
{
}
}
}
Here are my errors:
Error 1 The name 'IsString' does not exist in the current context
Error 2 No overload for method 'IsWithinRange' takes 6 arguments
Error 3 'Simple_Calculator.Form1.Calculation(decimal, decimal, string)': not all code paths return a value
Any help is greatly appreciated.
Thanks in advance.