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;
using Jay.Puduvalli.Math;
namespace SimpleCalculator
{
///
/// Written by: Jay Puduvalli
/// Date: 10/15/2009
///
public partial class SimpleCalculator : Form
{
private bool IsComputed = false;
public SimpleCalculator()
{
InitializeComponent();
}
private void SimpleCalculator_Load(object sender, EventArgs e)
{
}
private void btnNumber3_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber3);
}
private void EnableOperatorButtons(bool enable)
{
btnAdd.Enabled = enable;
btnSubtract.Enabled = enable;
btnMultiply.Enabled = enable;
btnDivide.Enabled = enable;
btnEquals.Enabled = enable;
}
///
/// Display the text in the label box and disable/enable the operator buttons
///
/// private void AddNumbersToDisplay(Button btn)
{
if (!IsComputed)
{
lblDisplay.Text += btn.Text;
EnableOperatorButtons(true);
}
}
private void AddOperatorToDisplay(Button btn)
{
if (!IsComputed)
{
lblDisplay.Text += btn.Text;
btnDecimalPoint.Enabled = true;
EnableOperatorButtons(false);
}
}
private void Reset()
{
IsComputed = false;
lblDisplay.Text = "";
btnDecimalPoint.Enabled = true;
}
private void btnNumber7_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber7);
}
private void btnNumber8_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber8);
}
private void btnNumber9_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber9);
}
private void btnNumber4_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber4);
}
private void btnNumber5_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber5);
}
private void btnNumber6_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber6);
}
private void btnNumber1_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber1);
}
private void btnNumber2_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber2);
}
private void btnNumber0_Click(object sender, EventArgs e)
{
AddNumbersToDisplay(btnNumber0);
}
private void btnDivide_Click(object sender, EventArgs e)
{
AddOperatorToDisplay(btnDivide);
}
private void btnMultiply_Click(object sender, EventArgs e)
{
AddOperatorToDisplay(btnMultiply);
}
private void btnSubtract_Click(object sender, EventArgs e)
{
AddOperatorToDisplay(btnSubtract);
}
private void btnAdd_Click(object sender, EventArgs e)
{
AddOperatorToDisplay(btnAdd);
}
private void btnDecimalPoint_Click(object sender, EventArgs e)
{
AddOperatorToDisplay(btnDecimalPoint);
btnDecimalPoint.Enabled = false;
}
///
/// Since the operator buttons are disabled, the label box can be trusted as a valid storage
/// Parse and extract the numbers. Use the public class Compute that has static methods to get
/// the result
///
/// /// private void btnEquals_Click(object sender, EventArgs e)
{
try
{
if (IsComputed)
return;
string resultsDisplay = lblDisplay.Text.Trim();
IsComputed = true;
if (resultsDisplay.Contains("+"))
{
string[] numbers = resultsDisplay.Split(new Char[]{'+'});
lblDisplay.Text+=btnEquals.Text;
lblDisplay.Text+=Compute.Add(float.Parse(numbers[0]),float.Parse(numbers[1])).ToString();
}
if (resultsDisplay.Contains("-"))
{
string[] numbers = resultsDisplay.Split(new Char[] { '-' });
lblDisplay.Text += btnEquals.Text;
lblDisplay.Text += Compute.Subtract(float.Parse(numbers[0]), float.Parse(numbers[1])).ToString();
}
if (resultsDisplay.Contains("*"))
{
string[] numbers = resultsDisplay.Split(new Char[] { '*' });
lblDisplay.Text += btnEquals.Text;
lblDisplay.Text += Compute.Multiply(float.Parse(numbers[0]), float.Parse(numbers[1])).ToString();
}
if (resultsDisplay.Contains("/"))
{
string[] numbers = resultsDisplay.Split(new Char[] { '/' });
lblDisplay.Text += btnEquals.Text;
lblDisplay.Text += Compute.Divide(float.Parse(numbers[0]), float.Parse(numbers[1])).ToString();
}
btnEquals.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
private void btnClear_Click(object sender, EventArgs e)
{
Reset();
}
}
}
The Compute class that uses the static method is shown below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jay.Puduvalli.Math
{
///
/// Written by: Jay Puduvalli
/// Dated: 10/15/2009
/// The reason for having a separate class library is demonstrate the programmer's Object Oriented knowledge
/// and decoupling of code from the UI layer
///
public class Compute
{
public Compute()
{
}
public static float Add(float firstNumber, float secondNumber)
{
return (firstNumber + secondNumber);
}
public static float Subtract(float firstNumber, float secondNumber)
{
return firstNumber - secondNumber;
}
public static float Multiply(float firstNumber, float secondNumber)
{
return (firstNumber * secondNumber);
}
public static float Divide(float firstNumber, float secondNumber)
{
return (firstNumber / secondNumber);
}
}
}
No comments:
Post a Comment