Lecture 16 Code

More ADTs; `enum`s

stack.h

// COMP1511 17s2 -- a stack
// Maintains a stack, a last-in-first-out structure.
//
// 2011-04-01   Richard Buckland <richardb@cse.unsw.edu.au>
// 2017-09-18   Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>
// licensed under Creative Commons

// Will determine how big stackData will be.
#define STACK_SIZE 1000

// we want it to be typedef so we could refer to it in multiple times.
// if you take an element of it = gives you a character.
// stackData is an object and you get a character back. This tells us that
// it is an array of characters.
// 'char' is the base type and 'stackData' is an object such as that from
// the pointer you will get an object.

// C is playing 'that game' with us again...
typedef char stackData[STACK_SIZE];
//      ^~~~          ~~~~~~~~~~~~

typedef struct _stack {
    char elements[STACK_SIZE];
    int size;
    int items;
} stack;

// this is our add function, 'char elt' = character element.
stack stackPush (stack s, char elt);

// returns the topmost element
char stackTop (stack s);

// this is our pop function:
// takes an element off, returns a new stack
stack stackPop (stack s);

teststack.c

// COMP1511 17s2 -- a stack
// Maintains a stack, a last-in-first-out structure.
//
// 2011-04-01   Richard Buckland <richardb@cse.unsw.edu.au>
// 2017-09-18   Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>
// licensed under Creative Commons

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "stack.h"

int main (int argc, char *argv[]) {
    stack s = {
        .elements = {0},
        .size = STACK_SIZE,
        .items = 0
    };

    assert (stackTop (s) == 0);
    s = stackPush (s, 'z');
    assert (stackTop (s) == 'z');
    s = stackPop (s);
    assert (stackTop (s) == 0);

    printf ("All tests passed. You are Awesome!\n");
    return EXIT_SUCCESS;
}

stack.c

// COMP1511 17s2 -- a Stack ADT
// Maintains a stack, a last-in-first-out structure.
//
// 2017-09-18   Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>

#include <err.h>
#include <stdlib.h>

#include "stack.h"

// this is our add function, 'char elt' = character element.
stack stackPush (stack s, char elt) {
    if (! (s.items < s.size)) {
        errx (EXIT_FAILURE, "pushing onto full stack");
    }

    s.items++;
    s.elements[s.items] = elt;

    return s;
}

// returns the topmost element
char stackTop (stack s) {
    return s.elements[s.items];
}

// this is our pop function:
// takes an element off, returns a new stack
stack stackPop (stack s) {
    if (s.items == 0) {
        errx (EXIT_FAILURE, "popping off empty stack");
    }

    s.elements[s.items] = 0;
    s.items--;

    return s;
}

Stack.h

// COMP1511 17s2 -- a Stack ADT
// Maintains a stack, a last-in-first-out structure.
//
// 2011-04-01   Richard Buckland <richardb@cse.unsw.edu.au>
// 2017-09-18   Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>
// licensed under Creative Commons

#ifndef _STACK_H_
#define _STACK_H_

// Will determine how big stackData will be.
#define STACK_SIZE 1000

// An ADT, at last!
typedef struct _stack *Stack;

// create a new Stack
Stack newStack (void);

// destroy a stack
void destroyStack (Stack s);

// this is our add function, 'char elt' = character element.
void stackPush (Stack s, char elt);

// returns the topmost element
char stackTop (Stack s);

// this is our pop function -- takes an element off.
char stackPop (Stack s);

#endif // !defined(_STACK_H_)

testStack.c

// COMP1511 17s2 -- a Stack ADT
// Maintains a stack, a last-in-first-out structure.
//
// 2011-04-01   Richard Buckland <richardb@cse.unsw.edu.au>
// 2017-09-18   Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>
// licensed under Creative Commons

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "Stack.h"

