Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.

Similar presentations


Presentation on theme: "Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪."— Presentation transcript:

1 Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. E-mail : comjyh@ccu.edu.tw TA: 鄭筱親 陳昱豪

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-2 Outline 11.1 USER-DEFINED STRUCTURE TYPES 11.2 STRUCTURE TYPE DATA AS INPUT AND OUTPUT PARAMETERS 11.3 FUNCTIONS WHOSE RESULT VALUES ARE STRUCTURED 11.4 PROBLEM SOLVING WITH STRUCTURE TYPES –CASE STUDY A USER-DEFINED TYPE FOR COMPLEX NUMBERS 11.5 PARALLEL ARRAYS AND ARRAYS OF STRUCTURES –CASE STUDY UNIVERSAL MEASUREMENT CONVERSION 11.6 UNION TYPES(OPTIONAL) 11.7 COMMON PROGRAMMING ERRORS

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-3 11.1 USER-DEFINED STRUCTURE TYPES Database –A collection of information stored in a computer’s memory or in a disk file Record –A collection of information about one data object Structure type –A data type for a record composed of multiple components Hierarchical structure –A structure containing components that are structures

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-4 Structure Type Definition Syntax typedef struct{ type 1 id_list 1 ; type 2 id_list 2 ; … type n id_list n ; } struct_type; Example typedef struct{ /* complex number structures */ double real_pt; double imag_pt; } complex_t;

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-5 Manipulating Individual Components of a Structured Data Object Direct component selection operator –A period placed between a structure type variable and a component name to create a reference to the component Figure 11.1 shows as an example the manipulation of the components of the variable current_planet

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-6 Figure 11.1 Assigning Values to Components of Variable current_planet

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-7 11.2 STRUCTURE TYPE DATA AS INPUT AND OUTPUT PARAMETERS a structured variable is passed as an input argument to a function –all of its component values are copied into the components of the function’s corresponding formal parameter. a variable is used as an output argument –the address-of operator must be applied in the same way that we would pass output arguments of the standard types char, int, and double

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-8 Figure 11.2 Function with a Structured Input Parameter

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-9 Figure 11.3 Function Comparing Two Structured Values for Equality

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-10 Figure 11.3 Function Comparing Two Structured Values for Equality (cont’d)

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-11 Figure 11.4 Function with a Structured Output Argument

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-12 Figure 11.5 Data Areas of main and scan_planet during Execution of status = scan_planet (&current_planet);

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-13 TABLE 11.2 Step-by-Step Analysis of Reference &(*plnp).diameter ReferenceTypeValue plnpplanet_t *address of structure that main refers to as current_planet *plnpplanet_tstructure that main refers to as current_planet (*plnp).diameterdouble12713.5 &(*plnp).diameterdouble *address of colored component of structure that main refers to as current_planet

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-14 11.3 FUNCTIONS WHOSE RESULT VALUES ARE STRUCTURED A function that computes a structured result can be modeled on a function computing a simple result. A local variable of the structure type can be allocated, filled with the desired data, and returned as the function result. –The function returns the values of all components.

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-15 Figure 11.6 Function get_planet Returning a Structured Result Type

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-16 Example 11.5 Use a computer program to simulate the experiment Keep track of the time of day as the experiment progresses Assuming a 24-hour clock, the structure type time_t is defined as follows typedef struct{ int hour, minute, second; } time_t;

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-17 Figure 11.7 Function to Compute an Updated Time Value

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-18 Figure 11.8 Structured Values as a Function Input Argument and as a Function Result

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-19 11.4 PROBLEM SOLVING WITH STRUCTURE TYPES Combining a user-defined type with a set of basic operations that allow one truly to see the type as undefined concept creates what is called an ADT ADT (abstract data type) –A data type combined with a set of basic operations

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-20 Figure 11.9 Data Type planet_t and Basic Operations

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-21 CASE STUDY: A USER-DEFINED TYPE FOR COMPLEX NUMBERS (1/4) Step 1: Problem –We are working on an engineering project that uses complex numbers for modeling of electrical circuits. –We need to develop a user-defined structure type and a set of operations that will make complex arithmetic virtually as straightforward as arithmetic on C’s built-in numeric types.

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-22 CASE STUDY: A USER-DEFINED TYPE FOR COMPLEX NUMBERS (2/4) Step 2: Analysis –A complex number is a number with a real part and an imaginary part –For example a+bi a is real part b is imaginary part

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-23 CASE STUDY: A USER-DEFINED TYPE FOR COMPLEX NUMBERS (3/4) Step 3: Design Specification of type complex_t and associated operations Structure: A complex is an object of type complex_t that consists of a pair of type double values Operators: /* * Complex number input function returns standard scanning * error code */

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-24 Specification of type complex_t and associated operations (continued) int scan_complex(complex_t *c) /* output-address of complex variable to fill */ /* * Complex output function displays value as a+bi or a-bi * Display only a if imaginary part is 0 * Display only bi if real part is 0 */ void print_complex(complex_t c) /* input-complex number to display*/

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-25 Specification of type complex_t and associated operations (continued) /* * Returns sum of complex values c1 and c2 */ complex_t add_complex(complex_t c1, complex_t c2)/* input */ /* * Returns difference c1- c2 */ complex_t subtract_complex(complex_t c1, complex_t c2)/* input */

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-26 Specification of type complex_t and associated operations (continued) /* * Returns product of complex values c1 and c2 */ complex_t multiply_complex(complex_t c1, complex_t c2)/* input */ /* * Returns quotient of complex values (c1/c2) */ complex_t divide_complex(complex_t c1, complex_t c2)/* input */ /* * Returns absolute value of complex number c */ complex_t abs_complex(complex_t c)/* input */

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-27 CASE STUDY: A USER-DEFINED TYPE FOR COMPLEX NUMBERS (4/4) Step 4: Implementation (Figure11.10) Step 5: Testing

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-28 Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-29 Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-30 Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-31 Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-32 Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-33 11.5 PARALLEL ARRAYS AND ARRAYS OF STRUCTURES Parallel arrays int id[50];/* id numbers and */ double gpa[50];/* gpa’s of up to 50 students */ double x[NUM_PTS]; /* (x,y) coordinates of */ double y[NUM_PTS]; /* up to NUM_PTS points*/ Declaring an array of structures #define MAX_STU 50 #define NUM_PTS 10 typedef struct { int id; double gpa; } student_t...

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-34 Figure 11.11 An Array of Structures

