Created
December 8, 2021 05:31
-
-
Save JustinChristensen/6136e41192fa0f98455701e4d4dabea5 to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
#include <stdlib.h> | |
enum state { | |
LOCKED, | |
UNLOCKED | |
}; | |
// 'c' for coin | |
// 'p' for push | |
enum state transition(char c, enum state current_state) { | |
switch (current_state) { | |
case LOCKED: | |
if (c == 'c') return UNLOCKED; | |
else if (c == 'p') return LOCKED; | |
break; | |
case UNLOCKED: | |
if (c == 'c') return UNLOCKED; | |
else if (c == 'p') return LOCKED; | |
break; | |
} | |
} | |
int main() { | |
enum state current_state = LOCKED; | |
char c; | |
while ((c = getc(stdin)) == 'c' || c == 'p') { | |
current_state = transition(c, current_state); | |
printf("%c %s\n", c, current_state == LOCKED ? "LOCKED" : "UNLOCKED"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment