1/18 Strukture. 2/18 6.1 Osnove struktura struct point { int x; int y; }; y x0, 0 (4, 3) struct { … } x, y,

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Intro.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + malloc +
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Lecture 4 RegExpr  NFA  DFA Topics Thompson Construction Subset construction Readings: 3.7, 3.6 January 23, 2006 CSCE 531 Compiler Construction.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
1 Chapter 16 Structures. 2 Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single.
1 Homework / Quiz Exam 2 – Solutions Posted – Questions? Continuing K&R Chapter 6 HW6 is on line – due class 22.
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
1 6.1 Basics of Structures 6.2 Structures and Functions 6.3 Arrays of Structures 6.4 Pointers to Structures 6.5 Self-referential Structures + Linked List.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
Lecture 3 RegExpr  NFA  DFA Topics Lex, Flex Thompson Construction Subset construction (maybe) Readings: , 3.7, 3.6 January 18, 2006 CSCE 531.
C Programming Lecture 11 : Structure. Data Type struct union enum typedef.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Struct Advanced Programming Language. Struct Struct allows us to create a new data type We can now declare “struct Point” as a new data type and can use.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
C: Advanced Topics Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
CSCE Systems Programming Lecture 06 - C++/Java  C, Memory CSCE 510 Jan 23, 2013.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
D. GotsevaPL-Lectures1 Programming Languages Lectures Assoc. Prof. PhD Daniela Gotseva
Low-level programming using C Programming in assembly lang. is a real nuisance … But in some situations such as implementing device drivers or OS functions,
Advanced Pointers and Structures Pointers in structures Memory allocation Linked Lists –Stacks and queues Trees –Binary tree example.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
C Structures and Memory Allocation
C++ Lesson 1.
LESSON 3 IO, Variables and Operators
Structures Collection of several variables under a single name
Reserved Words.
Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class
Programming Assignment #4 Binary Trees in C++
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Ključne reči,identifikatori, konstante i promenljive
Computing Science Thompson Rivers University
Lecture 4 (new improved) Subset construction NFA  DFA
CS1100 Computational Engineering
Govt. Polytechnic,Dhangar
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
CS111 Computer Programming
Variables in C Declaring , Naming, and Using Variables.
C Programming Basic – week 9
Homework Continuing K&R Chapter 6.
Structures CSE 2031 Fall February 2019.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Programming Language C Language.
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
CS1100 Computational Engineering
Structures EECS July 2019.
C Structures and Memory Allocation
Presentation transcript:

1/18 Strukture

2/ Osnove struktura struct point { int x; int y; }; y x0, 0 (4, 3) struct { … } x, y, z; int x, y, z; struct point pt; struct point maxpt = { 320, 200 }; ime-strukture.član printf("%d,%d", pt.x, pt.y); double dist, sqrt(double); dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y); struct oznaka { tip imeprom;... };

3/18 y x0, 0 pt2 pt1 struct rect { struct point pt1; struct point pt2; }; struct rect screen; screen.pt1.x;

4/ Strukture i funkcije /* makepoint: make a point from x and y components */ struct point makepoint(int x, int y) { struct point temp; temp.x = x; temp.y = y; return temp; } struct rect screen; struct point middle; struct point makepoint(int, int); screen.pt1 = makepoint(0, 0); screen.pt2 = makepoint(XMAX, YMAX); middle = makepoint((screen.pt1.x + screen.pt2.x)/2, (screen.pt1.y + screen.pt2.y)/2); /* addpoint: add two points */ struct point addpoint(struct point p1, struct point p2) { p1.x += p2.x; p1.y += p2.y; return p1; }

5/18 /* ptinrect: return 1 if p in r, 0 if not */ int ptinrect(struct point p, struct rect r) { return p.x >= r.pt1.x && p.x < r.pt2.x && p.y >= r.pt1.y && p.y < r.pt2.y; } #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) /* canonrect: canonicalize coordinates of rectangle */ struct rect canonrect(struct rect r) { struct rect temp; temp.pt1.x = min(r.pt1.x, r.pt2.x); temp.pt1.y = min(r.pt1.y, r.pt2.y); temp.pt2.x = max(r.pt1.x, r.pt2.x); temp.pt2.y = max(r.pt1.y, r.pt2.y); return temp; } struct point *pp; (*pp).x(*pp).ystruct point origin, *pp; pp = &origin; printf("origin is (%d, %d) \n", (*pp).x, (*pp).y); p->član-struktureprintf("origin is (%d, %d) \n", pp->x, pp->y);

