Title: User_defined Structure Type
1User_defined Structure Type
- Database, record and structure
- A structure type is a data type for a
- record composed of multiple components
- For example a database of planets needs
- Name Jupiter
- Diameter 142,980 km
- Moons 16
- Orbit time 11.9 yr
- Rotation time 9.925 hr
-
2User_defined Structure Type
- We need to use define a structure type
- define STRSIZE 10
- typedef struct
- char nameSTRSIZE
- double diameter
- int moons
- double orbit_time,
- rotation_time
- planet_t suffix _t is used to show it is a
type
3User_defined Structure Type
- Also we need to define structure variables of the
defined structure type - planet_t previous_planet, current_planet
- blank_planet , 0, 0, 0, 0
- In the program we use variables of the structure
type - current_planet blank_planet
4User_defined Structure Type
- Also we can define the a structure containing
components that are structure. Such an structure
is referred to as hierarchical structure - For example
- typedef struct
- double diameter
- planet_t planets9
- char galaxySTRSIZE
- solar_sys_t
-
5Manipulating Individual Components of a Structure
Data Object
- Direct component selection (.) can be used to
reference a component of a structure. For
example - strcpy(current_planet.name, Jupiter)
- current_planet.diameter 142980
- current_planet.moon16
- current_planet.orbit_time11.9
- current_planet.rotation_time9.925
6Operators Precedence
- aj, f(), left
highest - , -- (postfix) left
- , --, !(prefix) right
- -, , (unary) , right
- (type name) right
- , /, left
- , - left
- lt, gt, lt, gt left
- , ! left
- left
- left
- , , -, , /, right
lowest
7Structure Type Data as Input and Output
Parameters
- We can assign one structure to another structure
variable, however we can not apply the equality
and inequality operators to a structure type as a
unit. - When a structure variable is passed to a
function, if it is input its value is copied to
the formal variable and if it is output we must
apply the address of operator to pass it as a
pointer to the variable.
8Function with a Structured Input Variable
- /
- 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)
9Function Comparing Two Structured Values for
Equality
- include ltstring.hgt
- /
- 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)
10Function with a Structured Output Argument
- /
- Fills a type planet_t structure with input
data. Integer returned as - function result is success/failure/EOF
indicator. - 1 gt successful input of one planet
- 0 gt error encountered
- EOF gt insufficient data before end of
file - In case of error or EOF, value of type
planet_t output argument is - undefined.
- /
11- int
- scan_planet(planet_t plnp) / output - address
of planet_t structure -
to fill / -
- 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)
-
12Indirect Component Selection Operator
- In the indirect referencing (pnlp).moons
parenthesizing was used to override the default
operator precedence - C provide a single operator that cobines
indirection and component selection operator. - (structp). component is equal to
- strucpt -gt component
13- int
- scan_planet(planet_t plnp) / output - address
of planet_t structure -
to fill / -
- int result
- result scanf("slfdlflf",
plnp-gtname, - (plnp-gtdiameter),
- (plnp-gtmoons),
- (plnp-gtorbit_time),
- (plnp-gtrotation_time))
- if (result 5)
- result 1
- else if (result ! EOF)
- result 0
- return (result)
-
14Function get_planet Returning a Structured Result
Type
- /
- 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)
-
- current_planet get_planet() is same as
scan_planet(current_planet)
15A Function with Structure Result
- Suppose we defined the following structure for
time - typedef struct
- int hour, minute, seconds
- time_t
- And in the program time_now defined as a
variable of type time_t - If time_now initialized by 215832 and secs
initialized by 97 we want by calling - time_now new_time(time_now,secs)
- time_now becomes 220009
16- Function to Compute an Updated Time Value
- /
- Computes a new time represented as a time_t
structure - and based on time of day and elapsed seconds.
- /
- time_t
- new_time(time_t time_of_day, / input - time to
be updated / - int elapsed_secs) / input - seconds
since last update / -
- int new_hr, new_min, new_sec
- new_sec time_of_day.second
elapsed_secs - time_of_day.second new_sec 60
- new_min time_of_day.minute new_sec /
60 - time_of_day.minute new_min 60
- new_hr time_of_day.hour new_min / 60
- time_of_day.hour new_hr 24
- return (time_of_day)
17Abstract Data Type
- A user-defined data type combined with a set of
basic operations. - For example
- data type planet_t
- operations scan_planet, print_planet,
planet_equal - data type complex_t (complex number abi)
- operations scan, print, add, subtract,
multiply, divide, abs
18A User_Defined Type for Complex Numbers
- Partial Implementation of Type and Operators for
Complex Numbers - /
- Operators to process complex numbers
- /
- include ltstdio.hgt
- include ltmath.hgt
- / User-defined complex number type /
- typedef struct
- double real, imag
- complex_t
- int scan_complex(complex_t c)
- void print_complex(complex_t c)
- complex_t add_complex(complex_t c1, complex_t
c2) - complex_t subtract_complex(complex_t c1,
complex_t c2) - complex_t multiply_complex(complex_t c1,
complex_t c2) - complex_t divide_complex(complex_t c1, complex_t
c2) - complex_t abs_complex(complex_t c)
19- / Driver
/ - int
- main(void)
-
- complex_t com1, com2
-
- / Gets two complex numbers /
- printf("Enter the real and imaginary parts
of a complex number\n") - printf("separated by a spacegt ")
- scan_complex(com1)
- printf("Enter a second complex numbergt ")
- scan_complex(com2)
- / Forms and displays the sum /
- printf("\n")
- print_complex(com1)
- printf(" ")
- print_complex(com2)
- printf(" ")
- print_complex(add_complex(com1, com2))
20- / Forms and displays the difference /
- printf("\n\n")
- print_complex(com1)
- printf(" - ")
- print_complex(com2)
- printf(" ")
- print_complex(subtract_complex(com1,
com2)) - / Forms and displays the absolute value
of the first number / - printf("\n\n")
- print_complex(com1)
- printf(" ")
- print_complex(abs_complex(com1))
- printf("\n")
- return (0)
-
21- / Returns sum of complex values c1 and c2/
- complex_t
- add_complex(complex_t c1, complex_t c2) / input
- values to add / -
- complex_t csum
- csum.real c1.real c2.real
- csum.imag c1.imag c2.imag
- return (csum)
-
- /Returns difference c1 c /
- complex_t
- subtract_complex(complex_t c1, complex_t c2) /
input parameters / -
- complex_t cdiff
- cdiff.real c1.real - c2.real
- cdiff.imag c1.imag - c2.imag
- return (cdiff)
22Parallel Arrays And Arrays of Structures
- To represent student information we used parallel
arrays - int id50
- double gpa50
- Instead of using parallel arrays to represent the
student information - an array of structure can be used
- typedef struct
- int id
- double gpa
- student_t
- student_t stu_listMAX_STU
23Parallel Arrays And Arrays of Structures
- If function scan_student is available we can
- use
- for (i 0 i lt MAX_STU i)
- scan_student(stu_listi)
- To fill the array and
- for (i 0 i lt MAX_STU i)
- printf(d\t.2f\n, stu_listi.id,
stu_listi.gpa) - To print the array contents
24Case 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 /
25Case Study
- typedef struct / unit of
measurement type / - char nameNAME_LEN / character string
such as "milligrams" / - char abbrevABBREV_LEN/ shorter
character string such as "mg" / - char classCLASS_LEN / character string
such as "pressure", - "distance",
"mass" / - double standard / number of
standard units equivalent - to this unit
/ - unit_t
- int fscan_unit(FILE filep, unit_t unitp)
- void load_units(int unit_max, unit_t units, int
unit_sizep) - int search(const unit_t units, const char
target, int n) - double convert(double quantity, double old_stand,
double new_stand)
26- Data File and Sample Run of Measurement
Conversion Program - Data file units.dat
- miles mi distance
1609.3 - kilometers km distance
1000 - yards yd distance
0.9144 - meters m distance 1
- quarts qt liquid_volume
0.94635 - Sample run
- Enter a conversion problem or q to quit.
- To convert 25 kilometers to miles, you would
enter - gt 25 kilometers miles
- or, alternatively,
- gt 25 km mi
- gt 450 km miles
- Attempting conversion of 450.0000 km to miles . .
. - 450.0000km 279.6247 miles
- Enter a conversion problem or q to quit.
- gt 2.5 qt l
27Common Programming Errors
- Incorrect use of direct component selection .
- Indirect component selection -gt can solve the
problem of operator precedence of . - There is no comparison operator on structure
types and they can not directly used in printf
and scanf