Created
October 19, 2011 13:23
-
-
Save b-adams/1298270 to your computer and use it in GitHub Desktop.
Recursion example
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
/** @file inceptionish.c | |
@author Prof. Adams | |
@assistance Plot overview from Wikipedia [http://en.wikipedia.org/wiki/Inception] | |
@assistance Plot overview from Jamie LaFountain | |
@status funky | |
*/ | |
#include <stdio.h> | |
#include <stdbool.h> | |
/** Letter user enters for 'Dream' command in main menu */ | |
const char CMD_DREAM = 'D'; | |
/** Letter user enters for 'Look' command in main menu */ | |
const char CMD_LOOK = 'L'; | |
/** Letter user enters for 'Wake' command in main menu */ | |
const char CMD_WAKE = 'W'; | |
/** Unrealistic value to enable detection of unitialized day/month */ | |
const int UNINITIALIZED = -42; | |
/** Add given number of tabs to beginning of a fresh line, plus a > marker | |
@param numspaces How many tabs to add | |
@returns nothing | |
@sideeffect Prints a newline, numspaces many tabs, and a > sign | |
*/ | |
void indent(int numspaces); | |
/** Badger user until they enter a proper command | |
@returns The user's correct choice | |
@sideeffect Prompts user for a choice, and after a user has finally made a correct choice it thanks them for each time they made an incorrect one. | |
*/ | |
char getChoice(); | |
/** Make a new 'reality' buried inside the calling environment | |
@param depth How many tabs to put on the output for this level | |
@returns nothing | |
@sideeffect ... it's complicated | |
*/ | |
void incept(int depth); | |
/** Entry point for program | |
@returns 0 if all is well | |
*/ | |
int main() | |
{ | |
printf("Welcome to the Inception-ish program!\n"); | |
printf("See if you can figure out how many variables are in use!\n"); | |
incept(0); | |
printf("\nWelcome back to the 'TRUE' reality. Time to quit.\n"); | |
} | |
void indent(int numspaces) | |
{ | |
int index; | |
printf("\n"); | |
for(index=0; index<numspaces; index++) printf("\t"); | |
printf("> "); | |
} | |
char getChoice() | |
{ | |
char userInput; | |
printf("Please select (%c)ream, (%c)ake, or (%c)ook around: ", CMD_DREAM, CMD_WAKE, CMD_LOOK); | |
scanf(" %c", &userInput); | |
if(userInput==CMD_DREAM) printf("reaming..."); | |
else if(userInput==CMD_WAKE) printf("aking..."); | |
else if(userInput==CMD_LOOK) printf("ooking..."); | |
else | |
{ | |
printf("'%c' wasn't one of your choices. ", userInput); | |
userInput = getChoice(); | |
printf("Thank you. '%c' is a valid. ", userInput); | |
} | |
return userInput; | |
} | |
void incept(int depth) | |
{ | |
int year=UNINITIALIZED; | |
int month=UNINITIALIZED; | |
int day=UNINITIALIZED; | |
char userChoice; | |
indent(depth); printf("Welcome to Reality! Or so it seems...\n"); | |
while(true) | |
{ | |
userChoice = getChoice(); | |
switch(userChoice) | |
{ | |
default: | |
indent(depth); printf("We'll just ignore that little mistake.\n"); | |
break; | |
case CMD_LOOK: | |
indent(depth); printf("You look at the newspaper for a date. It says..."); | |
if(day==UNINITIALIZED) | |
{ | |
printf("\n(Please enter the date, in year/month/day format like 2009/12/30): "); | |
scanf("%d/%d/%d", &year, &month, &day); | |
} | |
indent(depth); printf("It is the %dth day of the %dth month of year %d\n", day, month, year); | |
break; | |
case CMD_DREAM: | |
indent(depth); printf("Off to dreamland\n"); | |
incept(depth+1); | |
indent(depth); printf("Welcome back from your dream.\n"); | |
break; | |
case CMD_WAKE: | |
indent(depth); printf("AWAKEN!"); | |
return; | |
} | |
} | |
indent(depth); printf("ERROR: You escaped a WHILE(TRUE) loop!?!?!?"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment