numeric types, expressions, and output

19
Numeric Types, Expressions, and Output ROBERT REAVES

Upload: ziva

Post on 09-Feb-2016

35 views

Category:

Documents


0 download

DESCRIPTION

Numeric Types, Expressions, and Output. Robert reaves. Additional String Operations. Four functions that we will use to operate on strings: l ength s ize f ind substr. Length Function. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Numeric Types, Expressions, and Output

Numeric Types, Expressions, and OutputROBERT REAVES

Page 2: Numeric Types, Expressions, and Output

Additional String Operations

Four functions that we will use to operate on strings: length size find substr

Page 3: Numeric Types, Expressions, and Output

Length Function

When applied to a string variable, it returns an unsigned integer value that equals the number of characters currently in the string.

If myName is a string variable, a call looks like this: myName.length();

Use parentheses to signify an empty argument list. This is a value-returning function, so the call must appear within

an expression.

Page 4: Numeric Types, Expressions, and Output

Dot Notation

Why the call to this function so weird? Certain programmer-defined data types, such as strings, have functions

that are associated with them and the dot notation is required in the calls.

myName.length();

Dot notation is the dot (period) between the variable name and the function name.

What is we don’t use the “dot” and say: length();

Page 5: Numeric Types, Expressions, and Output

Size Function

Same as length function. This was to accommodate both terms, since people say size and length interchangeably.

Example: myName.size();

Returns unsigned-integer value, thus you must declare results to be of type int. string firstName; int len;

firstName = “Robert”; len = firstName.length(); //len = firstName.size();

Page 6: Numeric Types, Expressions, and Output

Find Function

Find function searches a string to find the first occurrence of a particular substring and returns an unsigned integer value.

Takes one argument, a literal string or a string expression. Ex:

str1.find(“the”); str1.find(str2); str1.find(str2 + “abc”);

str1 is searched to see if substring can be found within it. If so, returns the position in str1 where the match beings. (Position start at 0) Else returns “npos”, meaning not a position within the string”

Evaluates to something like 4294967295 Has to match exact.

Page 7: Numeric Types, Expressions, and Output

Find Function

Find function can also be a char value, in this case it returns the position of the first occurrence of that character within the string else “npos”.

Only returns the FIRST matched position.

Page 8: Numeric Types, Expressions, and Output

Substr Function

Substr function returns a particular substring of a string. Ex:

String myString = “Hello, my name is Robert.”; myString.substr(5, 20);

First argument is an unsigned integer that specifies a position within the string, and the second is an unsigned integer that specifies the length of the desired substring.

Returns the piece of the string that starts at the specified location and continues for the number of characters given by the second argument. DOESN’T CHANGE YOUR ORIGINAL STRING

Page 9: Numeric Types, Expressions, and Output

Numeric Data Types

Data types: char, short, int, long

Are known as integral types (or integer types) because they refer to integer values, in other words WHOLE NUMBERS. ex:

22 16 1 0

Commas are NOT ALLOWED Minus sign preceding an integer will make it negative.

Page 10: Numeric Types, Expressions, and Output

Numeric Data Types

Exception to this is when you add the reserved word unsigned to the data type name. Ex: unsigned int

Assumed to only be positive or zero. Used for specialized situations.

Meaning you won’t use these in here.

Each data type is intended to represent different sizes of integers, however this is machine dependent.

Page 11: Numeric Types, Expressions, and Output

Numeric Data Types

Wait, why is a char an integer type? C++ classifies char as an integral type so it can be used to store

integer values with a very limited range -128 to 127

Page 12: Numeric Types, Expressions, and Output

Numeric Data Types

Float, or floating type are used to represent real numbers. Have an integer part and a fractional part, with a decimal between. Ex:

18.0 127.54 .8

Computer cannot always represent exactly. Slight inaccuracies in the rightmost fractional digits are to be

expected and are not the result of programmer error. Ex:

4.8 might be 4.79999998

Page 13: Numeric Types, Expressions, and Output

Named Constant Declartions

Just to show you can make constant variables of all data types. const float PI = 3.14159; const float E = 2.71828; const int MAX_SCORE = 100; const int MIN_SCORE = -100; const char LETTER = ‘W’; const string NAME = “Elizabeth”;

Page 14: Numeric Types, Expressions, and Output

Arithmetic Operators

Operators allowed in an expression depend on the data types of the constants and variables in the expressions. Arithmetic operators:

+ Unary Plus - Unary minus + Addition - Subtraction * Multiplication /

Floating-point division (floating-point result) Integer division (no fractional part)

% Modulus (remainder from integer division.

Page 15: Numeric Types, Expressions, and Output

Arithmetic Operators

Unary Operators an operator that has just one operand. Ex:

-54 +259.65 -rate (can use on variables)

Rarely use unary plus, numeric constant is assumed to be positive. Binary Operators an operator that has two operands.

Page 16: Numeric Types, Expressions, and Output

Arithmetic Operators

% (modulus) is only used with integers. Gives only the remainder.

10 / 7 = 3 / (division) can be used with integers or float

Integer division yields ONLY the quotient 10 / 7 = 1

Floating point yields a floating point result 10.0 / 7.0 = 1.3

Page 17: Numeric Types, Expressions, and Output

Increment and Decrement Operators

C++ provides increment and decrement operators ++ increment -- decrement

Unary operators take a single variable as an operand. For int and float operands, the effect is to add 1 or subtract 1 from the

operand.

If num currently contains 8, what would this statement yield? num++; and num--;

Page 18: Numeric Types, Expressions, and Output

Increment and Decrement Operators

The ++ and – operators can be either prefix operators: ++num;

or postfix operators: num++;

Page 19: Numeric Types, Expressions, and Output

Library Functions

C++ system includes a standard library-a large collection of prewritten functions, data types, and other times that any C++ programmer may use.

The functions in library are divided into separate files called header files. Ex:

#include <cmath> This library had this like sqrt() and abs()