35 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-35 CASE STUDY: UNIVERSAL MEASUREMENT CONVERSION(1/4) Step 1: Problem –We would like a program that takes a measurement in one unit (e.g., 4.5 quarts) and converts it to another unit (e.g., liters). –The program should produce an error message if a conversion between two units of different classes is requested. –The program should take a database of conversion information from an input file before accepting conversion problems entered interactively by the user.

36 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-36 CASE STUDY: UNIVERSAL MEASUREMENT CONVERSION(2/4) Step 2: Analysis –Structure Data Type unit_t components: name abbrev class standard –Problem Constants NAME_LEN 30 ABBREV_LEN 15 CLASS_LEN 20 MAX_UNITS 20

37 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-37 Step 2: Analysis (continued) –Problem Inputs unit_t units [MAX_UNITS] double quantity char old_units [NAME_LEN] char new_units [NAME_LEN] –Problem Output Message giving conversion

38 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-38 CASE STUDY: UNIVERSAL MEASUREMENT CONVERSION(3/4) Step 3: Design –Algorithm 1. Load units of measurement database 2. Get value to convert and old and new unit names 3. Repeat until data format error encountered 4. Search for old units in database 5. Search for new units in database 6. if conversion is impossible 7. Issue appropriate error message else 8. Compute and display conversion 9. Get value to convert and old and new unit names

39 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-39 Step 3: Design (continued) –Refinement 1.1 Open database file 1.2 Initialize subscripting variable i 1.3 Scan a unit structure from the database file 1.4 Repeat unit EOF, data format error, or attempted overflow of units list 1.4.1 Store unit structure in units array 1.4.2 Update i 1.4.3 Scan next unit structure from file 1.5 Close database file

40 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-40 CASE STUDY: UNIVERSAL MEASUREMENT CONVERSION(4/4) Step 4: Implementation (Figure 11.12) Step 5: Testing (Figure 11.13)

41 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-41 Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures

42 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-42 Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

43 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-43 Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

44 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-44 Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

45 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-45 Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

46 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-46 Figure 11.13 Data File and Sample Run of Measurement Conversion Program

47 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-47 11.6 UNION TYPES(OPTIONAL) union –A data structure that overlays components in memory, allowing one chunk of memory to be interpreted in multiple ways

48 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-48 Example 11.6 Define an union structure to use as the type of one component of a person’s physical description. If the person has hair, we would like to record the hair color. If the person is bald, we need to note whether or not this baldness is disguised by a wig

49 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-49 Figure 11.14 Function That Displays a Structure with a Union Type Component

50 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-50 Figure 11.15 Two Interpretations of Parameter hair

51 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-51 Example 11.7 A partial solution to the problem of finding the area and perimeter of a geometric figure. First, define structure types for each figure of interest. Then, define a union type with a component for each figure type. Finally, define a structure containing both a component of the union type and a component whose value denotes which is the correct interpretation of the union.

52 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-52 Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures

53 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-53 Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures (cont’d)

54 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-54 Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures (cont’d)

55 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-55 Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures (cont’d)

56 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-56 11.7 COMMON PROGRAMMING ERRORS Most common error is incorrect use of a component selected for processing When using the direct selection operator (.), always be aware of the type of the component selected, and use the value in a manner consistent with its type If a structure type output parameter is used in a function, one can avoid the operator precedence problems associated with combining the indirection(*) and direct component selection (.) operators by using the indirect component selection operator (->)

57 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-57 Chapter Review C permits the user to define a type composed of multiple named components A component of a structure is referenced by placing the direct component selection operator (.) between the structure variable name and the component name User-defined structure types can be used in most situations where built-in types are valid

58 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11-58 Question?


Download ppt "Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪."

Similar presentations


Ads by Google