Documente Academic
Documente Profesional
Documente Cultură
Assignment 01
Introduction
I have design the standard calculator in windows form application using C#. It performs the
basic operations. It includes Keys ÷, x. +,-, ±, =, C, CE, Dot (.) , Backspace and 0-9 digits. A
text box to show the results.
Requirements
All arithmetic operations should be performed correctly.
Equal button should calculate the result
Clear should clear text box as well as all the results stored previously
Clear Entry should clear the entered value
Backspace should clear the recent added value
± should negate the value (perform opposite every time clicked)
Dot(.) should enter only once in a value
Functionalities
I modified the operations as according to Windows 10 Calculator. It will have a default zero on
the text box.
Operators The coding for the operators click event is done such that if the user entered
an operator like +, - etc. and again entered the operator without entering value the
operator will be change to the recent entered. The user doesn’t need to use the
Backspace. The entered operator with values shown on the label. If user enters the
operator without first entering the value it will be calculated with default zero. If equal
operation is performed, then operator is pressed it will hold the previous value and
perform operations according to it.
Operations Any operation like +, -, x etc. will be performed on every entered two values
every time user enters the next operator or equal button it shows the result in label or
textbox. It will perform operation right on the value on the text box after the equal button
is clicked or not.
Equal Button This will perform the operation on the values and
o When user doesn’t enter the C, CE then if user again enter any value the result will
be erase from the text box and everything will be set to default. Equal button will
erase the label but result will be stored in it if user press it again it will calculate a
new result on previous result and last entered operator.
Backspace The backspace will erase the recent added values, but it will not backspace in
two conditions:
o When operator is entered it will not erase the operator(user can change it as explain
above)
o When user press the equal button the result is shown on screen
Clear Entry (CE) will clean the recent typed value but not clear the previously added
values and operator (unless user changes the operator). If user will press CE and then
enter value it will be performed with the previous values. The operator can be changed.
Dot (.) will perform same operation. If an operator is entered then a dot then it will
append zero before it. It will also perform one more function
o If result is performed or equal button is pressed, then if dot is entered by user it will
set all to default and the operation will be carried on the new values.
Design
Below is the detail of which control is used for which purpose and what is their layout:
Form Design Display name of the form is set as Calculator. It has the icon as calculator
picture and minimize, maximize, close buttons. It is set as size able. Its back color is set
as Control. It will be shown on windows taskbar. It has a Default Cursor.
Flow Layout Panel It is used to arrange all the buttons. Its Conventional name is set as
(flpanelcalcultr). Its back color set same as form that is control.
Buttons Twenty Buttons are used 10 for 0-9 digits others for C, CE, Dot (.), Backspace
and for operators +,-, x, ÷, ±. Their text and naming conventions are also according to
them changed (like for button divide convention is btndiv). All buttons have Tab and
focused on. A picture is added for the Backspace button.
o Back color the digits have white back color whereas other have white smoke.
o Size Their size is set as 119, 58.
o Font Style The text on the digits, C, CE, Dot (.) is Font size 12, Bold, Black color,
whereas remaining has size 14, Bold, Dim Grey color.
o Appearance on mouse event The back color of the digits, C, CE, Dot (.) and back
space will be change to Light Grey color when mouse enters the box when leave it
go back to original and for others operators it will be change to Cornflower Blue. It
is done with the events (in the code).
Text Box It’s for showing the values user entered and the result but not the operators.
The name convention is set as txtboxresult. It text is set as 0, which will be treated as a
default. Text is align to right side as 0. Back color is set same as the form Control. Font
size is 20, Bold and Black in color. It is set as Read only.
Label Two labels are used. Label 1 whose name is set as lblvalues is used to display the
values and the operators entered by the user. Its back color is set same as form. Its Font
size is 12, Regular, Grey color.
Implementation
From the above specified functionalities code is implemented.
}
Explanation Clear function is used that clear all the values on the text box named as
txtboxresult. In next line text box will be set as to default 0. It will also clear all other values and
set the operations as false.
For Dot(.)
For the Dot event we have one that is btndot_Click:
private void btndot_Click(object sender, EventArgs e)
{ if (operation)
{ txtboxresult.Text = "0";
operation = false;
}
if (rsltperformed)
{
rsltperformed = false;
operatr = "";
txtboxresult.Text = "0";
value = 0;
}
if (txtboxresult.Text.Contains( ".")) {
return;
}
else
txtboxresult.Text = txtboxresult.Text + "."; }
Explanation for the dot we only need to make sure it is not repeated in a value Contains
function is used which have argument any value or symbol to check whether it is in the calling
text, its return type is bool.
In first condition if operation is true text box will be set as zero and operation set as false.
In second condition if the result is performed then user press the dot then it’ll reset all the
variables set the rsltperformed false and operator variable equals to null and value set to
equal to start function from the start.
In the next condition it check if txtboxresult has already dot in value if Contains returns
True it will return from the function and nothing will happen if False it will add the dot.
For Backspace
Backspace will erase one digit every time clicked so it has one event below
private void btnbackspace_Click(object sender, EventArgs e)
{
if (txtboxresult.Text.Length != 0 && !operation && !rsltperformed) //if
operator or result event is clicked it will not backspace the value
{
txtboxresult.Text = txtboxreult.Text.Remove(txtboxresult.Text.Length- 1);
if (txtboxresult.TextLength == 0)
txtboxresult.Text = "0";
}
else
return; //return from the function
}
Explanation According to above defined conditions if equal button is clicked result is shown
and operator is clicked it will not backspace. In the first if condition Length function is used
which return the length of string it should not be zero and operator and equal button should not
be performed (&& is used because all should conditions should satisfy otherwise it will erase the
text) all are true text box length will be removed by 1 by using Remove function which remove
the characters from the specified length or position like here it is – 1 which will remove only at a
time.
For ± (negation)
Negate will simply reverse the positive to negative and vice versa. We only need to multiply
given text with -1 every time it is clicked. It has following event
private void btnnegate_Click(object sender, EventArgs e)
{
double temp = 0;
if (txtboxresult.Text != "0") //doesn't perform for zero
{
temp = double.Parse(txtboxresult.Text.ToString()) * -1; //multiply text
box value by -1
txtboxresult.Text = temp.ToString();
}
else
return; //return from the function
}
Explanation temp is taken as double to store the value of the textbox when multiply by -1.
Firstly, a condition is used that text box value shoul not be zero else will return from the function
because zero isn’t negative, then txtboxresult is converted to double value by using Parse
function. Then multiply by -1 and the temp value is assigned to txtboxresult.
First Condition (Explanation) First a type casting is used to have the text of the entered button.
If operation is true means the operator is entered again so this condition is written to change the
operator. Operatr variable is used to store the entered operator, then label’s text property is used
to set the label text equal to previous label value and entered value of the new operator.
Second Condition (Explanation) Then else if used for second condition which will store the
entered operator entered for first time on given certain conditions. The value is used to store the
previous entered digit by user when user not enter something or just entered the first digit it will
remain zero. According to given condition when value is not zero or in case value equal to zero
then it also have condition operator should not equal to null it executes the statement because if
value is equal to zero user enter an operator first and then enters some positive digit it will
operate with the values default 0 (e.g. opratr= -, value=0, digit=8, operatr=+ ,digit=3 result
should be minus five -5 as user first entered – then 8 but if we don’t have this condition it
will give 11 ignoring first entered minus). Then it performs operation on operator previously
stored and value and present textbox by btnequal event, PerformClick is function which
performs the required function. Then operatr is set to newly entered operatr and label shows the
values by concatenating the label’s text so previous value is not lost and value assigned to
variable so when CE is pressed previous value stored in variable value can be display on Text
box as CE only clear the text not previous history.
Operation will be performed on stored value and the operator previously as it calculates for every
two entered values. This equal operation invokes when the operator is entered for second time.
else if (value != 0 || (operatr != "" && value == 0)) //when value is not
zero or when value iz zero but operator event occured
{
btnequal.PerformClick(); //btnequal event is raised to calculate first
entered values
value = double.Parse(txtboxresult.Text.ToString()); //store the first
entered text
operatr = btnop.Text; //store the operator
operation = true; //operation set as true;
lblvalues.Text = lblvalues.Text + value + " " + operatr; //display
values to label text
Third Condition (Explanation)
else if (rsltperformed)
{
rsltperformed = false; //if equal operation is perform then operator is
clicked it starts from current value without performing any operation
operatr = btnop.Text; //store the operator
operation = true; //operation set as true;
value = double.Parse(txtboxresult.Text.ToString());
lblvalues.Text = lblvalues.Text +value + " " + operatr; //display values
to label
}
It is third condition that if user perform equal operation and then again pressed the operator then
this condition will check if equal operation is performed then operator is clicked it starts from
current value without performing any operation and then assign the entered to operatr variable and
displaying on it label. It will assign the text value to variable value so when user just enter the text
right again after equal (=) it will be stored.
Fourth Condition (Explanation) When all above false when value is equal to zero and operator
is entered for the first time it simply stored its value and value variable stores the textbox value
for operation to perform. Operation will be set as true.
else
{
value = double.Parse(txtboxresult.Text.ToString());
operatr = btnop.Text; //store the operator
operation = true; //operation set as true;
lblvalues.Text = lblvalues.Text + txtboxresult.Text + " " + operatr;
//display values to label
}
}
switch (operatr)
{
case "÷":
value = value/Convert.ToDouble(txtboxresult.Text.ToString());
txtboxresult.Text=value.ToString();
break;
case "x":
value = value *double.Parse(txtboxresult.Text.ToString());
txtboxresult.Text=value.ToString();
break;
case "+":
value = value + double.Parse(txtboxresult.Text.ToString());
txtboxresult.Text=value.ToString();
break;
case "-":
value = value- double.Parse(txtboxresult.Text.ToString());
txtboxresult.Text=value.ToString();
break;
default:
break;
}
rsltperformed = true; //as event is clicked
lblvalues.Text =""; //to clear the label text
}
Explanation A switch statement with all possible operators has been used. Operation will be
performed on stored value and the operator previously as it calculates for every two entered
values. This equal operation invokes when the operator is entered for second time.
}
private void btnopr_MouseLeave(object sender, EventArgs e) //function to change
the digits button backcolor when mouse leaves
{
Button btn = (Button)sender;
btn.BackColor = Color.WhiteSmoke;
}
CALCULATOR
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 Calculator
{
public partial class Form1 : Form
{
string operatr = ""; //to store the operator
double value = 0; //to store the previous operand
bool operation = false; //for the operator event to be performed or not
bool rsltperformed = false; //for result operation
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ }
private void btnclear_Click(object sender, EventArgs e)
{
txtboxresult.Clear(); //to clear textbox
txtboxresult.Text = "0"; //set a default zero
lblvalues.Text = ""; //clear the label text
value = 0; //to clear previous result
operation = false; //set to default
operatr = ""; //set to null
}
private void btncentry_Click(object sender, EventArgs e)
{
if (txtboxresult.Text.Contains( ".")) //if dot is already in text return from the function
{
return;
}
else
txtboxresult.Text = txtboxresult.Text + "."; //else append the dot
}
private void btnbackspace_Click(object sender, EventArgs e)
{
if (txtboxresult.Text.Length != 0 && !operation && !rsltperformed) //if operator or result
event is clicked it will not backspace the value
{
txtboxresult.Text = txtboxresult.Text.Remove(txtboxresult.Text.Length - 1);
if (txtboxresult.TextLength == 0)
txtboxresult.Text = "0";}
else
return; //return from the function }
private void btnnegate_Click(object sender, EventArgs e)
{
double temp = 0;
if (txtboxresult.Text != "0") //doesn't perform for zero
{
temp = double.Parse(txtboxresult.Text.ToString()) * -1; //multiply text box value by -1
txtboxresult.Text = temp.ToString();
}
else
return; //return from the function }
private void btnoperator_click(object sender, EventArgs e)
{
Button btnop = (Button)sender;
if (operation) //when operator is clicked again it will update its value
{
operatr = btnop.Text; //store the operator
lblvalues.Text = lblvalues.Text.Remove(lblvalues.Text.Length - 1); //remove the
previous operator value
lblvalues.Text = lblvalues.Text+operatr; //updated value will be displayed on label te
}
else if (value != 0 || (operatr != "" && value == 0)) //when value is not zero or when value
iz zero but operator event occured
{
btnequal.PerformClick(); //btnequal event is raised to calculate first entered values
value = double.Parse(txtboxresult.Text.ToString()); //store the first entered text
operatr = btnop.Text; //store the operator
operation = true; //operation set as true;
lblvalues.Text = lblvalues.Text + value + " " + operatr; //display values to label text
}
else if (rsltperformed)
{
rsltperformed = false; //if equal operation is perform then operator is clicked it starts
from current value without performing any operation
operatr = btnop.Text; //store the operator
operation = true; //operation set as true;
value = double.Parse(txtboxresult.Text.ToString());