Computer Programming Contest Preparation

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



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