From 0887d8f96950a060a122e14ed2981182ff1eb37d Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sat, 28 Dec 2019 21:16:58 +0700 Subject: [usth/ICT2.1] Algorithm and Data Structures --- usth/ICT2.1/labwork/3/Ex3.c | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 usth/ICT2.1/labwork/3/Ex3.c (limited to 'usth/ICT2.1/labwork/3/Ex3.c') 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 +#include +#include + +#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; +} -- cgit 1.4.1