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

Chapter 3 Expressions and Interactivity

  • Upload
    lawson

  • View
    74

  • Download
    1

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 3 Expressions and Interactivity

Chapter 3Chapter 3Expressions and InteractivityExpressions and Interactivity

Department of Computer ScienceDepartment of Computer ScienceMissouri State UniveristyMissouri State Univeristy

Page 2: Chapter 3 Expressions and Interactivity

OutlineOutline

The The cincin object object

ExpressionsExpressions

Type castingType casting

Named ConstantNamed Constant

More on I/OMore on I/O

Page 3: Chapter 3 Expressions and Interactivity

The The cin cin ObjectObject

Standard input objectStandard input object Used to read input from keyboardUsed to read input from keyboard Like Like coutcout, requires , requires iostreamiostream file file Often used with Often used with coutcout to display a user to display a user

prompt firstprompt first Information retrieved from Information retrieved from cincin with with >>>>

Page 4: Chapter 3 Expressions and Interactivity

The The cincin Object Object

User input goes from keyboard to keyboard User input goes from keyboard to keyboard bufferbuffer

cin converts information to the type that cin converts information to the type that matches the variable:matches the variable: Input a single integer;Input a single integer;

short aShort;short aShort;cout << “Input a short-type number:";cout << “Input a short-type number:";cin >> aShort;cin >> aShort;

cout<< “The number =”<<aShort<<endlcout<< “The number =”<<aShort<<endl;;

Page 5: Chapter 3 Expressions and Interactivity

The cin objectThe cin object Floating-point number inputFloating-point number input

double aDouble;double aDouble;cout << “Input a double type number:";cout << “Input a double type number:";cin >> aDouble;cin >> aDouble;

cout<< “The double number =”<<aDouble<<endlcout<< “The double number =”<<aDouble<<endl;; Character inputCharacter input

char aChar;char aChar;cout << “Input a character:";cout << “Input a character:";cin >> aChar;cin >> aChar;

cout<< “The character is ”<<aChar<<endlcout<< “The character is ”<<aChar<<endl;;

Page 6: Chapter 3 Expressions and Interactivity

The The cincin Object Object

Can be used to input Can be used to input > 1 value:> 1 value:

int int1,int2, int3;int int1,int2, int3; cout<<“Please input three int-type numbers:”cout<<“Please input three int-type numbers:” cin>>int1>>int2>>int3;cin>>int1>>int2>>int3; cout <<“int1= ”<<int1 <<“ int2= ”<<int2cout <<“int1= ”<<int1 <<“ int2= ”<<int2 <<“ int3= ”<<int3<<endl;<<“ int3= ”<<int3<<endl;

Multiple values from keyboard must be separated by Multiple values from keyboard must be separated by spacesspaces

Order is important: first value entered goes to first Order is important: first value entered goes to first variable, etc.variable, etc.

Page 7: Chapter 3 Expressions and Interactivity

The cin ObjectThe cin Object Gather multiple values with different data typesGather multiple values with different data types

int aInt;int aInt; double aDouble; double aDouble; char aChar;char aChar; cout<<“Please input three different data types values:”cout<<“Please input three different data types values:” cin>>aInt>>aDouble>>aChar;cin>>aInt>>aDouble>>aChar; cout <<“Int is ”<<aInt <<“ Double is ”<<aDoublecout <<“Int is ”<<aInt <<“ Double is ”<<aDouble <<“ char is ”<<aChar<<endl;<<“ char is ”<<aChar<<endl;

Page 8: Chapter 3 Expressions and Interactivity

ExerciseExercise//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;}

Page 9: Chapter 3 Expressions and Interactivity

The cin ObjectThe cin Object Character string inputCharacter string input

#include <iostream>#include <iostream>using namespace std;using namespace std;

int main()int main(){{

char name[21];char name[21];

cout<<“What is your name? ”;cout<<“What is your name? ”;cin>>name;cin>>name;cout<<“Good Morning ”<<name<<endl;cout<<“Good Morning ”<<name<<endl;return 0;return 0;

}}

The cin object will let the user enter a string larger than the array 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 can hold. If this happens, the string will overflow the array’s boundaries and destroy other data in memory.boundaries and destroy other data in memory.

Page 10: Chapter 3 Expressions and Interactivity

The cin objectThe cin object Multiple stringsMultiple strings

#include <iostream>#include <iostream>using namespace std;using namespace std;

int main()int main(){{

char first[21], last[21];char first[21], last[21];

cout<<“What is your first and last name? ”;cout<<“What is your first and last name? ”;cin>>first>>last;cin>>first>>last;cout<<“Good Morning ”<<last<<“,”<<first<<endl;cout<<“Good Morning ”<<last<<“,”<<first<<endl;return 0;return 0;

}}

Page 11: Chapter 3 Expressions and Interactivity

The cin ObjectThe cin Object

