Skip to content

Instantly share code, notes, and snippets.

@paulbarbu
Created July 8, 2011 19:06
Show Gist options
  • Save paulbarbu/1072550 to your computer and use it in GitHub Desktop.
Save paulbarbu/1072550 to your computer and use it in GitHub Desktop.
Kitty
#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);
}
#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
#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