Computer Programming Contest Preparation

ToolBox - Source for: 1/128/a.c



/home/toolbox/public_html/solutions/1/128/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2022-04-20
   18  * Purpose: fun
   19  * Problem: 128 - Software CRC
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_LINE 1040
   27 #define DIVISOR 34943
   28 
   29 char line[MAX_LINE+1];
   30 int tot;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35 } /* FUNCTION init */
   36 
   37 void dump()
   38 {
   39     /* FUNCTION dump */
   40 } /* FUNCTION dump */
   41 
   42 int getInput()
   43 {
   44     /* FUNCTION getInput */
   45     int dataReadFlag;
   46 
   47     fgets(line, MAX_LINE, stdin);
   48     dataReadFlag = ('#' != line[0]);
   49 
   50     return (dataReadFlag);
   51 } /* FUNCTION getInput */
   52 
   53 void process()
   54 {
   55     /* FUNCTION process */
   56     int i;
   57     int slen;
   58 
   59     tot = 0;
   60     slen = strlen(line) - 1;
   61     if (0 < slen)
   62         {
   63             /* non-empty string */
   64             for (i=0; i<slen; i++)
   65                 {
   66                     /* for each char in line */
   67                     DEBUG printf("%d: %d %x\n", i, tot, tot);
   68                     tot = ((256 * tot) + line[i] ) % DIVISOR;
   69                 } /* for each char in line */
   70             DEBUG printf("%d: %d %x\n", i, tot, tot);
   71             tot = (((tot * 256) % DIVISOR) * 256) % DIVISOR;
   72             tot = DIVISOR - tot;
   73         } /* non-empty string */
   74     printf("%02X %02X\n", (tot / 256), (tot % 256));
   75 
   76 } /* FUNCTION process */
   77 
   78 int main()
   79 {
   80     /* main */
   81     int moreToDo;
   82 
   83     init();
   84     moreToDo = getInput();
   85     while (moreToDo)
   86         {
   87             /* while */
   88             process();
   89             moreToDo = getInput();
   90         } /* while */
   91 
   92     return EXIT_SUCCESS;
   93 } /* main */
   94