Desktop
Calculator
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 Calculator
{
public partial class CalculatorForm : Form
{
Double value = 0;
Double memory = 0;
Double register = 0;
Boolean clearDisplay = false;
enum _operation
{
multiply,
divide,
subtract,
add
}
_operation operation = _operation.add;
public CalculatorForm()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
register = 0;
value = 0;
txtDisplay.Text = register.ToString();
}
private void btnClearMemory_Click(object sender, EventArgs e)
{
memory = 0;
}
private void btnAddToMemory_Click(object sender, EventArgs e)
{
memory += register;
}
private void btnSubtractFromMemory_Click(object sender, EventArgs e)
{
memory += register;
}
private void btnRecallMemory_Click(object sender, EventArgs e)
{
register = memory;
txtDisplay.Text = register.ToString();
}
private void btnMultiply_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.multiply;
}
private void btnDivide_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.divide;
}
private void btnSubtract_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.subtract;
}
private void btnAdd_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.add;
}
private void Calculate()
{
if (value == 0)
{
value = register;
}
else
{
switch (operation)
{
case _operation.add:
value += register;
break;
case _operation.subtract:
value -= register;
break;
case _operation.multiply:
value *= register;
break;
case _operation.divide:
value /= register;
break;
}
}
txtDisplay.Text = value.ToString();
register = 0;
clearDisplay = true;
}
private void btnChangeSign_Click(object sender, EventArgs e)
{
register = -register;
txtDisplay.Text = register.ToString();
}
private void btnSquareRoot_Click(object sender, EventArgs e)
{
register = Math.Sqrt(register);
txtDisplay.Text = register.ToString();
}
private void btnSquare_Click(object sender, EventArgs e)
{
register = register * register;
txtDisplay.Text = register.ToString();
}
private void btnEquals_Click(object sender, EventArgs e)
{
Calculate();
txtDisplay.Text = value.ToString();
register = 0;
}
private void btnPeriod_Click(object sender, EventArgs e)
{
AddDigit(".");
}
private void btn0_Click(object sender, EventArgs e)
{
AddDigit("0");
}
private void btn1_Click(object sender, EventArgs e)
{
AddDigit("1");
}
private void btn2_Click(object sender, EventArgs e)
{
AddDigit("2");
}
private void btn3_Click(object sender, EventArgs e)
{
AddDigit("3");
}
private void btn4_Click(object sender, EventArgs e)
{
AddDigit("4");
}
private void btn5_Click(object sender, EventArgs e)
{
AddDigit("5");
}
private void btn6_Click(object sender, EventArgs e)
{
AddDigit("6");
}
private void btn7_Click(object sender, EventArgs e)
{
AddDigit("7");
}
private void btn8_Click(object sender, EventArgs e)
{
AddDigit("8");
}
private void btn9_Click(object sender, EventArgs e)
{
AddDigit("9");
}
private void AddDigit(String digit)
{
// If we just had clicked an operation button (+-*/) then we should clear the display:
if (clearDisplay)
{
txtDisplay.Text = "";
clearDisplay = false;
}
// Get the current displayed value:
String sValue = txtDisplay.Text;
// Only one period is allowed, skip out if extra period is clicked:
if (sValue.Contains('.') && digit == ".")
{
return;
}
// Add the digit or period user clicked:
sValue += digit;
// Convert the string to the new value:
register = Double.Parse(sValue);
// Show the new string in the display:
txtDisplay.Text = sValue;
}
private void btnBackspace_Click(object sender, EventArgs e)
{
// Get the current displayed value:
String sValue = txtDisplay.Text;
// Remove last character:
sValue = sValue.Remove(sValue.Length - 1);
// Convert the string to the new value:
register = Double.Parse(sValue);
// Show the new string in the display:
txtDisplay.Text = sValue;
}
}
}
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 Calculator
{
public partial class CalculatorForm : Form
{
Double value = 0;
Double memory = 0;
Double register = 0;
Boolean clearDisplay = false;
enum _operation
{
multiply,
divide,
subtract,
add
}
_operation operation = _operation.add;
public CalculatorForm()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
register = 0;
value = 0;
txtDisplay.Text = register.ToString();
}
private void btnClearMemory_Click(object sender, EventArgs e)
{
memory = 0;
}
private void btnAddToMemory_Click(object sender, EventArgs e)
{
memory += register;
}
private void btnSubtractFromMemory_Click(object sender, EventArgs e)
{
memory += register;
}
private void btnRecallMemory_Click(object sender, EventArgs e)
{
register = memory;
txtDisplay.Text = register.ToString();
}
private void btnMultiply_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.multiply;
}
private void btnDivide_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.divide;
}
private void btnSubtract_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.subtract;
}
private void btnAdd_Click(object sender, EventArgs e)
{
Calculate();
operation = _operation.add;
}
private void Calculate()
{
if (value == 0)
{
value = register;
}
else
{
switch (operation)
{
case _operation.add:
value += register;
break;
case _operation.subtract:
value -= register;
break;
case _operation.multiply:
value *= register;
break;
case _operation.divide:
value /= register;
break;
}
}
txtDisplay.Text = value.ToString();
register = 0;
clearDisplay = true;
}
private void btnChangeSign_Click(object sender, EventArgs e)
{
register = -register;
txtDisplay.Text = register.ToString();
}
private void btnSquareRoot_Click(object sender, EventArgs e)
{
register = Math.Sqrt(register);
txtDisplay.Text = register.ToString();
}
private void btnSquare_Click(object sender, EventArgs e)
{
register = register * register;
txtDisplay.Text = register.ToString();
}
private void btnEquals_Click(object sender, EventArgs e)
{
Calculate();
txtDisplay.Text = value.ToString();
register = 0;
}
private void btnPeriod_Click(object sender, EventArgs e)
{
AddDigit(".");
}
private void btn0_Click(object sender, EventArgs e)
{
AddDigit("0");
}
private void btn1_Click(object sender, EventArgs e)
{
AddDigit("1");
}
private void btn2_Click(object sender, EventArgs e)
{
AddDigit("2");
}
private void btn3_Click(object sender, EventArgs e)
{
AddDigit("3");
}
private void btn4_Click(object sender, EventArgs e)
{
AddDigit("4");
}
private void btn5_Click(object sender, EventArgs e)
{
AddDigit("5");
}
private void btn6_Click(object sender, EventArgs e)
{
AddDigit("6");
}
private void btn7_Click(object sender, EventArgs e)
{
AddDigit("7");
}
private void btn8_Click(object sender, EventArgs e)
{
AddDigit("8");
}
private void btn9_Click(object sender, EventArgs e)
{
AddDigit("9");
}
private void AddDigit(String digit)
{
// If we just had clicked an operation button (+-*/) then we should clear the display:
if (clearDisplay)
{
txtDisplay.Text = "";
clearDisplay = false;
}
// Get the current displayed value:
String sValue = txtDisplay.Text;
// Only one period is allowed, skip out if extra period is clicked:
if (sValue.Contains('.') && digit == ".")
{
return;
}
// Add the digit or period user clicked:
sValue += digit;
// Convert the string to the new value:
register = Double.Parse(sValue);
// Show the new string in the display:
txtDisplay.Text = sValue;
}
private void btnBackspace_Click(object sender, EventArgs e)
{
// Get the current displayed value:
String sValue = txtDisplay.Text;
// Remove last character:
sValue = sValue.Remove(sValue.Length - 1);
// Convert the string to the new value:
register = Double.Parse(sValue);
// Show the new string in the display:
txtDisplay.Text = sValue;
}
}
}