Computer Programming Contest Preparation

ToolBox - Source for: 5/594/a.c



/home/toolbox/public_html/solutions/5/594/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: 2017-03-20
   18  * Purpose: fun
   19  * Problem: 594 - One Little, Two Little, Three Little Endians
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  *
   25  * for more info on union: https://www.tutorialspoint.com/cprogramming/c_unions.htm
   26  */
   27 
   28 #define BYTE unsigned char
   29 
   30 union MAGIC
   31 {
   32     int all;
   33     BYTE byte[4];
   34 };
   35 
   36 int num;
   37 int ans;
   38 
   39 void init()
   40 {
   41     /* FUNCTION init */
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 int getInput()
   50 {
   51     /* FUNCTION getInput */
   52     int dataReadFlag;
   53 
   54     dataReadFlag = 0<= scanf(" %d ", &num);
   55     return (dataReadFlag);
   56 } /* FUNCTION getInput */
   57 
   58 void process()
   59 {
   60     /* FUNCTION process */
   61     union MAGIC a1;
   62     union MAGIC a2;
   63 
   64     a1.all = num;
   65     a2.byte[0] = a1.byte[3];
   66     a2.byte[1] = a1.byte[2];
   67     a2.byte[2] = a1.byte[1];
   68     a2.byte[3] = a1.byte[0];
   69     ans = a2.all;
   70     printf("%d converts to %d\n", num, ans);
   71 } /* FUNCTION process */
   72 
   73 int main()
   74 {
   75     /* main */
   76     int moreToDo;
   77 
   78     init();
   79     moreToDo = getInput();
   80     while (moreToDo)
   81         {
   82             /* while */
   83             process();
   84             moreToDo = getInput();
   85         } /* while */
   86 
   87     return EXIT_SUCCESS;
   88 } /* main */
   89