class Calculator
{
// To store current number being input
string input = "0";
// To store first input number
string num1 = "0";
// To store second number
string num2 = "0";
// To store the operator
string function = string.empty;
// To initialize the program
public Form1()
{
initializeComponent();
}
// Handling numeric input
public void button_Zero(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "0";
}
else {
input = "0";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_One(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "1";
}
else {
input = "1";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Two(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "2";
}
else {
input = "2";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Three(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "3";
}
else {
input = "3";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Four(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "4";
}
else {
input = "4";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Five(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "5";
}
else {
input = "5";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Six(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "6";
}
else {
input = "6";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Seven(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "7";
}
else {
input = "7";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Eight(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "8";
}
else {
input = "8";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
public void button_Nine(object o, EventArgs e)
{
if (input != "0"){
// Updating input
input = input + "9";
}
else {
input = "9";
}
this.textBox.Text = "";
this.textBox.Text += input;
}
// Handling operator input
public void button_Plus(object o, EventArgs e)
{
num1 = input;
input = "0";
this.textBox.Text = "";
function = "+";
}
public void button_Minus(object o, EventArgs e)
{
num1 = input;
input = "0";
this.textBox.Text = "";
function = "-";
}
public void button_Multiply(object o, EventArgs e)
{
num1 = input;
input = "0";
this.textBox.Text = "";
function = "*";
}
public void button_Divide(object o, EventArgs e)
{
num1 = input;
input = "0";
this.textBox.Text = "";
function = "/";
}
// Handling result
public void button_Equal(object o, EventArgs e)
{
num2 = input;
if (function != string.empty){
double n1, n2, result;
try {
// Convert to double data type
double.TryParse(num1, n1);
double.TryParse(num2, n2);
// Checking operator and calculating output
switch(function){
case "+":
this.textBox.Text = "";
result = n1 + n2;
this.textBox.Text = result.ToString();
break;
case "-":
this.textBox.Text = "";
result = n1 - n2;
this.textBox.Text = result.ToString();
break;
case "*":
this.textBox.Text = "";
result = n1 * n2;
this.textBox.Text = result.ToString();
break;
case "/":
this.textBox.Text = "";
result = n1 / n2;
this.textBox.Text = result.ToString();
break;
}
}
catch { // Handling Math Errors
this.textBox.Text = "Math Error"
input = "";
num1 = "";
num2 = "";
function = "";
}
}
}
// Setting up clear button
public void button_Clear(object o, EventArgs e)
{
input = "";
num1 = "";
num2 = "";
function = "";
this.textBox.Text = "";
}
}
0 yorum :
Yorum Gönder