Calculator

  • Uploaded by: faiza
  • 0
  • 0
  • August 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Calculator as PDF for free.

More details

  • Words: 3,895
  • Pages: 15
Visual Programming Assignment 01

Designing a Standard Calculator In Windows Form Application

Submitted By: Faiza Mushtaq Registration No: 2017-BSE-065 Semester: IV Submitted to: Mam Hina

Fatima Jinnah Women University

Standard Calculator A standard calculator performs the basic operation such as division, multiplication, addition and subtraction.

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.

The other name is set as lblhistry. It will be shown when user maximize the form. It’s just for display.

Implementation From the above specified functionalities code is implemented. 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 Four variables are taken in class with public access specifier to be used in all functions.

   

The opratr is basically to store the entered operator value to keep track of what user entered and perform operation on it, value taken double to perform all kind of operations is used to store the first value entered by the user, operation is type bool to know if the user has entered an operator or not, rsltperformed is also type bool to know operation has been performed or not. Comments have been added for every function.

 For C (Clear) For the Clear function we have one button event that is click and two others as Mouse Enter & Mouse Leave (to change color) will be explained later. 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 rsltperformed = false; //set to false }

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 CE (Clear screen) private void btncentry_Click(object sender, EventArgs e) { txtboxresult.Text = "0"; //a set a default zero }

Explanation Clear Entry will clear set the txtboxresult only to 0 it will not set all other operations to default as it functions to store the history.

 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.

 For Button Digits (0-9) All digit buttons has the same operation to be entered value is display on text box and other certain conditions so one event is made that all mouse click event of these buttons have, they have other two events also as MouseEnter and MouseLeave( To change the color) will be explained later. private void btn_click(object sender, EventArgs e) { if (txtboxresult.Text == "0" || operation||rsltperformed) //to clear the textbox if there is default zero or operator has been entered or equal event occur// txtboxresult.Clear(); if (rsltperformed && !operation) //if equal operation is performed then user entered the text all should set to default { operatr = ""; value = 0; } Button btn = (Button)sender; txtboxresult.Text = txtboxresult .Text+ btn.Text; operation = false; //text has been entered operation will be set as false rsltperformed = false; //equal operation will be set as false }

Explanation In first condition if the textbox has the default zero means user hasn’t entered some values or the operation is true or the result is performed it will be clear by the Clear function (or is used because if anyone one of these is true text box should be clear). In second condition if the user has entered the digits and press equal operation for result then the variable rsltperformed is true but the operation will be false as user enter the digit last time for the result so to clear all when user press any digit after the result calculation this condition is given and && is used so the operation should be false else it will lost the value variable values and can’t calculate the two values. As this event is for all digits so we use type casting because sender will have the value of text on the button and as it is type object it doesn’t have property Text so we made an object of Button btn and set it to sender’s value by casting the sender’s value to Button . Then it is assigned to txtboxresult and concatenate to not lost the first enter value (like if he want to enter more than one digit). The Operation and rsltperformed will be set to false every time the text is entered. Because if they remain true it will set the txtboxresult again to zero.

 For Button Operators (÷, x, +,-) All operator buttons has the same operation to be entered value is display on label and other certain conditions so one event is made that all mouse click event of these buttons have. Also other events like MouseEnter and MouseLeave(To change the color) will be explained later. 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 text }

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 } }

 For Equal Button (=) Operator button will perform the calculations and then erase label by showing result to text box txtboxrslt. private void btnequal_Click(object sender, EventArgs e) { 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.

Functions for Back Color Changing 

For digits and C,CE, Backspace, negation and dot button when mouse enters or leave will change the specified color

private void btndgts_MouseEnter(object sender, EventArgs e) //funtion to change the digits button backcolor when mouse enter { Button btn = (Button)sender; btn.BackColor = Color.LightGray; } private void btndgts_MouseLeave(object sender, EventArgs e) //function to change the digits button backcolor when mouse leaves { Button btn = (Button)sender; btn.BackColor = Color.White; }  For Operators and Equal button when mouse enters or leave will change the specified

color private void btnopr_MouseEnter(object sender, EventArgs e) //function to change the digits button backcolor when mouse enter { Button btn = (Button)sender; btn.BackColor = Color.CornflowerBlue; } private void btnopr_MouseLeave(object sender, EventArgs e) the digits button backcolor when mouse leaves { Button btn = (Button)sender; btn.BackColor = Color.WhiteSmoke; }

 CALCULATOR

//function to change

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) { txtboxresult.Text = "0"; //a set a default zero } private void btn_click(object sender, EventArgs e) { if (txtboxresult.Text == "0" || operation||rsltperformed) //to clear the textbox if there is default zero or operator has been entered or equal event occur// txtboxresult.Clear(); if (rsltperformed && !operation) //if equal operation is performed then user entered the text all should set to default { operatr = ""; value = 0;}

Button btn = (Button)sender; txtboxresult.Text = txtboxresult .Text+ btn.Text; operation = false; //text has been entered operation will be set as false rsltperformed = false; //equal operation will be set as false } private void btndot_Click(object sender, EventArgs e) { if (operation) { txtboxresult.Text = "0"; //if operator or equal event is clicked it will append zero to text box for the dot if to click again operation = false;}

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()); lblvalues.Text = lblvalues.Text + value + " " + operatr; //display values to label } else { value = double.Parse(txtboxresult.Text.ToString()); operatr = btnop.Text; //store the operator operation = true; //operation set as true; lblvalues.Text = lblvalues.Text + value + " " + operatr; //display values to label } } private void btnequal_Click(object sender, EventArgs e) { 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; } private void btndgts_MouseEnter(object sender, EventArgs e) //funtion to change the digits button backcolor when mouse ente { Button btn = (Button)sender; btn.BackColor = Color.LightGray;

} private void btndgts_MouseLeave(object sender, EventArgs e) //function to change the digits button backcolor when mouse leaves { Button btn = (Button)sender; btn.BackColor = Color.White; } private void btnopr_MouseEnter(object sender, EventArgs e) //function to change the digits button backcolor when mouse enter { Button btn = (Button)sender; btn.BackColor = Color.CornflowerBlue; } 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; }}}

Related Documents

Calculator
August 2019 67
Calculator
November 2019 57
Calculator
October 2019 49
Calculator
November 2019 60
Calculator Code
June 2020 6
Emi Calculator
November 2019 27

More Documents from ""