1 / 48

ecs30 Summer 2014: Programming and Problem Solving # 09: Struct

ecs30 Summer 2014: Programming and Problem Solving # 09: Struct. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. Our Problem.

misae
Download Presentation

ecs30 Summer 2014: Programming and Problem Solving # 09: Struct

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ecs30 Summer 2014:Programming and Problem Solving#09: Struct Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 Winter 2012 Lecture #01

  2. Our Problem Today’s problem is to write a program that generates the instructions for the priests to follow in moving the disks. While quite difficult to solve iteratively, this problem has a simple and elegant recursive solution. ecs30 Winter 2012 Lecture #17

  3. [ 1] ==> move disk 1 from A to C [ 2] ==> move disk 2 from A to B [ 3] ==> move disk 1 from C to B [ 4] ==> move disk 3 from A to C [ 5] ==> move disk 1 from B to A [ 6] ==> move disk 2 from B to C [ 7] ==> move disk 1 from A to C [ 8] ==> move disk 4 from A to B [ 9] ==> move disk 1 from C to B [ 10] ==> move disk 2 from C to A [ 11] ==> move disk 1 from B to A [ 12] ==> move disk 3 from C to B [ 13] ==> move disk 1 from A to C [ 14] ==> move disk 2 from A to B [ 15] ==> move disk 1 from C to B ecs30 Winter 2012 Lecture #01

  4. The Tower FunctionFigure 10.24 void tower (char from_peg, char to_peg, char aux_peg, int n) { if (n == 1) printf(“move disk %d from %c to %c\n”, n, from_peg, to peg); tower(from_peg, aux_peg, to_peg, n-1); tower(from_peg, to_peg, aux_peg, 1); tower(aux_peg, to_peg, from_peg, n-1); } ecs30 Winter 2012 Lecture #17

  5. HW#5 • Tower of Hanoi with FOUR Pegs Number of Moves? (needs to be smaller than 3 pegs) ecs30 Winter 2012 Lecture #17

  6. http://www.exocortex.org/toh/ void tower4P (char from_peg, char to_peg, char aux1_peg, char aux2_peg, int n) { if (n == 1) printf(“move disk %d from %c to %c\n”, n,from_peg, to peg); … } ecs30 Winter 2012 Lecture #17

  7. structcompound data type #define STRSIZ 10 typedefintFelix_integer_t; typedefstruct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; }planet_t; … planet_tcurrent_planet; ecs30b Fall 2008 Lecture #21

  8. ecs30 Winter 2012 Lecture #17

  9. structcompound data type #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; }; … struct planet current_planet; ecs30b Fall 2008 Lecture #24

  10. Access the members (or attributes)! ecs30b Fall 2008 Lecture #21

  11. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet; ecs30b Fall 2008 Lecture #21

  12. &current_planet #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet; ecs30b Fall 2008 Lecture #21

  13. &current_planet #define STRSIZ 10 typedefstruct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_tcurrent_planet; sizeof(planet_t) == ? ecs30b Fall 2008 Lecture #21

  14. #include <stdio.h> #include <stdlib.h> #define STRSIZ 10 typedefstruct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; intmain(void) { printf("sizeof = %d\n", sizeof(planet_t)); return 0; } sizeof = 38 ecs30 Winter 2012 Lecture #17

  15. planet_t xyz(planet_t x) { strcpy(x.name, "abcdefghj"); return x; } int main(void) { planet_t current; planet_t next; strcpy(current.name, "rstuvwxyz"); next = xyz(current); printf("%s\n", next.name); return 0; } ecs30b Fall 2008 Lecture #21

  16. ecs30b Fall 2008 Lecture #21

  17. planet_t xyz(planet_t x) { strcpy(x.name, "abcdefghj"); return x; } int main(void) { planet_t current; scanf(“%d”, &(current.moons)); return 0; } ecs30b Fall 2008 Lecture #21

  18. planet_t * xyz_cbr(planet_t *xp) { strcpy(xp->name, "abcdefghj"); return xp; } int main(void) { planet_t current; planet_t *next; strcpy(current.name, "rstuvwxyz"); next = xyz_cbr(&current); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #21

  19. struct X vs. struct X * • Access to the individual attribute(s): • struct X x; ~ x.<attrID> • struct X *xp; ~ xp-><attrID> • xp = &x; • x = *xp; • y = x; ecs30b Fall 2008 Lecture #24

  20. ecs30 Winter 2012 Lecture #01

  21. ecs30b Fall 2008 Lecture #24

  22. planet_t * xyz_cbr(planet_t *xp) { strcpy(xp->name, "abcdefghj"); return xp; } int main(void) { planet_t current; planet_t *next; strcpy(current.name, "rstuvwxyz"); next = xyz_cbr(&current); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #24

  23. Assignment for struct planet_t x,y; y = x; strcpy(y.name, x.name); y.diameter = x.diameter; y.moons = x.moons; y.orbit_time = x.orbit_time; y.rotation_time = x.rotation_time; ecs30b Fall 2008 Lecture #24

  24. Assignment for struct planet_t x,y; y = x; strcpy(y.name, x.name); y.diameter = x.diameter; y.moons = x.moons; y.orbit_time = x.orbit_time; y.rotation_time = x.rotation_time; Not entirely right!! ecs30b Fall 2008 Lecture #24

  25. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); ecs30b Fall 2008 Lecture #24

  26. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); strncpy(&y, &x, sizeof(planet_t)); ecs30b Fall 2008 Lecture #24

  27. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); strncpy(&y, &x, sizeof(planet_t)); The strncpy() function copies at most n characters from &x into &y. If &x is less than n characters long, the remainder of &y is filled with ‘\0’ characters. Otherwise, &y is not terminated. ecs30b Fall 2008 Lecture #24

  28. Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); planet_t myfunc(…); x = myfunc(…); ecs30b Fall 2008 Lecture #24

  29. #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; struct planet next; struct planet previous; }; ecs30b Fall 2008 Lecture #24

  30. No Recursive in struct! #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; struct planet next; struct planet previous; }; sizeof(struct planet) == ? ecs30b Fall 2008 Lecture #24

  31. #define STRSIZ 10 struct planet { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; struct planet *next; struct planet *previous; }; sizeof(struct planet) == ? ecs30b Fall 2008 Lecture #24

  32. struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24

  33. struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24

  34. struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet p[12]; }; ecs30b Fall 2008 Lecture #24

  35. printf("%d\n", ((planet_t *) 0)->diameter); ecs30b Fall 2008 Lecture #24

  36. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; printf("%d\n", ((planet_t *) 0)->diameter); printf("%d\n", &(((planet_t *) 0)->diameter)); ecs30b Fall 2008 Lecture #24

  37. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; printf("%d\n", (int) &(((planet_t *) 0)->name)); printf("%d\n", (int) &(((planet_t *) 0)->diameter)); printf("%d\n", (int) &(((planet_t *) 0)->moons)); ecs30b Fall 2008 Lecture #24

  38. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; printf("%d\n", (int) &(((planet_t *) 0)->name)); printf("%d\n", (int) &(((planet_t *) 0)->diameter)); printf("%d\n", (int) &(((planet_t *) 0)->moons)); Expect: 0, 10, 18 MacBook/pc16 (i386): 0, 12, 20 pc26 (x86_64): 0, 16, 24 ecs30b Fall 2008 Lecture #24

  39. typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; &((planet_t *) 0)->diameter) “Be very careful! It’s Machine-Dependent actually!!” ecs30b Fall 2008 Lecture #24

  40. I386 architecture typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; ecs30b Fall 2008 Lecture #24

  41. fwrite/fread • format versus binary #include <stdio.h> size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); ecs30b Fall 2008 Lecture #26

  42. argv_test.c #include <stdio.h> #include <stdlib.h> #include <strings.h> #define MAX_NAME 8192 int main (int argc, char *argv[]) // argv is an array of strings { int i; fprintf(stderr, "totally, we have %d arguments\n", argc); for (i = 0; i < argc; i++) { fprintf(stderr, "the #%2d argument is [%20s].\n", i, argv[i]); } return 1; } ecs30b Fall 2008 Lecture #26

  43. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet[12]; fwrite(current_planet, sizeof(planet_t), 12, planetFile); fread(current_planet, sizeof(planet_t), 12, planetFile); ecs30b Fall 2008 Lecture #26

  44. ecs30b Fall 2008 Lecture #26

  45. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; } planet_t; sizeof(planet_t) == ? ecs30b Fall 2008 Lecture #26

  46. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; char tail2; } planet_t; sizeof(planet_t) == ? ecs30b Fall 2008 Lecture #26

  47. fwrite/fread Table 12.5 Pages 625~626 • format versus binary #include <stdio.h> size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); ecs30b Fall 2008 Lecture #26

  48. /usr/include/stdio.h typedefstruct __sFILE { unsigned char *_p; /* current position in (some) buffer */ int _r; /* read space left for getc() */ int _w; /* write space left for putc() */ short _flags; /* flags, below; this FILE is free if 0 */ short _file; /* fileno, if Unix descriptor, else -1 */ struct __sbuf _bf;/* the buffer (at least 1 byte, if !NULL) */ int _lbfsize; /* 0 or -_bf._size, for inline putc */ /* operations */ void *_cookie; /* cookie passed to io functions */ int (*_close)(void *); int (*_read) (void *, char *, int); fpos_t (*_seek) (void *, fpos_t, int); int (*_write)(void *, const char *, int); /* separate buffer for long sequences of ungetc() */ struct __sbuf _ub; /* ungetc buffer */ … } FILE; ecs30b Fall 2008 Lecture #26

More Related