aboutsummaryrefslogtreecommitdiff
path: root/simple-C-programs/TouringMachine.c
blob: e150a4266c1d509b536014600ae7f6cea22a56ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>

int main() {
  char token[256];
  int position = 0, value, length;
  char memory[256] = {0};

  while (scanf("%s", token) == 1) {

    if (token[0] == 'q') {
      break;
    }

    for (length = 0; token[length] != '\0'; length++)
      ;

    if (length > 1) {
      value = 0;
      for (int i = 0; token[i] != '\0'; i++) {
        if (length == 3) {
          int tmp = token[i];
          tmp = tmp - '0';
          tmp = tmp * 100;
          value += tmp;
          length--;
        } else if (length == 2) {
          int tmp = token[i];
          tmp = tmp - '0';
          tmp = tmp * 10;
          value += tmp;
          length--;
        } else if (length == 1) {
          int tmp = token[i];
          tmp = tmp - '0';
          value += tmp;
          length--;
          memory[position] = value;
        }
      }
    } else if (token[0] == '>') {
      position++;
    } else if (token[0] == '<') {
      position--;
    }
  }
  printf("Memory:\n%s\n", memory);
}