!
Calculator Project
Allan 56090
Introduction
This Visual Studio 2019 C# calculator program will take two or one
number and one operator such as +, - , / , * , ^, %, ! and calculate
them.
Example;
1 + 1 = 2
8 - 4 = 4
12 / 2 = 6
2 * 4 = 8
10 ^ 1 = 10
50 % 100 = 50
5 ! = 120
!
Photos
Photo1
(before adding anything)
Photo2
(after calculating two numbers)
5+5=10
Code and Algorithm
Initially I declared variables, then took one or two numbers (first number and second
number ) and use operations ( case & mathematical functions ) with the buttons to one
TextBox (text.Display) and calculate them (return result). This program uses switch,
function method.
!
String operation = "";
"
Double firstnumber, secondnumber;
Means
Calculator will takes 2 numbers and does an operation with them.
InitializeComponent();
Is a method which is used to initialize your form.
It might be setting up things like the buttons, labels, event handlers,
and so on on your User Interface.
Button b = (Button)sender;
Get the text value of the button
that was clicked
Sender refers to the object that
invoked the event that fired the
event handler.
Button b = (Button)sender;
firstnumber = Double.Parse(txtDisplay.Text);
operation = b.Text;
txtDisplay.Text = “";
firstnumber = Double.Parse(txtDisplay.Text);
Parsing to double
operation = b.Text;
if number includes point ( . )
txtDisplay.Text = “”;
Equals
!
Switch Case Syntax
The expression can be integer expression or a character expression.
In each case, the break keyword denotes the end of that case. Once the case has
been completed, the control will fall out of the switch and the case will be
terminated.
The default case is one that can be turned off. When the value of test-expression
does not match any of the switch's cases, the default is used.
Otherwise, default should not be written in the switch.
When the switch is activated, control is passed to statement-x, and the program
execution continues.
Statement such as;
1. Text = string. Empty;
2. Clear();
3. Text = "";
Will clear the text of the text box on button click
This Close();
Exits the program.
Code
public partial class Form1 : Form
{
String operation = "";
Double firstnumber, secondnumber;
public Form1()
{
InitializeComponent();
}
private void NumericValue(object sender, EventArgs e)
{
Button b = (Button)sender;
if (txtDisplay.Text == "0")
txtDisplay.Text = "";
if (b.Text == ".")
{
if (!txtDisplay.Text.Contains("."))
txtDisplay.Text = txtDisplay.Text + b.Text;
}
else
{
txtDisplay.Text = txtDisplay.Text + b.Text;
}
}
private void Operational_function(object sender, EventArgs e)
{
Button b = (Button)sender;
firstnumber = Double.Parse(txtDisplay.Text);
operation = b.Text;
txtDisplay.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
secondnumber = double.Parse(txtDisplay.Text);
switch (operation)
{
case "+":
txtDisplay.Text = Convert.ToString(firstnumber + secondnumber);
break;
case "-":
txtDisplay.Text = Convert.ToString(firstnumber - secondnumber);
break;
case "/":
txtDisplay.Text = Convert.ToString(firstnumber / secondnumber);
break;
case "%":
txtDisplay.Text = Convert.ToString(firstnumber % secondnumber);
break;
case "x":
txtDisplay.Text = Convert.ToString(firstnumber * secondnumber);
break;
case "^":
txtDisplay.Text = Math.Pow(firstnumber, secondnumber).ToString();
break;
default:
break;
}
}
private int Calculate_Factorial(int num)
{
// 5 = 1 times 2 Times 3 times 4 times 5
int answer = 1;
for (int x = 1; x <= num; x = x + 1)
answer = answer * x;
return answer;
}
private void btnfactional_Click(object sender, EventArgs e)
{
int fact, answer;
fact = int.Parse(txtDisplay.Text);
answer = Calculate_Factorial(fact);
txtDisplay.Text = answer.ToString();
}
private void btnclear_Click(object sender, EventArgs e)
{
txtDisplay.Text = "0";
string f, s;
s = Convert.ToString(secondnumber);
f = Convert.ToString(firstnumber);
s = "";
f = "";
}
private void btnEnd_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtDisplay.TextLength > 0)
{
txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.Text.Length - 1, 1);
}
if (txtDisplay.Text == "")
txtDisplay.Text = "0";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}