Created
July 8, 2011 19:20
-
-
Save paulbarbu/1072594 to your computer and use it in GitHub Desktop.
kitty2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "./kitty_actions.c" | |
void main(void){ | |
kitty jerry; | |
jerry = bornCat("jerry"); | |
jerry->watchCat = &watchCat; | |
jerry->feedCat = &feedCat; | |
jerry->feedCat(jerry); | |
jerry->watchCat(jerry); | |
/* | |
jerry->watchCat(); | |
jerry = bornCat("jerry"); | |
feedCat(jerry); | |
stopFeedCat(jerry); | |
watchCat(jerry); | |
*/ | |
killCat(jerry); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef KITTY_H | |
#define KITTY_H | |
typedef struct kitty_cat *kitty; | |
kitty bornCat(const char* name); | |
void nameCat(kitty, const char* name); | |
void playCat(const kitty cat); | |
void stopCat(const kitty cat); | |
void feedCat(const kitty cat); | |
void stopFeedCat(const kitty cat); | |
void killCat(kitty cat); | |
void watchCat(kitty cat); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "./kitty.h" | |
struct kitty_cat{ | |
char *name; | |
int play, | |
eating; | |
void (*nameCat)(kitty cat, const char* name); | |
void (*playCat)(const kitty cat); | |
void (*stopCat)(const kitty cat); | |
void (*feedCat)(const kitty cat); | |
void (*stopFeedCat)(const kitty cat); | |
void (*killCat)(kitty cat); | |
void (*watchCat)(kitty cat); | |
}; | |
kitty bornCat(const char *name){ | |
kitty cat=(kitty *)(malloc(sizeof(kitty))); | |
/*cat->nameCat(cat, name);*/ | |
return cat; | |
} | |
void nameCat(kitty cat, const char *name){ | |
cat->name = (char *)(malloc(strlen(name) * sizeof(char))); | |
strcpy(cat->name, name); | |
} | |
void playCat(const kitty cat){ | |
cat->play = 1; | |
} | |
void stopCat(const kitty cat){ | |
cat->play = 0; | |
} | |
void feedCat(const kitty cat){ | |
cat->eating = 1; | |
} | |
void stopFeedCat(const kitty cat){ | |
cat->eating = 0; | |
} | |
void killCat(kitty cat){ | |
free(cat); | |
} | |
void watchCat(kitty cat){ | |
if(cat->play){ | |
printf("%s is playing!\n", cat->name); | |
} | |
else{ | |
printf("%s is idle!\n", cat->name); | |
} | |
if(cat->eating){ | |
printf("%s is eating!\n", cat->name); | |
} | |
else{ | |
printf("%s is starving!\n", cat->name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment