Develop a calculator application using C#
Program:
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 Form1 : Form
{
bool eq = false;
double a = 0, b;
char op = ')';
public Form1()
{
InitializeComponent();
}
private void button19_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
return;
else
{
if (eq == true)
{
textBox1.Text = "0";
}
b = Convert.ToDouble(textBox1.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "1";
else
{
if (eq == true)
{
textBox1.Text = "1";
eq = false;
}
else
textBox1.Text += "1";
}
b = Convert.ToDouble(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "2";
else
{
if (eq == true)
{
textBox1.Text = "2";
eq = false;
}
else
{
textBox1.Text += "2";
}
}
b = Convert.ToDouble(textBox1.Text);
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "3";
else
{
if (eq == true)
{
textBox1.Text = "3";
eq = false;
}
else
{
textBox1.Text += "3";
}
}
b = Convert.ToDouble(textBox1.Text);
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "4";
else
{
if (eq == true)
{
textBox1.Text = "4";
eq = false;
}
else
{
textBox1.Text += "4";
}
}
b = Convert.ToDouble(textBox1.Text);
}
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "5";
else
{
if (eq == true)
{
textBox1.Text = "5";
eq = false;
}
else
{
textBox1.Text += "5";
}
}
b = Convert.ToDouble(textBox1.Text);
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "6";
else
{
if (eq == true)
{
textBox1.Text = "6";
eq = false;
}
else
textBox1.Text += "6";
}
b = Convert.ToDouble(textBox1.Text);
}
private void button7_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "7";
else
{
if (eq == true)
{
textBox1.Text = "7";
eq = false;
}
else
textBox1.Text += "7";
}
b = Convert.ToDouble(textBox1.Text);
}
private void button8_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "8";
else
{
if (eq == true)
{
textBox1.Text = "8";
eq = false;
}
else
textBox1.Text += "8";
}
b = Convert.ToDouble(textBox1.Text);
}
private void button9_Click(object sender, EventArgs e)
{
if (textBox1.Text == "0")
textBox1.Text = "9";
else
{
if (eq == true)
{
textBox1.Text = "9";
eq = false;
}
else
textBox1.Text += "9";
}
b = Convert.ToDouble(textBox1.Text);
}
private void button20_Click(object sender, EventArgs e)
{
if (eq == true)
{
textBox1.Text = "0";
eq = false;
}
else
{
if (textBox1.Text.IndexOf(".") == -1)
{
textBox1.Text += ".";
}
}
b = Convert.ToDouble(textBox1.Text);
}
private void button10_Click(object sender, EventArgs e)
{
eq = false;
a = 0;
b = 0;
op = '(';
textBox1.Text = "0";
}
private void button11_Click(object sender, EventArgs e)
{
if (eq == true)
{
a = Convert.ToDouble(textBox1.Text);
}
else
{
calc();
}
op = '+';
eq = true;
}
void cal()
{
switch (op)
{
case '+':
a = a + b;
textBox1.Text =
a.ToString();
break;
case '-':
a = a - b;
textBox1.Text =
a.ToString();
break;
case '*':
a = a * b;
textBox1.Text =
a.ToString();
break;
case '/':
a = a / b;
textBox1.Text =
a.ToString();
break;
case '^':
a = Math.Pow(a, b);
textBox1.Text =
a.ToString();
break;
case '#':
a = Math.Sqrt(b);
textBox1.Text =
a.ToString();
break;
default:
a = b;
break;
}
}
private void button25_Click(object sender, EventArgs e)
{
switch (op)
{
case '+':
a = a + b;
textBox1.Text =
a.ToString();
break;
case '-':
a = a - b;
textBox1.Text =
a.ToString();
break;
case '*':
a = a * b;
textBox1.Text =
a.ToString();
break;
case '/':
a = a / b;
textBox1.Text = a.ToString();
break;
case '^':
a = Math.Pow(a, b);
textBox1.Text =
a.ToString();
break;
case 's':
a = Math.Sqrt(a);
textBox1.Text = a.ToString();
break;
default:
a = b;
break;
}
}
private void button12_Click(object sender, EventArgs e)
{
if (eq == true)
{
a = Convert.ToDouble(textBox1.Text);
}
else
{
calc();
}
op = '-';
eq = true;
}
private void button13_Click(object sender, EventArgs e)
{
if (eq == true)
{
a = Convert.ToDouble(textBox1.Text);
}
else
{
calc();
}
op = '*';
eq = true;
}
private void button14_Click(object sender, EventArgs e)
{
if (eq == true)
{
a = Convert.ToDouble(textBox1.Text);
}
else
{
calc();
}
op = '/';
eq = true;
}
private void button15_Click(object sender, EventArgs e)
{
if (eq == true)
{
a = Convert.ToDouble(textBox1.Text);
op = ')';
}
{
calc();
b = a;
}
// a =
Convert.ToDouble(textBox1.Text);
op = '^';
//a=Math.Sqrt(a);
//textBox1.Text=a.ToString();
//op=')';
eq = true;
}
private void button17_Click(object sender, EventArgs e)
{
if (eq == true)
{
a = Convert.ToDouble(textBox1.Text);
op = ')';
}
{
calc();
b = a;
}
a = Convert.ToDouble(textBox1.Text);
a = Math.Sqrt(a);
textBox1.Text = a.ToString();
op = ')';
eq = true;
}
private void calc()
{
switch (op)
{
case '+':
a = a + b;
textBox1.Text =
a.ToString();
break;
case '-':
a = a - b;
textBox1.Text =
a.ToString();
break;
case '*':
a = a * b;
textBox1.Text =
a.ToString();
break;
case '/':
a = a / b;
textBox1.Text =
a.ToString();
break;
case '^':
a = Math.Pow(a, b);
textBox1.Text =
a.ToString();
break;
case 's':
a = Math.Sqrt(a);
textBox1.Text =
a.ToString();
break;
default:
a = b;
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "0";
}
private void button16_Click(object sender, EventArgs e)
{
if (eq != true)
{
if (textBox1.Text.Length != 1)
textBox1.Text =
textBox1.Text.Substring(0, textBox1.Text.Length - 1);
else
textBox1.Text = "0";
}
b = Convert.ToDouble(textBox1.Text);
}
}
}
No comments:
Post a Comment