learning-cc4e

Solution to https://cc4e.com/. If you copy these you're not right in the head.
git clone https://kaka.farm/~git/learning-cc4e
Log | Files | Refs

solution.c (810B)


      1 #include <stdio.h>
      2 
      3 int main()
      4 {
      5   char line[256];
      6   char memory[256];
      7   char opcode;
      8   int count,address,value;
      9 
     10   while(fgets(line, 256, stdin) != NULL) {
     11     /* printf("\nLine: %s\n",line); */
     12     if ( line[0] == 'X' ) break;
     13     if ( line[0] == '*' ) {
     14       continue;
     15     }
     16     count = sscanf(line, "%d %c %d", &address, &opcode, &value);
     17     if ( count != 3 ) continue;
     18     /* printf("address: %d opcode: %c value: %d\n", address, opcode, value); */
     19 
     20     if ( opcode == '=' ) {
     21       memory[address] = value;
     22     } else if ( opcode == '+' ) {
     23       memory[address] += value;
     24     } else if ( opcode == '-' ) {
     25       memory[address] -= value;
     26     } else {
     27       printf("Bad opcode: %c\n", opcode);
     28       return -1;
     29     }
     30 
     31     /* printf("Memory:\n%s\n", memory); */
     32   }
     33   printf("Memory:\n%s\n", memory);
     34 }