6/18 r.pt1.x rp->pt1.x (r.pt1).x (rp->pt1).x struct { int len; char *str; } *p; ++p->len uvecava len struct rect r, *rp = &r; () [] ->. (++p)->len uvecava p (p++)->len uvecava p nakon pristupanja clanu len, zagrade nisu neophodne *p->str /* string na koji pokazivac str pokazuje */ *p->str++ /* uvecava pokazivac str nakon pristupanja onome na sta on pokazuje, slicno kao *s++ */ (*p->str)++ /* uvecava ono nasta str pokazuje */ *p++->str /* uvecava p nakon pristupanja onome nasta str pokazuje */

7/ Nizovi struktura char *keyword[NKEYS]; int keycount[NKEYS]; Primer: brojanje primeraka svake ključne reči iz jezika C char *word; int count; struct key { char *word; int count; } keytab[NKEYS]; struct key { char *word; int count; } ; struct key keytab[NKEYS];

8/18 struct key { char *word; int count; } keytab[] = { "auto", 0, "break", 0, "case", 0, "char", 0, "const", 0, "continue", 0, "default", 0, /*... */ "unsigned", 0, "void", 0, "volatile", 0, "while", 0 }; { "auto", 0 }, { "break", 0 }, { "case", 0 }, … #include #define MAXWORD 100 int getword(char *, int); int binsearch(char *, struct key *, int); /* count C keywords */ main() { int n; char word[MAXWORD]; while (getword(word, MAXWORD) != EOF) if (isalpha(word[0])) if ((n = binsearch(word, keytab, NKEYS)) >= 0) keytab[n].count++; for (n = 0; n < NKEYS; n++) if (keytab[n].count > 0) printf("%4d %s\n", keytab[n].count, keytab[n].word); return 0; } /* binsearch: find word in tab[0]...tab[n-1] */ int binsearch(char *word, struct key tab[], int n) { int cond; int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high) / 2; if ((cond = strcmp(word, tab[mid].word)) < 0) high = mid - 1; else if (cond > 0) low = mid + 1; else return mid; } return -1; }

9/18 #define NKEYS (sizeof keytab / sizeof(struct key)) #define NKEYS (sizeof keytab / sizeof keytab[0]) /* getword: get next word or character from input */ int getword(char *word, int lim) { int c, getch(void); void ungetch(int); char *w = word; while (isspace(c = getch())) ; if (c != EOF) *w++ = c; if (!isalpha(c)) { *w = '\0'; return c; } for ( ; --lim > 0; w++) if (!isalnum(*w = getch())) { ungetch(*w); break; } *w = '\0'; return word[0]; }

10/ Pokazivači na strukture #include #define MAXWORD 100 int getword(char *, int); struct key *binsearch(char *, struct key *, int); /* count C keywords; pointer version */ main() { char word[MAXWORD]; struct key *p; while (getword(word, MAXWORD) != EOF) if (isalpha(word[0])) if ((p=binsearch(word, keytab, NKEYS)) != NULL) p->count++; for (p = keytab; p < keytab + NKEYS; p++) if (p->count > 0) printf("%4d %s\n", p->count, p->word); return 0; } /* binsearch: find word in tab[0]...tab[n-1] */ struct key *binsearch(char *word, struct key *tab, int n) { int cond; struct key *low = &tab[0]; struct key *high = &tab[n]; struct key *mid; while (low < high) { mid = low + (high-low) / 2; if ((cond = strcmp(word, mid->word)) < 0) high = mid; else if (cond > 0) low = mid + 1; else return mid; } return NULL; } mid = low + high / 2 /* pogrešno */ struct { char c; int i; }; sizeof8 bajtova for (p = keytab; p < keytab + NKEYS; p++) struct key *binsearch(char *word, struct key *tab, int n) struct key * binsearch(char *word, struct key *tab, int n)

11/ Samoreferencirajuće strukture struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left; /* left child */ struct tnode *right; /* right child */ }; struct tnode *left; struct t { … struct s *p; /* p pokazuje na s */ }; struct s { … struct t *q; /* q pokazuje na t */ }; Rešiti opštiji problem brojanja primeraka svih reči na ulazu Struktura podataka: binarno stablo sadrži po jedan "čvor" za svaku različitu reč, svaki čvor sadrži: pokazivač na tekst reči broj primeraka te reči pokazivač na levi čvor dete pokazivač na desni čvor dete now is the time for all good men to come to the aid of their party now is the for men of time all good party their to aid come

