1 / 23

Chapter 3 Expressions and Interactivity

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

lawson
Download Presentation

Chapter 3 Expressions and Interactivity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3Expressions and Interactivity Department of Computer Science Missouri State Univeristy

  2. Outline • The cin object • Expressions • Type casting • Named Constant • More on I/O

  3. 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 >>

  4. 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;

  5. 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;

  6. 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.

  7. 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;

  8. 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; }

  9. 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.

  10. 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; }

  11. The cin Object • Boolean data input bool aBool=true; cout<<“Please input a boolean data: ” cin>>aBool; cout <<“bool data is ”<<aBool<<endl;

  12. 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.

  13. Expressions • In assignment statement result = x; result = 4; result = 15/3; result = 22*number; result = sizeof(int); result = a+b+c;

  14. 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

  15. 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

  16. 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) = ?

  17. 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);

  18. Library Functions • A collection of specialized functions. • A “library function” as a “routine” that performs a specific operation. • area = pow(4, 2) • Some Examples:

  19. 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; }

  20. 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

  21. 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)

  22. 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; }

  23. Simplified twenty one points game

More Related