Math Operations in C Operator Name Example Multiplication

  • Slides: 31
Download presentation
Math Operations in C++

Math Operations in C++

Operator Name Example * Multiplication X = 17 * 4; ; x iis set

Operator Name Example * Multiplication X = 17 * 4; ; x iis set to 68 / Division (real) X = 17. 0/4; x is set to 4. 25 / Division (Integer) X = 17/4; x is set to 4 (4 goes into 17 4 times; the decimal remainder is dropped). Both operand must be integers. % Modulus (Mod) X = 17% 4; x is set to 1 (the integer remainder from the division of one integer another integer division) + - Addition X = 17 + 4; x is set to 21 Subtraction X = 17 -4; x is set to 13

Variables can also be operands • num 1 + num 2 The result can

Variables can also be operands • num 1 + num 2 The result can be assigned a variable • sum = num 1 + numb 2;

Sometimes it can be confusing • num = num + 1; • Here you

Sometimes it can be confusing • num = num + 1; • Here you are taking a number, adding one to it and reassigning it the variable, num • The = sign is not an equals sign but an assignment operator

Operator precedence • Multiplicaation, division, modulus First • Addition and subtraction after • Then

Operator precedence • Multiplicaation, division, modulus First • Addition and subtraction after • Then left to right

Concatenation operator • Adding strings • Example: • text. Box 3 ->Text = text.

Concatenation operator • Adding strings • Example: • text. Box 3 ->Text = text. Box 1 ->Text + text. Box 2>Text • 1234 34

Shorthand Shortcuts num = num + 1; Is the same as num += 1;

Shorthand Shortcuts num = num + 1; Is the same as num += 1;

More short cuts • num 1 = num 1 + num 2; • Is

More short cuts • num 1 = num 1 + num 2; • Is the same as • num 1 += num 2;

Other short hand operators • • -= *= /= %=

Other short hand operators • • -= *= /= %=

+= with strings • text. Box 1 ->Text = “Hello “’; • text. Box

+= with strings • text. Box 1 ->Text = “Hello “’; • text. Box 1 ->Text += “World!”;

Math Library • Math: : Pow() method a value and its exponent answer •

Math Library • Math: : Pow() method a value and its exponent answer • Math: : Sqrt() method square root

 • c = square root of a 2 + b 2 • C

• c = square root of a 2 + b 2 • C = Math: : sqrt(Math: : Pow(a, 3) + Math: : Pow (b, 2): : ;

Outputting Data into textbox as string • To. String() Method • text. Box 3

Outputting Data into textbox as string • To. String() Method • text. Box 3 ->Text = sum. To. String();

Let’s make an addition program • Objective: add two numbers and have the display

Let’s make an addition program • Objective: add two numbers and have the display the result • For simplicity, we will make all numbers doubles

Interface design Textbox 1 Textbox 2 Button 1 (add) (Click()) Textbox 3

Interface design Textbox 1 Textbox 2 Button 1 (add) (Click()) Textbox 3

Data Table Variable Name double PUrpose num 1 double Stores data from textbox 1

Data Table Variable Name double PUrpose num 1 double Stores data from textbox 1 num 2 double Stores data from textbox 2 sum double Sum of num 1 and num 2

Alogrithm 1. 2. 3. 4. 5. Declare variables Read num 1 Read num 2

Alogrithm 1. 2. 3. 4. 5. Declare variables Read num 1 Read num 2 Compute sum Display sum

Making the Program • Start Visual C++ • Create a Windows Forms Project (follow

Making the Program • Start Visual C++ • Create a Windows Forms Project (follow directions from first lesson) • Create an interface with 3 textboxes and one button • Place four Label controls from the Toolbox onto the form: one above each textbox and one at the top of the form.

Form 1 will look like: • Label 1 • Label 2 • Textbox 1

Form 1 will look like: • Label 1 • Label 2 • Textbox 1 label 3 Textbok 2 • Button 1 (Add) • label 4 Textbox 3

Change the text properties of each of the interface objects Object Text property Form

Change the text properties of each of the interface objects Object Text property Form 1 Addition Program Label 1 Addition Program label 2 Enter first number label 3 Enter second number label 4 Sum: button 1 Add

Align text in Textboxes • Because we are entering numbers we want to align

Align text in Textboxes • Because we are entering numbers we want to align the text to the right hand side. To do this: • Called the Text. Align property: • Select textbox 1 on form 1 • Press and hold the control key and then select each of the other two textboxes by clicking them. All textboxes should now be highlighted

Properties window now displays all properties in common • Change the textalign property on

Properties window now displays all properties in common • Change the textalign property on all three to right-justified • Now select label 1 and change its font property to at least 14 and reposition the label to the middle of the center of the top of the form • Your interface design is now complete

Planning the code/Alogrithm • The data table identified 3 variables: double num 1, num

Planning the code/Alogrithm • The data table identified 3 variables: double num 1, num 2, sum; • Bring the two numbers into the program double: : Try. Parse(text. Box 1 ->Text, num 1); double: : Try. Parse(text. Box 2 ->Text, num 2); • Do the math sum= num 1 + num 2; Convert to string, display in text. Box 3: text. Box 3 ->Text = sum. To. String();

 • Display the sum text. Box 3 ->Text = sum. To. String();

• Display the sum text. Box 3 ->Text = sum. To. String();

Put the code into the Event. Handler for button 1_Click() • Double click button

Put the code into the Event. Handler for button 1_Click() • Double click button 1 to go to the code window • Hit return at the end of the last line of code: Private: System: Void button 1_Click( etc…{ • Indent your code so that it shows up about half way underthe word System

Add comments: • Two ways: • Block comments /* */ • Line comments //

Add comments: • Two ways: • Block comments /* */ • Line comments // to end of line

Put in comments for each line of code you wrote above the line //

Put in comments for each line of code you wrote above the line // Declare variables //Read in data //Process data //Display results

Test your program • Use first set of C++ notes for review • REMEMBER

Test your program • Use first set of C++ notes for review • REMEMBER TO SAVE ALL!!

TASK 1: Modify the interface • Rearrange the controls on the form, resize them,

TASK 1: Modify the interface • Rearrange the controls on the form, resize them, and add more information to the labels to make a more pleasing interface. • Test then show me

Task 2: add more operations (-, /, *) • Add three more buttons on

Task 2: add more operations (-, /, *) • Add three more buttons on the form. You have addition, so do a subtraction, division, and multiplication button. • Test and show me.