Computer Programming Contest Preparation

ToolBox - Source for: 3/392/a.c



/home/toolbox/public_html/solutions/3/392/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 #define MAX_LINE 257
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2021-02-05
   21  * Purpose: fun
   22  * Problem: 392 - Polynomial Showdown
   23  */
   24 
   25 /*
   26  * This template reads lines of data at a time until end of file.
   27  */
   28 
   29 int num;
   30 int p[9];
   31 char ex[9] = {'8', '7', '6', '5', '4', '3', '2', '1', '0'};
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 int getInput()
   44 {
   45     /* FUNCTION getInput */
   46     int dataReadFlag;
   47 
   48     dataReadFlag = (9 == scanf(" %d %d %d %d %d %d %d %d %d ", &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &p[6], &p[7], &p[8]));
   49     return (dataReadFlag);
   50 } /* FUNCTION getInput */
   51 
   52 void doX(int a, int c)
   53 {
   54     /* FUNCTION doX */
   55     if (8 == a)
   56         {
   57             /* constant case */
   58             printf("%d", c);
   59         } /* constant case */
   60     else
   61         {
   62             /* coefficient cases */
   63             /* omit coeeficient if it is 1 */
   64             if (1 < c)
   65                 {
   66                     printf("%d", c);
   67                 }
   68             /* now do x and super script */
   69             printf("x");
   70             if (7 > a)
   71                 {
   72                     printf("^%c", ex[a]);
   73                 }
   74         } /* coefficient cases */
   75 } /* FUNCTION doX */
   76 
   77 void process()
   78 {
   79     /* FUNCTION process */
   80     int start;
   81     int stop;
   82     int i;
   83 
   84     if ((0 == p[0]) && (0 == p[1]) && (0 == p[2]) &&
   85             (0 == p[3]) && (0 == p[4]) && (0 == p[5]) &&
   86             (0 == p[6]) && (0 == p[7]) && (0 == p[8]))
   87         {
   88             /* all 0 coefficients */
   89             printf("0");
   90         } /* all 0 coefficients */
   91     else
   92         {
   93             /* stuff to do */
   94             /* find first non zero from front */
   95             start = 0;
   96             while (0 == p[start])
   97                 {
   98                     start++;
   99                 };
  100             /* find first non zero from back */
  101             stop = 8;
  102             while (0 == p[stop])
  103                 {
  104                     stop--;
  105                 };
  106             /* now do the middle */
  107             i = start;
  108             if (0 > p[i])
  109                 {
  110                     printf("-");
  111                     p[i] = -p[i];
  112                 }
  113             doX(i, p[i]);
  114             while (i < stop)
  115                 {
  116                     /* loop for remaining coeeficients */
  117                     i++;
  118                     if (0 != p[i])
  119                         {
  120                             /* non-zero coefficient */
  121                             if (0 > p[i])
  122                                 {
  123                                     /* negative */
  124                                     printf(" - ");
  125                                     p[i] = -p[i];
  126                                 } /* negative */
  127                             else
  128                                 {
  129                                     /* non-negative */
  130                                     printf(" + ");
  131                                 } /* non-negative */
  132                             doX(i, p[i]);
  133                         } /* non-zero coefficient */
  134                 } /* loop for remaining coeeficients */
  135         } /* stuff to do */
  136     printf("\n");
  137 } /* FUNCTION process */
  138 
  139 int main()
  140 {
  141     /* main */
  142     int moreToDo;
  143 
  144     init();
  145     moreToDo = getInput();
  146     while (moreToDo)
  147         {
  148             /* while */
  149             process();
  150             moreToDo = getInput();
  151         } /* while */
  152 
  153     return EXIT_SUCCESS;
  154 } /* main */
  155