Computer Programming Contest Preparation

ToolBox - Source for: 104/10424/a.c



/home/toolbox/public_html/solutions/104/10424/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: 2015-08-30
   20  * Purpose: fun
   21  * Problem: 10424
   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 sum[2];
   30 
   31 int sumLine()
   32 {
   33     /* FUNCTION sumLine */
   34     int i;
   35     int total = 0;
   36 
   37     for (i=0; i<strlen(line); i++)
   38         {
   39             /* step through each character */
   40             if (isalpha(line[i]))
   41                 {
   42                     /* is a letter */
   43                     total = total + tolower(line[i]) - 'a' + 1;
   44                 } /* is a letter */
   45         } /* step through each character */
   46     return total;
   47 } /* FUNCTION sumLine */
   48 
   49 int reduce(int x)
   50 {
   51     /* FUNCTION reduce */
   52     int tot;
   53 
   54     tot = x;
   55     while (9 < tot)
   56         {
   57             /* still to big */
   58             tot = (tot / 10) + (tot % 10);
   59         } /* still to big */
   60     return tot;
   61 } /* FUNCTION reduce */
   62 
   63 int getInput()
   64 {
   65     /* FUNCTION getInput */
   66     int dataReadFlag;
   67 
   68     fgets(line, MAX_LINE, stdin);
   69     if (feof(stdin))
   70         dataReadFlag = FALSE;
   71     else
   72         {
   73             /* something to read */
   74             dataReadFlag = TRUE;
   75             sum[0] = sumLine();
   76             fgets(line, MAX_LINE, stdin);
   77             sum[1] = sumLine();
   78         } /* something to read */
   79     return (dataReadFlag);
   80 } /* FUNCTION getInput */
   81 
   82 void process()
   83 {
   84     /* FUNCTION process */
   85     double percent;
   86 
   87     DEBUG printf("sum = %d   %d\n", sum[0], sum[1]);
   88     sum[0] = reduce(sum[0]);
   89     sum[1] = reduce(sum[1]);
   90     DEBUG printf("red = %d   %d\n", sum[0], sum[1]);
   91     if (sum[0] == sum[1])
   92         {
   93             /* equal */
   94             printf("100.00 %\n");
   95         } /* equal */
   96     else
   97         {
   98             /* not equal -- do the arthmetic */
   99             if (sum[0] < sum[1])
  100                 {
  101                     /* first name is smaller */
  102                     percent = ((double) (sum[0] * 100)) / ((double) sum[1]);
  103                 } /* first name is smaller */
  104             else
  105                 {
  106                     /* second number is smaller */
  107                     percent = ((double) (sum[1] * 100)) / ((double) sum[0]);
  108                 } /* second number is smaller */
  109             printf("%0.2f \%\n", percent);
  110         } /* not equal -- do the arthmetic */
  111 } /* FUNCTION process */
  112 
  113 int main()
  114 {
  115     /* main */
  116     int moreToDo;
  117 
  118     moreToDo = getInput();
  119     while (moreToDo)
  120         {
  121             /* while */
  122             process();
  123             moreToDo = getInput();
  124         } /* while */
  125 
  126     return EXIT_SUCCESS;
  127 } /* main */
  128