© 2010 pearson addison-wesley. all rights reserved. addison wesley is an imprint of chapter 11:...

31
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman

Upload: angel-doyle

Post on 27-Mar-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

© 2010 Pearson Addison-Wesley. All rights reserved.

Addison Wesley is an imprint of

Chapter 11:Structure and Union Types

Problem Solving & Program Design in C

Sixth Edition

By Jeri R. Hanly &

Elliot B. Koffman

Page 2: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-2

© 2010 Pearson Addison-Wesley. All rights reserved. 1-2

Structures and Union Types

A database is a collection of information stored in a computer’s memory or in a disk file.

A database is subdivided into records, which normally contains information regarding specific data objects.

The structure of the record is determined by the structure of the object’s data type.

Page 3: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-3

© 2010 Pearson Addison-Wesley. All rights reserved. 1-3

Structures and Union TypesDefine a structure type before it can be created or saved. #define STRSZ 10typedef struct {

char name[STRSZ];double diameter;int moons;double orbit_time, rotation_time;

} planet_t

planet_t current_planet, previous_planet, blank_planet = { “”, 0, 0, 0, 0};

Page 4: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-4

© 2010 Pearson Addison-Wesley. All rights reserved. 1-4

Figure 11.1 Assigning Values to Components of Variable current_planet p569

Page 5: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-5

© 2010 Pearson Addison-Wesley. All rights reserved. 1-5

Figure 11.2 Function with a Structured Input Parameter

Page 6: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-6

© 2010 Pearson Addison-Wesley. All rights reserved. 1-6

Figure 11.3 Function Comparing Two Structured Values for Equality

Page 7: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-7

© 2010 Pearson Addison-Wesley. All rights reserved. 1-7

Figure 11.3 Function Comparing Two Structured Values for Equality (cont’d)

Page 8: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-8

© 2010 Pearson Addison-Wesley. All rights reserved. 1-8

Figure 11.4 Function with a Structured Output Argument

Page 9: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-9

© 2010 Pearson Addison-Wesley. All rights reserved. 1-9

Figure 11.5 Data Areas of main and scan_planet during Execution of status = scan_planet (&current_planet);

Page 10: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-10

© 2010 Pearson Addison-Wesley. All rights reserved. 1-10

Figure 11.6 Function get_planet Returning a Structured Result Type

Page 11: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-11

© 2010 Pearson Addison-Wesley. All rights reserved. 1-11

Figure 11.7 Function to Compute an Updated Time Value

Page 12: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-12

© 2010 Pearson Addison-Wesley. All rights reserved. 1-12

Figure 11.8 Structured Values as a Function Input Argument and as a Function Result

Page 13: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-13

© 2010 Pearson Addison-Wesley. All rights reserved. 1-13

Figure 11.9 Data Type planet_t and Basic Operations

Page 14: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-14

© 2010 Pearson Addison-Wesley. All rights reserved. 1-14

Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers

Page 15: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-15

© 2010 Pearson Addison-Wesley. All rights reserved. 1-15

Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Page 16: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-16

© 2010 Pearson Addison-Wesley. All rights reserved. 1-16

Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Page 17: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-17

© 2010 Pearson Addison-Wesley. All rights reserved. 1-17

Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Page 18: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-18

© 2010 Pearson Addison-Wesley. All rights reserved. 1-18

Figure 11.10 Partial Implementation of Type and Operators for Complex Numbers (cont’d)

Page 19: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-19

© 2010 Pearson Addison-Wesley. All rights reserved. 1-19

Figure 11.11 An Array of Structures

Page 20: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-20

© 2010 Pearson Addison-Wesley. All rights reserved. 1-20

Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures

Page 21: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-21

© 2010 Pearson Addison-Wesley. All rights reserved. 1-21

Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Page 22: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-22

© 2010 Pearson Addison-Wesley. All rights reserved. 1-22

Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Page 23: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-23

© 2010 Pearson Addison-Wesley. All rights reserved. 1-23

Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Page 24: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-24

© 2010 Pearson Addison-Wesley. All rights reserved. 1-24

Figure 11.12 Universal Measurement Conversion Program Using an Array of Structures (cont’d)

Page 25: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-25

© 2010 Pearson Addison-Wesley. All rights reserved. 1-25

Figure 11.13 Data File and Sample Run of Measurement Conversion Program

Page 26: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-26

© 2010 Pearson Addison-Wesley. All rights reserved. 1-26

Figure 11.14 Function That Displays a Structure with a Union Type Component

Page 27: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-27

© 2010 Pearson Addison-Wesley. All rights reserved. 1-27

Figure 11.15 Two Interpretations of Parameter hair

Page 28: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-28

© 2010 Pearson Addison-Wesley. All rights reserved. 1-28

Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures

Page 29: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-29

© 2010 Pearson Addison-Wesley. All rights reserved. 1-29

Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures (cont’d)

Page 30: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-30

© 2010 Pearson Addison-Wesley. All rights reserved. 1-30

Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures (cont’d)

Page 31: © 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design

1-31

© 2010 Pearson Addison-Wesley. All rights reserved. 1-31

Figure 11.16 Program to Compute Area and Perimeter of Geometric Figures (cont’d)