Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 17: 10/29/2002
CS149D Fall Outline Priority of Operators Revisit Increment and decrement operators Standard Input and Output
Lecture 17: 10/29/2002CS149D Fall 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?)
Lecture 17: 10/29/2002CS149D Fall Algebraic Expressions into C++ Expressions f = (x 3 – 2x 2 + x – 6.3)/(x x-3.14) A C++ expression Would be f = (x*x*x - 2*x*x + x – 6.3) / (x*x *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 *x – 3.14; f = numerator/denominator;
Lecture 17: 10/29/2002CS149D Fall 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
Lecture 17: 10/29/2002CS149D Fall 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
Lecture 17: 10/29/2002CS149D Fall 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.”
Lecture 17: 10/29/2002CS149D Fall 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
Lecture 17: 10/29/2002CS149D Fall 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 printed as with three blanks to its left (Why?) setprecision manipulator is applied to all subsequent output
Lecture 17: 10/29/2002CS149D Fall Keyboard Input int x; float y; cin >> y >> x; User input x assigned 5 y assigned 15.5 User input x assigned 5 y assigned 15.5 User input x assigned 15 y assigned 5.0