int main (int argc, char *argv[]) {
    Stack s = newStack ();

    assert (stackTop (s) == 0);
    stackPush (s, 'z');
    assert (stackTop (s) == 'z');
    assert (stackPop (s) == 'z');
    assert (stackTop (s) == 0);

    char i = 'A';
    while (i <= 'Z') {
        stackPush (s, i);
        i++;
    }

    i = 'Z';
    while (i >= 'A') {
        assert (stackPop (s) == i);
        i--;
    }


    destroyStack (s);

    printf ("All tests passed. You are Awesome!\n");
    return EXIT_SUCCESS;
}

Stack.c

// COMP1511 17s2 -- a Stack ADT
// Maintains a stack, a last-in-first-out structure.
//
// 2017-09-18   Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>

#include <err.h>
#include <stdlib.h>

#include "Stack.h"

typedef struct _stack {
    char elements[STACK_SIZE];
    int size;
    int items;
} stack;

Stack newStack (void) {
    Stack s = calloc (1, sizeof (stack));
    if (s == NULL) {
        err (EXIT_FAILURE, "couldn't allocate memory");
    }
    s->size = STACK_SIZE;
    return s;
}

void destroyStack (Stack s) {
    free (s);
}

// this is our add function, 'char elt' = character element.
void stackPush (Stack s, char elt) {
    if (s == NULL) {
        errx (EXIT_FAILURE, "no stack?");
    }
    if (! (s->items < s->size)) {
        errx (EXIT_FAILURE, "pushing onto full stack");
    }

    s->items++;
    s->elements[s->items] = elt;
}

// returns the topmost element
char stackTop (Stack s) {
    if (s == NULL) {
        errx (EXIT_FAILURE, "no stack?");
    }

    return s->elements[s->items];
}

// this is our pop function:
// takes an element off, returns a new stack
char stackPop (Stack s) {
    if (s == NULL) {
        errx (EXIT_FAILURE, "no stack?");
    }
    if (s->items == 0) {
        errx (EXIT_FAILURE, "popping off empty stack");
    }

    char top = s->elements[s->items];
    s->elements[s->items] = 0;
    s->items--;

    return top;
}

card.c

// Playing cards?  Playing cards.
// 2017-09-18   Jashank Jeremy <{jashankj,z5017851}@cse.unsw.edu.au>

#include <stdio.h>
#include <stdlib.h>

typedef enum {
    HEARTS,
    DIAMONDS,
    SPADES,
    CLUBS
} suit;

typedef enum {
    ONE = 1, TWO, THREE, FOUR, FIVE,
    SIX, SEVEN, EIGHT, NINE, TEN,
    // JACK, QUEEN, KING
} value;

typedef enum {
    RED, YELLOW, GREEN, BLUE
} color;

typedef struct _card {
    value value;
    suit suit;
    color color;
} card;

static void printCard (card c);
static char *suitAsString (suit s);
static char *colorAsString (color c);

int main (int argc, char *argv[]) {
    card c = { FOUR, HEARTS, RED };
    // card hand[52];
    printCard (c);

    return EXIT_SUCCESS;
}

static void printCard (card c) {
/*
    if (c.value == JACK) {
        printf ("the jack of %s", suitAsString (c.suit));
    } else if (c.value == QUEEN) {
        printf ("the queen of %s", suitAsString (c.suit));
    } else if (c.value == KING) {
        printf ("the king of %s", suitAsString (c.suit));
    } else if (c.value == ACE) {
        printf ("the ace of %s", suitAsString (c.suit));
    } else {
        printf ("the %d of %s", c.value, suitAsString (c.suit));
    }
*/

    printf ("the %s %d of %s",
        colorAsString (c.color), c.value, suitAsString (c.suit));
}

static char *suitAsString (suit s) {
    char *strings[4] = {
        "hearts",
        "diamonds",
        "spades",
        "clubs"
    };

    return strings[s];
}

static char *colorAsString (color s) {
    char *strings[4] = {
        "red",
        "yellow",
        "green",
        "blue"
    };

    return strings[s];
}