Calculator Design phase
Coding phase
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 calculator_program
{
public partial class Form1 : Form
{
String opr;
double operand1, operand2, result;
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
// textBox1.Text = string.Empty;
textBox1.Text = "0";
}
else
{
textBox1.Text += "0";
}
}
private void btn1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "1";
}
else
{
textBox1.Text += "1";
}
}
private void btn2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "2";
}
else
{
textBox1.Text += "2";
}
}
private void btn3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "3";
}
else
{
textBox1.Text += "3";
}
}
private void btn4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "4";
}
else
{
textBox1.Text += "4";
}
}
private void btn5_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "5";
}
else
{
textBox1.Text += "5";
}
}
private void btn6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "6";
}
else
{
textBox1.Text += "6";
}
}
private void btn7_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "7";
}
else
{
textBox1.Text += "7";
}
}
private void btn8_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "8";
}
else
{
textBox1.Text += "8";
}
}
private void btn9_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
{
textBox1.Text = string.Empty;
textBox1.Text = "9";
}
else
{
textBox1.Text += "9";
}
}
private void btnplus_Click(object sender, EventArgs e)
{
operand1 = Convert.ToDouble(textBox1.Text);
opr = "+";
textBox1.Clear();
}
private void btnmin_Click(object sender, EventArgs e)
{
operand1 = Convert.ToDouble(textBox1.Text);
opr = "-";
textBox1.Clear();
}
private void btnmul_Click(object sender, EventArgs e)
{
operand1 = Convert.ToDouble(textBox1.Text);
opr = "*";
textBox1.Clear();
}
private void btndiv_Click(object sender, EventArgs e)
{
operand1 = Convert.ToDouble(textBox1.Text);
opr = "/";
textBox1.Clear();
}
private void btnc_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void buttondot_Click(object sender, EventArgs e)
{
if (textBox1.Text.Contains("."))
{
textBox1.Text += "0.";
}
else
{
textBox1.Text += ".";
}
}
private void btnprod_Click(object sender, EventArgs e)
{
operand1 = Convert.ToDouble(textBox1.Text);
opr = "%";
textBox1.Clear();
}
private void btnequal_Click(object sender, EventArgs e)
{
operand2 = Convert.ToDouble(textBox1.Text);
switch (opr)
{
case"+":
result = operand1 + operand2;
textBox1.Text = Convert.ToString(result);
break;
case "-":
result = operand1 - operand2;
textBox1.Text = Convert.ToString(result);
break;
case "*":
result = operand1 * operand2;
textBox1.Text = Convert.ToString(result);
break;
case "/":
if (operand2 == 0)
{
textBox1.Text = "0.0";
break;
}
else
{
result = operand1 / operand2;
textBox1.Text = Convert.ToString(result);
break;
}
case "%":
result = operand1 % operand2;
textBox1.Text = Convert.ToString(result);
break;
}
}
}
}

Comments
Post a Comment