12/18 #include #define MAXWORD 100 struct tnode *addtree(struct tnode *, char *); void treeprint(struct tnode *); int getword(char *, int); /* word frequency count */ main() { struct tnode *root; char word[MAXWORD]; root = NULL; while (getword(word, MAXWORD) != EOF) if (isalpha(word[0])) root = addtree(root, word); treeprint(root); return 0; } struct tnode *talloc(void); char *strdup(char *); /* addtree: add a node with w, at or below p */ struct treenode *addtree(struct tnode *p, char *w) { int cond; if (p == NULL) { /* a new word has arrived */ p = talloc(); /* make a new node */ p->word = strdup(w); p->count = 1; p->left = p->right = NULL; } else if ((cond = strcmp(w, p->word)) == 0) p->count++; /* repeated word */ else if (cond < 0) /* less than into left subtree */ p->left = addtree(p->left, w); else /* greater than into right subtree */ p->right = addtree(p->right, w); return p; }

13/18 /* treeprint: in-order print of tree p */ void treeprint(struct tnode *p) { if (p != NULL) { treeprint(p->left); printf("%4d %s\n", p->count, p->word); treeprint(p->right); } #include /* talloc: make a tnode */ struct tnode *talloc(void) { return (struct tnode *) malloc(sizeof(struct tnode)); } char *strdup(char *s) /* make a duplicate of s */ { char *p; p = (char *) malloc(strlen(s)+1); /* +1 for '\0' */ if (p != NULL) strcpy(p, s); return p; }

14/ Pretraživanje tabele #define IN 1 state = IN; struct nlist { /* table entry: */ struct nlist *next; /* next entry in chain */ char *name; /* defined name */ char *defn; /* replacement text */ }; #define HASHSIZE 101 static struct nlist *hashtab[HASHSIZE]; /* pointer table */ /* hash: form hash value for string s */ unsigned hash(char *s) { unsigned hashval; for (hashval = 0; *s != '\0'; s++) hashval = *s + 31 * hashval; return hashval % HASHSIZE; } algoritam heširanja name defn install(s, t) lookup(s)

15/18 /* lookup: look for s in hashtab */ struct nlist *lookup(char *s) { struct nlist *np; for (np = hashtab[hash(s)]; np != NULL; np = np->next) if (strcmp(s, np->name) == 0) return np; /* found */ return NULL; /* not found */ } for (ptr = head; ptr != NULL; ptr = ptr->next) … struct nlist *lookup(char *); char *strdup(char *); /* install: put (name, defn) in hashtab */ struct nlist *install(char *name, char *defn) { struct nlist *np; unsigned hashval; if ((np = lookup(name)) == NULL) { /* not found */ np = (struct nlist *) malloc(sizeof(*np)); if (np == NULL || (np->name = strdup(name)) == NULL) return NULL; hashval = hash(name); np->next = hashtab[hashval]; hashtab[hashval] = np; } else /* already there */ free((void *) np->defn); /*free previous defn */ if ((np->defn = strdup(defn)) == NULL) return NULL; return np; }

16/ Typedef typedef int Length; Length len, maxlen; Length *lengths[]; typedef char *String; String p, lineptr[MAXLINES], alloc(int); int strcmp(String, String); p = (String) malloc(100); typedef struct tnode *Treeptr; typedef struct tnode { /* the tree node: */ char *word; /* points to the text */ int count; /* number of occurrences */ struct tnode *left; /* left child */ struct tnode *right; /* right child */ } Treenode; Treeptr talloc(void) { return (Treeptr) malloc(sizeof(Treenode)); } typedef int (*PFI)(char *, char *);PFI strcmp, numcmp

17/ Unije union u_tag { inti ival; float fval; char *sval; } u; if (utype == INT) printf("%d\n", u.ival); else if (utype == FLOAT) printf("%f\n", u.fval); else if (utype == STRING) printf("%s\n", u.sval); else printf("bad type %d in utype\n", utype); struct { char *name; int flags; int utype; union { int ival; float fval; char *sval; } u; } symtab[NSYM]; symtab[i].u.ival *symtab[i].u.sval symtab[i].u.sval[0] ime-unije. član pokazivač-na-uniju -> član

18/ Bit-polja #define KEYWORD 01 #define EXTERNAL 02 #define STATIC 04 enum { KEYWORD = 01, EXTERNAL = 02, STATIC = 04 }; flags |= EXTERNAL | STATIC; flags &= ~(EXTERNAL | STATIC); if ((flags & (EXTERNAL | STATIC)) == 0) … struct { unsigned int is_keyword : 1; unsigned int is_extern : 1; unsigned int is_static : 1; } flags; flags.is_extern = flags.is_ static = 1; flags.is_extern = flags.is_ static = 0; if (flags.is_extern == 0 && flags.is_ static == 0) … ne može se primeniti operator &