Computer Programming Contest Preparation

ToolBox - Source for: 100/10033/b.c



/home/toolbox/public_html/solutions/100/10033/b.c
    1 #include <stdio.h>
    2 
    3 #define TRUE  (1 == 1)
    4 #define FALSE (1 != 1)
    5 #define RAMSIZEPLUS 1500
    6 #define RAMSIZE 1000
    7 #define NUMREGS 10
    8 #define NUMREGSPLUS 15
    9 #define BUFFERSIZE  256
   10 #define MAXVALUE 1000
   11 
   12 //op codes
   13 #define HALT 100
   14 
   15 
   16 void getInput(int ram[])
   17 {
   18     // BEGIN FUNCTION getInput
   19     int numRead = 0;
   20     char * res;
   21     int i;
   22     int i1;
   23     int i2;
   24     int i3;
   25     char buffer [BUFFERSIZE];
   26     res = fgets(buffer,BUFFERSIZE,stdin);
   27     printf( "Beginning while loop.\n");
   28     while (((NULL != res) && (!feof(stdin))) && (0 != strcmp(buffer,"\n")))
   29         {
   30             printf( "%s\n",buffer);
   31             //strip(buffer);
   32             for (i = 0; ((' ' == buffer[i]) || ('\t' == buffer[i])); i ++) printf(".");
   33             i1 = buffer[i++] - '0';
   34             i2 = buffer[i++] - '0';
   35             i3 = buffer[i] - '0';
   36             ram[numRead] = (i1 * 100) + (i2 *10) + i3;
   37             numRead++;
   38             res = fgets(buffer,BUFFERSIZE,stdin);
   39         }
   40 } // END FUNCTION getInput
   41 
   42 void dump(int ram[])
   43 {
   44     int i;
   45     for (i = 0; i < RAMSIZEPLUS; i ++)
   46         {
   47             if(0 != ram[i])
   48                 {
   49                     printf("%d: %d\n", i, ram[i]);
   50                 }
   51         }
   52 }
   53 
   54 
   55 int process(int ram[], int regs[])
   56 {
   57     // BEGIN FUNCTION process
   58     int IP = 0;
   59     int numInstructions = 1;
   60     int opCode;
   61     int p1;
   62     int p2;
   63     int temp;
   64 
   65     while (HALT != ram[IP])
   66         {
   67             //BEGIN while
   68             for (temp = 0; NUMREGS > temp; temp ++)
   69                 {
   70                     printf("%d ",regs[temp]);
   71                 }
   72             printf("\n");
   73             opCode = ram[IP] / 100;
   74             p1 = (ram[IP] / 10) % 10;
   75             p2 = ram[IP] % 10;
   76             printf( "%d: %d %d %d\n", IP, opCode, p1, p2);
   77             IP ++;
   78             switch(opCode)
   79                 {
   80                 case 2 :
   81                     regs[p1] = p2;
   82                     break;
   83                 case 3 :
   84                     regs[p1] = (regs[p1] + p2) % MAXVALUE;
   85                     break;
   86                 case 4 :
   87                     regs[p1] = (regs[p1] * p2) % MAXVALUE;
   88                     break;
   89                 case 5 :
   90                     regs[p1] = regs[p2];
   91                     break;
   92                 case 6 :
   93                     regs[p1] = (regs[p1] + regs[p2]) % MAXVALUE;
   94                     break;
   95                 case 7 :
   96                     regs[p1] = (regs[p1] * regs[p2]) % MAXVALUE;
   97                     break;
   98                 case 8 :
   99                     regs[p1] = ram[regs[p2]];
  100                     break;
  101                 case 9 :
  102                     ram[regs[p2]] = regs[p1];
  103                     break;
  104                 case 0 :
  105                     if (0 != regs[p2]) IP = regs[p1];
  106                     break;
  107                 default :
  108                     printf( "Invalid op code Operation = [%d]\n", ram[IP - 1]);
  109                 }
  110             numInstructions ++;
  111         }  //END while
  112 
  113     return numInstructions;
  114 } // END FUNCTION process
  115 
  116 
  117 int numCases()
  118 {
  119     //START numCases()
  120     int cases;
  121     scanf(" %d ",&cases);
  122     return cases;
  123 } //END numCases()
  124 
  125 
  126 void init(int ram[], int regs[])
  127 {
  128     // START init()
  129     int i;
  130 
  131     for ( i = 0; i < NUMREGSPLUS; i ++)
  132         {
  133             regs[i] = 0;
  134             ram[i] = 0;
  135         }
  136     for ( i = NUMREGSPLUS; i < RAMSIZEPLUS; i ++)
  137         {
  138             ram[i] = 0;
  139         }
  140 } // END init()
  141 
  142 
  143 int main ()
  144 {
  145     // main
  146     int moreToDo;
  147     int cases;
  148     int currCase;
  149     int result;
  150     int ram [RAMSIZEPLUS];
  151     int regs [NUMREGSPLUS];
  152 
  153     cases = numCases();
  154 
  155     for (currCase = 0; currCase < cases; currCase ++)
  156         {
  157             // for
  158             init(ram, regs);
  159             getInput(ram);
  160             //dump(ram);
  161             result = process(ram, regs);
  162             printf("%d\n", result);
  163             printf("result; [%d]\n",result);
  164             //printRes(result);
  165             //printf("hello\n");
  166         } // for
  167 
  168     return 0;
  169 } // main
  170