1 / 40

ecs30 Winter 2012: Programming and Problem Solving # 18: Chapters 11~12, Structure and File

ecs30 Winter 2012: Programming and Problem Solving # 18: Chapters 11~12, Structure and File. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/. Recursion. A function calls itself, maybe with a different set of parameter values…

red
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 18: Chapters 11~12, Structure and File

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 Winter 2012:Programming and Problem Solving#18: Chapters 11~12, Structure and File Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #17

  2. Recursion • A function calls itself, maybe with a different set of parameter values… • Different sets of local variables… (n-1)! N! ecs30 Winter 2012 Lecture #17

  3. A Legend Legend has it that there were three diamond needles set into the floor of the temple of Brahma in Hanoi. Stacked upon the leftmost needle were 64 golden disks, each a different size, stacked in concentric order: ecs30 Winter 2012 Lecture #17

  4. A Legend The priests were to transfer the disks from the first needle to the second needle, using the third as necessary. But they could only moveone disk at a time, and could never put a larger disk on top of a smaller one. When they completed this task, the world would end! ecs30 Winter 2012 Lecture #17

  5. Testing The Hanoi Towers Enter how many disks: 4 move a disk from A to B move a disk from C to B move a disk from A to C move a disk from B to A move a disk from B to C move a disk from A to C move a disk from A to B move a disk from C to B move a disk from C to A move a disk from B to A move a disk from C to B move a disk from A to C move a disk from A to B move a disk from C to B ecs30 Winter 2012 Lecture #17

  6. void tower (char from_peg, char to_peg, char aux_peg, int n) { … } int main(void) { int n; scanf(“%d”, &n); tower(‘A’, ‘B’, ‘C’, n); } ecs30 Winter 2012 Lecture #17

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

  8. 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

  9. 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

  10. ecs30 Winter 2012 Lecture #17

  11. 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

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

  13. #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

  14. &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

  15. &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; sizeof(planet_t) == ? ecs30b Fall 2008 Lecture #21

  16. 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

  17. ecs30b Fall 2008 Lecture #21

  18. 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

  19. 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

  20. 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

  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

More Related