Zimmer CSCI 130 3/18/2016 CHAPTER 10 - Simple Data Types: Built- in and User – Defined Additional C++ Operators – Combined Assignment Operators: +=, -=, *=, /= Increment and Decrement Operators: ++, -Other Operators: ( ) cast sizeof ?: Ex - avg = float(sum) / float(count); Ex - sizeof(int) exp1 ? exp2 : exp3 if (exp1) exp2; else exp3; Ex- x = (y>z) ? y : z; Operator Precedence : (see table in textbook) 1 Zimmer CSCI 130 3/18/2016 Working with Character Data – External Representation - printable character Internal Representation - How it is represented in the computer (ASCII or EBCIDIC) Special characters – See textbook Converting Character digits to integers - subtract the character '0' (zero) not 48!! (48 only works for machines that use ASCII). Converting to lower and uppercase - include the header file cctype to use the toupper( ) and tolower( ) functions. Accessing characters in a string using the position number string s; s[0] = 'H'; s[1] = 'I'; s[2] = '\0'; // null terminator character must be in the string! cout << s; // prints: HI on the screen Floating Point Numbers Read textbook closely… Significant digits - leftmost nonzero digit to the rightmost nonzero digit precision - maximum significant digits Normalization - leftmost digit is non zero, decimal pt is assumed after the rightmost digit then floats to appropriate position based on exponent. Ex - 120 is represented as 1200 x 10-1 +-11200 Representational Error - Arithmetic error when significant digits > precision of machine. - underflow - trying to represent numbers that are too small - overflow - trying to represent numbers that are too large - Cancellation errors - error caused by adding numbers of differing magnitudes. ----Programmers working with Floating pt. numbers need Numerical Analysis ----- 2 Zimmer CSCI 130 3/18/2016 User Defined Simple DataTypes Typedef - Creating a new name for an existing data type. Creates self-documenting code. typedef ExistingTypeName NewTypeName; ex: typedef int Boolean; const Boolean TRUE = 1; const Boolean FALSE = 0; … Boolean dataOK; … dataOK = TRUE; typedef char string [256]; string InFileName, OutFileName; 3 Zimmer CSCI 130 3/18/2016 Enumerated Data Types - Ordered sequence of identifiers used in place of integers as an aid to the programmer. - Provides a name for the new type - Explicitly lists all the values (enumerators) of this new type - Compiler performs an object to integer mapping (default starts at 0) - Easier for computer to keep track of than the programmer. enum DataType {enumerators separated by commas}; Example: enum DayType {SUN, MON, TUE, WED, THU, FRI, SAT}; ( SUN has the integer value 0, MON has the value 1…) enum DayType {SUN = 4, MON = 18, TUE = 22, WED,THU,FRI,SAT); (WED value is 23, THU is 24…) enum VowelType { ‘a’, ‘e’, ‘i’, ’o’, ’u’} enum PlacesType { 1st, 2nd, 3rd} // ERROR, not valid ident. // ERROR, not legal ident. enum StarchType { corn, rice, potato, bean} //ERROR, together these enum GrainType {wheat, corn, rye, barley} // overlap, ident.s must be unique enum AnimalType { rodent, cat, dog, reptile, bird, horse, sheep}; AnimalType animal1, animal2; Assignment - Implicit type coercion is defined from enumeration type to int, but not from int to an enumeration type. animal1 = dog; // does not assign “dog” or the value of a variable dog, but assigns the value dog. animal1 = animal2; // valid animal1 = 2; // ERROR, compile time error someInt = dog; // valid, because of implicit type coercion BECAFEFUL enum Boolean {FALSE, TRUE}; Boolean dataOK; dataOK = (someInt > -1) // ERROR, relational expressions yield an int // Better in this case to use typedef 4 Zimmer CSCI 130 3/18/2016 Incrementing - Can increment or decrement if type casting is used. animal1 = animal1 + 1; // ERROR, animal1 + 1 yields and integer value animal1++; // ERROR, same as above animal1 = AnimalType (animal1 + 1); // Valid, type casting for (animal1 = rodent; animal1 < sheep; animal1 = AnimalType(animal1 + 1)) // valid Comparison - Ordinal sequence is determined by the order in enum statement, or the integer it is mapped to. animal1 <= bird; switch (animal1) { case rodent : … case cat: … … } // valid Input/Output - enumerated types cannot be input or output directly.. char ch1, ch2 cin >> ch1 >> ch2; switch (ch1) { case ‘r’: if (ch2 = ‘o’) animal1 = rodent; else animal1 = reptile; case ‘c’ : animal1 = cat; … } User-Written Header files create your own header files in your directory need to use the preprocessor directive to include in your program: #include “filename.h” ex: #include “bool.h” // typedef & const for boolean values 5