230 likes | 362 Views
Chapter 3 Expressions and Interactivity. Department of Computer Science Missouri State Univeristy. Outline. The cin object Expressions Type casting Named Constant More on I/O. The cin Object. Standard input object Used to read input from keyboard
E N D
Chapter 3Expressions and Interactivity Department of Computer Science Missouri State Univeristy
Outline • The cin object • Expressions • Type casting • Named Constant • More on I/O
The cin Object • Standard input object • Used to read input from keyboard • Like cout, requires iostream file • Often used with cout to display a user prompt first • Information retrieved from cin with >>
The cin Object • User input goes from keyboard to keyboard buffer • cin converts information to the type that matches the variable: • Input a single integer; short aShort; cout << “Input a short-type number:"; cin >> aShort; cout<< “The number =”<<aShort<<endl;
The cin object • Floating-point number input double aDouble; cout << “Input a double type number:"; cin >> aDouble; cout<< “The double number =”<<aDouble<<endl; • Character input char aChar; cout << “Input a character:"; cin >> aChar; cout<< “The character is ”<<aChar<<endl;
The cin Object • Can be used to input > 1 value: int int1,int2, int3; cout<<“Please input three int-type numbers:” cin>>int1>>int2>>int3; cout <<“int1= ”<<int1 <<“ int2= ”<<int2 <<“ int3= ”<<int3<<endl; • Multiple values from keyboard must be separated by spaces • Order is important: first value entered goes to first variable, etc.
The cin Object • Gather multiple values with different data types int aInt; double aDouble; char aChar; cout<<“Please input three different data types values:” cin>>aInt>>aDouble>>aChar; cout <<“Int is ”<<aInt <<“ Double is ”<<aDouble <<“ char is ”<<aChar<<endl;
Exercise //This program calculates Hui Liu's part-time pay #include <iostream> using namespace std; void main() { float workPayRate,pay; int workHour; int seeResults; cout << "How many hours do you work every work? "<<endl; cin >> workHour; cout<<"How much do you get paid per hour?"<<endl; cin>>workPayRate; pay=workPayRate*workHour; cout<<"Hui Liu: You will get "<<pay <<" dollars every work for the part time job!"<<endl; cin>>seeResults; }
The cin Object • Character string input #include <iostream> using namespace std; int main() { char name[21]; cout<<“What is your name? ”; cin>>name; cout<<“Good Morning ”<<name<<endl; return 0; } • The cin object will let the user enter a string larger than the array can hold. If this happens, the string will overflow the array’s boundaries and destroy other data in memory.
The cin object • Multiple strings #include <iostream> using namespace std; int main() { char first[21], last[21]; cout<<“What is your first and last name? ”; cin>>first>>last; cout<<“Good Morning ”<<last<<“,”<<first<<endl; return 0; }
The cin Object • Boolean data input bool aBool=true; cout<<“Please input a boolean data: ” cin>>aBool; cout <<“bool data is ”<<aBool<<endl;
Mathematical Expressions • A math formula • x(a+b) 3+6b • Can create complex expressions using multiple mathematical operators • An expression is a programming statement that has a value. • sum = 10+1; • can be a constant, a variable, or a mathematical combination of constants and variables • Can be used in assignment, cout, other statements.
Expressions • In assignment statement result = x; result = 4; result = 15/3; result = 22*number; result = sizeof(int); result = a+b+c;
Expression //This program calculates Hui Liu's part-time pay #include <iostream> using namespace std; void main() { float workPayRate; int workHour; int seeResults; cout << "How many hours do you work every work? "<<endl; cin >> workHour; cout<<"How much do you get paid per hour?"<<endl; cin>>workPayRate; //pay=workPayRate*workHour; cout<<"Hui Liu: You will get "<<(workPayRate*workHour) <<" dollars every work for the part time job!"<<endl; cin>>seeResults; } • In output statement
Order of Operations In an expression with > 1 operator, evaluate in this order: - (unary negation), in order, left to right * / %, in order, left to right + -, in order, left to right In the expression 2 + 2 * 2 – 2 , evaluate second evaluate first evaluate third
Associativity of Operators • - (unary negation) associates right to left • *, /, %, +, - associate left to right • parentheses ( ) can be used to override the order of operations: 2 + 2 * 2 – 2 = ? (2 + 2) * 2 – 2 = ? 2 + 2 * (2 – 2) = ? (2 + 2) * (2 – 2) = ?
Algebraic Expressions • Multiplication requires an operator: Area=lw is written as Area = l * w; • There is no exponentiation operator: Area=s2 is written as Area = pow(s, 2); • Parentheses may be needed to maintain order of operations: is written as m = (y2-y1) /(x2-x1);
Library Functions • A collection of specialized functions. • A “library function” as a “routine” that performs a specific operation. • area = pow(4, 2) • Some Examples:
pow function //This program calculates the volume of a sphere #include <iostream> using namespace std; void main() { float radius, volume,pi=3.14155926; cout << “What is the radius of the sphere? "<<endl; cin >> radius; volume= cout<<“The volume of the sphere is “ << <<volume<<endl; }
More math library functions • Require cmath header file • Take double as input, return a double • Commonly used functions: sin Sine cos Cosine tan Tangent sqrt Square root log Natural (e) log Log10 base-10logarithm exp exponentialfunction abs Absolute value (takes and returns an int) fmod remainder
Random number generator • #include <cstdlib> • y = rand(); • Psuedorandom For example: cout<<rand()<<endl; cout<<rand()<<endl; cout<<rand()<<endl; • In order to randomize the results of rand() • srand (unsigned int)
rand() and srand() //This program demonstrates random numbers. #include <iostream> #include <cstdlib> using namespace std; void main() { unsigned int seed; cout << “Enter a seed value : "; cin >> seed; srand(seed); cout<<rand()<<endl; cout<<rand()<<endl; cout<<rand()<<endl; }