diff options
author | Nguyễn Gia Phong <vn.mcsinyx@gmail.com> | 2019-12-28 21:16:58 +0700 |
---|---|---|
committer | Nguyễn Gia Phong <vn.mcsinyx@gmail.com> | 2019-12-28 21:21:44 +0700 |
commit | 0887d8f96950a060a122e14ed2981182ff1eb37d (patch) | |
tree | 68be7a59c323c1fd901455ffae8f21874c4bb4c6 /usth/ICT2.1/labwork/3/Ex3.c | |
parent | e461df7573c2b7b7e26c965d8cf2d8e175d67378 (diff) | |
download | cp-0887d8f96950a060a122e14ed2981182ff1eb37d.tar.gz |
[usth/ICT2.1] Algorithm and Data Structures
Diffstat (limited to 'usth/ICT2.1/labwork/3/Ex3.c')
-rw-r--r-- | usth/ICT2.1/labwork/3/Ex3.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/usth/ICT2.1/labwork/3/Ex3.c b/usth/ICT2.1/labwork/3/Ex3.c new file mode 100644 index 0000000..b934ec0 --- /dev/null +++ b/usth/ICT2.1/labwork/3/Ex3.c @@ -0,0 +1,46 @@ +/* + * Interactive guessing game. + * This is free and unencumbered software released into the public domain. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +#include "stack.h" + +char *random10(char *c) +{ + char *p = malloc(sizeof(char)); + *p = rand() % 10; + while (c != NULL && *p == *c) { + free(p); + p = malloc(sizeof(char)); + *p = rand() % 10; + } + return p; +} + +int main() +{ + stack *s = mkstack(); + char guess, lost = 0; + srand(time(NULL)); + stack_push(s, random10(NULL)); +STEP2: + stack_push(s, random10(stack_top(s))); + char *p = stack_pop(s); + puts(lost ? "Make another guess between 0 and 9" + : "Make a guess between 0 and 9"); + scanf("%hhd", &guess); + if ((guess - *p) * (guess - *(char *) stack_top(s)) < 0) { + puts("YOU WIN!"); + return 0; + } else if (lost) { + puts("YOU LOSE!"); + return 0; + } + + lost = 1; + goto STEP2; +} |