Computer Programming Contest Preparation

ToolBox - Source for: 4/446/a.c



/home/toolbox/public_html/solutions/4/446/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 #define MAX_LINE 257
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2017-01-22
   20  * Purpose: fun
   21  * Problem: 446 - Kibbles ‘n’ Bits ‘n’ Bits ‘n’ Bits
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 char line[MAX_LINE];
   29 int cnt;
   30 int h1;
   31 int h2;
   32 char op[10];
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37     scanf(" %d ", &cnt);
   38 } /* FUNCTION init */
   39 
   40 void dump()
   41 {
   42     /* FUNCTION dump */
   43 } /* FUNCTION dump */
   44 
   45 void getInput()
   46 {
   47     /* FUNCTION getInput */
   48     scanf(" %x %s %x ", &h1, op, &h2);
   49 } /* FUNCTION getInput */
   50 
   51 void binDump(int x)
   52 {
   53     /* FUNCTION binDump */
   54     int mask = 4096;
   55     int tmp;
   56 
   57     while (0 < mask)
   58         {
   59             /* while */
   60             tmp = mask & x;
   61             if (0 == tmp)
   62                 {
   63                     printf("0");
   64                 }
   65             else
   66                 {
   67                     printf("1");
   68                 }
   69             mask = mask / 2;
   70         } /* while */
   71 } /* FUNCTION binDump */
   72 
   73 void process()
   74 {
   75     /* FUNCTION process */
   76     int tot;
   77 
   78     switch (op[0])
   79         {
   80         /* switch */
   81         case '+':
   82             tot = h1 + h2;
   83             break;
   84         case '-':
   85             tot = h1 - h2;
   86             break;
   87         } /* switch */
   88     binDump(h1);
   89     printf(" %s ", op);
   90     binDump(h2);
   91     printf(" = %d\n", tot);
   92 } /* FUNCTION process */
   93 
   94 int main()
   95 {
   96     /* main */
   97     int i;
   98 
   99     init();
  100     for (i=0; i<cnt; i++)
  101         {
  102             /* for */
  103             getInput();
  104             process();
  105         } /* for */
  106 
  107     return EXIT_SUCCESS;
  108 } /* main */
  109