#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; }