Computer Programming Contest Preparation

ToolBox - Source for: 117/11743/a.c



/home/toolbox/public_html/solutions/117/11743/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2021-11-11
   21  * Purpose: fun
   22  * Problem: 11743 - Credit Check
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 /* 5181 2710 9900 0012
   30  * 0123 5678 0123 5678
   31  * 0000 0000 1111 1111
   32  * eoeo eoeo eoeo eoeo
   33  */
   34 
   35 #define MAX_LENGTH 32
   36 
   37 int numberOfTimes;
   38 char card[MAX_LENGTH];
   39 int oddOffsetArray[8] =  {1, 3, 6, 8, 11, 13, 16, 18};
   40 int evenOffsetArray[8] = {0, 2, 5, 7, 10, 12, 15, 17};
   41 
   42 void init()
   43 {
   44     /* FUNCTION init */
   45     scanf("%d ", &numberOfTimes);
   46 } /* FUNCTION init */
   47 
   48 void dump()
   49 {
   50     /* FUNCTION dump */
   51 } /* FUNCTION dump */
   52 
   53 void getInput()
   54 {
   55     /* FUNCTION getInput */
   56     fgets(card, MAX_LENGTH - 1, stdin);
   57 } /* FUNCTION getInput */
   58 
   59 int digitValue(char c, int mult)
   60 {
   61     /* FUNCTION digitValue */
   62     int tmp;
   63 
   64     tmp = (c - '0') * mult;
   65     if (9 < tmp)
   66         {
   67             /* two digit result */
   68             tmp = (tmp / 10) + (tmp % 10);
   69         } /* two digit result */
   70     return tmp;
   71 } /* FUNCTION digitValue */
   72 
   73 void process()
   74 {
   75     /* FUNCTION process */
   76     int i;
   77     int oddTot = 0;
   78     int evenTot = 0;
   79 
   80     for (i=0; 8>i; i++)
   81         {
   82             /* compute totals */
   83             oddTot = oddTot + digitValue(card[oddOffsetArray[i]], 1);
   84             evenTot = evenTot + digitValue(card[evenOffsetArray[i]], 2);
   85         } /* compute totals */
   86     if (0 == ((oddTot + evenTot) % 10))
   87         {
   88             /* valid */
   89             printf("Valid\n");
   90         } /* valid */
   91     else
   92         {
   93             /* invalid */
   94             printf("Invalid\n");
   95         } /* invalid */
   96 } /* FUNCTION process */
   97 
   98 int main()
   99 {
  100     /* main */
  101     int i;
  102 
  103     init();
  104     for (i=0; i<numberOfTimes; i++)
  105         {
  106             /* while */
  107             getInput();
  108             process();
  109         } /* while */
  110 
  111     return EXIT_SUCCESS;
  112 } /* main */
  113 
  114