Boolean data inputBoolean data input bool aBool=true;bool aBool=true; cout<<“Please input a boolean data: ”cout<<“Please input a boolean data: ” cin>>aBool;cin>>aBool; cout <<“bool data is ”<<aBool<<endl;cout <<“bool data is ”<<aBool<<endl;

Page 12: Chapter 3 Expressions and Interactivity

Mathematical ExpressionsMathematical Expressions A math formulaA math formula

x(a+b) 3+6bx(a+b) 3+6b Can create complex expressions using multiple Can create complex expressions using multiple

mathematical operatorsmathematical operators An expression is a programming statement that has An expression is a programming statement that has

a value. a value. sum = 10+1;sum = 10+1;

can be a constant, a variable, or a mathematical can be a constant, a variable, or a mathematical combination of constants and variablescombination of constants and variables

Can be used in assignment, Can be used in assignment, coutcout, other , other statementsstatements..

Page 13: Chapter 3 Expressions and Interactivity

ExpressionsExpressions

In assignment statementIn assignment statement

result = x;result = x;result = 4;result = 4;result = 15/3;result = 15/3;result = 22*number;result = 22*number;result = sizeof(int);result = sizeof(int);result = a+b+c;result = a+b+c;

Page 14: Chapter 3 Expressions and Interactivity

ExpressionExpression In output statementIn output statement

//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;}

Page 15: Chapter 3 Expressions and Interactivity

Order of OperationsOrder of Operations

In an expression with > 1 operator, In an expression with > 1 operator, evaluate in this order:evaluate in this order:

-- (unary negation), in order, left to right (unary negation), in order, left to right* / %* / %, in order, left to right, in order, left to right+ -+ -, in order, left to right, in order, left to right

In the expression In the expression 2 + 2 * 2 – 2 ,2 + 2 * 2 – 2 ,

evaluate first

evaluate second

evaluate third

Page 16: Chapter 3 Expressions and Interactivity

Associativity of OperatorsAssociativity of Operators

-- (unary negation) associates right to left (unary negation) associates right to left *, /, %, +, -*, /, %, +, - associate left to right associate left to right parentheses ( ) can be used to override parentheses ( ) can be used to override

the order of operations:the order of operations: 2 + 2 * 2 – 2 = ?2 + 2 * 2 – 2 = ?(2 + 2) * 2 – 2 = ?(2 + 2) * 2 – 2 = ? 2 + 2 * (2 – 2) = ?2 + 2 * (2 – 2) = ?(2 + 2) * (2 – 2) = ?(2 + 2) * (2 – 2) = ?

Page 17: Chapter 3 Expressions and Interactivity

Algebraic ExpressionsAlgebraic Expressions

Multiplication requires an operator:Multiplication requires an operator:Area=lwArea=lw is written as is written as Area = l * w;Area = l * w;

There is no exponentiation operator:There is no exponentiation operator:Area=sArea=s22 is written as is written as Area = pow(s, 2);Area = pow(s, 2);

Parentheses may be needed to maintain order Parentheses may be needed to maintain order of operations:of operations:

is written asis written asm = (y2-y1) /(x2-x1);m = (y2-y1) /(x2-x1);

12

12

xxyym

Page 18: Chapter 3 Expressions and Interactivity

Library FunctionsLibrary Functions

A collection of specialized functions. A collection of specialized functions. A “library function” as a “routine” that A “library function” as a “routine” that

performs a specific operation.performs a specific operation. area = pow(4, 2)area = pow(4, 2) Some Examples:Some Examples:

Page 19: Chapter 3 Expressions and Interactivity

pow functionpow 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;

}

Page 20: Chapter 3 Expressions and Interactivity

More math library functionsMore math library functions Require Require cmathcmath header file header file Take Take doubledouble as input, return a as input, return a doubledouble Commonly used functions:Commonly used functions:

sinsin Sine Sine coscos CosineCosinetantan TangentTangentsqrtsqrt Square rootSquare rootloglog Natural (e) logNatural (e) logLog10 Log10 base-10base-10 logarithmlogarithm exp exp exponentialexponential functionfunction absabs Absolute value Absolute value (takes and returns an int)(takes and returns an int)fmodfmod remainder remainder

Page 21: Chapter 3 Expressions and Interactivity

Random number generatorRandom number generator #include <cstdlib>#include <cstdlib> y = rand();y = rand(); PsuedorandomPsuedorandom

For example:For example:cout<<rand()<<endl;cout<<rand()<<endl;cout<<rand()<<endl;cout<<rand()<<endl;cout<<rand()<<endl;cout<<rand()<<endl;

In order to randomize the results of rand()In order to randomize the results of rand() srand (unsigned int)srand (unsigned int)

Page 22: Chapter 3 Expressions and Interactivity

rand() and srand()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;}

Page 23: Chapter 3 Expressions and Interactivity

Simplified twenty one points gameSimplified twenty one points game