/home/toolbox/public_html/solutions/3/392/s.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 void init() { /* FUNCTION init */ } /* FUNCTION init */
33 void dump() { /* FUNCTION dump */ } /* FUNCTION dump */
34 int getInput()
35 {
36 /* FUNCTION getInput */ int dataReadFlag;
37 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]));
38 return (dataReadFlag);
39 } /* FUNCTION getInput */
40
41 void doX(int a, int c)
42 {
43 /* FUNCTION doX */
44 if (8 == a)
45 {
46 /* constant case */
47 printf("%d", c);
48 } /* constant case */
49 else
50 {
51 /* coefficient cases */
52 /* omit coeeficient if it is 1 */
53 if (1 < c)
54 {
55 printf("%d", c);
56 }
57 /* now do x and super script */
58 printf("x");
59 if (7 > a)
60 {
61 printf("^%c", ex[a]);
62 }
63 } /* coefficient cases */
64 } /* FUNCTION doX */
65
66 void process()
67 {
68 /* FUNCTION process */
69 int start;
70 int stop;
71 int i;
72
73 if ((0 == p[0]) && (0 == p[1]) && (0 == p[2]) &&
74 (0 == p[3]) && (0 == p[4]) && (0 == p[5]) &&
75 (0 == p[6]) && (0 == p[7]) && (0 == p[8]))
76 {
77 /* all 0 coefficients */
78 printf("0");
79 } /* all 0 coefficients */
80 else
81 {
82 /* stuff to do */
83 /* find first non zero from front */
84 start = 0;
85 while (0 == p[start])
86 {
87 start++;
88 };
89 /* find first non zero from back */
90 stop = 8;
91 while (0 == p[stop])
92 {
93 stop--;
94 };
95 /* now do the middle */
96 i = start;
97 if (0 > p[i])
98 {
99 printf("-");
100 p[i] = -p[i];
101 }
102 doX(i, p[i]);
103 while (i < stop)
104 {
105 /* loop for remaining coeeficients */
106 i++;
107 if (0 != p[i]) /* non-zero coefficient */
108 {
109 if (0 > p[i])
110 {
111 /* negative */ printf(" - "); /* negative */
112 p[i] = -p[i];
113 }
114 else
115 {
116 /* non-negative */ printf(" + "); /* non-negative */
117 }
118 doX(i, p[i]);
119 } /* non-zero coefficient */
120 } /* loop for remaining coeeficients */
121 } /* stuff to do */
122 printf("\n");
123 } /* FUNCTION process */
124
125 int main()
126 {
127 /* main */
128 int moreToDo;
129
130 init();
131 moreToDo = getInput();
132 while (moreToDo)
133 {
134 /* while */
135 process();
136 moreToDo = getInput();
137 } /* while */
138
139 return EXIT_SUCCESS;
140 } /* main */
141