Presentation is loading. Please wait.

Presentation is loading. Please wait.

TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)

Similar presentations


Presentation on theme: "TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)"— Presentation transcript:

1 TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…) Data abstraction (abstract data types) specify the data objects and the operations to be performed on these data objects A queue: how to implement? Array, linked list and the operations: enqueue(), dequeue(), first() Encapsulation of data objects and operations leads to reusable code

2 TDBA66, Lecture Ch. 11, vt-03 2 Header Files A text file used to create personal librarys name: name_of_area.h Typical contents. 1.A block comment telling the purpose of the library 2.#define directives for naming constants 3.User defined data types 4.Block comments telling the purpose of every library function and prototype declarations of the form extern extern has the effect that the function that is declared extern can be called from any function where #include ”name_of_area.h” is included

3 TDBA66, Lecture Ch. 11, vt-03 3 Figure 11.2 Header File planet.h for Personal Library with Data Type and Associated Functions /* planet.h * * abstract data type planet * * Type planet_t has these components: * name, diameter, moons, orbit_time, rotation_time * * Operators: * print_planet, planet_equal, scan_planet */ #define PLANET_STRSIZ 10 typedef struct { /* planet structure */ char name[PLANET_STRSIZ]; double diameter; /* equatorial diameter in km*/ int moons; /* number of moons*/ double orbit_time, /* years to orbit sun once*/ rotation_time; /* hours to complete one revolution on axis*/ } planet_t;

4 TDBA66, Lecture Ch. 11, vt-03 4 Fig 11.2 cont. /* * Displays with labels all components of a planet_t structure */ extern void print_planet(planet_t pl); /* input-one planet structure*/ /* * Determines whether or not the components of planet_1 and planet_2 match */ extern int planet_equal(planet_t planet_1, /* input - planets to */ planet_t planet_2); /* compare*/ /* * Fills a type planet_t structure with input data. Integer returned as * function result is success/failure/EOF indicator. * 1 => successful input of planet * 0 => error encountered * EOF => insufficient data before end of file * In case of error or EOF, value of type planet_t output argument is undefined. */ extern int scan_planet(planet_t *plnp); /* output - address of planet_t structure to fill*/

5 TDBA66, Lecture Ch. 11, vt-03 5 Figure 11.3 Portion of Program That Uses Functions from a Personal Library /* * Beginning of source file in which a personal library and system I/O library are used. */ #include /* system's standard I/O functions* / #include "planet.h" /* personal library with planet_t data type and operators*/...

6 TDBA66, Lecture Ch. 11, vt-03 6 Implementation Files Figure 11.4 Implementation File planet.c Containing Library with Planet Data Type and Operators /* * planet.c */ #include #include "planet.h" /* * 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); }

7 TDBA66, Lecture Ch. 11, vt-03 7 Fig 11.4 cont. /* * 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); }

8 TDBA66, Lecture Ch. 11, vt-03 8 Fig 11.4 cont. /* * Fills a type planet_t structure with input data. Integer returned as * function result is success/failure/EOF indicator. * 1 => successful input of planet * 0 => error encountered * EOF => insufficient data before end of file * In case of error or EOF, value of type planet_t output argument is * undefined. */ int scan_planet(planet_t *plnp) /* output - address of planet_t structure to fill*/ { int result; result = scanf("%s%lf%d%lf%lf", plnp->name, &plnp->diameter, &plnp->moons, &plnp->orbit_time, &plnp->rotation_time); fflush(stdin); if (result == 5) result = 1; else if (result != EOF) result = 0; return (result); }

9 TDBA66, Lecture Ch. 11, vt-03 9 Storage classes There are 4 storage classes autolocal variables in functions. Exist only within the function externwhen declared outside a function and reachable from everywhere (gtlobal variables). E.g. Prototypes See examples in Fig. 11.6 and 11.7 static static float num; /* creates local variable num that ”lives for ever” */ Used to remember its value form one call to another register(not used very often)

10 TDBA66, Lecture Ch. 11, vt-03 10 Exit() Figure 11.8 Function factorial with Premature Exit on Negative Data /* * Computes n! * n is greater than or equal to zero -- premature exit on negative data */ int factorial(int n) { int i, /* local variables */ product = 1; if (n < 0) { printf("\n***Function factorial reports "); printf("ERROR: %d! is undefined***\n", n); exit(1); } else { /* Compute the product nx(n-1)x(n-2)x...x2x1 */ for (i = n; i > 1; --i) { product = product * i; } /* Return function result */ return (product); }

11 TDBA66, Lecture Ch. 11, vt-03 11 Ch. 11.6, 11.8. not included in course Ch. 11.7 Arguments to function main Figure 11.12 File Backup Using Arguments to Function main (only beginning) /* * Makes a backup of the file whose name is the first command line argument. * The second command line argument is the name of the new file. */ #include int main(int argc, /* input-argument count(including program name) */ char *argv[]) /* input - argument vector */ { FILE *inp, /* file pointers for input */ *outp; /* and backup files */ char ch; /* one character of input file */ int status; /* Open input and backup files if possible */ inp = fopen(argv[1], "r"); if (inp == NULL) { printf("\nCannot open file %s for input\n", argv[1]); exit(1); } if ((outp = fopen(argv[2], "w")) == NULL) { printf("\nCannot open file %s for output\n", argv[2]); exit(1); }


Download ppt "TDBA66, Lecture Ch. 11, vt-03 1 Abstraction Prucedural abstraction moduralize by hiding the details in a function() Ex.functions in get_params(…) display_match(…)"

Similar presentations


Ads by Google