Microsoft+Visual+Studio+Activity+2

Create a calculator (that can operate with both integers and decimal numbers)
In the last activity we learned how to create a calculator using integers. Integers, as we saw can only operate with (integers), if we insert a value such as 5.2 or .000065, the program will have an error and colapse as the values do not match. Therefore, to be able to use real numbers in a calculator that can operate with decimals, we must use a new tool, "double ". In this calculator, we will add a new button for a new operation, SQUARE.

First of all, we must open Microsoft Visual Studio and create a new project. In Form1, we must create two text boxes, which will allow the user to write two numbers. Next, we must create FIVE different buttons, one for adding, subsrtacting, dividing, multiplying and squaring. Finally, we must create FIVE different labels in order to send the results there.

Now, we are ready to program!

First of all, we need to set an order to our programming order. We must decide which will be the order in which actions must be done to program it in that way. In this case our ordr is as follows:

1.- User introduces a value in text box number one. 2.- User introduces a value in text box number two. -Up to this moment, no changes have ocurred- 3.- Once the two values are set, the user must click ONE of the buttons (addition, substraction, division or multiplication) 4.- Each button will chage the text of the 4 different lables and will express the result of the operation. -The user may click one after the other, and the labels will change- -The user may change a value and the labels will not change AUTOMATICALLY, another clikc is needed to get the new result.

ADDITION

double x; x = double.Parse(textBox1.Text); (change the text in text box 1 to numbers using DECIMALS) double y; y = double.Parse(textBox2.Text); (change the text in text box 2 to numbers using DECIMALS) (WE NOW HAVE CONVERTED TO NUMBERS THE VALUES WRITTEN BY THE USER AS TEXT AS DECIMAL NUMBERS) double z; z = x + y; (WE ASSIGN A NEW VARIABLE, Z AND USE THE FORMER VARIABLES ALREADY SET TWO ADD THEM) label1.Text = "The addition of " + x +" + "+ y + " is " + z; (WE CHANGE THE LABEL 1 TEXT SO IT SAYS THE RESULT) label1.ForeColor = Color.Blue; (WE CHANGE THE FORECOLOR [LETTER COLOR] OF THE LABEL1)

SUBSTRACTION

double x; x = double.Parse(textBox1.Text); (change the text in text box 1 to numbers using DECIMALS) double y; y = double.Parse(textBox2.Text); (change the text in text box 2 to numbers using DECIMALS) (WE NOW HAVE CONVERTED TO NUMBERS THE VALUES WRITTEN BY THE USER AS TEXT AS DECIMAL NUMBERS) double z; z = x - y; (WE ASSIGN A NEW VARIABLE, Z AND USE THE FORMER VARIABLES ALREADY SET TWO ADD THEM) label2.Text = "The addition of " + x -" + "+ y + " is " + z; (WE CHANGE THE LABEL 1 TEXT SO IT SAYS THE RESULT) label2.ForeColor = Color.Red; (WE CHANGE THE FORECOLOR [LETTER COLOR] OF THE LABEL1)

DIVISION

double x; x = double.Parse(textBox1.Text); (change the text in text box 1 to numbers using DECIMALS) double y; y = double.Parse(textBox2.Text); (change the text in text box 2 to numbers using DECIMALS) (WE NOW HAVE CONVERTED TO NUMBERS THE VALUES WRITTEN BY THE USER AS TEXT AS DECIMAL NUMBERS) double z; z = x / y; (WE ASSIGN A NEW VARIABLE, Z AND USE THE FORMER VARIABLES ALREADY SET TWO ADD THEM) label3.Text = "The addition of " + x /" + "+ y + " is " + z; (WE CHANGE THE LABEL 1 TEXT SO IT SAYS THE RESULT) label3.ForeColor = Color.Green; (WE CHANGE THE FORECOLOR [LETTER COLOR] OF THE LABEL1)

MULTIPLICATION

double x; x = double.Parse(textBox1.Text); (change the text in text box 1 to numbers using DECIMALS) double y; y = double.Parse(textBox2.Text); (change the text in text box 2 to numbers using DECIMALS) (WE NOW HAVE CONVERTED TO NUMBERS THE VALUES WRITTEN BY THE USER AS TEXT AS DECIMAL NUMBERS) double z; z = x * y; (WE ASSIGN A NEW VARIABLE, Z AND USE THE FORMER VARIABLES ALREADY SET TWO ADD THEM) label4.Text = "The addition of " + x *" + "+ y + " is " + z; (WE CHANGE THE LABEL 1 TEXT SO IT SAYS THE RESULT) label4.ForeColor = Color.Violet; (WE CHANGE THE FORECOLOR [LETTER COLOR] OF THE LABEL1)

SQUARE

double x; x = double.Parse(textBox1.Text); (change the text in text box 1 to numbers using DECIMALS) (WE CAN ONLY USE ONE VALUE, AS WE ARE GOING TO SQUARE IT, THEREFORE WE MUST ONLY DECLARE ONE VALUE) double z; (CHANGE THE "Z" VALUE INTO A DECIMAL NUMBER) z = x * x; (WE USE THE VARIABLE WE HAVE IN RODER TO SQUARE IT) label5.Text = "The square of x is " + z; (WE CHANGE THE LABEL'S TEXT IN ORDER TO SHOW THE RESULT) label5.ForeColor = Color.HotPink; (THE COLOR OF THE TEXT IS CHANGED INTO A HOT PINK COLOR)

EXAMPLE

{ double x; x = double .Parse(textBox1.Text); double y; y = double .Parse(textBox2.Text); double z; z = x + y; textBox3.Text = "" + z; label2.Text = "The addition of " + x + " + " + y + " is " + z; } private void button2_Click( object sender, EventArgs e) { double x; x = double .Parse(textBox1.Text); double y; y = double .Parse(textBox2.Text); double z; z = x - y; textBox3.Text = "" + z; label2.Text = "The substraction of " + x + " - " + y + " is " + z;

} private void button3_Click( object sender, EventArgs e) { double x; x = double .Parse(textBox1.Text); double y; y = double .Parse(textBox2.Text); double z; z = x * y; textBox3.Text = "" + z; label2.Text = "The multiplication of " + x + " x " + y + " is " + z; } private void button4_Click( object sender, EventArgs e) { double x; x = double .Parse(textBox1.Text); double y; y = double .Parse(textBox2.Text); double z; z = x / y; textBox3.Text = "" + z; label2.Text = "The division of " + x + " / " + y + " is " + z; } private void label2_Click( object sender, EventArgs e) {

} private void button5_Click( object sender, EventArgs e) { double x; x = double .Parse(textBox1.Text);

double z; z = x *x; textBox3.Text = "" + z; label2.Text = "The power of " + x + " is " + z; } } }