From 8833b7a5b246cad02e253838c07ba1689834e523 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 9 Dec 2021 13:32:48 +0000 Subject: Initial checkin --- 2.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2.c (limited to '2.c') diff --git a/2.c b/2.c new file mode 100644 index 0000000..84b511c --- /dev/null +++ b/2.c @@ -0,0 +1,50 @@ +#include +#include +#include + +int main(void) +{ + char buff[80]; + int depth = 0; + int horiz = 0; + + while(fgets(buff, sizeof buff, stdin)) + { + char *tok[2] = {0}; + + tok[0] = strtok(buff," "); + + if (tok[0]) tok[1] = strtok(NULL, " "); + + if (tok[0] && tok[1]) + { + int i = atoi(tok[1]); + + if(strcmp(tok[0], "forward") == 0) + { + horiz += i; + } + + if(strcmp(tok[0], "down") == 0) + { + depth += i; + } + + if(strcmp(tok[0], "up") == 0) + { + depth -= i; + } + } + else + { + printf("Bad input"); + exit(1); + } + } + + printf("depth=%d\n", depth); + printf("horiz=%d\n", horiz); + printf("product=%d\n", depth*horiz); + + return 0; +} -- cgit v1.3