Структури - 1

Chapter 6 - Structures
6.1 Basics of Structures
6.2 Structures and Functions
6.3 Arrays of Structures
6.4 Pointers to Structures

Основи на структурите

 struct point {
       int x;
       int y;
};


ebook - The C Programming Language Ritchie & kernighan -.doc struct point pt;
struct point maxpt = { 320, 200 };


double dist;;
dist = sqrt((double)pt.x * pt.x + (double)pt.y * pt.y);


    struct rect {
       struct point pt1;
       struct point pt2;
    };
    struct rect screen;
    screen.pt1.x



Структури и функции


/* 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);



/* addpoints: add two points */
struct point addpoint(struct point p1, struct point p2)
{

       p1.x += p2.x;
       p1.y += p2.y;
       return p1;
}




/* 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 origin, *pp;
pp = &origin;
printf("origin is (%d,%d)\n", (*pp).x, (*pp).y);
printf("origin is (%d,%d)\n", pp->x, pp->y);


struct rect r, *rp = &r;

     r.pt1.x
     rp->pt1.x
     (r.pt1).x
     (rp->pt1).x


Масиви от структури

char *keyword[NKEYS];
int keycount[NKEYS];
 
char *word;
int cout;

struct key {
       char *word;
       int count;
   } keytab[NKEYS];

 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
};


#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAXWORD 100

int getword(char *, int);
int binsearch(char *, struct key *, int);

/* count C keywords */
int 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;
}



Указатели към структури

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXWORD 100

int getword(char *, int);
struct key *binsearch(char *, struct key *, int);

/* count C keywords; pointer version */
int 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, struck 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;
}