Download presentation
Presentation is loading. Please wait.
Published byTobias Neal Modified over 8 years ago
1
Lecture 17: 10/29/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 17: 10/29/2002
2
CS149D Fall 20022 Outline Priority of Operators Revisit Increment and decrement operators Standard Input and Output
3
Lecture 17: 10/29/2002CS149D Fall 20023 Priority of Operators Precedence of arithmetic operators PrecedenceOperatorAssociativity (grouping order) 1( )innermost first 2unary operators + - castright to left 3binary operators * / %left to right 4binary operators + -left to right Expression x-y+z is evaluated as (x-y)+z(left to right associativity) Expression a*b+b/c*dEvaluated as if written (a*b) + ((b/c)*d) Expression 5.0 * 2.0 / 4.0 * 2.0 yields 5.0 (Why?) Expression 10 % 3 – 4 /2 yields –1 (Why?)
4
Lecture 17: 10/29/2002CS149D Fall 20024 Algebraic Expressions into C++ Expressions f = (x 3 – 2x 2 + x – 6.3)/(x 2 +0.05005x-3.14) A C++ expression Would be f = (x*x*x - 2*x*x + x – 6.3) / (x*x + 0.05005*x – 3.14); Or can divide into computing numerator and denominator float numerator, denominator, f; numerator = x*x*x - 2*x*x + x – 6.3; denominator = x*x + 0.05005*x – 3.14; f = numerator/denominator;
5
Lecture 17: 10/29/2002CS149D Fall 20025 Revisit Increment and Decrement Operators int y = 5; x = y++ * 3;//postfix position Is equivalent to x = y *3; y = y +1x 15y 6 x = ++y *3;//prefix position Is equivalent toy = y+1 x = y *3x 18 y 6
6
Lecture 17: 10/29/2002CS149D Fall 20026 Abbreviated Assignment Operators x = x +3;is equivalent to x += 3; identifier = identifier operator expression can be written as identifier operator= expression a = b += c +d; Assignment operators have least priority and group right to left Then previous expression evaluated as a = (b += (c+d)); Or b = b + (c+d); a = b; The previous multiple assignment use is not recommended but is available as part of the C++ language
7
Lecture 17: 10/29/2002CS149D Fall 20027 Standard Input and Output Must include Header file iostream.h contains prototype definitions of input/output functions cout << “angle = “ << angle << “radians” << endl; Escape characters Backslash (\) is an escape character \” used to print a “ \\ used to print a \ \n used to represent a new line, equivalent to endl functionality \t used to print a horizontal tab cout << “\”The End.\”’ << “\n” ;output is “The End.”
8
Lecture 17: 10/29/2002CS149D Fall 20028 Formatted Output To use must include Field Width Format manipulator Set field width (number of character positions that a number will occupy on the screen) setw(size) cout << setw(8) << x;// print value of x in field of 8 spaces Field width increased if necessary to print the value If field width specifies more positions than needed, value is right justified Only specifies field width for the next item in cout. After item is displayed the field size reverts to default value of 0 cout << setw(10) <<x << y; //only value of x is printed in 10 spaces, value of y will be printed in enough space to display it
9
Lecture 17: 10/29/2002CS149D Fall 20029 Formatted Output Precision manipulator Specify precision of value to be displayed Precision is number of digits after decimal point cout << setprecision(2) << setw(8) << x; Print value of x with 2 digits after decimal point, using a total field width of 8 spaces Decimal portion is rounded to the specified precision 14.51578 printed as 14.52 with three blanks to its left (Why?) setprecision manipulator is applied to all subsequent output
10
Lecture 17: 10/29/2002CS149D Fall 200210 Keyboard Input int x; float y; cin >> y >> x; User input 15.5 5 x assigned 5 y assigned 15.5 User input 15.5 5 x assigned 5 y assigned 15.5 User input 5 15.5 x assigned 15 y assigned 5.0
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.