Created
July 8, 2011 19:06
-
-
Save paulbarbu/1072550 to your computer and use it in GitHub Desktop.
Kitty
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"); | |
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(const 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; | |
}; | |
kitty bornCat(const char *name){ | |
kitty cat=(kitty *)(malloc(sizeof(kitty))); | |
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(const 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