How to make a simple Calculator in C-sharp.Net
Calculator is a
device use to perform arithmetic operations on numbers. It helps to solve large calculations in very
short duration of time . The simplest calculator can do only Addition ,
subtraction ,multiplication and division. More sophisticated calculators can
handle exponential operations, roots,
logarithms, trigonometric functions, and hyperbolic functions. Internally, some
calculators actually perform all of these functions by repeated processes of
addition.
Addition
Subtraction
Multiplication
Division
Percentage
C# Code
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
double Digit1, Digit2, result=0;
string opr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object
sender, EventArgs e)
{
}
private void digit4_Click(object
sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"4";
}
private void
digit8_Click(object sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"8";
}
private void digit0_Click(object
sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"0";
}
private void digit1_Click(object sender,
EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"1";
}
private void digit2_Click(object
sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"2";
}
private void digit3_Click(object sender,
EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"3";
}
private void digit9_Click(object
sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"9";
}
private void digit5_Click(object
sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"5";
}
private void digit6_Click(object sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"6";
}
private void digit7_Click(object
sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"7";
}
private void digit00_Click(object
sender, EventArgs e)
{
if (result != 0)
{
textBox1.Clear();
result = 0;
}
textBox1.Text = textBox1.Text +
"00";
}
private void btnAdd_Click(object
sender, EventArgs e)
{
Digit1 =
Convert.ToDouble(textBox1.Text);
opr = "+";
textBox1.Clear();
}
private void btnEqualTo_Click(object
sender, EventArgs e)
{
Digit2 =
Convert.ToDouble(textBox1.Text);
switch (opr)
{
case "+": result =
Digit1 + Digit2;
textBox1.Text =
result.ToString();
break;
case "*": result =
Digit1 * Digit2;
textBox1.Text =
result.ToString();
break;
case "-": result = Digit1 - Digit2;
textBox1.Text =
result.ToString();
break;
case "/": result =
Digit1 / Digit2;
textBox1.Text =
result.ToString();
break;
case "%": result =
Digit1 * Digit2/100;
textBox1.Text =
result.ToString();
break;
}
}
private void btnClr_Click(object
sender, EventArgs e)
{
textBox1.Clear();
}
private void btnMinus_Click(object
sender, EventArgs e)
{
Digit1 =
Convert.ToDouble(textBox1.Text);
opr = "-";
textBox1.Clear();
}
private void btnMulti_Click(object
sender, EventArgs e)
{
Digit1 =
Convert.ToDouble(textBox1.Text);
opr = "*";
textBox1.Clear();
}
private void btnDiv_Click(object
sender, EventArgs e)
{
Digit1 =
Convert.ToDouble(textBox1.Text);
opr = "/";
textBox1.Clear();
}
private void btnPerc_Click(object
sender, EventArgs e)
{
Digit1 =
Convert.ToDouble(textBox1.Text);
opr = "%";
textBox1.Clear();
}
}
}

No comments:
Post a Comment