CPS 125: Digital Computation and Programming - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CPS 125: Digital Computation and Programming

Description:

Case Study. Common Programming Errors. Structure Data Type. Simple data type ... Name, Diameter, Moons, Orbit time, Rotation time. Actual values ' ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 23
Provided by: Che6158
Category:

less

Transcript and Presenter's Notes

Title: CPS 125: Digital Computation and Programming


1
CPS 125 Digital Computation and Programming
  • Structure Types

2
Outline
  • User-Defined Structure Types
  • Structure Type Data as Input and Output
    Parameters
  • Functions whose Result Values are Structured
  • Case Study
  • Common Programming Errors

3
Structure Data Type
  • Simple data type
  • int, long, float, double, char,
  • Structure type
  • A composite data type, which consist of several
    data items with simple or composite data types
  • e.g. Description of a planet
  • Name, Diameter, Moons, Orbit time, Rotation time
  • Actual values
  • Jupiter, 142980(km), 16, 11.9(yr), 9.925(hr)

4
Definitions
  • struct date
  • int month, day, year
  • hire_date, current_date
  • typedef struct
  • int month, day, year
  • date_t
  • date_t hire_date, current_date

5
Definition for Planet
  • define STRSIZE 10
  • typedef struct
  • char nameSTRSIZE
  • double diameter
  • int moons
  • double orbit_time,
  • rotation_time
  • planet_t
  • planet_t blank_planet , 0, 0, 0, 0
  • planet_t previous_planet, current_planet
  • previous_planet current_planet

6
Hierarchical Structure Type
  • typedef struct
  • double diameter
  • planet_t planets9
  • char galaxySTRSIZE
  • solar_sys_t

7
Referencing Components
  • planet_t current_planet, plnp
  • Two operators
  • Component selection operator (a period .)
  • e.g. current_planet.diameter 142980
  • strcpy(current_planet.name, Jupiter)
  • Indirect component selection operator (an arrow
    -gt)
  • e.g. plnp (planet_t )malloc(sizeof(planet_t))
  • plnp-gtdiameter 142980
  • strcpy(plnp-gtname, Jupiter)
  • plnp-gtname ltgt (plnp).name (plnp.name?)

8
Operator Precedence
  • aj, f(), a.name, p-gtname left
  • , -- (postfix) left
  • , --, (prefix) !, -, , (unary) , right
  • (type name) right
  • , /, left
  • , - left
  • lt, gt, lt, gt left
  • , ! left
  • left
  • left
  • , , -, , /, right

high
low
9
  • typedef struct
  • char city21
  • char state3
  • char zip7
  • address_t
  • typedef struct
  • int id_num
  • char name15
  • address_t addr
  • double income
  • family_rec_t next
  • family_rec_t
  • family_rec_t family1, family2, fap

10
(No Transcript)
11
  • family1.id_num 5
  • strcpy(family1.name,Jones)
  • strcpy(family1.addr.city,Toronto)
  • strcpy(family1.addr.state,ON)
  • strcpy(family1.addr.zip,M5G1X1)
  • family1.income 6000.00
  • family1.next family2
  • / initialization on family2 /
  • fptr family1

A linked list
12
  • Define a type named long_lat_t that would store
    longitude or latitude values. Include components
    named degrees, minutes and direction (N, S,
    E, or W)
  • A catalog listing for a textbook consists of the
    authors names, the title, the publisher, and the
    year of publication. Declare a structure type
    catalog_entry_t and a variable book, and write
    statements that store the relevant data for this
    textbook in book.

13
Structure Type used in Functions
  • / Displays with labels all components of a
    planet_t structure /
  • void print_planet(planet_t pl) / input - one
    planet structure /
  • printf("s\n", pl.name)
  • printf(" Equatorial diameter .0f km\n",
    pl.diameter)
  • printf(" Number of moons d\n",
    pl.moons)
  • printf(" Time to complete one orbit of the
    sun .2f years\n",
  • pl.orbit_time)
  • printf(" Time to complete one rotation on
    axis .4f hours\n",
  • pl.rotation_time)
  • Call the function print_planet(current_planet)

Input parameter
14
  • / Determines whether or not the components of
    planet_1 and planet_2 match /
  • int
  • planet_equal(planet_t planet_1, / input -
    planets to /
  • planet_t planet_2) /
    compare /
  • return (strcmp(planet_1.name,
    planet_2.name) 0
  • planet_1.diameter
    planet_2.diameter
  • planet_1.moons planet_2.moons
  • planet_1.orbit_time
    planet_2.orbit_time
  • planet_1.rotation_time
    planet_2.rotation_time)
  • Call the function if (planet_equal(current_planet
    , previous_planet) 1)

15
  • int scan_planet(planet_t plnp) / output -
    address of planet_t /
  • int result
  • result scanf("slfdlflf",
    (plnp).name,

  • (plnp).diameter,

  • (plnp).moons,

  • (plnp).orbit_time,

  • (plnp).rotation_time)
  • if (result 5)
  • result 1
  • else if (result ! EOF)
  • result 0
  • return (result)
  • Call the function if (scan_planet(current_planet
    ) 1)

Output parameter
plnp-gtname, (plnp-gtdiameter), (plnp-gtmoons), (p
lnp-gtorbit_time), (plnp-gtrotation_time)
16
  • / Gets and returns a planet_t structure /
  • planet_t get_planet(void)
  • planet_t planet
  • scanf("slfdlflf", planet.name,
  • planet.diameter,
  • planet.moons,
  • planet.orbit_time,
  • planet.rotation_tim
    e)
  • return (planet)
  • Call the function current_planet get_planet()
  • With the same effect as scan_planet(current_plan
    et)

17
(No Transcript)
18
Abstract Data Type
  • A user-defined data type combined with a set of
    basic operations.
  • e.g. data type planet_t
  • operations scan_planet, print_planet,
    planet_equal
  • e.g. data type complex_t (complex number abi)
  • operations scan, print, add, subtract,
    multiply, divide, abs

19
Array of Structures
  • Structure data as a record, array as a database
  • typedef struct
  • int id
  • double gpa
  • student_t
  • typedef struct
  • double x, y
  • point_t
  • student_t stu_listMAX_STU
  • point_t polygonNUM_PTS

20
Array of Structures
  • for (i 0 i lt MAX_STU i)
  • scan_student(stu_listi)
  • for (i 0 i lt MAX_STU i)
  • printf(d\t.2f\n, stu_listi.id,
    stu_listi.gpa)

21
Case Study
  • Problem Write a program that takes a measurement
    in one unit (e.g. 4.5 quarts) and converts it to
    another unit (e.g. liters).
  • Structured data type
  • unit_t components
  • name / e.g. milligrams /
  • abbrev / e.g. mg /
  • class / liquid_volume, distance, or
    mass /
  • standard / number of standard units equivalent
    to this /

22
Common Programming Errors
  • Always use the right operation on selected
    component
  • e.g. If selected component is string, cannot use
    assignment operator
  • If selected component is array, when passing to
    a function as output, no required
  • No comparison operator on structure types
  • Cannot directly used in printf and scanf
Write a Comment
User Comments (0)
About PowerShow.com