Upload
htb495
View
221
Download
0
Embed Size (px)
Citation preview
8/10/2019 06-C++ Expressions computer science
1/14
C++ ExpressionsCMPT 111
8/10/2019 06-C++ Expressions computer science
2/14
Demo! See demo1.cpp
8/10/2019 06-C++ Expressions computer science
3/14
Exercise #1 Write a C++ program that will:
1. Ask the user to enter a temperature in Fahrenheit.
2. Read in that value.
3. Convert the value to Celsius
4. Print out a message telling the user what the convertedtemperature is.
)32(9
5 FC
8/10/2019 06-C++ Expressions computer science
4/14
Demo: Functions See: http://www.cplusplus.com/reference
Example function prototypes:
double sin(double x);
double pow(double base, int exponent);
double pow(double base, double exponent);
int isdigit(int c);
int rand(void); OR int rand();
void srand(int seed);
http://www.cplusplus.com/referencehttp://www.cplusplus.com/reference8/10/2019 06-C++ Expressions computer science
5/14
Exercise #2 Using the cmath functions:
double cos(double x);
double sin(double x);
double tan(double x); double pow(double base, double exponent);
Write a C++ program that will input a floating point
value, x, and print out the values of:
tan(x)
sin(x)
cos(x)
sin2(x)+cos2(x)
(sin(x))cos(x)
8/10/2019 06-C++ Expressions computer science
6/14
Relational Operators Relational operators:
, =, ==, !=
Careful not to confuse = with ==
= is assignment
== is a test for equality
Can compare any two things that are the same.
ex: two numbers (int vs. int, int vs. float, float vs. float)
ex: two characters
Compared lexicographically (using ASCII table)
ex: two strings
Compared lexicographically (using ASCII table)
8/10/2019 06-C++ Expressions computer science
7/14
Boolean Operators Operate on boolean values.
Non-booleans are converted to boolean first, if possible.
numeric: 0 -> false; everything else -> true
Three operators: ! (not)
&& (and)
|| (or)
8/10/2019 06-C++ Expressions computer science
8/14
Boolean NOT Unary operator
It is applied to a single boolean value
Changes true to false, and false to true.
As a set relation:
Everything not in the set
ex:
!true is false !false is true
!(x < 0) is the same as (x >= 0)
A!A
8/10/2019 06-C++ Expressions computer science
9/14
Boolean NOT: Exercises
Given that:
x = 4
y = -3.1415926
What is the value of:
A) !(x > 0)
B) !(x < y)
C) !(x == y)
D) !(x < x)
E) !(y != x)
8/10/2019 06-C++ Expressions computer science
10/14
Boolean AND Binary operator
Applied to a pair of boolean values (just like +)
Is true only if both values are true
As a set relation
Everything in both sets.
A B
A and B
A and B true false
true true false
false false false
8/10/2019 06-C++ Expressions computer science
11/14
Boolean AND: Exercises
Given that:
x = 4
y = -3.1415926
What is the value of:
A) (0 < x) && (x < 5)
B) (x < y) && (y < 0)
C) true && (x == 4)
D) (x != y) && ((x*y) < 0) && ((x-y) > 0)
E) !(x
8/10/2019 06-C++ Expressions computer science
12/14
Boolean OR Binary operator
Applied to a pair of boolean values (just like +)
Is true when either value is true
As a set relation
Everything in either set.
A BA or BA or B true false
true true true
false true false
8/10/2019 06-C++ Expressions computer science
13/14
Boolean OR: Exercises
Given that:
x = 4
y = -3.1415926
What is the value of:
A) (0 < x) || (x < 5)
B) (x < y) || (y < 0)
C) true || (x == 4)
D) (x != y) && ((x*y) < 0) || ((x-y) > 0)
E) !(x
8/10/2019 06-C++ Expressions computer science
14/14
Expressions In C++ Boolean Expressions are very similar to arithmetic
expressions
We can evaluate them and store their result