480 likes | 594 Views
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.
E N D
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
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
[ 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
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
HW#5 • Tower of Hanoi with FOUR Pegs Number of Moves? (needs to be smaller than 3 pegs) ecs30 Winter 2012 Lecture #17
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
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
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
Access the members (or attributes)! ecs30b Fall 2008 Lecture #21
#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
¤t_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
¤t_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
#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
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
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
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(¤t); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #21
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
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(¤t); printf("%s\n", next->name); return 0; } ecs30b Fall 2008 Lecture #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; ecs30b Fall 2008 Lecture #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
Assignment for struct planet_t x,y; y = x; bcopy(&x, &y, sizeof(planet_t)); ecs30b Fall 2008 Lecture #24
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
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
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
#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
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
#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
struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24
struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet *p[12]; }; ecs30b Fall 2008 Lecture #24
struct orbit; struct planet { char name[STRSIZ]; struct orbit *xyz; }; struct orbit { struct planet p[12]; }; ecs30b Fall 2008 Lecture #24
printf("%d\n", ((planet_t *) 0)->diameter); ecs30b Fall 2008 Lecture #24
#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
#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
#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
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
I386 architecture typedef struct { char name[10]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; ecs30b Fall 2008 Lecture #24
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
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
#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
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
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
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
/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