Created
March 24, 2019 22:42
-
-
Save blinkybool/174628f18d8e59fe226506014fde7dd3 to your computer and use it in GitHub Desktop.
Makes cupcakes
This file contains 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 <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <time.h> | |
#include <stdbool.h> | |
#include <ctype.h> | |
#include <assert.h> | |
#define MAX_CUP_FILL 4 | |
#define MAX_INGREDIENT_SIZE 64 | |
#define INITIAL_BOWL_SIZE 64 | |
typedef enum { | |
FAN_FORCED, | |
GRILL, | |
HALF_GRILL | |
} Oven_Setting; | |
typedef struct { | |
float temp; | |
Oven_Setting setting; | |
} Oven; | |
typedef char Cup[MAX_CUP_FILL+1]; | |
typedef struct { | |
Cup *cups; | |
int num_cups; | |
} MuffinPan; | |
MuffinPan * | |
init_muffin_pan(int num_cups) { | |
MuffinPan *pan = malloc(sizeof(*pan)); | |
assert(pan); | |
pan->cups = (Cup *) malloc(num_cups*sizeof(Cup)); | |
pan->num_cups = num_cups; | |
return pan; | |
} | |
void | |
char_swap(char *a, char *b) { | |
char tmp = *a; | |
*a = *b; | |
*b = tmp; | |
} | |
void | |
mix_bowl(char *bowl) { | |
// init random number generator for mixing | |
srand((unsigned) time(NULL)); | |
int bowl_level = strlen(bowl); | |
char *rand_char; | |
int i; | |
for (i=0; i<bowl_level; i++) { | |
//pick a random char in the bowl with index >= i | |
rand_char = bowl + (rand() % (bowl_level-i)); | |
//swap that char into position i | |
char_swap(bowl+i, rand_char); | |
} | |
} | |
void cook(char *stuff) { | |
char *c; | |
for (c = stuff; *c!='\0'; c++) { | |
if (isalpha(*c)) { | |
*c = toupper(*c); | |
} | |
} | |
} | |
int | |
main(int argc, char *argv[]) { | |
// Get bowl | |
int bowl_size = INITIAL_BOWL_SIZE; | |
char *bowl = malloc(INITIAL_BOWL_SIZE*sizeof(char)); | |
int bowl_level = 0; | |
*bowl = '\0'; | |
int ingredient_size; | |
char ingredient[MAX_INGREDIENT_SIZE]; | |
// Read ingredients and add to bowl | |
printf("enter ingredients (Ctrl-D to finish):\n"); | |
while (scanf("%s", ingredient)!=EOF) { | |
ingredient_size = strlen(ingredient); | |
// Ensure bowl is big enough | |
while (bowl_level + ingredient_size + 1 >= bowl_size) { | |
printf("\n---getting a bigger bowl---\n"); | |
bowl_size *= 2; | |
bowl = realloc(bowl, bowl_size*sizeof(char)); | |
} | |
// add ingredient to bowl | |
strcat(bowl, ingredient); | |
bowl_level += ingredient_size; | |
} | |
// Mix batter | |
mix_bowl(bowl); | |
// Add batter to muffin pan | |
MuffinPan *pan = init_muffin_pan((bowl_level/MAX_CUP_FILL) + 1); | |
int i; | |
for (i=0; i<pan->num_cups; i++) { | |
strncpy(pan->cups[i], bowl, MAX_CUP_FILL); | |
bowl += MAX_CUP_FILL; | |
} | |
// Cook mixture in oven | |
Oven oven = {.setting = FAN_FORCED, .temp = 180}; | |
printf("Cooking in oven at %.2lf degrees\n", oven.temp); | |
for (i=0; i<pan->num_cups; i++) { | |
cook(pan->cups[i]); | |
} | |
// Serve | |
printf("*Ding!*\n"); | |
for (i=0; i<pan->num_cups; i++) { | |
// Don't serve empty pans | |
if (*(pan->cups[i])=='\0') continue; | |
printf("\\_%s_/, ", pan->cups[i]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment